Skip to content

Commit 3db3bef

Browse files
chore: disable foreign keys on drop table (#312)
Co-authored-by: David Abram <[email protected]>
1 parent b46ede5 commit 3db3bef

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

scripts/migrate.mjs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const fingersCrossedMode = command === 'fingers-crossed';
1818
const yoloMode = command === 'yolo';
1919
const noMigrationMode = !fingersCrossedMode && !yoloMode;
2020

21+
const DISABLE_FOREIGN_KEYS_SQL = "PRAGMA foreign_keys=OFF";
22+
2123
/**
2224
* @typedef MigrationState
2325
* @property {string[]} files
@@ -41,16 +43,18 @@ const selectTablesFromDatabase = async (/**@type {LibSQLClient}*/client) => {
4143

4244
const dropAllDatabaseTables = async (/**@type {LibSQLClient}*/client) => {
4345
const tableNames = await selectTablesFromDatabase(client);
44-
if (tableNames.length === 1) return console.log('No tables to drop...');
46+
if (tableNames.length === 0) return console.log('No tables to drop...');
47+
4548
for (const tableName of tableNames) {
4649
try {
4750
console.warn('Dropping table:\x1b[31m', tableName, '\x1b[0m...');
48-
await client.execute(`drop table '${tableName}'`);
51+
await client.executeMultiple(`${DISABLE_FOREIGN_KEYS_SQL};drop table '${tableName}';`);
4952
} catch (error) {
5053
console.error('FAILED TO DROP TABLE');
5154
throw error;
5255
}
5356
}
57+
5458
}
5559
const upstreamMigrationStatePath = (/**@type {string}*/databaseId) => `scripts/out/${databaseId}-migrations.upstream-ref.json`;
5660
const upstreamMigrationStateExists = (/**@type {string}*/databaseId) => fs.existsSync(upstreamMigrationStatePath(databaseId));

0 commit comments

Comments
 (0)