Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

refactor: split to files

Roman Pominovc1c337c8a58f
diff --git a/packages/prisma-client/migrations-cli/args.ts b/packages/prisma-client/migrations-cli/args.ts
new file mode 100644
index 0000000000..08a3ae1e8b
--- /dev/null
+++ b/packages/prisma-client/migrations-cli/args.ts
@@ -0,0 +1,5 @@
+import minimist from "minimist";
+
+export default minimist(process.argv.slice(2), {
+ boolean: ["dev"],
+}) as { _: string[]; dev: boolean };
diff --git a/packages/prisma-client/migrations-cli/cli.ts b/packages/prisma-client/migrations-cli/cli.ts
index 0e22df6986..571b3d43f2 100755
--- a/packages/prisma-client/migrations-cli/cli.ts
+++ b/packages/prisma-client/migrations-cli/cli.ts
@@ -1,211 +1,42 @@
#!/usr/bin/env tsx
-/* eslint-disable no-console */
-
-import { Umzug, FileLocker, MigrationMeta } from "umzug";
-import fs from "node:fs";
-import path from "node:path";
import * as dotenv from "dotenv";
-import minimist from "minimist";
-import * as prismaMigrations from "./prisma-migrations";
-
-const templateFilePath = path.join(
- prismaMigrations.migrationsDir,
- "template.txt"
-);
-const lockfilePath = path.join(prismaMigrations.migrationsDir, "lockfile");
-
-const args = minimist(process.argv.slice(2), {
- boolean: ["dev"],
-}) as { _: string[]; dev: boolean };
-
-const DEV_MODE = args.dev === true;
-
-const generateClient = (migrationName: string) => {
- const migrationDir = path.join(prismaMigrations.migrationsDir, migrationName);
-
- const schemaPath = path.join(migrationDir, "schema.prisma");
- const clientPath = path.join(migrationDir, "client");
-
- if (fs.existsSync(schemaPath) === false) {
- const tsFilePath = prismaMigrations.getMigrationFilePath(
- migrationName,
- "ts"
- );
- if (fs.existsSync(tsFilePath)) {
- throw new Error(
- `Can't generate client for ${migrationName} because ${migrationName}/schema.prisma is missing`
- );
- }
- return;
- }
-
- if (fs.existsSync(clientPath)) {
- fs.rmSync(clientPath, { recursive: true });
- }
-
- prismaMigrations.cliGenerate(schemaPath);
-};
+import * as commands from "./commands";
+import * as logger from "./logger";
+import args from "./args";
-export const migrator = new Umzug({
- migrations: {
- glob: [
- `./${"[0-9]".repeat(14)}_*`,
- { cwd: prismaMigrations.migrationsDir },
- ],
- resolve(params) {
- const sqlFilePath = prismaMigrations.getMigrationFilePath(
- params.name,
- "sql"
- );
- const tsFilePath = prismaMigrations.getMigrationFilePath(
- params.name,
- "ts"
- );
-
- if (fs.existsSync(sqlFilePath)) {
- return {
- ...params,
- up: async () => {
- await prismaMigrations.setStarted(params.name, sqlFilePath);
- prismaMigrations.cliExecute(sqlFilePath);
- },
- };
- }
-
- if (fs.existsSync(tsFilePath)) {
- return {
- ...params,
- up: async () => {
- generateClient(params.name);
-
- // eslint-disable-next-line @typescript-eslint/no-var-requires
- const migration = require(tsFilePath);
-
- if (typeof migration.default !== "function") {
- throw new Error(
- `Migration file's ${tsFilePath} default export must be a function`
- );
- }
-
- await prismaMigrations.setStarted(params.name, tsFilePath);
- await migration.default();
- },
- };
- }
-
- throw new Error(
- `Couldn't find a migration.ts or migration.sql file in migrations/${params.name}`
- );
- },
- },
- context: {},
- storage: {
- logMigration(params) {
- return prismaMigrations.setApplied(params.name);
- },
- unlogMigration() {
- // this is for rollbacks, that we don't currently support
- throw new Error("unlogMigration is not implemented");
- },
- async executed() {
- const migrations = await prismaMigrations.getMigrations();
- return migrations
- .filter(prismaMigrations.isAppliedMigration)
- .map((row) => row.migration_name);
- },
- },
- logger: {
- info: (event) => {
- if (event.event === "migrating") {
- console.log(`Starting ${event.name}`);
- return;
- }
- if (event.event === "migrated") {
- console.log(`${event.name} done in ${event.durationSeconds}s`);
- return;
- }
- if (event.event === "up") {
- console.log(event.message);
- return;
- }
- if (event.event === "created") {
- console.log(`Created: ${event.path}`);
- return;
- }
- console.log(event);
- },
- error: (event) => console.error(event),
- warn: (event) => console.warn(event),
- debug: (event) => console.log(event),
- },
-});
-
-type StartedMigration = {
- migration: prismaMigrations.PrismaMigration;
- state: "applied" | "failed" | "rolled-back";
- fileState: "ok" | "changed" | "deleted";
-};
-
-type Status = {
- started: StartedMigration[];
- pending: MigrationMeta[];
+const failWith = (message: string) => {
+ logger.error(message);
+ logger.error("");
+ process.exit(1);
};
-const getStatus = async (): Promise<Status> => {
- const dbMigrations = await prismaMigrations.getMigrations();
-
- const started = dbMigrations.map((migration): StartedMigration => {
- const filePathTS = prismaMigrations.getMigrationFilePath(
- migration.migration_name,
- "ts"
- );
- const filePathSQL = prismaMigrations.getMigrationFilePath(
- migration.migration_name,
- "sql"
- );
- const filePath = fs.existsSync(filePathTS) ? filePathTS : filePathSQL;
-
- const fileState = fs.existsSync(filePath)
- ? prismaMigrations.getFileChecksum(filePath) === migration.checksum
- ? "ok"
- : "changed"
- : "deleted";
-
- const state = prismaMigrations.isAppliedMigration(migration)
- ? "applied"
- : prismaMigrations.isFailedMigration(migration)
- ? "failed"
- : "rolled-back";
-
- return { migration, state, fileState };
- });
+const USAGE = `Usage: migrations <command> [--dev]
- return {
- started,
- pending: await migrator.pending(),
- };
-};
+Commands:
+ create-schema <name> — Create a migration based on the changes in schema.prisma
+ create-data <name> — Create a migration that will change data rather than schema
+ migrate — Apply all pending migrations
+ reset — Clear the database and apply all migrations
+ status — Information about the state of the migrations
+ resolve — Mark a failed migration as applied or rolled-back
-// @todo: check what prisma does when a migrations fails, and provide similar functionality
-// - what ends up recorded in _prisma_migrations
-// - what happens in the following attempt to apply the migrations
-// - `migrate resolve` support
+Arguments
+ --dev — Lets the CLI know that it's running in a development environment
+`;
-export const runCLI = async () => {
- if (DEV_MODE) {
+const main = async () => {
+ if (args.dev) {
dotenv.config();
}
const command = args._[0];
if (command === undefined) {
- console.info(USAGE);
+ logger.info(USAGE);
return;
}
- const status = await getStatus();
-
if (command === "create-schema") {
const name = args._[1];
if (name === undefined) {
@@ -214,7 +45,7 @@ export const runCLI = async () => {
);
return;
}
- await commands.createSchema({ status, name });
+ await commands.createSchema({ name });
return;
}
@@ -226,259 +57,21 @@ export const runCLI = async () => {
);
return;
}
- await commands.createData({ status, name });
+ await commands.createData({ name });
return;
}
if (command === "migrate") {
- await commands.migrate({ status });
+ await commands.migrate();
return;
}
if (command === "status") {
- await commands.status({ status });
+ await commands.status();
return;
}
failWith(`Unknown command: ${command}`);
};
-const ensureNoFailed = (status: Status) => {
- const failed = status.started.filter(
- (migration) => migration.state === "failed"
- );
- if (failed.length > 0) {
- failWith(
- `There are failed migrations:\n${failed
- .map((item) => {
- let text = ` - ${item.migration.migration_name}`;
- const logs = (item.migration.logs || "").trim();
- if (logs.length > 0) {
- text += `\n\n${logs}\n`;
- }
- return text;
- })
- .join(
- "\n"
- )}\n\nPlease mark them as resolved or reset the database before you can proceed.`
- );
- }
-};
-
-const ensureNoPending = (status: Status) => {
- if (status.pending.length > 0) {
- failWith(
- `There are pending migrations:\n${status.pending
- .map((migration) => ` - ${migration.name}`)
- .join(
- "\n"
- )}\n\nPlease apply them first before you can create a new schema migration.\nIf this is a migration you created locally, you can also delete it if you want to start over.`
- );
- }
-};
-
-const ensureLastPending = async (migrationsName: string) => {
- const pending = await migrator.pending();
- const last = pending[pending.length - 1];
-
- if (last === undefined || last.name !== migrationsName) {
- failWith(
- "The migrations you've created did not appear as the last pending migration. This isn't supposed to happen. Please investigate / report this issue."
- );
- }
-};
-
-const commands = {
- async createSchema({ name, status }: { name: string; status: Status }) {
- ensureNoFailed(status);
-
- // Can't proceed if there are pending migrations.
- // We need the database to be up to date before we can do a diff.
- ensureNoPending(status);
-
- const sqlScript = prismaMigrations.cliDiff();
-
- if (
- sqlScript === undefined ||
- sqlScript
- .split("\n")
- // When there are no changes, prisma generates "-- This is an empty migration"
- .filter((line) => line.startsWith("--") === false)
- .join("\n")
- .trim().length === 0
- ) {
- console.info("No changes to apply");
- process.exit(0);
- }
-
- const migrationName = prismaMigrations.generateMigrationName(name);
-
- const filePath = prismaMigrations.getMigrationFilePath(
- migrationName,
- "sql"
- );
-
- writeFile(filePath, sqlScript);
- console.info(`Created: ${filePath}`);
-
- console.info("");
-
- await ensureLastPending(migrationName);
-
- console.info("The migration is ready. You can now apply it to a database.");
- console.info("");
- },
- async createData({ name, status }: { name: string; status: Status }) {
- ensureNoFailed(status);
-
- const migrationName = prismaMigrations.generateMigrationName(name);
-
- const filePath = prismaMigrations.getMigrationFilePath(migrationName, "ts");
- writeFile(filePath, fs.readFileSync(templateFilePath, "utf8"));
- console.info(`Created: ${filePath}`);
-
- let schemaContent = fs.readFileSync(
- prismaMigrations.schemaFilePath,
- "utf8"
- );
-
- schemaContent = `// DO NOT EDIT THIS FILE!
-// This a copy of your schema.prisma that corresponds to the state of the database
-// when all migrations up until this one are applied.
-// It's used to generate a Prisma Client for the migration.
-
-${schemaContent}`;
-
- schemaContent = schemaContent.replace(
- "// <output-placeholder-for-migrations>",
- `output = "client"`
- );
-
- const schemaFilePath = path.join(
- prismaMigrations.migrationsDir,
- migrationName,
- "schema.prisma"
- );
- writeFile(schemaFilePath, schemaContent);
- console.info(`Created: ${schemaFilePath}`);
-
- generateClient(migrationName);
- console.info(
- `Created: ${path.join(
- prismaMigrations.migrationsDir,
- migrationName,
- "client"
- )}`
- );
-
- console.info("");
-
- await ensureLastPending(migrationName);
-
- console.info(
- "The migrations templete is ready. You can now edit the migration.ts file and apply it to a database."
- );
- console.info("");
- },
- async migrate({ status }: { status: Status }) {
- ensureNoFailed(status);
-
- if (status.pending.length === 0) {
- console.info("No pending migrations\n");
- process.exit(0);
- }
-
- let locker: FileLocker | undefined;
- if (DEV_MODE) {
- locker = new FileLocker({ path: lockfilePath });
- }
-
- if (locker) {
- try {
- await locker.getLock();
- } catch (e) {
- failWith(
- `Could not acquire lock!
-This means that another process is already running migrations.
-If you're sure no other process is running, please delete the lockfile:
- ${lockfilePath}`
- );
- }
- }
-
- try {
- await migrator.up();
- } catch (err) {
- console.error();
- console.error(err.stack);
- console.error();
-
- const migrationName = (err.migration || undefined)?.name;
- if (typeof migrationName === "string") {
- prismaMigrations.setFailed(migrationName, err.stack);
- }
-
- process.exitCode = 1;
- } finally {
- if (locker) {
- await locker.releaseLock();
- }
- }
- },
- async status({ status }: { status: Status }) {
- console.info(
- `Applied or failed: ${status.started.length === 0 ? "none" : ""}`
- );
- for (const migration of status.started) {
- const fileState =
- migration.fileState === "ok" ? "" : `, file ${migration.fileState}`;
- console.info(
- ` - ${migration.migration.migration_name} (${migration.state}${fileState})`
- );
- const logs = (migration.migration.logs || "").trim();
- if (migration.state === "failed" && logs.length > 0) {
- console.info("");
- console.info(logs);
- console.info("");
- }
- }
-
- console.info("");
- console.info(`Pending: ${status.pending.length === 0 ? "none" : ""}`);
- for (const migration of status.pending) {
- console.info(` - ${migration.name}`);
- }
-
- console.info("");
- },
-};
-
-const failWith = (message: string) => {
- console.error(message);
- console.error("");
- process.exit(1);
-};
-
-const writeFile = (filePath: string, content: string) => {
- const dir = path.dirname(filePath);
- if (!fs.existsSync(dir)) {
- fs.mkdirSync(dir, { recursive: true });
- }
- fs.writeFileSync(filePath, content);
-};
-
-const USAGE = `Usage: migrations <command> [--dev]
-
-Commands:
- create-schema <name> — Create a migration based on the changes in schema.prisma
- create-data <name> — Create a migration that will change data rather than schema
- migrate — Apply all pending migrations
- reset — Clear the database and apply all migrations
- status — Information about the state of the migrations
- resolve — Mark a failed migration as applied or rolled-back
-
-Arguments
- --dev — Lets the CLI know that it's running in a development environment
-`;
-
-runCLI();
+main();
diff --git a/packages/prisma-client/migrations-cli/commands.ts b/packages/prisma-client/migrations-cli/commands.ts
new file mode 100644
index 0000000000..e16b3ed983
--- /dev/null
+++ b/packages/prisma-client/migrations-cli/commands.ts
@@ -0,0 +1,286 @@
+import { FileLocker, MigrationMeta } from "umzug";
+import fs from "node:fs";
+import path from "node:path";
+import * as prismaMigrations from "./prisma-migrations";
+import { umzug } from "./umzug";
+import * as logger from "./logger";
+import args from "./args";
+
+const templateFilePath = path.join(
+ prismaMigrations.migrationsDir,
+ "template.txt"
+);
+const lockfilePath = path.join(prismaMigrations.migrationsDir, "lockfile");
+
+const failWith = (message: string) => {
+ logger.error(message);
+ logger.error("");
+ process.exit(1);
+};
+
+const writeFile = (filePath: string, content: string) => {
+ const dir = path.dirname(filePath);
+ if (!fs.existsSync(dir)) {
+ fs.mkdirSync(dir, { recursive: true });
+ }
+ fs.writeFileSync(filePath, content);
+};
+
+type StartedMigration = {
+ migration: prismaMigrations.PrismaMigration;
+ state: "applied" | "failed" | "rolled-back";
+ fileState: "ok" | "changed" | "deleted";
+};
+
+type Status = {
+ started: StartedMigration[];
+ pending: MigrationMeta[];
+};
+
+const getStatus = async (): Promise<Status> => {
+ const dbMigrations = await prismaMigrations.getMigrations();
+
+ const started = dbMigrations.map((migration): StartedMigration => {
+ const filePathTS = prismaMigrations.getMigrationFilePath(
+ migration.migration_name,
+ "ts"
+ );
+ const filePathSQL = prismaMigrations.getMigrationFilePath(
+ migration.migration_name,
+ "sql"
+ );
+ const filePath = fs.existsSync(filePathTS) ? filePathTS : filePathSQL;
+
+ const fileState = fs.existsSync(filePath)
+ ? prismaMigrations.getFileChecksum(filePath) === migration.checksum
+ ? "ok"
+ : "changed"
+ : "deleted";
+
+ const state = prismaMigrations.isAppliedMigration(migration)
+ ? "applied"
+ : prismaMigrations.isFailedMigration(migration)
+ ? "failed"
+ : "rolled-back";
+
+ return { migration, state, fileState };
+ });
+
+ return {
+ started,
+ pending: await umzug.pending(),
+ };
+};
+
+const ensureNoFailed = (status: Status) => {
+ const failed = status.started.filter(
+ (migration) => migration.state === "failed"
+ );
+ if (failed.length > 0) {
+ failWith(
+ `There are failed migrations:\n${failed
+ .map((item) => {
+ let text = ` - ${item.migration.migration_name}`;
+ const logs = (item.migration.logs || "").trim();
+ if (logs.length > 0) {
+ text += `\n\n${logs}\n`;
+ }
+ return text;
+ })
+ .join(
+ "\n"
+ )}\n\nPlease mark them as resolved or reset the database before you can proceed.`
+ );
+ }
+};
+
+const ensureNoPending = (status: Status) => {
+ if (status.pending.length > 0) {
+ failWith(
+ `There are pending migrations:\n${status.pending
+ .map((migration) => ` - ${migration.name}`)
+ .join(
+ "\n"
+ )}\n\nPlease apply them first before you can create a new schema migration.\nIf this is a migration you created locally, you can also delete it if you want to start over.`
+ );
+ }
+};
+
+const ensureLastPending = async (migrationsName: string) => {
+ const pending = await umzug.pending();
+ const last = pending[pending.length - 1];
+
+ if (last === undefined || last.name !== migrationsName) {
+ failWith(
+ "The migrations you've created did not appear as the last pending migration. This isn't supposed to happen. Please investigate / report this issue."
+ );
+ }
+};
+
+export const createSchema = async ({ name }: { name: string }) => {
+ const status = await getStatus();
+
+ ensureNoFailed(status);
+
+ // Can't proceed if there are pending migrations.
+ // We need the database to be up to date before we can do a diff.
+ ensureNoPending(status);
+
+ const sqlScript = prismaMigrations.cliDiff();
+
+ if (
+ sqlScript === undefined ||
+ sqlScript
+ .split("\n")
+ // When there are no changes, prisma generates "-- This is an empty migration"
+ .filter((line) => line.startsWith("--") === false)
+ .join("\n")
+ .trim().length === 0
+ ) {
+ logger.info("No changes to apply");
+ process.exit(0);
+ }
+
+ const migrationName = prismaMigrations.generateMigrationName(name);
+
+ const filePath = prismaMigrations.getMigrationFilePath(migrationName, "sql");
+
+ writeFile(filePath, sqlScript);
+ logger.info(`Created: ${filePath}`);
+
+ logger.info("");
+
+ await ensureLastPending(migrationName);
+
+ logger.info("The migration is ready. You can now apply it to a database.");
+ logger.info("");
+};
+
+export const createData = async ({ name }: { name: string }) => {
+ const status = await getStatus();
+
+ ensureNoFailed(status);
+
+ const migrationName = prismaMigrations.generateMigrationName(name);
+
+ const filePath = prismaMigrations.getMigrationFilePath(migrationName, "ts");
+ writeFile(filePath, fs.readFileSync(templateFilePath, "utf8"));
+ logger.info(`Created: ${filePath}`);
+
+ let schemaContent = fs.readFileSync(prismaMigrations.schemaFilePath, "utf8");
+
+ schemaContent = `// DO NOT EDIT THIS FILE!
+// This a copy of your schema.prisma that corresponds to the state of the database
+// when all migrations up until this one are applied.
+// It's used to generate a Prisma Client for the migration.
+
+${schemaContent}`;
+
+ schemaContent = schemaContent.replace(
+ "// <output-placeholder-for-migrations>",
+ `output = "client"`
+ );
+
+ const schemaFilePath = path.join(
+ prismaMigrations.migrationsDir,
+ migrationName,
+ "schema.prisma"
+ );
+ writeFile(schemaFilePath, schemaContent);
+ logger.info(`Created: ${schemaFilePath}`);
+
+ prismaMigrations.generateMigrationClient(migrationName);
+ logger.info(
+ `Created: ${path.join(
+ prismaMigrations.migrationsDir,
+ migrationName,
+ "client"
+ )}`
+ );
+
+ logger.info("");
+
+ await ensureLastPending(migrationName);
+
+ logger.info(
+ "The migrations templete is ready. You can now edit the migration.ts file and apply it to a database."
+ );
+ logger.info("");
+};
+
+export const migrate = async () => {
+ const status = await getStatus();
+
+ ensureNoFailed(status);
+
+ if (status.pending.length === 0) {
+ logger.info("No pending migrations\n");
+ process.exit(0);
+ }
+
+ let locker: FileLocker | undefined;
+ if (args.dev) {
+ locker = new FileLocker({ path: lockfilePath });
+ }
+
+ if (locker) {
+ try {
+ await locker.getLock();
+ } catch (e) {
+ failWith(
+ `Could not acquire lock!
+This means that another process is already running migrations.
+If you're sure no other process is running, please delete the lockfile:
+ ${lockfilePath}`
+ );
+ }
+ }
+
+ try {
+ await umzug.up();
+ } catch (err) {
+ logger.error("");
+ logger.error(err.stack);
+ logger.error("");
+
+ const migrationName = (err.migration || undefined)?.name;
+ if (typeof migrationName === "string") {
+ prismaMigrations.setFailed(migrationName, err.stack);
+ }
+
+ process.exitCode = 1;
+ } finally {
+ if (locker) {
+ await locker.releaseLock();
+ }
+ }
+};
+
+export const status = async () => {
+ const status = await getStatus();
+
+ logger.info(
+ `Applied or failed: ${status.started.length === 0 ? "none" : ""}`
+ );
+ for (const migration of status.started) {
+ const fileState =
+ migration.fileState === "ok" ? "" : `, file ${migration.fileState}`;
+ logger.info(
+ ` - ${migration.migration.migration_name} (${migration.state}${fileState})`
+ );
+ const logs = (migration.migration.logs || "").trim();
+ if (migration.state === "failed" && logs.length > 0) {
+ logger.info("");
+ logger.info(logs);
+ logger.info("");
+ }
+ }
+
+ logger.info("");
+ logger.info(`Pending: ${status.pending.length === 0 ? "none" : ""}`);
+ for (const migration of status.pending) {
+ logger.info(` - ${migration.name}`);
+ }
+
+ logger.info("");
+};
diff --git a/packages/prisma-client/migrations-cli/logger.ts b/packages/prisma-client/migrations-cli/logger.ts
new file mode 100644
index 0000000000..2b778f27b4
--- /dev/null
+++ b/packages/prisma-client/migrations-cli/logger.ts
@@ -0,0 +1,20 @@
+/* eslint-disable no-console */
+
+// When you're adding a log for debugging use console.log as normal.
+// Use logger only for CLI output.
+
+export const info = (message: unknown) => {
+ console.info(message);
+};
+
+export const error = (message: unknown) => {
+ console.error(message);
+};
+
+export const warn = (message: unknown) => {
+ console.warn(message);
+};
+
+export const debug = (message: unknown) => {
+ console.log(message);
+};
diff --git a/packages/prisma-client/migrations-cli/prisma-migrations.ts b/packages/prisma-client/migrations-cli/prisma-migrations.ts
index 1b2131324c..5fd88acb75 100644
--- a/packages/prisma-client/migrations-cli/prisma-migrations.ts
+++ b/packages/prisma-client/migrations-cli/prisma-migrations.ts
@@ -253,3 +253,26 @@ export const cliExecute = (filePath: string) => {
schemaFilePath,
]);
};
+
+export const generateMigrationClient = (migrationName: string) => {
+ const migrationDir = path.join(migrationsDir, migrationName);
+
+ const schemaPath = path.join(migrationDir, "schema.prisma");
+ const clientPath = path.join(migrationDir, "client");
+
+ if (fs.existsSync(schemaPath) === false) {
+ const tsFilePath = getMigrationFilePath(migrationName, "ts");
+ if (fs.existsSync(tsFilePath)) {
+ throw new Error(
+ `Can't generate client for ${migrationName} because ${migrationName}/schema.prisma is missing`
+ );
+ }
+ return;
+ }
+
+ if (fs.existsSync(clientPath)) {
+ fs.rmSync(clientPath, { recursive: true });
+ }
+
+ cliGenerate(schemaPath);
+};
diff --git a/packages/prisma-client/migrations-cli/umzug.ts b/packages/prisma-client/migrations-cli/umzug.ts
new file mode 100755
index 0000000000..a9923ed6ec
--- /dev/null
+++ b/packages/prisma-client/migrations-cli/umzug.ts
@@ -0,0 +1,96 @@
+import { Umzug } from "umzug";
+import fs from "node:fs";
+import * as prismaMigrations from "./prisma-migrations";
+import * as logger from "./logger";
+
+export const umzug = new Umzug({
+ migrations: {
+ glob: [
+ `./${"[0-9]".repeat(14)}_*`,
+ { cwd: prismaMigrations.migrationsDir },
+ ],
+ resolve(params) {
+ const sqlFilePath = prismaMigrations.getMigrationFilePath(
+ params.name,
+ "sql"
+ );
+ const tsFilePath = prismaMigrations.getMigrationFilePath(
+ params.name,
+ "ts"
+ );
+
+ if (fs.existsSync(sqlFilePath)) {
+ return {
+ ...params,
+ up: async () => {
+ await prismaMigrations.setStarted(params.name, sqlFilePath);
+ prismaMigrations.cliExecute(sqlFilePath);
+ },
+ };
+ }
+
+ if (fs.existsSync(tsFilePath)) {
+ return {
+ ...params,
+ up: async () => {
+ prismaMigrations.generateMigrationClient(params.name);
+
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
+ const migration = require(tsFilePath);
+
+ if (typeof migration.default !== "function") {
+ throw new Error(
+ `Migration file's ${tsFilePath} default export must be a function`
+ );
+ }
+
+ await prismaMigrations.setStarted(params.name, tsFilePath);
+ await migration.default();
+ },
+ };
+ }
+
+ throw new Error(
+ `Couldn't find a migration.ts or migration.sql file in migrations/${params.name}`
+ );
+ },
+ },
+ context: {},
+ storage: {
+ logMigration(params) {
+ return prismaMigrations.setApplied(params.name);
+ },
+ unlogMigration() {
+ // this is for rollbacks, which we don't currently support
+ throw new Error("unlogMigration is not implemented");
+ },
+ async executed() {
+ const migrations = await prismaMigrations.getMigrations();
+ return migrations
+ .filter(prismaMigrations.isAppliedMigration)
+ .map((row) => row.migration_name);
+ },
+ },
+ logger: {
+ ...logger,
+ info: (event) => {
+ if (event.event === "migrating") {
+ logger.info(`Starting ${event.name}`);
+ return;
+ }
+ if (event.event === "migrated") {
+ logger.info(`${event.name} done in ${event.durationSeconds}s`);
+ return;
+ }
+ if (event.event === "up") {
+ logger.info(event.message);
+ return;
+ }
+ if (event.event === "created") {
+ logger.info(`Created: ${event.path}`);
+ return;
+ }
+ logger.info(event);
+ },
+ },
+});