Harden migration preflight and startup adoption checks
All checks were successful
Verify and publish container / build-and-push (push) Successful in 1m26s
All checks were successful
Verify and publish container / build-and-push (push) Successful in 1m26s
This commit is contained in:
parent
931e674814
commit
5db7fc8afd
4 changed files with 228 additions and 54 deletions
|
|
@ -38,10 +38,14 @@ function hasUniqueIndex(database, table, indexName, column) {
|
|||
return columns.length === 1 && columns[0].name === column;
|
||||
}
|
||||
|
||||
function schemaSnapshot(database) {
|
||||
function schemaSnapshot(database, includeMigrationTable = false) {
|
||||
const tables = database
|
||||
.prepare(
|
||||
"SELECT name FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' AND name <> '_prisma_migrations' ORDER BY name"
|
||||
`SELECT name FROM sqlite_master
|
||||
WHERE type = 'table'
|
||||
AND name NOT LIKE 'sqlite_%'
|
||||
${includeMigrationTable ? "" : "AND name <> '_prisma_migrations'"}
|
||||
ORDER BY name`
|
||||
)
|
||||
.all()
|
||||
.map((row) => row.name);
|
||||
|
|
@ -113,14 +117,20 @@ export function inspectMigrationState(databasePath) {
|
|||
}
|
||||
const database = new Database(databasePath, { readonly: true, fileMustExist: true });
|
||||
try {
|
||||
const migrationNames = tableExists(database, "_prisma_migrations")
|
||||
const migrationRecords = tableExists(database, "_prisma_migrations")
|
||||
? database
|
||||
.prepare(
|
||||
'SELECT "migration_name" FROM "_prisma_migrations" WHERE "finished_at" IS NOT NULL AND "rolled_back_at" IS NULL'
|
||||
'SELECT "migration_name", "finished_at", "rolled_back_at" FROM "_prisma_migrations"'
|
||||
)
|
||||
.all()
|
||||
.map((row) => row.migration_name)
|
||||
: [];
|
||||
const activeRecords = migrationRecords.filter((row) => row.rolled_back_at === null);
|
||||
const migrationNames = activeRecords
|
||||
.filter((row) => row.finished_at !== null)
|
||||
.map((row) => row.migration_name);
|
||||
const failedMigrationNames = activeRecords
|
||||
.filter((row) => row.finished_at === null)
|
||||
.map((row) => row.migration_name);
|
||||
const committedMigrations = readdirSync(
|
||||
path.join(process.cwd(), "prisma", "migrations"),
|
||||
{ withFileTypes: true }
|
||||
|
|
@ -131,9 +141,7 @@ export function inspectMigrationState(databasePath) {
|
|||
const priorMigrations = committedMigrations.filter(
|
||||
(name) => name < GROUP_MIGRATION
|
||||
);
|
||||
const laterMigrations = committedMigrations.filter(
|
||||
(name) => name > GROUP_MIGRATION
|
||||
);
|
||||
const groupMigrationIndex = committedMigrations.indexOf(GROUP_MIGRATION);
|
||||
|
||||
const artifacts = {
|
||||
materialGroup: tableExists(database, "MaterialGroup"),
|
||||
|
|
@ -168,19 +176,12 @@ export function inspectMigrationState(databasePath) {
|
|||
"groupId"
|
||||
),
|
||||
};
|
||||
const values = Object.values(artifacts);
|
||||
const groupArtifactsPresent = values.every(Boolean);
|
||||
const groupArtifactsAbsent = values.every((value) => !value);
|
||||
const groupMigrationApplied = migrationNames.includes(GROUP_MIGRATION);
|
||||
const priorHistoryComplete = priorMigrations.every((name) =>
|
||||
migrationNames.includes(name)
|
||||
);
|
||||
const knownMigrationNames = new Set(committedMigrations);
|
||||
const historyHasUnknown = migrationNames.some(
|
||||
(name) => !knownMigrationNames.has(name)
|
||||
);
|
||||
const laterHistoryPresent = laterMigrations.some((name) =>
|
||||
migrationNames.includes(name)
|
||||
const historyHasUnknown = activeRecords.some(
|
||||
(row) => !knownMigrationNames.has(row.migration_name)
|
||||
);
|
||||
const actualSchema = schemaSnapshot(database);
|
||||
const matches = (expected) =>
|
||||
|
|
@ -189,42 +190,66 @@ export function inspectMigrationState(databasePath) {
|
|||
const adoptedGroupSchemaExact = matches(
|
||||
expectedSchemaSnapshot([...priorMigrations, GROUP_MIGRATION])
|
||||
);
|
||||
const appliedPrefix = committedMigrations.filter((name, index) =>
|
||||
committedMigrations.slice(0, index + 1).every((candidate) =>
|
||||
migrationNames.includes(candidate)
|
||||
)
|
||||
);
|
||||
const appliedHistoryExact =
|
||||
let historyPrefixLength = 0;
|
||||
while (
|
||||
historyPrefixLength < committedMigrations.length &&
|
||||
migrationNames.includes(committedMigrations[historyPrefixLength])
|
||||
) {
|
||||
historyPrefixLength += 1;
|
||||
}
|
||||
const historyIsExactPrefix =
|
||||
!historyHasUnknown &&
|
||||
migrationNames.length === appliedPrefix.length &&
|
||||
migrationNames.every((name) => appliedPrefix.includes(name));
|
||||
migrationNames.length === historyPrefixLength &&
|
||||
migrationNames.every((name) =>
|
||||
committedMigrations.slice(0, historyPrefixLength).includes(name)
|
||||
);
|
||||
const trackedSchemaExact =
|
||||
appliedHistoryExact && matches(expectedSchemaSnapshot(appliedPrefix));
|
||||
historyIsExactPrefix &&
|
||||
matches(expectedSchemaSnapshot(committedMigrations.slice(0, historyPrefixLength)));
|
||||
let schemaPrefixLength = -1;
|
||||
for (let index = 0; index <= committedMigrations.length; index += 1) {
|
||||
if (matches(expectedSchemaSnapshot(committedMigrations.slice(0, index)))) {
|
||||
schemaPrefixLength = index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
const migrationsToAdopt =
|
||||
schemaPrefixLength > historyPrefixLength
|
||||
? committedMigrations.slice(historyPrefixLength, schemaPrefixLength)
|
||||
: [];
|
||||
const failuresAreAdoptable = failedMigrationNames.every((name) =>
|
||||
migrationsToAdopt.includes(name)
|
||||
);
|
||||
|
||||
let classification = "CONFLICT";
|
||||
if (groupMigrationApplied && trackedSchemaExact) classification = "CURRENT";
|
||||
else if (
|
||||
!groupMigrationApplied &&
|
||||
!laterHistoryPresent &&
|
||||
if (
|
||||
historyIsExactPrefix &&
|
||||
failedMigrationNames.length === 0 &&
|
||||
schemaPrefixLength === historyPrefixLength
|
||||
) {
|
||||
classification =
|
||||
historyPrefixLength === committedMigrations.length ? "CURRENT" : "APPLY";
|
||||
} else if (
|
||||
historyIsExactPrefix &&
|
||||
priorHistoryComplete &&
|
||||
migrationNames.length === priorMigrations.length &&
|
||||
groupArtifactsAbsent &&
|
||||
preGroupSchemaExact
|
||||
) classification = "APPLY";
|
||||
else if (
|
||||
!groupMigrationApplied &&
|
||||
!laterHistoryPresent &&
|
||||
priorHistoryComplete &&
|
||||
migrationNames.length === priorMigrations.length &&
|
||||
groupArtifactsPresent &&
|
||||
adoptedGroupSchemaExact
|
||||
) classification = "ADOPT";
|
||||
historyPrefixLength >= groupMigrationIndex &&
|
||||
schemaPrefixLength > historyPrefixLength &&
|
||||
failuresAreAdoptable
|
||||
) {
|
||||
classification = "ADOPT";
|
||||
}
|
||||
|
||||
return {
|
||||
classification,
|
||||
artifacts,
|
||||
migrationNames,
|
||||
failedMigrationNames,
|
||||
migrationsToAdopt,
|
||||
priorHistoryComplete,
|
||||
schemaMigrationPrefix:
|
||||
schemaPrefixLength >= 0
|
||||
? committedMigrations[schemaPrefixLength - 1] ?? null
|
||||
: null,
|
||||
exactSchema: {
|
||||
preGroup: preGroupSchemaExact,
|
||||
adoptedGroup: adoptedGroupSchemaExact,
|
||||
|
|
@ -249,17 +274,20 @@ function verifyBackup(backupPath, sourcePath) {
|
|||
if (backup.pragma("integrity_check", { simple: true }) !== "ok") {
|
||||
throw new Error("Backup failed SQLite integrity_check");
|
||||
}
|
||||
if (JSON.stringify(schemaSnapshot(backup)) !== JSON.stringify(schemaSnapshot(source))) {
|
||||
if (
|
||||
JSON.stringify(schemaSnapshot(backup, true)) !==
|
||||
JSON.stringify(schemaSnapshot(source, true))
|
||||
) {
|
||||
throw new Error("Backup schema does not match the source database");
|
||||
}
|
||||
const sourceCounts = Object.fromEntries(
|
||||
schemaSnapshot(source).map(({ table }) => [
|
||||
schemaSnapshot(source, true).map(({ table }) => [
|
||||
table,
|
||||
source.prepare(`SELECT COUNT(*) AS count FROM ${JSON.stringify(table)}`).get().count,
|
||||
])
|
||||
);
|
||||
const backupCounts = Object.fromEntries(
|
||||
schemaSnapshot(backup).map(({ table }) => [
|
||||
schemaSnapshot(backup, true).map(({ table }) => [
|
||||
table,
|
||||
backup.prepare(`SELECT COUNT(*) AS count FROM ${JSON.stringify(table)}`).get().count,
|
||||
])
|
||||
|
|
@ -273,11 +301,11 @@ function verifyBackup(backupPath, sourcePath) {
|
|||
}
|
||||
}
|
||||
|
||||
function resolveMigration(databaseUrl) {
|
||||
function resolveMigration(databaseUrl, migrationName) {
|
||||
const cli = path.join(process.cwd(), "node_modules", "prisma", "build", "index.js");
|
||||
const result = spawnSync(
|
||||
process.execPath,
|
||||
[cli, "migrate", "resolve", "--applied", GROUP_MIGRATION],
|
||||
[cli, "migrate", "resolve", "--applied", migrationName],
|
||||
{
|
||||
cwd: process.cwd(),
|
||||
env: { ...process.env, DATABASE_URL: databaseUrl },
|
||||
|
|
@ -292,7 +320,16 @@ function resolveMigration(databaseUrl) {
|
|||
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
const databaseUrl = process.env.DATABASE_URL;
|
||||
const databasePath = databasePathFromUrl(databaseUrl);
|
||||
const state = inspectMigrationState(databasePath);
|
||||
const startup = process.argv.includes("--startup");
|
||||
const state =
|
||||
startup && !existsSync(databasePath)
|
||||
? {
|
||||
classification: "EMPTY",
|
||||
migrationNames: [],
|
||||
failedMigrationNames: [],
|
||||
migrationsToAdopt: [],
|
||||
}
|
||||
: inspectMigrationState(databasePath);
|
||||
console.info(JSON.stringify(state, null, 2));
|
||||
|
||||
if (process.argv.includes("--resolve")) {
|
||||
|
|
@ -304,8 +341,17 @@ if (import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|||
backupIndex >= 0 ? process.argv[backupIndex + 1] : undefined,
|
||||
databasePath
|
||||
);
|
||||
resolveMigration(databaseUrl);
|
||||
console.info(`Marked ${GROUP_MIGRATION} applied after exact-schema preflight.`);
|
||||
for (const migrationName of state.migrationsToAdopt) {
|
||||
resolveMigration(databaseUrl, migrationName);
|
||||
}
|
||||
console.info(
|
||||
`Marked ${state.migrationsToAdopt.join(", ")} applied after exact-schema preflight.`
|
||||
);
|
||||
} else if (startup && state.classification === "ADOPT") {
|
||||
console.error(
|
||||
"Database schema requires explicit migration adoption. Stop, create and verify a backup, then run db:preflight -- --resolve --backup <backup-path>."
|
||||
);
|
||||
process.exitCode = 3;
|
||||
} else if (state.classification === "CONFLICT") {
|
||||
process.exitCode = 2;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue