ic-mops 2.10.0 → 2.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/bundle/cli.tgz +0 -0
- package/cli.ts +24 -0
- package/commands/build.ts +10 -1
- package/commands/check-stable.ts +52 -21
- package/commands/check.ts +65 -52
- package/commands/migrate.ts +165 -0
- package/declarations/main/main.did +38 -0
- package/declarations/main/main.did.d.ts +36 -0
- package/declarations/main/main.did.js +36 -0
- package/dist/cli.js +18 -0
- package/dist/commands/build.js +5 -1
- package/dist/commands/check-stable.d.ts +1 -0
- package/dist/commands/check-stable.js +39 -21
- package/dist/commands/check.js +51 -42
- package/dist/commands/migrate.d.ts +2 -0
- package/dist/commands/migrate.js +104 -0
- package/dist/declarations/main/main.did +38 -0
- package/dist/declarations/main/main.did.d.ts +36 -0
- package/dist/declarations/main/main.did.js +36 -0
- package/dist/helpers/migrations.d.ts +10 -0
- package/dist/helpers/migrations.js +109 -0
- package/dist/helpers/resolve-canisters.d.ts +1 -1
- package/dist/helpers/resolve-canisters.js +15 -1
- package/dist/package.json +1 -1
- package/dist/tests/migrate.test.d.ts +1 -0
- package/dist/tests/migrate.test.js +160 -0
- package/dist/types.d.ts +7 -0
- package/helpers/migrations.ts +166 -0
- package/helpers/resolve-canisters.ts +17 -0
- package/package.json +1 -1
- package/tests/__snapshots__/migrate.test.ts.snap +119 -0
- package/tests/migrate/basic/deployed.most +12 -0
- package/tests/migrate/basic/migrations/20250101_000000_Init.mo +5 -0
- package/tests/migrate/basic/migrations/20250201_000000_AddName.mo +5 -0
- package/tests/migrate/basic/migrations/20250301_000000_AddEmail.mo +9 -0
- package/tests/migrate/basic/mops.toml +15 -0
- package/tests/migrate/basic/next-migration/.gitkeep +0 -0
- package/tests/migrate/basic/src/main.mo +11 -0
- package/tests/migrate/with-next/deployed.most +12 -0
- package/tests/migrate/with-next/migrations/20250101_000000_Init.mo +5 -0
- package/tests/migrate/with-next/migrations/20250201_000000_AddName.mo +5 -0
- package/tests/migrate/with-next/migrations/20250301_000000_AddEmail.mo +9 -0
- package/tests/migrate/with-next/mops.toml +15 -0
- package/tests/migrate/with-next/next-migration/20250401_000000_RenameId.mo +9 -0
- package/tests/migrate/with-next/src/main.mo +11 -0
- package/tests/migrate.test.ts +228 -0
- package/types.ts +8 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, symlinkSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { rm } from "node:fs/promises";
|
|
4
|
+
import chalk from "chalk";
|
|
5
|
+
import { cliError } from "../error.js";
|
|
6
|
+
import { resolveConfigPath } from "../mops.js";
|
|
7
|
+
const MIGRATIONS_TEMP_DIR = ".mops/.migrations";
|
|
8
|
+
export function getMigrationFiles(dir) {
|
|
9
|
+
if (!existsSync(dir)) {
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
return readdirSync(dir)
|
|
13
|
+
.filter((f) => f.endsWith(".mo"))
|
|
14
|
+
.sort();
|
|
15
|
+
}
|
|
16
|
+
export function getNextMigrationFile(nextDir) {
|
|
17
|
+
if (!existsSync(nextDir)) {
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
const files = readdirSync(nextDir).filter((f) => f.endsWith(".mo"));
|
|
21
|
+
if (files.length > 1) {
|
|
22
|
+
cliError(`next-migration directory must contain at most 1 .mo file, found ${files.length} in ${nextDir}`);
|
|
23
|
+
}
|
|
24
|
+
return files[0] ?? null;
|
|
25
|
+
}
|
|
26
|
+
export function validateNextMigrationOrder(chainDirOrFiles, nextFile) {
|
|
27
|
+
const chainFiles = typeof chainDirOrFiles === "string"
|
|
28
|
+
? getMigrationFiles(chainDirOrFiles)
|
|
29
|
+
: chainDirOrFiles;
|
|
30
|
+
const lastChainFile = chainFiles[chainFiles.length - 1];
|
|
31
|
+
if (lastChainFile && nextFile <= lastChainFile) {
|
|
32
|
+
cliError(`Next migration "${nextFile}" must sort after all files in the chain.\n` +
|
|
33
|
+
`Last chain file: "${lastChainFile}".\n` +
|
|
34
|
+
"Use a timestamp prefix (e.g. YYYYMMDD_HHMMSS_Name.mo) to ensure correct ordering.");
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
export function validateMigrationsConfig(migrations, canisterName) {
|
|
38
|
+
if (!migrations.chain) {
|
|
39
|
+
cliError(`[canisters.${canisterName}.migrations] is missing required field "chain"`);
|
|
40
|
+
}
|
|
41
|
+
for (const field of ["check-limit", "build-limit"]) {
|
|
42
|
+
const value = migrations[field];
|
|
43
|
+
if (value !== undefined && (!Number.isInteger(value) || value <= 0)) {
|
|
44
|
+
cliError(`[canisters.${canisterName}.migrations] ${field} must be a positive integer`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export async function prepareMigrationArgs(migrations, canisterName, mode, verbose) {
|
|
49
|
+
const noOp = {
|
|
50
|
+
migrationArgs: [],
|
|
51
|
+
cleanup: async () => { },
|
|
52
|
+
};
|
|
53
|
+
if (!migrations) {
|
|
54
|
+
return noOp;
|
|
55
|
+
}
|
|
56
|
+
validateMigrationsConfig(migrations, canisterName);
|
|
57
|
+
const chainDir = resolveConfigPath(migrations.chain);
|
|
58
|
+
const nextDir = migrations.next
|
|
59
|
+
? resolveConfigPath(migrations.next)
|
|
60
|
+
: undefined;
|
|
61
|
+
const nextFile = nextDir ? getNextMigrationFile(nextDir) : null;
|
|
62
|
+
if (!existsSync(chainDir) && !nextFile) {
|
|
63
|
+
cliError(`Migration chain directory not found: ${chainDir}\n` +
|
|
64
|
+
"Run `mops migrate new <Name>` to initialize the migration chain.");
|
|
65
|
+
}
|
|
66
|
+
const chainFiles = getMigrationFiles(chainDir);
|
|
67
|
+
if (nextFile) {
|
|
68
|
+
validateNextMigrationOrder(chainFiles, nextFile);
|
|
69
|
+
}
|
|
70
|
+
const allMigrations = chainFiles.map((f) => ({
|
|
71
|
+
file: f,
|
|
72
|
+
dir: chainDir,
|
|
73
|
+
}));
|
|
74
|
+
if (nextFile && nextDir) {
|
|
75
|
+
allMigrations.push({ file: nextFile, dir: nextDir });
|
|
76
|
+
}
|
|
77
|
+
const limit = mode === "check" ? migrations["check-limit"] : migrations["build-limit"];
|
|
78
|
+
const isTrimming = limit !== undefined && limit < allMigrations.length;
|
|
79
|
+
const needsTempDir = nextFile !== null || isTrimming;
|
|
80
|
+
if (!needsTempDir) {
|
|
81
|
+
return {
|
|
82
|
+
migrationArgs: [`--enhanced-migration=${chainDir}`],
|
|
83
|
+
cleanup: async () => { },
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
const tempDir = join(MIGRATIONS_TEMP_DIR, canisterName);
|
|
87
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
88
|
+
mkdirSync(tempDir, { recursive: true });
|
|
89
|
+
const filesToInclude = isTrimming
|
|
90
|
+
? allMigrations.slice(-limit)
|
|
91
|
+
: allMigrations;
|
|
92
|
+
for (const { file, dir } of filesToInclude) {
|
|
93
|
+
symlinkSync(resolve(dir, file), join(tempDir, file));
|
|
94
|
+
}
|
|
95
|
+
if (verbose) {
|
|
96
|
+
console.log(chalk.blue("migrations"), chalk.gray(`Prepared ${filesToInclude.length} migration(s) for ${canisterName}` +
|
|
97
|
+
(isTrimming ? ` (trimmed from ${allMigrations.length})` : "")));
|
|
98
|
+
}
|
|
99
|
+
const migrationArgs = [`--enhanced-migration=${tempDir}`];
|
|
100
|
+
if (isTrimming) {
|
|
101
|
+
migrationArgs.push("-A=M0254");
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
migrationArgs,
|
|
105
|
+
cleanup: async () => {
|
|
106
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
@@ -6,4 +6,4 @@ export declare function resolveSingleCanister(config: Config, canisterName?: str
|
|
|
6
6
|
canister: CanisterConfig;
|
|
7
7
|
};
|
|
8
8
|
export declare function looksLikeFile(arg: string): boolean;
|
|
9
|
-
export declare function validateCanisterArgs(canister: CanisterConfig, canisterName: string): void;
|
|
9
|
+
export declare function validateCanisterArgs(canister: CanisterConfig, canisterName: string, config?: Config): void;
|
|
@@ -39,8 +39,22 @@ export function looksLikeFile(arg) {
|
|
|
39
39
|
arg.includes("/") ||
|
|
40
40
|
arg.includes("\\"));
|
|
41
41
|
}
|
|
42
|
-
export function validateCanisterArgs(canister, canisterName) {
|
|
42
|
+
export function validateCanisterArgs(canister, canisterName, config) {
|
|
43
43
|
if (canister.args && typeof canister.args === "string") {
|
|
44
44
|
cliError(`Canister config 'args' should be an array of strings for canister ${canisterName}`);
|
|
45
45
|
}
|
|
46
|
+
if (!canister.migrations) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const flagSources = [
|
|
50
|
+
[`[canisters.${canisterName}].args`, canister.args],
|
|
51
|
+
["[moc].args", config?.moc?.args],
|
|
52
|
+
["[build].args", config?.build?.args],
|
|
53
|
+
];
|
|
54
|
+
for (const [section, args] of flagSources) {
|
|
55
|
+
if (args?.some((a) => a.startsWith("--enhanced-migration"))) {
|
|
56
|
+
cliError(`Canister '${canisterName}' has [migrations] config but --enhanced-migration in ${section}.\n` +
|
|
57
|
+
"Remove --enhanced-migration — it is managed automatically when [migrations] is configured.");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
46
60
|
}
|
package/dist/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { describe, expect, test, afterEach } from "@jest/globals";
|
|
2
|
+
import { readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { cp, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import { cli, cliSnapshot, normalizePaths } from "./helpers";
|
|
6
|
+
const normalizeTimestamp = (text) => text.replace(/\d{8}_\d{6}/g, "<TIMESTAMP>");
|
|
7
|
+
const fixturesDir = path.join(import.meta.dirname, "migrate");
|
|
8
|
+
describe("migrate", () => {
|
|
9
|
+
const tempDirs = [];
|
|
10
|
+
async function makeTempFixture(fixture) {
|
|
11
|
+
const src = path.join(fixturesDir, fixture);
|
|
12
|
+
const dest = path.join(fixturesDir, `_tmp_${fixture}_${Date.now()}`);
|
|
13
|
+
await cp(src, dest, { recursive: true });
|
|
14
|
+
tempDirs.push(dest);
|
|
15
|
+
return dest;
|
|
16
|
+
}
|
|
17
|
+
async function patchMigrations(cwd, extra) {
|
|
18
|
+
const tomlPath = path.join(cwd, "mops.toml");
|
|
19
|
+
const toml = readFileSync(tomlPath, "utf-8");
|
|
20
|
+
await writeFile(tomlPath, toml.replace('next = "next-migration"', `next = "next-migration"\n${extra}`));
|
|
21
|
+
}
|
|
22
|
+
afterEach(async () => {
|
|
23
|
+
for (const dir of tempDirs) {
|
|
24
|
+
await rm(dir, { recursive: true, force: true });
|
|
25
|
+
}
|
|
26
|
+
tempDirs.length = 0;
|
|
27
|
+
});
|
|
28
|
+
describe("migrate new", () => {
|
|
29
|
+
test("creates a migration file with timestamp and template", async () => {
|
|
30
|
+
const cwd = await makeTempFixture("basic");
|
|
31
|
+
const result = await cli(["migrate", "new", "AddPhone"], { cwd });
|
|
32
|
+
expect(result.exitCode).toBe(0);
|
|
33
|
+
const nextDir = path.join(cwd, "next-migration");
|
|
34
|
+
const files = readdirSync(nextDir).filter((f) => f.endsWith(".mo"));
|
|
35
|
+
expect(files).toHaveLength(1);
|
|
36
|
+
expect(files[0]).toMatch(/^\d{8}_\d{6}_AddPhone\.mo$/);
|
|
37
|
+
const content = readFileSync(path.join(nextDir, files[0]), "utf-8");
|
|
38
|
+
expect({
|
|
39
|
+
exitCode: result.exitCode,
|
|
40
|
+
stdout: normalizeTimestamp(normalizePaths(result.stdout)),
|
|
41
|
+
stderr: normalizePaths(result.stderr),
|
|
42
|
+
template: content,
|
|
43
|
+
}).toMatchSnapshot();
|
|
44
|
+
});
|
|
45
|
+
test("errors when next already has a file", async () => {
|
|
46
|
+
const cwd = await makeTempFixture("basic");
|
|
47
|
+
await cli(["migrate", "new", "First"], { cwd });
|
|
48
|
+
const result = await cli(["migrate", "new", "Second"], { cwd });
|
|
49
|
+
expect(result.exitCode).toBe(1);
|
|
50
|
+
expect(result.stderr).toMatch(/next migration already exists/i);
|
|
51
|
+
});
|
|
52
|
+
test("errors on invalid migration name", async () => {
|
|
53
|
+
const cwd = await makeTempFixture("basic");
|
|
54
|
+
for (const name of ["../evil", "has space", "123start", "foo/bar"]) {
|
|
55
|
+
const result = await cli(["migrate", "new", name], { cwd });
|
|
56
|
+
expect(result.exitCode).toBe(1);
|
|
57
|
+
expect(result.stderr).toMatch(/invalid migration name/i);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
test("errors when [migrations] not configured", async () => {
|
|
61
|
+
const cwd = await makeTempFixture("basic");
|
|
62
|
+
const tomlPath = path.join(cwd, "mops.toml");
|
|
63
|
+
const toml = readFileSync(tomlPath, "utf-8");
|
|
64
|
+
await writeFile(tomlPath, toml.replace(/\[canisters\.backend\.migrations\][\s\S]*$/, ""));
|
|
65
|
+
const result = await cli(["migrate", "new", "Test"], { cwd });
|
|
66
|
+
expect(result.exitCode).toBe(1);
|
|
67
|
+
expect(result.stderr).toMatch(/\[migrations\]/i);
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
describe("migrate freeze", () => {
|
|
71
|
+
test("moves the file from next to chain", async () => {
|
|
72
|
+
const cwd = await makeTempFixture("with-next");
|
|
73
|
+
const result = await cli(["migrate", "freeze"], { cwd });
|
|
74
|
+
expect(result.exitCode).toBe(0);
|
|
75
|
+
const nextFiles = readdirSync(path.join(cwd, "next-migration")).filter((f) => f.endsWith(".mo"));
|
|
76
|
+
expect(nextFiles).toHaveLength(0);
|
|
77
|
+
const chainFiles = readdirSync(path.join(cwd, "migrations")).filter((f) => f.endsWith(".mo"));
|
|
78
|
+
expect(chainFiles).toContain("20250401_000000_RenameId.mo");
|
|
79
|
+
expect({
|
|
80
|
+
exitCode: result.exitCode,
|
|
81
|
+
stdout: normalizePaths(result.stdout),
|
|
82
|
+
stderr: normalizePaths(result.stderr),
|
|
83
|
+
}).toMatchSnapshot();
|
|
84
|
+
});
|
|
85
|
+
test("errors when next is empty", async () => {
|
|
86
|
+
const cwd = await makeTempFixture("basic");
|
|
87
|
+
const result = await cli(["migrate", "freeze"], { cwd });
|
|
88
|
+
expect(result.exitCode).toBe(1);
|
|
89
|
+
expect(result.stderr).toMatch(/no next migration/i);
|
|
90
|
+
});
|
|
91
|
+
test("errors when next-migration does not sort last", async () => {
|
|
92
|
+
const cwd = await makeTempFixture("basic");
|
|
93
|
+
await writeFile(path.join(cwd, "next-migration", "00000000_000000_Early.mo"), "module {\n public func migration(_ : {}) : {} {\n {}\n }\n}\n");
|
|
94
|
+
const result = await cli(["migrate", "freeze"], { cwd });
|
|
95
|
+
expect(result.exitCode).toBe(1);
|
|
96
|
+
expect(result.stderr).toMatch(/must sort after/i);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
describe("check", () => {
|
|
100
|
+
test("check fails without next migration, passes with it", async () => {
|
|
101
|
+
const cwd = await makeTempFixture("with-next");
|
|
102
|
+
const nextFile = readdirSync(path.join(cwd, "next-migration")).find((f) => f.endsWith(".mo"));
|
|
103
|
+
const nextPath = path.join(cwd, "next-migration", nextFile);
|
|
104
|
+
const nextContent = readFileSync(nextPath, "utf-8");
|
|
105
|
+
await rm(nextPath);
|
|
106
|
+
await cliSnapshot(["check"], { cwd }, 1);
|
|
107
|
+
await writeFile(nextPath, nextContent);
|
|
108
|
+
await cliSnapshot(["check"], { cwd }, 0);
|
|
109
|
+
});
|
|
110
|
+
test("check with trimming shows reduced chain", async () => {
|
|
111
|
+
const cwd = await makeTempFixture("basic");
|
|
112
|
+
await patchMigrations(cwd, "check-limit = 2");
|
|
113
|
+
await cliSnapshot(["check", "--verbose"], { cwd }, 0);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
describe("build", () => {
|
|
117
|
+
test("build produces .most with full migration chain", async () => {
|
|
118
|
+
const cwd = await makeTempFixture("basic");
|
|
119
|
+
const result = await cli(["build"], { cwd });
|
|
120
|
+
expect(result.exitCode).toBe(0);
|
|
121
|
+
const most = readFileSync(path.join(cwd, ".mops", ".build", "backend.most"), "utf-8");
|
|
122
|
+
expect(most).toMatchSnapshot();
|
|
123
|
+
});
|
|
124
|
+
test("build with build-limit produces trimmed .most", async () => {
|
|
125
|
+
const cwd = await makeTempFixture("basic");
|
|
126
|
+
await patchMigrations(cwd, "build-limit = 2");
|
|
127
|
+
const result = await cli(["build"], { cwd });
|
|
128
|
+
expect(result.exitCode).toBe(0);
|
|
129
|
+
const most = readFileSync(path.join(cwd, ".mops", ".build", "backend.most"), "utf-8");
|
|
130
|
+
expect(most).toMatchSnapshot();
|
|
131
|
+
});
|
|
132
|
+
test("build-limit counts next migration as part of the chain", async () => {
|
|
133
|
+
const cwd = await makeTempFixture("with-next");
|
|
134
|
+
await patchMigrations(cwd, "build-limit = 2");
|
|
135
|
+
const result = await cli(["build"], { cwd });
|
|
136
|
+
expect(result.exitCode).toBe(0);
|
|
137
|
+
const most = readFileSync(path.join(cwd, ".mops", ".build", "backend.most"), "utf-8");
|
|
138
|
+
expect(most).toMatchSnapshot();
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
describe("stable check hint", () => {
|
|
142
|
+
test("stable check fails with hint when deployed.most is incompatible", async () => {
|
|
143
|
+
const cwd = await makeTempFixture("basic");
|
|
144
|
+
await writeFile(path.join(cwd, "deployed.most"), "// Version: 1.0.0\nactor {\n stable var a : Nat;\n stable var name : Int\n};\n");
|
|
145
|
+
await cliSnapshot(["check"], { cwd }, 1);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
describe("conflict detection", () => {
|
|
149
|
+
test("errors when both [migrations] and --enhanced-migration in args", async () => {
|
|
150
|
+
const cwd = await makeTempFixture("basic");
|
|
151
|
+
const tomlPath = path.join(cwd, "mops.toml");
|
|
152
|
+
const toml = readFileSync(tomlPath, "utf-8");
|
|
153
|
+
await writeFile(tomlPath, toml.replace("[canisters.backend.migrations]", 'args = ["--enhanced-migration=migrations"]\n\n[canisters.backend.migrations]'));
|
|
154
|
+
const result = await cli(["check"], { cwd });
|
|
155
|
+
expect(result.exitCode).toBe(1);
|
|
156
|
+
expect(result.stderr).toMatch(/--enhanced-migration/);
|
|
157
|
+
expect(result.stderr).toMatch(/managed automatically/i);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
});
|
package/dist/types.d.ts
CHANGED
|
@@ -34,6 +34,12 @@ export type Config = {
|
|
|
34
34
|
extra?: Record<string, string[]>;
|
|
35
35
|
};
|
|
36
36
|
};
|
|
37
|
+
export type MigrationsConfig = {
|
|
38
|
+
chain: string;
|
|
39
|
+
next?: string;
|
|
40
|
+
"check-limit"?: number;
|
|
41
|
+
"build-limit"?: number;
|
|
42
|
+
};
|
|
37
43
|
export type CanisterConfig = {
|
|
38
44
|
main?: string;
|
|
39
45
|
args?: string[];
|
|
@@ -43,6 +49,7 @@ export type CanisterConfig = {
|
|
|
43
49
|
path: string;
|
|
44
50
|
skipIfMissing?: boolean;
|
|
45
51
|
};
|
|
52
|
+
migrations?: MigrationsConfig;
|
|
46
53
|
};
|
|
47
54
|
export type Dependencies = Record<string, Dependency>;
|
|
48
55
|
export type Dependency = {
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, symlinkSync } from "node:fs";
|
|
2
|
+
import { join, resolve } from "node:path";
|
|
3
|
+
import { rm } from "node:fs/promises";
|
|
4
|
+
import chalk from "chalk";
|
|
5
|
+
import { cliError } from "../error.js";
|
|
6
|
+
import { resolveConfigPath } from "../mops.js";
|
|
7
|
+
import { MigrationsConfig } from "../types.js";
|
|
8
|
+
|
|
9
|
+
const MIGRATIONS_TEMP_DIR = ".mops/.migrations";
|
|
10
|
+
|
|
11
|
+
export interface MigrationArgsResult {
|
|
12
|
+
migrationArgs: string[];
|
|
13
|
+
cleanup: () => Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getMigrationFiles(dir: string): string[] {
|
|
17
|
+
if (!existsSync(dir)) {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
return readdirSync(dir)
|
|
21
|
+
.filter((f) => f.endsWith(".mo"))
|
|
22
|
+
.sort();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function getNextMigrationFile(nextDir: string): string | null {
|
|
26
|
+
if (!existsSync(nextDir)) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
const files = readdirSync(nextDir).filter((f) => f.endsWith(".mo"));
|
|
30
|
+
if (files.length > 1) {
|
|
31
|
+
cliError(
|
|
32
|
+
`next-migration directory must contain at most 1 .mo file, found ${files.length} in ${nextDir}`,
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
return files[0] ?? null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function validateNextMigrationOrder(
|
|
39
|
+
chainDirOrFiles: string | string[],
|
|
40
|
+
nextFile: string,
|
|
41
|
+
): void {
|
|
42
|
+
const chainFiles =
|
|
43
|
+
typeof chainDirOrFiles === "string"
|
|
44
|
+
? getMigrationFiles(chainDirOrFiles)
|
|
45
|
+
: chainDirOrFiles;
|
|
46
|
+
const lastChainFile = chainFiles[chainFiles.length - 1];
|
|
47
|
+
if (lastChainFile && nextFile <= lastChainFile) {
|
|
48
|
+
cliError(
|
|
49
|
+
`Next migration "${nextFile}" must sort after all files in the chain.\n` +
|
|
50
|
+
`Last chain file: "${lastChainFile}".\n` +
|
|
51
|
+
"Use a timestamp prefix (e.g. YYYYMMDD_HHMMSS_Name.mo) to ensure correct ordering.",
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function validateMigrationsConfig(
|
|
57
|
+
migrations: MigrationsConfig,
|
|
58
|
+
canisterName: string,
|
|
59
|
+
): void {
|
|
60
|
+
if (!migrations.chain) {
|
|
61
|
+
cliError(
|
|
62
|
+
`[canisters.${canisterName}.migrations] is missing required field "chain"`,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
for (const field of ["check-limit", "build-limit"] as const) {
|
|
66
|
+
const value = migrations[field];
|
|
67
|
+
if (value !== undefined && (!Number.isInteger(value) || value <= 0)) {
|
|
68
|
+
cliError(
|
|
69
|
+
`[canisters.${canisterName}.migrations] ${field} must be a positive integer`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export async function prepareMigrationArgs(
|
|
76
|
+
migrations: MigrationsConfig | undefined,
|
|
77
|
+
canisterName: string,
|
|
78
|
+
mode: "check" | "build",
|
|
79
|
+
verbose?: boolean,
|
|
80
|
+
): Promise<MigrationArgsResult> {
|
|
81
|
+
const noOp: MigrationArgsResult = {
|
|
82
|
+
migrationArgs: [],
|
|
83
|
+
cleanup: async () => {},
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
if (!migrations) {
|
|
87
|
+
return noOp;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
validateMigrationsConfig(migrations, canisterName);
|
|
91
|
+
|
|
92
|
+
const chainDir = resolveConfigPath(migrations.chain);
|
|
93
|
+
const nextDir = migrations.next
|
|
94
|
+
? resolveConfigPath(migrations.next)
|
|
95
|
+
: undefined;
|
|
96
|
+
const nextFile = nextDir ? getNextMigrationFile(nextDir) : null;
|
|
97
|
+
|
|
98
|
+
if (!existsSync(chainDir) && !nextFile) {
|
|
99
|
+
cliError(
|
|
100
|
+
`Migration chain directory not found: ${chainDir}\n` +
|
|
101
|
+
"Run `mops migrate new <Name>` to initialize the migration chain.",
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const chainFiles = getMigrationFiles(chainDir);
|
|
106
|
+
|
|
107
|
+
if (nextFile) {
|
|
108
|
+
validateNextMigrationOrder(chainFiles, nextFile);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Treat chain + next as one virtual merged list
|
|
112
|
+
type MigrationEntry = { file: string; dir: string };
|
|
113
|
+
const allMigrations: MigrationEntry[] = chainFiles.map((f) => ({
|
|
114
|
+
file: f,
|
|
115
|
+
dir: chainDir,
|
|
116
|
+
}));
|
|
117
|
+
if (nextFile && nextDir) {
|
|
118
|
+
allMigrations.push({ file: nextFile, dir: nextDir });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const limit =
|
|
122
|
+
mode === "check" ? migrations["check-limit"] : migrations["build-limit"];
|
|
123
|
+
const isTrimming = limit !== undefined && limit < allMigrations.length;
|
|
124
|
+
const needsTempDir = nextFile !== null || isTrimming;
|
|
125
|
+
|
|
126
|
+
if (!needsTempDir) {
|
|
127
|
+
return {
|
|
128
|
+
migrationArgs: [`--enhanced-migration=${chainDir}`],
|
|
129
|
+
cleanup: async () => {},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const tempDir = join(MIGRATIONS_TEMP_DIR, canisterName);
|
|
134
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
135
|
+
mkdirSync(tempDir, { recursive: true });
|
|
136
|
+
|
|
137
|
+
const filesToInclude = isTrimming
|
|
138
|
+
? allMigrations.slice(-limit)
|
|
139
|
+
: allMigrations;
|
|
140
|
+
|
|
141
|
+
for (const { file, dir } of filesToInclude) {
|
|
142
|
+
symlinkSync(resolve(dir, file), join(tempDir, file));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (verbose) {
|
|
146
|
+
console.log(
|
|
147
|
+
chalk.blue("migrations"),
|
|
148
|
+
chalk.gray(
|
|
149
|
+
`Prepared ${filesToInclude.length} migration(s) for ${canisterName}` +
|
|
150
|
+
(isTrimming ? ` (trimmed from ${allMigrations.length})` : ""),
|
|
151
|
+
),
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const migrationArgs = [`--enhanced-migration=${tempDir}`];
|
|
156
|
+
if (isTrimming) {
|
|
157
|
+
migrationArgs.push("-A=M0254");
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
migrationArgs,
|
|
162
|
+
cleanup: async () => {
|
|
163
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
}
|
|
@@ -74,10 +74,27 @@ export function looksLikeFile(arg: string): boolean {
|
|
|
74
74
|
export function validateCanisterArgs(
|
|
75
75
|
canister: CanisterConfig,
|
|
76
76
|
canisterName: string,
|
|
77
|
+
config?: Config,
|
|
77
78
|
): void {
|
|
78
79
|
if (canister.args && typeof canister.args === "string") {
|
|
79
80
|
cliError(
|
|
80
81
|
`Canister config 'args' should be an array of strings for canister ${canisterName}`,
|
|
81
82
|
);
|
|
82
83
|
}
|
|
84
|
+
if (!canister.migrations) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const flagSources: [string, string[] | undefined][] = [
|
|
88
|
+
[`[canisters.${canisterName}].args`, canister.args],
|
|
89
|
+
["[moc].args", config?.moc?.args],
|
|
90
|
+
["[build].args", config?.build?.args],
|
|
91
|
+
];
|
|
92
|
+
for (const [section, args] of flagSources) {
|
|
93
|
+
if (args?.some((a) => a.startsWith("--enhanced-migration"))) {
|
|
94
|
+
cliError(
|
|
95
|
+
`Canister '${canisterName}' has [migrations] config but --enhanced-migration in ${section}.\n` +
|
|
96
|
+
"Remove --enhanced-migration — it is managed automatically when [migrations] is configured.",
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
83
100
|
}
|
package/package.json
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
|
|
2
|
+
|
|
3
|
+
exports[`migrate build build produces .most with full migration chain 1`] = `
|
|
4
|
+
"// Version: 4.0.0
|
|
5
|
+
{
|
|
6
|
+
"20250101_000000_Init" : {} -> {a : Nat};
|
|
7
|
+
"20250201_000000_AddName" : (old : {a : Nat}) -> {a : Nat; name : Text};
|
|
8
|
+
"20250301_000000_AddEmail" :
|
|
9
|
+
(old : {a : Nat; name : Text}) -> {a : Nat; email : Text; name : Text}
|
|
10
|
+
}
|
|
11
|
+
actor {
|
|
12
|
+
stable a : Nat;
|
|
13
|
+
stable email : Text;
|
|
14
|
+
stable name : Text
|
|
15
|
+
};
|
|
16
|
+
"
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
exports[`migrate build build with build-limit produces trimmed .most 1`] = `
|
|
20
|
+
"// Version: 4.0.0
|
|
21
|
+
{
|
|
22
|
+
"20250201_000000_AddName" : (old : {a : Nat}) -> {a : Nat; name : Text};
|
|
23
|
+
"20250301_000000_AddEmail" :
|
|
24
|
+
(old : {a : Nat; name : Text}) -> {a : Nat; email : Text; name : Text}
|
|
25
|
+
}
|
|
26
|
+
actor {
|
|
27
|
+
stable a : Nat;
|
|
28
|
+
stable email : Text;
|
|
29
|
+
stable name : Text
|
|
30
|
+
};
|
|
31
|
+
"
|
|
32
|
+
`;
|
|
33
|
+
|
|
34
|
+
exports[`migrate build build-limit counts next migration as part of the chain 1`] = `
|
|
35
|
+
"// Version: 4.0.0
|
|
36
|
+
{
|
|
37
|
+
"20250301_000000_AddEmail" :
|
|
38
|
+
(old : {a : Nat; name : Text}) -> {a : Nat; email : Text; name : Text};
|
|
39
|
+
"20250401_000000_RenameId" :
|
|
40
|
+
(old : {a : Nat; email : Text; name : Text}) ->
|
|
41
|
+
{email : Text; id : Nat; name : Text}
|
|
42
|
+
}
|
|
43
|
+
actor {
|
|
44
|
+
stable email : Text;
|
|
45
|
+
stable id : Nat;
|
|
46
|
+
stable name : Text
|
|
47
|
+
};
|
|
48
|
+
"
|
|
49
|
+
`;
|
|
50
|
+
|
|
51
|
+
exports[`migrate check check fails without next migration, passes with it 1`] = `
|
|
52
|
+
{
|
|
53
|
+
"exitCode": 1,
|
|
54
|
+
"stderr": "src/main.mo:3.1-11.2: Compatibility error [M0169], the stable variable \`a\` of version \`20250301_000000_AddEmail\` cannot be implicitly discarded. The variable can only be dropped by an explicit migration function, please see https://internetcomputer.org/docs/motoko/fundamentals/actors/compatibility#explicit-migration-using-a-migration-function
|
|
55
|
+
migrations/20250101_000000_Init.mo:0.1: warning [M0254], initial actor requires field \`id\` of type
|
|
56
|
+
Nat
|
|
57
|
+
✗ Check failed for canister backend (exit code: 1)",
|
|
58
|
+
"stdout": "",
|
|
59
|
+
}
|
|
60
|
+
`;
|
|
61
|
+
|
|
62
|
+
exports[`migrate check check fails without next migration, passes with it 2`] = `
|
|
63
|
+
{
|
|
64
|
+
"exitCode": 0,
|
|
65
|
+
"stderr": "",
|
|
66
|
+
"stdout": "✓ backend
|
|
67
|
+
✓ Stable compatibility check passed for canister 'backend'",
|
|
68
|
+
}
|
|
69
|
+
`;
|
|
70
|
+
|
|
71
|
+
exports[`migrate check check with trimming shows reduced chain 1`] = `
|
|
72
|
+
{
|
|
73
|
+
"exitCode": 0,
|
|
74
|
+
"stderr": "",
|
|
75
|
+
"stdout": "check Using --all-libs for richer diagnostics
|
|
76
|
+
migrations Prepared 2 migration(s) for backend (trimmed from 3)
|
|
77
|
+
check Checking canister backend:
|
|
78
|
+
<CACHE>moc-wrapper ["src/main.mo","--check","--all-libs","--default-persistent-actors","--enhanced-migration=.mops/.migrations/backend","-A=M0254"]
|
|
79
|
+
✓ backend
|
|
80
|
+
check-stable Generating stable types for src/main.mo
|
|
81
|
+
<CACHE>moc-wrapper ["--stable-types","-o",".mops/.check-stable/new.wasm","src/main.mo","--default-persistent-actors","--enhanced-migration=.mops/.migrations/backend","-A=M0254"]
|
|
82
|
+
check-stable Comparing deployed.most ↔ .mops/.check-stable/new.most
|
|
83
|
+
<CACHE>moc-wrapper ["--stable-compatible","deployed.most",".mops/.check-stable/new.most"]
|
|
84
|
+
✓ Stable compatibility check passed for canister 'backend'",
|
|
85
|
+
}
|
|
86
|
+
`;
|
|
87
|
+
|
|
88
|
+
exports[`migrate migrate freeze moves the file from next to chain 1`] = `
|
|
89
|
+
{
|
|
90
|
+
"exitCode": 0,
|
|
91
|
+
"stderr": "",
|
|
92
|
+
"stdout": "✓ Frozen migration: 20250401_000000_RenameId.mo → migrations/",
|
|
93
|
+
}
|
|
94
|
+
`;
|
|
95
|
+
|
|
96
|
+
exports[`migrate migrate new creates a migration file with timestamp and template 1`] = `
|
|
97
|
+
{
|
|
98
|
+
"exitCode": 0,
|
|
99
|
+
"stderr": "",
|
|
100
|
+
"stdout": "✓ Created migration: next-migration/<TIMESTAMP>_AddPhone.mo",
|
|
101
|
+
"template": "module {
|
|
102
|
+
public func migration(old : {}) : {} {
|
|
103
|
+
{}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
",
|
|
107
|
+
}
|
|
108
|
+
`;
|
|
109
|
+
|
|
110
|
+
exports[`migrate stable check hint stable check fails with hint when deployed.most is incompatible 1`] = `
|
|
111
|
+
{
|
|
112
|
+
"exitCode": 1,
|
|
113
|
+
"stderr": "(unknown location): Compatibility error [M0169], the stable variable \`a\` of the previous version cannot be implicitly discarded. The variable can only be dropped by an explicit migration function, please see https://internetcomputer.org/docs/motoko/fundamentals/actors/compatibility#explicit-migration-using-a-migration-function
|
|
114
|
+
(unknown location): Compatibility error [M0169], the stable variable \`name\` of the previous version cannot be implicitly discarded. The variable can only be dropped by an explicit migration function, please see https://internetcomputer.org/docs/motoko/fundamentals/actors/compatibility#explicit-migration-using-a-migration-function
|
|
115
|
+
Hint: You may need a migration. Run \`mops migrate new <Name>\` to create one.
|
|
116
|
+
✗ Stable compatibility check failed for canister 'backend'",
|
|
117
|
+
"stdout": "✓ backend",
|
|
118
|
+
}
|
|
119
|
+
`;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// Version: 4.0.0
|
|
2
|
+
{
|
|
3
|
+
"20250101_000000_Init" : {} -> {a : Nat};
|
|
4
|
+
"20250201_000000_AddName" : (old : {a : Nat}) -> {a : Nat; name : Text};
|
|
5
|
+
"20250301_000000_AddEmail" :
|
|
6
|
+
(old : {a : Nat; name : Text}) -> {a : Nat; email : Text; name : Text}
|
|
7
|
+
}
|
|
8
|
+
actor {
|
|
9
|
+
stable a : Nat;
|
|
10
|
+
stable email : Text;
|
|
11
|
+
stable name : Text
|
|
12
|
+
};
|