-
Notifications
You must be signed in to change notification settings - Fork 1
/
migrate.js
71 lines (55 loc) · 1.95 KB
/
migrate.js
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
const ora = require("ora");
const { createClient } = require("./utils/graphcms-client");
const {
checkMigrationModelExists,
} = require("./utils/check-migration-model-exists");
const { createMigrationModel } = require("./utils/create-migration-model");
const { getCompletedMigrations } = require("./utils/get-completed-migrations");
const { getLocalMigrations } = require("./utils/get-local-migrations");
const { runMigration } = require("./utils/run-migration");
const migrate = async (directory = "graphcms-migrations", options) => {
const { endpoint, token, dryRun } = options;
const graphcmsClient = createClient({
endpoint,
token,
});
const spinner = ora("Checking migration model exists").start();
if (!(await checkMigrationModelExists({ graphcmsClient }))) {
spinner.text = "Migration model missing. Creating it";
await createMigrationModel({ endpoint, token });
}
spinner.text = "Fetching existing migrations from project";
const completedMigrations = await getCompletedMigrations({ graphcmsClient });
spinner.text = "Fetching local migrations";
const localMigrations = getLocalMigrations({ directory });
spinner.text = "Fetching migrations not yet ran";
const migrationsToRun = localMigrations.filter(
(fileName) => !completedMigrations.includes(fileName)
);
const migrationCount = migrationsToRun.length;
if (!migrationCount) {
spinner.info("No migrations to run");
process.exit();
}
spinner.succeed(`Detected ${migrationCount} pending migrations`);
if (dryRun) {
console.log("Dry mode enabled");
}
spinner.start("Running migrations");
for (const fileName of migrationsToRun) {
await runMigration({
endpoint,
token,
directory,
fileName,
dryRun,
graphcmsClient,
});
}
if (!dryRun) {
spinner.succeed(`Schema migration completed successfully`);
} else {
spinner.info(`Dry run completed successfully`);
}
};
exports.migrate = migrate;