diff --git a/.changeset/ninety-islands-deny.md b/.changeset/ninety-islands-deny.md new file mode 100644 index 0000000000..8a91cd7d98 --- /dev/null +++ b/.changeset/ninety-islands-deny.md @@ -0,0 +1,5 @@ +--- +"@astrojs/db": patch +--- + +Conditionally drop table with --force-reset diff --git a/packages/db/src/core/cli/migration-queries.ts b/packages/db/src/core/cli/migration-queries.ts index 105c3e5dca..89359bd9bc 100644 --- a/packages/db/src/core/cli/migration-queries.ts +++ b/packages/db/src/core/cli/migration-queries.ts @@ -7,6 +7,7 @@ import { hasPrimaryKey } from '../../runtime/index.js'; import { getCreateIndexQueries, getCreateTableQuery, + getDropTableIfExistsQuery, getModifiers, getReferencesConfig, hasDefault, @@ -455,7 +456,7 @@ export async function getProductionCurrentSnapshot({ function getDropTableQueriesForSnapshot(snapshot: DBSnapshot) { const queries = []; for (const tableName of Object.keys(snapshot.schema)) { - const dropQuery = `DROP TABLE ${sqlite.escapeName(tableName)}`; + const dropQuery = getDropTableIfExistsQuery(tableName); queries.unshift(dropQuery); } return queries; diff --git a/packages/db/test/unit/reset-queries.test.js b/packages/db/test/unit/reset-queries.test.js index 9e3a595bc2..4d74e01482 100644 --- a/packages/db/test/unit/reset-queries.test.js +++ b/packages/db/test/unit/reset-queries.test.js @@ -32,7 +32,7 @@ describe('force reset', () => { }); expect(queries).to.deep.equal([ - `DROP TABLE "${TABLE_NAME}"`, + `DROP TABLE IF EXISTS "${TABLE_NAME}"`, `CREATE TABLE "${TABLE_NAME}" (_id INTEGER PRIMARY KEY, "name" text NOT NULL, "age" integer NOT NULL, "email" text NOT NULL UNIQUE, "mi" text)`, ]); });