feat: v0.1.0 — sqlite-migrate package extraction from authd #1
19
dist/migrate.js
vendored
19
dist/migrate.js
vendored
@@ -11,6 +11,7 @@ const SCHEMA_MIGRATIONS_DDL = `
|
|||||||
applied_at INTEGER NOT NULL
|
applied_at INTEGER NOT NULL
|
||||||
);
|
);
|
||||||
`;
|
`;
|
||||||
|
const INSERT_MIGRATION_SQL = `INSERT INTO schema_migrations (version, name, checksum, applied_at) VALUES (?, ?, ?, ?)`;
|
||||||
function sha256Hex(body) {
|
function sha256Hex(body) {
|
||||||
return createHash('sha256').update(body).digest('hex');
|
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: [] };
|
return { applied: 0, pending: 0, alreadyApplied: 0, stamped: [] };
|
||||||
}
|
}
|
||||||
const schemaMigrationsExists = tableExists(db, 'schema_migrations');
|
const schemaMigrationsExists = tableExists(db, 'schema_migrations');
|
||||||
const stamped = [];
|
|
||||||
const genesisFile = files.find((f) => f.version === GENESIS_VERSION);
|
const genesisFile = files.find((f) => f.version === GENESIS_VERSION);
|
||||||
const shouldStampGenesis = !schemaMigrationsExists &&
|
const shouldStampGenesis = !schemaMigrationsExists &&
|
||||||
genesisFile !== undefined &&
|
genesisFile !== undefined &&
|
||||||
(opts.stampGenesis === true || tableExists(db, probeTable));
|
(opts.stampGenesis === true || tableExists(db, probeTable));
|
||||||
if (shouldStampGenesis && genesisFile) {
|
|
||||||
db.exec(SCHEMA_MIGRATIONS_DDL);
|
db.exec(SCHEMA_MIGRATIONS_DDL);
|
||||||
|
const insertRow = db.prepare(INSERT_MIGRATION_SQL);
|
||||||
|
const stamped = [];
|
||||||
|
if (shouldStampGenesis && genesisFile) {
|
||||||
const reason = opts.stampGenesis
|
const reason = opts.stampGenesis
|
||||||
? 'stampGenesis option set'
|
? 'stampGenesis option set'
|
||||||
: `detected pre-existing '${probeTable}' table`;
|
: `detected pre-existing '${probeTable}' table`;
|
||||||
log(`WARN genesis-stamp: marked ${genesisFile.name} as applied without executing (${reason})`);
|
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);
|
stamped.push(genesisFile.version);
|
||||||
}
|
}
|
||||||
else {
|
const appliedByVersion = new Map(readAppliedRows(db).map((r) => [r.version, r]));
|
||||||
db.exec(SCHEMA_MIGRATIONS_DDL);
|
|
||||||
}
|
|
||||||
const appliedRows = readAppliedRows(db);
|
|
||||||
const appliedByVersion = new Map(appliedRows.map((r) => [r.version, r]));
|
|
||||||
let applied = 0;
|
let applied = 0;
|
||||||
let alreadyApplied = 0;
|
let alreadyApplied = 0;
|
||||||
const insertRow = db.prepare(`INSERT INTO schema_migrations (version, name, checksum, applied_at) VALUES (?, ?, ?, ?)`);
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const existing = appliedByVersion.get(file.version);
|
const existing = appliedByVersion.get(file.version);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
@@ -103,11 +100,11 @@ export function stampMigration(db, migrationsDir, version) {
|
|||||||
}
|
}
|
||||||
db.exec(SCHEMA_MIGRATIONS_DDL);
|
db.exec(SCHEMA_MIGRATIONS_DDL);
|
||||||
const existing = db
|
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);
|
.get(file.version);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
throw new Error(`migration already applied: ${file.name}`);
|
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;
|
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 {
|
function sha256Hex(body: string): string {
|
||||||
return createHash('sha256').update(body).digest('hex');
|
return createHash('sha256').update(body).digest('hex');
|
||||||
}
|
}
|
||||||
@@ -97,38 +99,31 @@ export function applyMigrations(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const schemaMigrationsExists = tableExists(db, 'schema_migrations');
|
const schemaMigrationsExists = tableExists(db, 'schema_migrations');
|
||||||
const stamped: string[] = [];
|
|
||||||
|
|
||||||
const genesisFile = files.find((f) => f.version === GENESIS_VERSION);
|
const genesisFile = files.find((f) => f.version === GENESIS_VERSION);
|
||||||
const shouldStampGenesis =
|
const shouldStampGenesis =
|
||||||
!schemaMigrationsExists &&
|
!schemaMigrationsExists &&
|
||||||
genesisFile !== undefined &&
|
genesisFile !== undefined &&
|
||||||
(opts.stampGenesis === true || tableExists(db, probeTable));
|
(opts.stampGenesis === true || tableExists(db, probeTable));
|
||||||
|
|
||||||
if (shouldStampGenesis && genesisFile) {
|
|
||||||
db.exec(SCHEMA_MIGRATIONS_DDL);
|
db.exec(SCHEMA_MIGRATIONS_DDL);
|
||||||
|
const insertRow = db.prepare(INSERT_MIGRATION_SQL);
|
||||||
|
|
||||||
|
const stamped: string[] = [];
|
||||||
|
if (shouldStampGenesis && genesisFile) {
|
||||||
const reason = opts.stampGenesis
|
const reason = opts.stampGenesis
|
||||||
? 'stampGenesis option set'
|
? 'stampGenesis option set'
|
||||||
: `detected pre-existing '${probeTable}' table`;
|
: `detected pre-existing '${probeTable}' table`;
|
||||||
log(
|
log(
|
||||||
`WARN genesis-stamp: marked ${genesisFile.name} as applied without executing (${reason})`,
|
`WARN genesis-stamp: marked ${genesisFile.name} as applied without executing (${reason})`,
|
||||||
);
|
);
|
||||||
db.prepare(
|
insertRow.run(genesisFile.version, genesisFile.name, genesisFile.checksum, Date.now());
|
||||||
`INSERT INTO schema_migrations (version, name, checksum, applied_at) VALUES (?, ?, ?, ?)`,
|
|
||||||
).run(genesisFile.version, genesisFile.name, genesisFile.checksum, Date.now());
|
|
||||||
stamped.push(genesisFile.version);
|
stamped.push(genesisFile.version);
|
||||||
} else {
|
|
||||||
db.exec(SCHEMA_MIGRATIONS_DDL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const appliedRows = readAppliedRows(db);
|
const appliedByVersion = new Map(readAppliedRows(db).map((r) => [r.version, r]));
|
||||||
const appliedByVersion = new Map(appliedRows.map((r) => [r.version, r]));
|
|
||||||
|
|
||||||
let applied = 0;
|
let applied = 0;
|
||||||
let alreadyApplied = 0;
|
let alreadyApplied = 0;
|
||||||
const insertRow = db.prepare(
|
|
||||||
`INSERT INTO schema_migrations (version, name, checksum, applied_at) VALUES (?, ?, ?, ?)`,
|
|
||||||
);
|
|
||||||
|
|
||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const existing = appliedByVersion.get(file.version);
|
const existing = appliedByVersion.get(file.version);
|
||||||
@@ -166,15 +161,16 @@ export function stampMigration(
|
|||||||
}
|
}
|
||||||
db.exec(SCHEMA_MIGRATIONS_DDL);
|
db.exec(SCHEMA_MIGRATIONS_DDL);
|
||||||
const existing = db
|
const existing = db
|
||||||
.prepare(
|
.prepare(`SELECT 1 FROM schema_migrations WHERE version = ?`)
|
||||||
`SELECT version, name, checksum, applied_at FROM schema_migrations WHERE version = ?`,
|
.get(file.version);
|
||||||
)
|
|
||||||
.get(file.version) as MigrationRow | undefined;
|
|
||||||
if (existing) {
|
if (existing) {
|
||||||
throw new Error(`migration already applied: ${file.name}`);
|
throw new Error(`migration already applied: ${file.name}`);
|
||||||
}
|
}
|
||||||
db.prepare(
|
db.prepare(INSERT_MIGRATION_SQL).run(
|
||||||
`INSERT INTO schema_migrations (version, name, checksum, applied_at) VALUES (?, ?, ?, ?)`,
|
file.version,
|
||||||
).run(file.version, file.name, file.checksum, Date.now());
|
file.name,
|
||||||
|
file.checksum,
|
||||||
|
Date.now(),
|
||||||
|
);
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user