fix(api): survive existing-schema boots without DB shell
Nginx 502 Host-not-found meant the API never stayed listening. Stamp all migrations when tracking is empty but a late table exists, apply DDL statement-by-statement skipping already-exists, harden remaining CREATE TYPE/TABLE files, and never let seed crash the process. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+36
-24
@@ -164233,6 +164233,13 @@ var require_migrate = __commonJS({
|
||||
) AS "exists"`, typname);
|
||||
return Boolean(rows[0]?.exists);
|
||||
}
|
||||
async function tableExists(prisma, tableName) {
|
||||
const rows = await prisma.$queryRawUnsafe(`SELECT EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_schema = current_schema() AND table_name = $1
|
||||
) AS "exists"`, tableName);
|
||||
return Boolean(rows[0]?.exists);
|
||||
}
|
||||
async function recordApplied(prisma, name) {
|
||||
await prisma.$executeRawUnsafe(`INSERT INTO "${TRACKING_TABLE}" (name) VALUES ($1)
|
||||
ON CONFLICT (name) DO NOTHING`, name);
|
||||
@@ -164276,6 +164283,15 @@ var require_migrate = __commonJS({
|
||||
console.warn(`[migrate] Adopted ${adopted.length} migration(s) from _prisma_migrations: ${adopted.join(", ")}`);
|
||||
}
|
||||
const applied = [];
|
||||
if (appliedNames.size === 0 && (await tableExists(prisma, "Admin") || await tableExists(prisma, "AccountingOutbox") || await tableExists(prisma, "AdminSession"))) {
|
||||
for (const file2 of files) {
|
||||
await recordApplied(prisma, file2.name);
|
||||
appliedNames.add(file2.name);
|
||||
applied.push(file2.name);
|
||||
}
|
||||
console.warn(`[migrate] Empty tracking on existing schema; recorded ${applied.length} migration(s) without SQL`);
|
||||
return { freshDatabase, applied };
|
||||
}
|
||||
for (const file2 of files) {
|
||||
const legacyName = LEGACY_NAME_ALIASES[file2.name];
|
||||
if (appliedNames.has(file2.name) || legacyName && appliedNames.has(legacyName))
|
||||
@@ -164291,24 +164307,16 @@ var require_migrate = __commonJS({
|
||||
const normalized = statement.replace(/\s+/g, " ").trim().toLowerCase();
|
||||
return !normalized.startsWith(`drop table "${TRACKING_TABLE.toLowerCase()}"`) && !normalized.startsWith(`drop table ${TRACKING_TABLE.toLowerCase()}`);
|
||||
});
|
||||
try {
|
||||
await prisma.$transaction(
|
||||
async (tx) => {
|
||||
for (const statement of statements) {
|
||||
await tx.$executeRawUnsafe(statement);
|
||||
}
|
||||
await tx.$executeRawUnsafe(`INSERT INTO "${TRACKING_TABLE}" (name) VALUES ($1)`, file2.name);
|
||||
},
|
||||
// The baseline migration is hundreds of DDL statements; the 5s default is
|
||||
// not enough, especially against a remote database.
|
||||
{ maxWait: 6e4, timeout: 6e5 }
|
||||
);
|
||||
} catch (err) {
|
||||
if (!isAlreadyExistsPgError(err))
|
||||
throw err;
|
||||
await recordApplied(prisma, file2.name);
|
||||
console.warn(`[migrate] ${file2.name}: objects already exist \u2014 recorded as applied (${err instanceof Error ? err.message.split("\n")[0] : String(err)})`);
|
||||
for (const statement of statements) {
|
||||
try {
|
||||
await prisma.$executeRawUnsafe(statement);
|
||||
} catch (err) {
|
||||
if (!isAlreadyExistsPgError(err))
|
||||
throw err;
|
||||
console.warn(`[migrate] ${file2.name}: skipped already-existing object (${err instanceof Error ? err.message.split("\n")[0] : String(err)})`);
|
||||
}
|
||||
}
|
||||
await recordApplied(prisma, file2.name);
|
||||
appliedNames.add(file2.name);
|
||||
applied.push(file2.name);
|
||||
}
|
||||
@@ -164368,13 +164376,17 @@ var require_bootstrap = __commonJS({
|
||||
logger.warn("No seed script found \u2014 skipping seed");
|
||||
return;
|
||||
}
|
||||
const seeded = await (0, migrate_1.runOnce)(prisma, "seed:initial", async () => {
|
||||
const { stdout } = await execFileAsync(process.execPath, [seedScript]);
|
||||
if (stdout.trim())
|
||||
logger.log(stdout.trim());
|
||||
});
|
||||
if (seeded)
|
||||
logger.log("Seed data inserted");
|
||||
try {
|
||||
const seeded = await (0, migrate_1.runOnce)(prisma, "seed:initial", async () => {
|
||||
const { stdout } = await execFileAsync(process.execPath, [seedScript]);
|
||||
if (stdout.trim())
|
||||
logger.log(stdout.trim());
|
||||
});
|
||||
if (seeded)
|
||||
logger.log("Seed data inserted");
|
||||
} catch (err) {
|
||||
logger.warn(`Seed skipped after error: ${err instanceof Error ? err.message : String(err)}`);
|
||||
}
|
||||
} finally {
|
||||
await prisma.$disconnect();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user