fix: make radio migrations safe for existing schemas
This commit is contained in:
1 parent
6c63ed52cd
commit
d99b71a2d3
5 files changed
+52
-20
No files matched your search
@@ -3,6 +3,7 @@ import { readFileSync, readdirSync } from "node:fs";
|
||||
import { resolve, dirname } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { splitSqlStatements } from "./sql-statements";
|
||||
import { mysqlConnectionUrl } from "./db-url";
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||||
const MIGRATIONS_DIR = resolve(__dirname, "../prisma/migrations");
|
||||
@@ -17,8 +18,9 @@ interface MigrationFile {
|
||||
function getDbConfig(): { url: string; database: string } {
|
||||
const url = process.env.DATABASE_URL;
|
||||
if (!url) throw new Error("DATABASE_URL is required");
|
||||
const dbName = url.split("/").pop()?.split("?")[0] ?? "atomcms";
|
||||
return { url, database: dbName };
|
||||
const parsed = new URL(url);
|
||||
const dbName = decodeURIComponent(parsed.pathname.replace(/^\//, "")) || "atomcms";
|
||||
return { url: mysqlConnectionUrl(url), database: dbName };
|
||||
}
|
||||
|
||||
async function ensureConnection(): Promise<void> {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { mysqlConnectionUrl } from "./db-url";
|
||||
|
||||
describe("mysqlConnectionUrl", () => {
|
||||
it("removes Prisma-only pool options before passing the URL to MySQL2", () => {
|
||||
const result = mysqlConnectionUrl("mysql://user:pass@localhost:3306/cms?connection_limit=20&pool_timeout=30&charset=utf8mb4");
|
||||
expect(result).not.toContain("connection_limit");
|
||||
expect(result).not.toContain("pool_timeout");
|
||||
expect(result).toContain("charset=utf8mb4");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,7 @@
|
||||
const MYSQL2_UNSUPPORTED_OPTIONS = ["connection_limit", "pool_timeout"] as const;
|
||||
|
||||
export function mysqlConnectionUrl(value: string): string {
|
||||
const url = new URL(value);
|
||||
for (const option of MYSQL2_UNSUPPORTED_OPTIONS) url.searchParams.delete(option);
|
||||
return url.toString();
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { resolve } from "node:path";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
describe("radio columns migration", () => {
|
||||
it("adds every column idempotently for partially migrated databases", () => {
|
||||
const sql = readFileSync(resolve("prisma/migrations/0009_radio_contests_giveaways_columns.sql"), "utf8");
|
||||
const additions = sql.match(/ADD COLUMN(?! IF NOT EXISTS)/gi) ?? [];
|
||||
expect(additions).toEqual([]);
|
||||
expect(sql).toContain("ADD COLUMN IF NOT EXISTS `title`");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user