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.
Files changed (48) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/bundle/cli.tgz +0 -0
  3. package/cli.ts +24 -0
  4. package/commands/build.ts +10 -1
  5. package/commands/check-stable.ts +52 -21
  6. package/commands/check.ts +65 -52
  7. package/commands/migrate.ts +165 -0
  8. package/declarations/main/main.did +38 -0
  9. package/declarations/main/main.did.d.ts +36 -0
  10. package/declarations/main/main.did.js +36 -0
  11. package/dist/cli.js +18 -0
  12. package/dist/commands/build.js +5 -1
  13. package/dist/commands/check-stable.d.ts +1 -0
  14. package/dist/commands/check-stable.js +39 -21
  15. package/dist/commands/check.js +51 -42
  16. package/dist/commands/migrate.d.ts +2 -0
  17. package/dist/commands/migrate.js +104 -0
  18. package/dist/declarations/main/main.did +38 -0
  19. package/dist/declarations/main/main.did.d.ts +36 -0
  20. package/dist/declarations/main/main.did.js +36 -0
  21. package/dist/helpers/migrations.d.ts +10 -0
  22. package/dist/helpers/migrations.js +109 -0
  23. package/dist/helpers/resolve-canisters.d.ts +1 -1
  24. package/dist/helpers/resolve-canisters.js +15 -1
  25. package/dist/package.json +1 -1
  26. package/dist/tests/migrate.test.d.ts +1 -0
  27. package/dist/tests/migrate.test.js +160 -0
  28. package/dist/types.d.ts +7 -0
  29. package/helpers/migrations.ts +166 -0
  30. package/helpers/resolve-canisters.ts +17 -0
  31. package/package.json +1 -1
  32. package/tests/__snapshots__/migrate.test.ts.snap +119 -0
  33. package/tests/migrate/basic/deployed.most +12 -0
  34. package/tests/migrate/basic/migrations/20250101_000000_Init.mo +5 -0
  35. package/tests/migrate/basic/migrations/20250201_000000_AddName.mo +5 -0
  36. package/tests/migrate/basic/migrations/20250301_000000_AddEmail.mo +9 -0
  37. package/tests/migrate/basic/mops.toml +15 -0
  38. package/tests/migrate/basic/next-migration/.gitkeep +0 -0
  39. package/tests/migrate/basic/src/main.mo +11 -0
  40. package/tests/migrate/with-next/deployed.most +12 -0
  41. package/tests/migrate/with-next/migrations/20250101_000000_Init.mo +5 -0
  42. package/tests/migrate/with-next/migrations/20250201_000000_AddName.mo +5 -0
  43. package/tests/migrate/with-next/migrations/20250301_000000_AddEmail.mo +9 -0
  44. package/tests/migrate/with-next/mops.toml +15 -0
  45. package/tests/migrate/with-next/next-migration/20250401_000000_RenameId.mo +9 -0
  46. package/tests/migrate/with-next/src/main.mo +11 -0
  47. package/tests/migrate.test.ts +228 -0
  48. package/types.ts +8 -0
@@ -0,0 +1,5 @@
1
+ module {
2
+ public func migration(old : { a : Nat }) : { a : Nat; name : Text } {
3
+ { old with name = "" };
4
+ };
5
+ };
@@ -0,0 +1,9 @@
1
+ module {
2
+ public func migration(old : { a : Nat; name : Text }) : {
3
+ a : Nat;
4
+ name : Text;
5
+ email : Text;
6
+ } {
7
+ { old with email = "" };
8
+ };
9
+ };
@@ -0,0 +1,15 @@
1
+ [toolchain]
2
+ moc = "1.5.0"
3
+
4
+ [moc]
5
+ args = ["--default-persistent-actors"]
6
+
7
+ [canisters.backend]
8
+ main = "src/main.mo"
9
+
10
+ [canisters.backend.migrations]
11
+ chain = "migrations"
12
+ next = "next-migration"
13
+
14
+ [canisters.backend.check-stable]
15
+ path = "deployed.most"
File without changes
@@ -0,0 +1,11 @@
1
+ import Prim "mo:prim";
2
+
3
+ actor {
4
+ let a : Nat;
5
+ let name : Text;
6
+ let email : Text;
7
+
8
+ public func check() : async () {
9
+ Prim.debugPrint(debug_show { a; name; email });
10
+ };
11
+ };
@@ -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
+ };
@@ -0,0 +1,5 @@
1
+ module {
2
+ public func migration(_ : {}) : { a : Nat } {
3
+ { a = 0 };
4
+ };
5
+ };
@@ -0,0 +1,5 @@
1
+ module {
2
+ public func migration(old : { a : Nat }) : { a : Nat; name : Text } {
3
+ { old with name = "" };
4
+ };
5
+ };
@@ -0,0 +1,9 @@
1
+ module {
2
+ public func migration(old : { a : Nat; name : Text }) : {
3
+ a : Nat;
4
+ name : Text;
5
+ email : Text;
6
+ } {
7
+ { old with email = "" };
8
+ };
9
+ };
@@ -0,0 +1,15 @@
1
+ [toolchain]
2
+ moc = "1.5.0"
3
+
4
+ [moc]
5
+ args = ["--default-persistent-actors"]
6
+
7
+ [canisters.backend]
8
+ main = "src/main.mo"
9
+
10
+ [canisters.backend.migrations]
11
+ chain = "migrations"
12
+ next = "next-migration"
13
+
14
+ [canisters.backend.check-stable]
15
+ path = "deployed.most"
@@ -0,0 +1,9 @@
1
+ module {
2
+ public func migration(old : { a : Nat; name : Text; email : Text }) : {
3
+ id : Nat;
4
+ name : Text;
5
+ email : Text;
6
+ } {
7
+ { id = old.a; name = old.name; email = old.email };
8
+ };
9
+ };
@@ -0,0 +1,11 @@
1
+ import Prim "mo:prim";
2
+
3
+ actor {
4
+ let id : Nat;
5
+ let name : Text;
6
+ let email : Text;
7
+
8
+ public func check() : async () {
9
+ Prim.debugPrint(debug_show { id; name; email });
10
+ };
11
+ };
@@ -0,0 +1,228 @@
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
+
7
+ const normalizeTimestamp = (text: string) =>
8
+ text.replace(/\d{8}_\d{6}/g, "<TIMESTAMP>");
9
+
10
+ const fixturesDir = path.join(import.meta.dirname, "migrate");
11
+
12
+ describe("migrate", () => {
13
+ const tempDirs: string[] = [];
14
+
15
+ async function makeTempFixture(fixture: string): Promise<string> {
16
+ const src = path.join(fixturesDir, fixture);
17
+ const dest = path.join(fixturesDir, `_tmp_${fixture}_${Date.now()}`);
18
+ await cp(src, dest, { recursive: true });
19
+ tempDirs.push(dest);
20
+ return dest;
21
+ }
22
+
23
+ async function patchMigrations(cwd: string, extra: string): Promise<void> {
24
+ const tomlPath = path.join(cwd, "mops.toml");
25
+ const toml = readFileSync(tomlPath, "utf-8");
26
+ await writeFile(
27
+ tomlPath,
28
+ toml.replace(
29
+ 'next = "next-migration"',
30
+ `next = "next-migration"\n${extra}`,
31
+ ),
32
+ );
33
+ }
34
+
35
+ afterEach(async () => {
36
+ for (const dir of tempDirs) {
37
+ await rm(dir, { recursive: true, force: true });
38
+ }
39
+ tempDirs.length = 0;
40
+ });
41
+
42
+ describe("migrate new", () => {
43
+ test("creates a migration file with timestamp and template", async () => {
44
+ const cwd = await makeTempFixture("basic");
45
+ const result = await cli(["migrate", "new", "AddPhone"], { cwd });
46
+ expect(result.exitCode).toBe(0);
47
+
48
+ const nextDir = path.join(cwd, "next-migration");
49
+ const files = readdirSync(nextDir).filter((f) => f.endsWith(".mo"));
50
+ expect(files).toHaveLength(1);
51
+ expect(files[0]).toMatch(/^\d{8}_\d{6}_AddPhone\.mo$/);
52
+
53
+ const content = readFileSync(path.join(nextDir, files[0]!), "utf-8");
54
+
55
+ expect({
56
+ exitCode: result.exitCode,
57
+ stdout: normalizeTimestamp(normalizePaths(result.stdout)),
58
+ stderr: normalizePaths(result.stderr),
59
+ template: content,
60
+ }).toMatchSnapshot();
61
+ });
62
+
63
+ test("errors when next already has a file", async () => {
64
+ const cwd = await makeTempFixture("basic");
65
+ await cli(["migrate", "new", "First"], { cwd });
66
+ const result = await cli(["migrate", "new", "Second"], { cwd });
67
+ expect(result.exitCode).toBe(1);
68
+ expect(result.stderr).toMatch(/next migration already exists/i);
69
+ });
70
+
71
+ test("errors on invalid migration name", async () => {
72
+ const cwd = await makeTempFixture("basic");
73
+ for (const name of ["../evil", "has space", "123start", "foo/bar"]) {
74
+ const result = await cli(["migrate", "new", name], { cwd });
75
+ expect(result.exitCode).toBe(1);
76
+ expect(result.stderr).toMatch(/invalid migration name/i);
77
+ }
78
+ });
79
+
80
+ test("errors when [migrations] not configured", async () => {
81
+ const cwd = await makeTempFixture("basic");
82
+ const tomlPath = path.join(cwd, "mops.toml");
83
+ const toml = readFileSync(tomlPath, "utf-8");
84
+ await writeFile(
85
+ tomlPath,
86
+ toml.replace(/\[canisters\.backend\.migrations\][\s\S]*$/, ""),
87
+ );
88
+ const result = await cli(["migrate", "new", "Test"], { cwd });
89
+ expect(result.exitCode).toBe(1);
90
+ expect(result.stderr).toMatch(/\[migrations\]/i);
91
+ });
92
+ });
93
+
94
+ describe("migrate freeze", () => {
95
+ test("moves the file from next to chain", async () => {
96
+ const cwd = await makeTempFixture("with-next");
97
+ const result = await cli(["migrate", "freeze"], { cwd });
98
+ expect(result.exitCode).toBe(0);
99
+
100
+ const nextFiles = readdirSync(path.join(cwd, "next-migration")).filter(
101
+ (f) => f.endsWith(".mo"),
102
+ );
103
+ expect(nextFiles).toHaveLength(0);
104
+
105
+ const chainFiles = readdirSync(path.join(cwd, "migrations")).filter((f) =>
106
+ f.endsWith(".mo"),
107
+ );
108
+ expect(chainFiles).toContain("20250401_000000_RenameId.mo");
109
+
110
+ expect({
111
+ exitCode: result.exitCode,
112
+ stdout: normalizePaths(result.stdout),
113
+ stderr: normalizePaths(result.stderr),
114
+ }).toMatchSnapshot();
115
+ });
116
+
117
+ test("errors when next is empty", async () => {
118
+ const cwd = await makeTempFixture("basic");
119
+ const result = await cli(["migrate", "freeze"], { cwd });
120
+ expect(result.exitCode).toBe(1);
121
+ expect(result.stderr).toMatch(/no next migration/i);
122
+ });
123
+
124
+ test("errors when next-migration does not sort last", async () => {
125
+ const cwd = await makeTempFixture("basic");
126
+ await writeFile(
127
+ path.join(cwd, "next-migration", "00000000_000000_Early.mo"),
128
+ "module {\n public func migration(_ : {}) : {} {\n {}\n }\n}\n",
129
+ );
130
+ const result = await cli(["migrate", "freeze"], { cwd });
131
+ expect(result.exitCode).toBe(1);
132
+ expect(result.stderr).toMatch(/must sort after/i);
133
+ });
134
+ });
135
+
136
+ describe("check", () => {
137
+ test("check fails without next migration, passes with it", async () => {
138
+ const cwd = await makeTempFixture("with-next");
139
+ const nextFile = readdirSync(path.join(cwd, "next-migration")).find((f) =>
140
+ f.endsWith(".mo"),
141
+ )!;
142
+ const nextPath = path.join(cwd, "next-migration", nextFile);
143
+ const nextContent = readFileSync(nextPath, "utf-8");
144
+ await rm(nextPath);
145
+
146
+ await cliSnapshot(["check"], { cwd }, 1);
147
+
148
+ await writeFile(nextPath, nextContent);
149
+ await cliSnapshot(["check"], { cwd }, 0);
150
+ });
151
+
152
+ test("check with trimming shows reduced chain", async () => {
153
+ const cwd = await makeTempFixture("basic");
154
+ await patchMigrations(cwd, "check-limit = 2");
155
+ await cliSnapshot(["check", "--verbose"], { cwd }, 0);
156
+ });
157
+ });
158
+
159
+ describe("build", () => {
160
+ test("build produces .most with full migration chain", async () => {
161
+ const cwd = await makeTempFixture("basic");
162
+ const result = await cli(["build"], { cwd });
163
+ expect(result.exitCode).toBe(0);
164
+
165
+ const most = readFileSync(
166
+ path.join(cwd, ".mops", ".build", "backend.most"),
167
+ "utf-8",
168
+ );
169
+ expect(most).toMatchSnapshot();
170
+ });
171
+
172
+ test("build with build-limit produces trimmed .most", async () => {
173
+ const cwd = await makeTempFixture("basic");
174
+ await patchMigrations(cwd, "build-limit = 2");
175
+ const result = await cli(["build"], { cwd });
176
+ expect(result.exitCode).toBe(0);
177
+
178
+ const most = readFileSync(
179
+ path.join(cwd, ".mops", ".build", "backend.most"),
180
+ "utf-8",
181
+ );
182
+ expect(most).toMatchSnapshot();
183
+ });
184
+
185
+ test("build-limit counts next migration as part of the chain", async () => {
186
+ const cwd = await makeTempFixture("with-next");
187
+ await patchMigrations(cwd, "build-limit = 2");
188
+ const result = await cli(["build"], { cwd });
189
+ expect(result.exitCode).toBe(0);
190
+
191
+ const most = readFileSync(
192
+ path.join(cwd, ".mops", ".build", "backend.most"),
193
+ "utf-8",
194
+ );
195
+ expect(most).toMatchSnapshot();
196
+ });
197
+ });
198
+
199
+ describe("stable check hint", () => {
200
+ test("stable check fails with hint when deployed.most is incompatible", async () => {
201
+ const cwd = await makeTempFixture("basic");
202
+ await writeFile(
203
+ path.join(cwd, "deployed.most"),
204
+ "// Version: 1.0.0\nactor {\n stable var a : Nat;\n stable var name : Int\n};\n",
205
+ );
206
+ await cliSnapshot(["check"], { cwd }, 1);
207
+ });
208
+ });
209
+
210
+ describe("conflict detection", () => {
211
+ test("errors when both [migrations] and --enhanced-migration in args", async () => {
212
+ const cwd = await makeTempFixture("basic");
213
+ const tomlPath = path.join(cwd, "mops.toml");
214
+ const toml = readFileSync(tomlPath, "utf-8");
215
+ await writeFile(
216
+ tomlPath,
217
+ toml.replace(
218
+ "[canisters.backend.migrations]",
219
+ 'args = ["--enhanced-migration=migrations"]\n\n[canisters.backend.migrations]',
220
+ ),
221
+ );
222
+ const result = await cli(["check"], { cwd });
223
+ expect(result.exitCode).toBe(1);
224
+ expect(result.stderr).toMatch(/--enhanced-migration/);
225
+ expect(result.stderr).toMatch(/managed automatically/i);
226
+ });
227
+ });
228
+ });
package/types.ts CHANGED
@@ -35,6 +35,13 @@ export type Config = {
35
35
  };
36
36
  };
37
37
 
38
+ export type MigrationsConfig = {
39
+ chain: string;
40
+ next?: string;
41
+ "check-limit"?: number;
42
+ "build-limit"?: number;
43
+ };
44
+
38
45
  export type CanisterConfig = {
39
46
  main?: string;
40
47
  args?: string[];
@@ -44,6 +51,7 @@ export type CanisterConfig = {
44
51
  path: string;
45
52
  skipIfMissing?: boolean;
46
53
  };
54
+ migrations?: MigrationsConfig;
47
55
  };
48
56
 
49
57
  export type Dependencies = Record<string, Dependency>;