refactor: dedupe INSERT SQL and DDL exec in migrate.ts
All checks were successful
ci / test (pull_request) Successful in 12s
All checks were successful
ci / test (pull_request) Successful in 12s
- Extract INSERT_MIGRATION_SQL constant; was repeated three times. - Hoist db.exec(SCHEMA_MIGRATIONS_DDL) above the genesis-stamp branch so it is no longer duplicated in both arms of the if/else. - Prepare insertRow once and reuse it for both genesis-stamping and the per-file insert in the apply loop. - Simplify existence probe in stampMigration to SELECT 1 and drop the misleading MigrationRow cast (only existence was checked).
This commit is contained in:
19
dist/migrate.js
vendored
19
dist/migrate.js
vendored
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user