diff --git a/dist/migrate.js b/dist/migrate.js index 3aefa21..447bb34 100644 --- a/dist/migrate.js +++ b/dist/migrate.js @@ -11,6 +11,7 @@ const SCHEMA_MIGRATIONS_DDL = ` applied_at INTEGER NOT NULL ); `; +const INSERT_MIGRATION_SQL = `INSERT INTO schema_migrations (version, name, checksum, applied_at) VALUES (?, ?, ?, ?)`; function sha256Hex(body) { return createHash('sha256').update(body).digest('hex'); } @@ -54,28 +55,24 @@ export function applyMigrations(db, migrationsDir, opts = {}) { return { applied: 0, pending: 0, alreadyApplied: 0, stamped: [] }; } const schemaMigrationsExists = tableExists(db, 'schema_migrations'); - const stamped = []; const genesisFile = files.find((f) => f.version === GENESIS_VERSION); const shouldStampGenesis = !schemaMigrationsExists && genesisFile !== undefined && (opts.stampGenesis === true || tableExists(db, probeTable)); + db.exec(SCHEMA_MIGRATIONS_DDL); + const insertRow = db.prepare(INSERT_MIGRATION_SQL); + const stamped = []; if (shouldStampGenesis && genesisFile) { - db.exec(SCHEMA_MIGRATIONS_DDL); const reason = opts.stampGenesis ? 'stampGenesis option set' : `detected pre-existing '${probeTable}' table`; log(`WARN genesis-stamp: marked ${genesisFile.name} as applied without executing (${reason})`); - db.prepare(`INSERT INTO schema_migrations (version, name, checksum, applied_at) VALUES (?, ?, ?, ?)`).run(genesisFile.version, genesisFile.name, genesisFile.checksum, Date.now()); + insertRow.run(genesisFile.version, genesisFile.name, genesisFile.checksum, Date.now()); stamped.push(genesisFile.version); } - else { - db.exec(SCHEMA_MIGRATIONS_DDL); - } - const appliedRows = readAppliedRows(db); - const appliedByVersion = new Map(appliedRows.map((r) => [r.version, r])); + const appliedByVersion = new Map(readAppliedRows(db).map((r) => [r.version, r])); let applied = 0; let alreadyApplied = 0; - const insertRow = db.prepare(`INSERT INTO schema_migrations (version, name, checksum, applied_at) VALUES (?, ?, ?, ?)`); for (const file of files) { const existing = appliedByVersion.get(file.version); if (existing) { @@ -103,11 +100,11 @@ export function stampMigration(db, migrationsDir, version) { } db.exec(SCHEMA_MIGRATIONS_DDL); const existing = db - .prepare(`SELECT version, name, checksum, applied_at FROM schema_migrations WHERE version = ?`) + .prepare(`SELECT 1 FROM schema_migrations WHERE version = ?`) .get(file.version); if (existing) { throw new Error(`migration already applied: ${file.name}`); } - db.prepare(`INSERT INTO schema_migrations (version, name, checksum, applied_at) VALUES (?, ?, ?, ?)`).run(file.version, file.name, file.checksum, Date.now()); + db.prepare(INSERT_MIGRATION_SQL).run(file.version, file.name, file.checksum, Date.now()); return file; } diff --git a/src/migrate.ts b/src/migrate.ts index 252a7ee..58df0c7 100644 --- a/src/migrate.ts +++ b/src/migrate.ts @@ -43,6 +43,8 @@ const SCHEMA_MIGRATIONS_DDL = ` ); `; +const INSERT_MIGRATION_SQL = `INSERT INTO schema_migrations (version, name, checksum, applied_at) VALUES (?, ?, ?, ?)`; + function sha256Hex(body: string): string { return createHash('sha256').update(body).digest('hex'); } @@ -97,38 +99,31 @@ export function applyMigrations( } const schemaMigrationsExists = tableExists(db, 'schema_migrations'); - const stamped: string[] = []; - const genesisFile = files.find((f) => f.version === GENESIS_VERSION); const shouldStampGenesis = !schemaMigrationsExists && genesisFile !== undefined && (opts.stampGenesis === true || tableExists(db, probeTable)); + db.exec(SCHEMA_MIGRATIONS_DDL); + const insertRow = db.prepare(INSERT_MIGRATION_SQL); + + const stamped: string[] = []; if (shouldStampGenesis && genesisFile) { - db.exec(SCHEMA_MIGRATIONS_DDL); const reason = opts.stampGenesis ? 'stampGenesis option set' : `detected pre-existing '${probeTable}' table`; log( `WARN genesis-stamp: marked ${genesisFile.name} as applied without executing (${reason})`, ); - db.prepare( - `INSERT INTO schema_migrations (version, name, checksum, applied_at) VALUES (?, ?, ?, ?)`, - ).run(genesisFile.version, genesisFile.name, genesisFile.checksum, Date.now()); + insertRow.run(genesisFile.version, genesisFile.name, genesisFile.checksum, Date.now()); stamped.push(genesisFile.version); - } else { - db.exec(SCHEMA_MIGRATIONS_DDL); } - const appliedRows = readAppliedRows(db); - const appliedByVersion = new Map(appliedRows.map((r) => [r.version, r])); + const appliedByVersion = new Map(readAppliedRows(db).map((r) => [r.version, r])); let applied = 0; let alreadyApplied = 0; - const insertRow = db.prepare( - `INSERT INTO schema_migrations (version, name, checksum, applied_at) VALUES (?, ?, ?, ?)`, - ); for (const file of files) { const existing = appliedByVersion.get(file.version); @@ -166,15 +161,16 @@ export function stampMigration( } db.exec(SCHEMA_MIGRATIONS_DDL); const existing = db - .prepare( - `SELECT version, name, checksum, applied_at FROM schema_migrations WHERE version = ?`, - ) - .get(file.version) as MigrationRow | undefined; + .prepare(`SELECT 1 FROM schema_migrations WHERE version = ?`) + .get(file.version); if (existing) { throw new Error(`migration already applied: ${file.name}`); } - db.prepare( - `INSERT INTO schema_migrations (version, name, checksum, applied_at) VALUES (?, ?, ?, ?)`, - ).run(file.version, file.name, file.checksum, Date.now()); + db.prepare(INSERT_MIGRATION_SQL).run( + file.version, + file.name, + file.checksum, + Date.now(), + ); return file; }