-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.mjs
executable file
·97 lines (85 loc) · 2.86 KB
/
index.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env node
import { loadConfig } from "@platformatic/config";
import { platformaticDB } from "@platformatic/db";
import { Migrator } from "@platformatic/db/lib/migrator.mjs";
import chalk from "chalk";
import { execaCommand } from "execa";
import fs from "fs";
import path from "path";
import parser from "yargs-parser";
try {
const argv = parser(process.argv.slice(2), {
boolean: ["down", "up"],
default: {
description: "",
down: true,
schema: path.resolve("prisma", "schema.prisma"),
up: true,
},
});
// Create migration scripts
const [upMigration, downMigration] = await Promise.all([
execaCommand(
`npx prisma migrate diff --from-schema-datasource ${argv.schema} --to-schema-datamodel ${argv.schema} --script`
),
argv.down
? execaCommand(
`npx prisma migrate diff --from-schema-datamodel ${argv.schema} --to-schema-datasource ${argv.schema} --script`
)
: Promise.resolve(null),
]);
if (upMigration.stdout.trim() === "-- This is an empty migration.") {
console.log(chalk.green("No migrations are needed"));
} else {
const { configManager } = await loadConfig({}, "", platformaticDB);
const config = configManager.current;
if (config.migrations === undefined) {
throw new Error(
'Missing "migrations" property in your Platformatic configuration file'
);
}
const migrator = new Migrator(config.migrations, config.db, {
...console,
debug: () => undefined,
trace: () => undefined,
});
await migrator.checkMigrationsDirectoryExists();
const nextMigrationVersion = await migrator.getNextMigrationVersion();
const nextMigrationVersionStr =
migrator.convertVersionToStr(nextMigrationVersion);
const writeMigrationFile = async (content, action = "do") => {
const migrationFilename = getMigrationFilename(
nextMigrationVersionStr,
action,
argv.description
);
const migrationPath = path.resolve(
migrator.migrationDir,
migrationFilename
);
await fs.promises.writeFile(migrationPath, content);
console.log(`\t${chalk.underline(migrationPath)}`);
};
console.log(chalk.green("Generated migration files:"));
// Save migration files
await Promise.all([
migrator.close(),
argv.up ? writeMigrationFile(upMigration.stdout) : Promise.resolve(null),
argv.down
? writeMigrationFile(downMigration.stdout, "undo")
: Promise.resolve(null),
]);
}
} catch (error) {
console.log(chalk.red(error.message));
process.exitCode = 1;
}
/**
* Returns a valid Postgrator migration filename
* https://github.com/rickbergfalk/postgrator#usage
*/
function getMigrationFilename(version, action = "do", description = "") {
return `${version}.${action}.${
description?.length ? `${description}.` : ""
}sql`;
}