chore(db): keep drizzle migrations folder journal-only
This commit is contained in:
parent
29b43b9921
commit
310dd115b2
@ -3,11 +3,49 @@ import { sql } from "drizzle-orm";
|
|||||||
import { drizzle } from "drizzle-orm/postgres-js";
|
import { drizzle } from "drizzle-orm/postgres-js";
|
||||||
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
||||||
import postgres from "postgres";
|
import postgres from "postgres";
|
||||||
|
import { readFileSync, readdirSync } from "node:fs";
|
||||||
|
import { join } from "node:path";
|
||||||
|
|
||||||
config({
|
config({
|
||||||
path: ".env.local",
|
path: ".env.local",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function assertMigrationJournalIsConsistent() {
|
||||||
|
const migrationsDir = join(process.cwd(), "lib/db/migrations");
|
||||||
|
const journalPath = join(migrationsDir, "meta", "_journal.json");
|
||||||
|
const journal = JSON.parse(readFileSync(journalPath, "utf8")) as {
|
||||||
|
entries: { tag: string }[];
|
||||||
|
};
|
||||||
|
|
||||||
|
const tags = new Set(journal.entries.map((entry) => entry.tag));
|
||||||
|
const sqlFiles = readdirSync(migrationsDir)
|
||||||
|
.filter((file) => file.endsWith(".sql"))
|
||||||
|
.map((file) => file.slice(0, -4));
|
||||||
|
const sqlSet = new Set(sqlFiles);
|
||||||
|
|
||||||
|
const extraSqlFiles = sqlFiles.filter((tag) => !tags.has(tag));
|
||||||
|
const missingSqlFiles = Array.from(tags).filter((tag) => !sqlSet.has(tag));
|
||||||
|
|
||||||
|
if (extraSqlFiles.length === 0 && missingSqlFiles.length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const details = [
|
||||||
|
extraSqlFiles.length > 0
|
||||||
|
? `extra sql files not in journal: ${extraSqlFiles.join(", ")}`
|
||||||
|
: null,
|
||||||
|
missingSqlFiles.length > 0
|
||||||
|
? `journal entries missing sql files: ${missingSqlFiles.join(", ")}`
|
||||||
|
: null,
|
||||||
|
]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(" | ");
|
||||||
|
|
||||||
|
throw new Error(
|
||||||
|
`Migration journal mismatch. Keep lib/db/migrations/*.sql in sync with meta/_journal.json. ${details}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const runMigrate = async () => {
|
const runMigrate = async () => {
|
||||||
if (!process.env.POSTGRES_URL) {
|
if (!process.env.POSTGRES_URL) {
|
||||||
throw new Error("POSTGRES_URL is not defined");
|
throw new Error("POSTGRES_URL is not defined");
|
||||||
@ -19,6 +57,7 @@ const runMigrate = async () => {
|
|||||||
console.log("⏳ Running migrations...");
|
console.log("⏳ Running migrations...");
|
||||||
|
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
assertMigrationJournalIsConsistent();
|
||||||
await migrate(db, { migrationsFolder: "./lib/db/migrations" });
|
await migrate(db, { migrationsFolder: "./lib/db/migrations" });
|
||||||
await db.execute(sql`
|
await db.execute(sql`
|
||||||
ALTER TABLE "AiProvider" DROP CONSTRAINT IF EXISTS "AiProvider_type_check";
|
ALTER TABLE "AiProvider" DROP CONSTRAINT IF EXISTS "AiProvider_type_check";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user