@dxos/migrations 0.5.8 → 0.5.9-main.0a0e87d

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.
@@ -1,35 +1,193 @@
1
- // packages/sdk/migrations/src/migrations.ts
2
- import { SpaceState } from "@dxos/client/echo";
1
+ // packages/sdk/migrations/src/migration-builder.ts
2
+ import { getHeads } from "@dxos/automerge/automerge";
3
+ import { CreateEpochRequest } from "@dxos/client/halo";
4
+ import { ObjectCore } from "@dxos/echo-db";
5
+ import { requireTypeReference } from "@dxos/echo-schema";
3
6
  import { invariant } from "@dxos/invariant";
4
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
7
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migration-builder.ts";
8
+ var MigrationBuilder = class {
9
+ constructor(_space) {
10
+ this._space = _space;
11
+ this._newLinks = {};
12
+ this._flushStates = [];
13
+ this._deleteObjects = [];
14
+ this._newRoot = void 0;
15
+ this._repo = this._space.db.coreDatabase.automerge.repo;
16
+ this._automergeContext = this._space.db.coreDatabase.automerge;
17
+ this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().docSync();
18
+ }
19
+ async findObject(id) {
20
+ const documentId = this._rootDoc.links?.[id] || this._newLinks[id];
21
+ const docHandle = documentId && this._repo.find(documentId);
22
+ if (!docHandle) {
23
+ return void 0;
24
+ }
25
+ await docHandle.whenReady();
26
+ const doc = docHandle.docSync();
27
+ return doc.objects?.[id];
28
+ }
29
+ async migrateObject(id, migrate) {
30
+ const objectStructure = await this.findObject(id);
31
+ if (!objectStructure) {
32
+ return;
33
+ }
34
+ const { schema, props } = migrate(objectStructure);
35
+ this._createObject({
36
+ id,
37
+ schema,
38
+ props
39
+ });
40
+ }
41
+ addObject(schema, props) {
42
+ const core = this._createObject({
43
+ schema,
44
+ props
45
+ });
46
+ return core.id;
47
+ }
48
+ deleteObject(id) {
49
+ this._deleteObjects.push(id);
50
+ }
51
+ changeProperties(changeFn) {
52
+ if (!this._newRoot) {
53
+ this._buildNewRoot();
54
+ }
55
+ invariant(this._newRoot, "New root not created", {
56
+ F: __dxlog_file,
57
+ L: 74,
58
+ S: this,
59
+ A: [
60
+ "this._newRoot",
61
+ "'New root not created'"
62
+ ]
63
+ });
64
+ this._newRoot.change((doc) => {
65
+ const propertiesStructure = doc.objects?.[this._space.properties.id];
66
+ propertiesStructure && changeFn(propertiesStructure);
67
+ });
68
+ this._flushStates.push({
69
+ documentId: this._newRoot.documentId,
70
+ heads: getHeads(this._newRoot.docSync())
71
+ });
72
+ }
73
+ /**
74
+ * @internal
75
+ */
76
+ async _commit() {
77
+ if (!this._newRoot) {
78
+ this._buildNewRoot();
79
+ }
80
+ invariant(this._newRoot, "New root not created", {
81
+ F: __dxlog_file,
82
+ L: 93,
83
+ S: this,
84
+ A: [
85
+ "this._newRoot",
86
+ "'New root not created'"
87
+ ]
88
+ });
89
+ await this._automergeContext.flush({
90
+ states: this._flushStates
91
+ });
92
+ await this._space.internal.createEpoch({
93
+ migration: CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,
94
+ automergeRootUrl: this._newRoot.url
95
+ });
96
+ }
97
+ _buildNewRoot() {
98
+ const previousLinks = {
99
+ ...this._rootDoc.links ?? {}
100
+ };
101
+ for (const id of this._deleteObjects) {
102
+ delete previousLinks[id];
103
+ }
104
+ this._newRoot = this._repo.create({
105
+ access: {
106
+ spaceKey: this._space.key.toHex()
107
+ },
108
+ objects: this._rootDoc.objects,
109
+ links: {
110
+ ...previousLinks,
111
+ ...this._newLinks
112
+ }
113
+ });
114
+ this._flushStates.push({
115
+ documentId: this._newRoot.documentId,
116
+ heads: getHeads(this._newRoot.docSync())
117
+ });
118
+ }
119
+ _createObject({ id, schema, props }) {
120
+ const core = new ObjectCore();
121
+ if (id) {
122
+ core.id = id;
123
+ }
124
+ core.initNewObject(props);
125
+ core.setType(requireTypeReference(schema));
126
+ const newHandle = this._repo.create({
127
+ access: {
128
+ spaceKey: this._space.key.toHex()
129
+ },
130
+ objects: {
131
+ [core.id]: core.getDoc()
132
+ }
133
+ });
134
+ this._newLinks[core.id] = newHandle.url;
135
+ this._flushStates.push({
136
+ documentId: newHandle.documentId,
137
+ heads: getHeads(newHandle.docSync())
138
+ });
139
+ return core;
140
+ }
141
+ };
142
+
143
+ // packages/sdk/migrations/src/migrations.ts
144
+ import { create, SpaceState } from "@dxos/client/echo";
145
+ import { invariant as invariant2 } from "@dxos/invariant";
146
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
5
147
  var Migrations = class {
6
148
  static {
7
149
  this.migrations = [];
8
150
  }
151
+ static {
152
+ this._state = create({
153
+ running: []
154
+ });
155
+ }
9
156
  static get versionProperty() {
10
157
  return this.namespace && `${this.namespace}.version`;
11
158
  }
12
159
  static get targetVersion() {
13
160
  return this.migrations[this.migrations.length - 1].version;
14
161
  }
162
+ static running(space) {
163
+ return this._state.running.includes(space.key.toHex());
164
+ }
15
165
  static define(namespace, migrations) {
16
166
  this.namespace = namespace;
17
167
  this.migrations = migrations;
18
168
  }
19
- // TODO(wittjosiah): Multi-space migrations.
20
169
  static async migrate(space, targetVersion) {
21
- invariant(this.versionProperty, "Migrations namespace not set", {
22
- F: __dxlog_file,
23
- L: 40,
170
+ invariant2(!this.running(space), "Migration already running", {
171
+ F: __dxlog_file2,
172
+ L: 44,
173
+ S: this,
174
+ A: [
175
+ "!this.running(space)",
176
+ "'Migration already running'"
177
+ ]
178
+ });
179
+ invariant2(this.versionProperty, "Migrations namespace not set", {
180
+ F: __dxlog_file2,
181
+ L: 45,
24
182
  S: this,
25
183
  A: [
26
184
  "this.versionProperty",
27
185
  "'Migrations namespace not set'"
28
186
  ]
29
187
  });
30
- invariant(space.state.get() === SpaceState.READY, "Space not ready", {
31
- F: __dxlog_file,
32
- L: 41,
188
+ invariant2(space.state.get() === SpaceState.READY, "Space not ready", {
189
+ F: __dxlog_file2,
190
+ L: 46,
33
191
  S: this,
34
192
  A: [
35
193
  "space.state.get() === SpaceState.READY",
@@ -43,29 +201,36 @@ var Migrations = class {
43
201
  if (currentIndex === targetIndex) {
44
202
  return false;
45
203
  }
204
+ this._state.running.push(space.key.toHex());
46
205
  if (targetIndex > currentIndex) {
47
206
  const migrations = this.migrations.slice(currentIndex, targetIndex);
48
207
  for (const migration of migrations) {
49
- await migration.up({
50
- space
208
+ const builder = new MigrationBuilder(space);
209
+ await migration.next({
210
+ space,
211
+ builder
51
212
  });
52
- space.properties[this.versionProperty] = migration.version;
53
- }
54
- } else {
55
- const migrations = this.migrations.slice(targetIndex, currentIndex);
56
- migrations.reverse();
57
- for (const migration of migrations) {
58
- await migration.down({
59
- space
213
+ builder.changeProperties((propertiesStructure) => {
214
+ invariant2(this.versionProperty, "Migrations namespace not set", {
215
+ F: __dxlog_file2,
216
+ L: 62,
217
+ S: this,
218
+ A: [
219
+ "this.versionProperty",
220
+ "'Migrations namespace not set'"
221
+ ]
222
+ });
223
+ propertiesStructure.data[this.versionProperty] = migration.version;
60
224
  });
61
- const index = this.migrations.indexOf(migration);
62
- space.properties[this.versionProperty] = this.migrations[index - 1]?.version;
225
+ await builder._commit();
63
226
  }
64
227
  }
228
+ this._state.running.splice(this._state.running.indexOf(space.key.toHex()), 1);
65
229
  return true;
66
230
  }
67
231
  };
68
232
  export {
233
+ MigrationBuilder,
69
234
  Migrations
70
235
  };
71
236
  //# sourceMappingURL=index.mjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../../src/migrations.ts"],
4
- "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Space, SpaceState } from '@dxos/client/echo';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\n// TODO(burdon): Merge with successor to serialization mechanism in braneframe/types.\n\nexport type MigrationContext = {\n space: Space;\n};\n\nexport type Migration = {\n version: string | number;\n up: (context: MigrationContext) => MaybePromise<void>;\n down: (context: MigrationContext) => MaybePromise<void>;\n};\n\nexport class Migrations {\n static namespace?: string;\n static migrations: Migration[] = [];\n\n static get versionProperty() {\n return this.namespace && `${this.namespace}.version`;\n }\n\n static get targetVersion() {\n return this.migrations[this.migrations.length - 1].version;\n }\n\n static define(namespace: string, migrations: Migration[]) {\n this.namespace = namespace;\n this.migrations = migrations;\n }\n\n // TODO(wittjosiah): Multi-space migrations.\n static async migrate(space: Space, targetVersion?: string | number) {\n invariant(this.versionProperty, 'Migrations namespace not set');\n invariant(space.state.get() === SpaceState.READY, 'Space not ready');\n const currentVersion = space.properties[this.versionProperty];\n const currentIndex = this.migrations.findIndex((m) => m.version === currentVersion) + 1;\n const i = this.migrations.findIndex((m) => m.version === targetVersion);\n const targetIndex = i === -1 ? this.migrations.length : i + 1;\n if (currentIndex === targetIndex) {\n return false;\n }\n\n if (targetIndex > currentIndex) {\n const migrations = this.migrations.slice(currentIndex, targetIndex);\n for (const migration of migrations) {\n await migration.up({ space });\n space.properties[this.versionProperty] = migration.version;\n }\n } else {\n const migrations = this.migrations.slice(targetIndex, currentIndex);\n migrations.reverse();\n for (const migration of migrations) {\n await migration.down({ space });\n const index = this.migrations.indexOf(migration);\n space.properties[this.versionProperty] = this.migrations[index - 1]?.version;\n }\n }\n\n return true;\n }\n}\n"],
5
- "mappings": ";AAIA,SAAqBA,kBAAkB;AACvC,SAASC,iBAAiB;;AAenB,IAAMC,aAAN,MAAMA;EAEX;SAAOC,aAA0B,CAAA;;EAEjC,WAAWC,kBAAkB;AAC3B,WAAO,KAAKC,aAAa,GAAG,KAAKA,SAAS;EAC5C;EAEA,WAAWC,gBAAgB;AACzB,WAAO,KAAKH,WAAW,KAAKA,WAAWI,SAAS,CAAA,EAAGC;EACrD;EAEA,OAAOC,OAAOJ,WAAmBF,YAAyB;AACxD,SAAKE,YAAYA;AACjB,SAAKF,aAAaA;EACpB;;EAGA,aAAaO,QAAQC,OAAcL,eAAiC;AAClEL,cAAU,KAAKG,iBAAiB,gCAAA;;;;;;;;;AAChCH,cAAUU,MAAMC,MAAMC,IAAG,MAAOb,WAAWc,OAAO,mBAAA;;;;;;;;;AAClD,UAAMC,iBAAiBJ,MAAMK,WAAW,KAAKZ,eAAe;AAC5D,UAAMa,eAAe,KAAKd,WAAWe,UAAU,CAACC,MAAMA,EAAEX,YAAYO,cAAAA,IAAkB;AACtF,UAAMK,IAAI,KAAKjB,WAAWe,UAAU,CAACC,MAAMA,EAAEX,YAAYF,aAAAA;AACzD,UAAMe,cAAcD,MAAM,KAAK,KAAKjB,WAAWI,SAASa,IAAI;AAC5D,QAAIH,iBAAiBI,aAAa;AAChC,aAAO;IACT;AAEA,QAAIA,cAAcJ,cAAc;AAC9B,YAAMd,aAAa,KAAKA,WAAWmB,MAAML,cAAcI,WAAAA;AACvD,iBAAWE,aAAapB,YAAY;AAClC,cAAMoB,UAAUC,GAAG;UAAEb;QAAM,CAAA;AAC3BA,cAAMK,WAAW,KAAKZ,eAAe,IAAImB,UAAUf;MACrD;IACF,OAAO;AACL,YAAML,aAAa,KAAKA,WAAWmB,MAAMD,aAAaJ,YAAAA;AACtDd,iBAAWsB,QAAO;AAClB,iBAAWF,aAAapB,YAAY;AAClC,cAAMoB,UAAUG,KAAK;UAAEf;QAAM,CAAA;AAC7B,cAAMgB,QAAQ,KAAKxB,WAAWyB,QAAQL,SAAAA;AACtCZ,cAAMK,WAAW,KAAKZ,eAAe,IAAI,KAAKD,WAAWwB,QAAQ,CAAA,GAAInB;MACvE;IACF;AAEA,WAAO;EACT;AACF;",
6
- "names": ["SpaceState", "invariant", "Migrations", "migrations", "versionProperty", "namespace", "targetVersion", "length", "version", "define", "migrate", "space", "state", "get", "READY", "currentVersion", "properties", "currentIndex", "findIndex", "m", "i", "targetIndex", "slice", "migration", "up", "reverse", "down", "index", "indexOf"]
3
+ "sources": ["../../../src/migration-builder.ts", "../../../src/migrations.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { getHeads, type Doc } from '@dxos/automerge/automerge';\nimport { type AnyDocumentId, type DocHandle, type Repo } from '@dxos/automerge/automerge-repo';\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { type AutomergeContext, ObjectCore } from '@dxos/echo-db';\nimport { type ObjectStructure, type SpaceDoc } from '@dxos/echo-protocol';\nimport { requireTypeReference, type S } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { type FlushRequest } from '@dxos/protocols/proto/dxos/echo/service';\n\nexport class MigrationBuilder {\n private readonly _repo: Repo;\n private readonly _automergeContext: AutomergeContext;\n private readonly _rootDoc: Doc<SpaceDoc>;\n\n // echoId -> automergeUrl\n private readonly _newLinks: Record<string, string> = {};\n private readonly _flushStates: FlushRequest.DocState[] = [];\n private readonly _deleteObjects: string[] = [];\n\n private _newRoot?: DocHandle<SpaceDoc> = undefined;\n\n constructor(private readonly _space: Space) {\n this._repo = this._space.db.coreDatabase.automerge.repo;\n this._automergeContext = this._space.db.coreDatabase.automerge;\n // TODO(wittjosiah): Accessing private API.\n this._rootDoc = (this._space.db.coreDatabase as any)._automergeDocLoader\n .getSpaceRootDocHandle()\n .docSync() as Doc<SpaceDoc>;\n }\n\n async findObject(id: string): Promise<ObjectStructure | undefined> {\n const documentId = (this._rootDoc.links?.[id] || this._newLinks[id]) as AnyDocumentId | undefined;\n const docHandle = documentId && this._repo.find(documentId);\n if (!docHandle) {\n return undefined;\n }\n\n await docHandle.whenReady();\n const doc = docHandle.docSync() as Doc<SpaceDoc>;\n return doc.objects?.[id];\n }\n\n async migrateObject(\n id: string,\n migrate: (objectStructure: ObjectStructure) => { schema: S.Schema<any>; props: any },\n ) {\n const objectStructure = await this.findObject(id);\n if (!objectStructure) {\n return;\n }\n\n const { schema, props } = migrate(objectStructure);\n this._createObject({ id, schema, props });\n }\n\n addObject(schema: S.Schema<any>, props: any) {\n const core = this._createObject({ schema, props });\n return core.id;\n }\n\n deleteObject(id: string) {\n this._deleteObjects.push(id);\n }\n\n changeProperties(changeFn: (properties: ObjectStructure) => void) {\n if (!this._newRoot) {\n this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n this._newRoot.change((doc: SpaceDoc) => {\n const propertiesStructure = doc.objects?.[this._space.properties.id];\n propertiesStructure && changeFn(propertiesStructure);\n });\n this._flushStates.push({\n documentId: this._newRoot.documentId,\n heads: getHeads(this._newRoot.docSync()),\n });\n }\n\n /**\n * @internal\n */\n async _commit() {\n if (!this._newRoot) {\n this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n await this._automergeContext.flush({\n states: this._flushStates,\n });\n\n // Create new epoch.\n await this._space.internal.createEpoch({\n migration: CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,\n automergeRootUrl: this._newRoot.url,\n });\n }\n\n private _buildNewRoot() {\n const previousLinks = { ...(this._rootDoc.links ?? {}) };\n for (const id of this._deleteObjects) {\n delete previousLinks[id];\n }\n\n this._newRoot = this._repo.create<SpaceDoc>({\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: this._rootDoc.objects,\n links: {\n ...previousLinks,\n ...this._newLinks,\n },\n });\n this._flushStates.push({\n documentId: this._newRoot.documentId,\n heads: getHeads(this._newRoot.docSync()),\n });\n }\n\n private _createObject({ id, schema, props }: { id?: string; schema: S.Schema<any>; props: any }) {\n const core = new ObjectCore();\n if (id) {\n core.id = id;\n }\n\n core.initNewObject(props);\n core.setType(requireTypeReference(schema));\n const newHandle = this._repo.create<SpaceDoc>({\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: {\n [core.id]: core.getDoc(),\n },\n });\n this._newLinks[core.id] = newHandle.url;\n this._flushStates.push({\n documentId: newHandle.documentId,\n heads: getHeads(newHandle.docSync()),\n });\n\n return core;\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Space, create, SpaceState } from '@dxos/client/echo';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\nimport { MigrationBuilder } from './migration-builder';\n\nexport type MigrationContext = {\n space: Space;\n builder: MigrationBuilder;\n};\n\nexport type Migration = {\n version: string;\n next: (context: MigrationContext) => MaybePromise<void>;\n};\n\nexport class Migrations {\n static namespace?: string;\n static migrations: Migration[] = [];\n private static _state = create<{ running: string[] }>({ running: [] });\n\n static get versionProperty() {\n return this.namespace && `${this.namespace}.version`;\n }\n\n static get targetVersion() {\n return this.migrations[this.migrations.length - 1].version;\n }\n\n static running(space: Space) {\n return this._state.running.includes(space.key.toHex());\n }\n\n static define(namespace: string, migrations: Migration[]) {\n this.namespace = namespace;\n this.migrations = migrations;\n }\n\n static async migrate(space: Space, targetVersion?: string | number) {\n invariant(!this.running(space), 'Migration already running');\n invariant(this.versionProperty, 'Migrations namespace not set');\n invariant(space.state.get() === SpaceState.READY, 'Space not ready');\n const currentVersion = space.properties[this.versionProperty];\n const currentIndex = this.migrations.findIndex((m) => m.version === currentVersion) + 1;\n const i = this.migrations.findIndex((m) => m.version === targetVersion);\n const targetIndex = i === -1 ? this.migrations.length : i + 1;\n if (currentIndex === targetIndex) {\n return false;\n }\n\n this._state.running.push(space.key.toHex());\n if (targetIndex > currentIndex) {\n const migrations = this.migrations.slice(currentIndex, targetIndex);\n for (const migration of migrations) {\n const builder = new MigrationBuilder(space);\n await migration.next({ space, builder });\n builder.changeProperties((propertiesStructure) => {\n invariant(this.versionProperty, 'Migrations namespace not set');\n propertiesStructure.data[this.versionProperty] = migration.version;\n });\n await builder._commit();\n }\n }\n this._state.running.splice(this._state.running.indexOf(space.key.toHex()), 1);\n\n return true;\n }\n}\n"],
5
+ "mappings": ";AAIA,SAASA,gBAA0B;AAGnC,SAASC,0BAA0B;AACnC,SAAgCC,kBAAkB;AAElD,SAASC,4BAAoC;AAC7C,SAASC,iBAAiB;;AAGnB,IAAMC,mBAAN,MAAMA;EAYXC,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,eA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AAYF,aAAAA;AACzD,UAAMoB,cAAcD,MAAM,KAAK,KAAKzB,WAAWO,SAASkB,IAAI;AAC5D,QAAIH,iBAAiBI,aAAa;AAChC,aAAO;IACT;AAEA,SAAKzB,OAAOE,QAAQwB,KAAKlB,MAAME,IAAIC,MAAK,CAAA;AACxC,QAAIc,cAAcJ,cAAc;AAC9B,YAAMtB,aAAa,KAAKA,WAAW4B,MAAMN,cAAcI,WAAAA;AACvD,iBAAWG,aAAa7B,YAAY;AAClC,cAAM8B,UAAU,IAAIC,iBAAiBtB,KAAAA;AACrC,cAAMoB,UAAUG,KAAK;UAAEvB;UAAOqB;QAAQ,CAAA;AACtCA,gBAAQG,iBAAiB,CAACC,wBAAAA;AACxBnB,UAAAA,WAAU,KAAKX,iBAAiB,gCAAA;;;;;;;;;AAChC8B,8BAAoBC,KAAK,KAAK/B,eAAe,IAAIyB,UAAUrB;QAC7D,CAAA;AACA,cAAMsB,QAAQM,QAAO;MACvB;IACF;AACA,SAAKnC,OAAOE,QAAQkC,OAAO,KAAKpC,OAAOE,QAAQmC,QAAQ7B,MAAME,IAAIC,MAAK,CAAA,GAAK,CAAA;AAE3E,WAAO;EACT;AACF;",
6
+ "names": ["getHeads", "CreateEpochRequest", "ObjectCore", "requireTypeReference", "invariant", "MigrationBuilder", "constructor", "_space", "_newLinks", "_flushStates", "_deleteObjects", "_newRoot", "undefined", "_repo", "db", "coreDatabase", "automerge", "repo", "_automergeContext", "_rootDoc", "_automergeDocLoader", "getSpaceRootDocHandle", "docSync", "findObject", "id", "documentId", "links", "docHandle", "find", "whenReady", "doc", "objects", "migrateObject", "migrate", "objectStructure", "schema", "props", "_createObject", "addObject", "core", "deleteObject", "push", "changeProperties", "changeFn", "_buildNewRoot", "change", "propertiesStructure", "properties", "heads", "_commit", "flush", "states", "internal", "createEpoch", "migration", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "url", "previousLinks", "create", "access", "spaceKey", "key", "toHex", "initNewObject", "setType", "newHandle", "getDoc", "create", "SpaceState", "invariant", "Migrations", "migrations", "_state", "create", "running", "versionProperty", "namespace", "targetVersion", "length", "version", "space", "includes", "key", "toHex", "define", "migrate", "invariant", "state", "get", "SpaceState", "READY", "currentVersion", "properties", "currentIndex", "findIndex", "m", "i", "targetIndex", "push", "slice", "migration", "builder", "MigrationBuilder", "next", "changeProperties", "propertiesStructure", "data", "_commit", "splice", "indexOf"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/sdk/migrations/src/migrations.ts":{"bytes":8236,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/sdk/migrations/src/index.ts":{"bytes":501,"imports":[{"path":"packages/sdk/migrations/src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"packages/sdk/migrations/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":3895},"packages/sdk/migrations/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"exports":["Migrations"],"entryPoint":"packages/sdk/migrations/src/index.ts","inputs":{"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2163},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0}},"bytes":2268}}}
1
+ {"inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytes":16903,"imports":[{"path":"@dxos/automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","kind":"import-statement","external":true},{"path":"@dxos/echo-db","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/sdk/migrations/src/migrations.ts":{"bytes":9817,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"packages/sdk/migrations/src/index.ts":{"bytes":698,"imports":[{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"},{"path":"packages/sdk/migrations/src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"packages/sdk/migrations/dist/lib/browser/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":12698},"packages/sdk/migrations/dist/lib/browser/index.mjs":{"imports":[{"path":"@dxos/automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","kind":"import-statement","external":true},{"path":"@dxos/echo-db","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"exports":["MigrationBuilder","Migrations"],"entryPoint":"packages/sdk/migrations/src/index.ts","inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytesInOutput":3783},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2745}},"bytes":6706}}}
@@ -18,40 +18,197 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
  var node_exports = {};
20
20
  __export(node_exports, {
21
+ MigrationBuilder: () => MigrationBuilder,
21
22
  Migrations: () => Migrations
22
23
  });
23
24
  module.exports = __toCommonJS(node_exports);
24
- var import_echo = require("@dxos/client/echo");
25
+ var import_automerge = require("@dxos/automerge/automerge");
26
+ var import_halo = require("@dxos/client/halo");
27
+ var import_echo_db = require("@dxos/echo-db");
28
+ var import_echo_schema = require("@dxos/echo-schema");
25
29
  var import_invariant = require("@dxos/invariant");
26
- var __dxlog_file = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
30
+ var import_echo = require("@dxos/client/echo");
31
+ var import_invariant2 = require("@dxos/invariant");
32
+ var __dxlog_file = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migration-builder.ts";
33
+ var MigrationBuilder = class {
34
+ constructor(_space) {
35
+ this._space = _space;
36
+ this._newLinks = {};
37
+ this._flushStates = [];
38
+ this._deleteObjects = [];
39
+ this._newRoot = void 0;
40
+ this._repo = this._space.db.coreDatabase.automerge.repo;
41
+ this._automergeContext = this._space.db.coreDatabase.automerge;
42
+ this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().docSync();
43
+ }
44
+ async findObject(id) {
45
+ const documentId = this._rootDoc.links?.[id] || this._newLinks[id];
46
+ const docHandle = documentId && this._repo.find(documentId);
47
+ if (!docHandle) {
48
+ return void 0;
49
+ }
50
+ await docHandle.whenReady();
51
+ const doc = docHandle.docSync();
52
+ return doc.objects?.[id];
53
+ }
54
+ async migrateObject(id, migrate) {
55
+ const objectStructure = await this.findObject(id);
56
+ if (!objectStructure) {
57
+ return;
58
+ }
59
+ const { schema, props } = migrate(objectStructure);
60
+ this._createObject({
61
+ id,
62
+ schema,
63
+ props
64
+ });
65
+ }
66
+ addObject(schema, props) {
67
+ const core = this._createObject({
68
+ schema,
69
+ props
70
+ });
71
+ return core.id;
72
+ }
73
+ deleteObject(id) {
74
+ this._deleteObjects.push(id);
75
+ }
76
+ changeProperties(changeFn) {
77
+ if (!this._newRoot) {
78
+ this._buildNewRoot();
79
+ }
80
+ (0, import_invariant.invariant)(this._newRoot, "New root not created", {
81
+ F: __dxlog_file,
82
+ L: 74,
83
+ S: this,
84
+ A: [
85
+ "this._newRoot",
86
+ "'New root not created'"
87
+ ]
88
+ });
89
+ this._newRoot.change((doc) => {
90
+ const propertiesStructure = doc.objects?.[this._space.properties.id];
91
+ propertiesStructure && changeFn(propertiesStructure);
92
+ });
93
+ this._flushStates.push({
94
+ documentId: this._newRoot.documentId,
95
+ heads: (0, import_automerge.getHeads)(this._newRoot.docSync())
96
+ });
97
+ }
98
+ /**
99
+ * @internal
100
+ */
101
+ async _commit() {
102
+ if (!this._newRoot) {
103
+ this._buildNewRoot();
104
+ }
105
+ (0, import_invariant.invariant)(this._newRoot, "New root not created", {
106
+ F: __dxlog_file,
107
+ L: 93,
108
+ S: this,
109
+ A: [
110
+ "this._newRoot",
111
+ "'New root not created'"
112
+ ]
113
+ });
114
+ await this._automergeContext.flush({
115
+ states: this._flushStates
116
+ });
117
+ await this._space.internal.createEpoch({
118
+ migration: import_halo.CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,
119
+ automergeRootUrl: this._newRoot.url
120
+ });
121
+ }
122
+ _buildNewRoot() {
123
+ const previousLinks = {
124
+ ...this._rootDoc.links ?? {}
125
+ };
126
+ for (const id of this._deleteObjects) {
127
+ delete previousLinks[id];
128
+ }
129
+ this._newRoot = this._repo.create({
130
+ access: {
131
+ spaceKey: this._space.key.toHex()
132
+ },
133
+ objects: this._rootDoc.objects,
134
+ links: {
135
+ ...previousLinks,
136
+ ...this._newLinks
137
+ }
138
+ });
139
+ this._flushStates.push({
140
+ documentId: this._newRoot.documentId,
141
+ heads: (0, import_automerge.getHeads)(this._newRoot.docSync())
142
+ });
143
+ }
144
+ _createObject({ id, schema, props }) {
145
+ const core = new import_echo_db.ObjectCore();
146
+ if (id) {
147
+ core.id = id;
148
+ }
149
+ core.initNewObject(props);
150
+ core.setType((0, import_echo_schema.requireTypeReference)(schema));
151
+ const newHandle = this._repo.create({
152
+ access: {
153
+ spaceKey: this._space.key.toHex()
154
+ },
155
+ objects: {
156
+ [core.id]: core.getDoc()
157
+ }
158
+ });
159
+ this._newLinks[core.id] = newHandle.url;
160
+ this._flushStates.push({
161
+ documentId: newHandle.documentId,
162
+ heads: (0, import_automerge.getHeads)(newHandle.docSync())
163
+ });
164
+ return core;
165
+ }
166
+ };
167
+ var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
27
168
  var Migrations = class {
28
169
  static {
29
170
  this.migrations = [];
30
171
  }
172
+ static {
173
+ this._state = (0, import_echo.create)({
174
+ running: []
175
+ });
176
+ }
31
177
  static get versionProperty() {
32
178
  return this.namespace && `${this.namespace}.version`;
33
179
  }
34
180
  static get targetVersion() {
35
181
  return this.migrations[this.migrations.length - 1].version;
36
182
  }
183
+ static running(space) {
184
+ return this._state.running.includes(space.key.toHex());
185
+ }
37
186
  static define(namespace, migrations) {
38
187
  this.namespace = namespace;
39
188
  this.migrations = migrations;
40
189
  }
41
- // TODO(wittjosiah): Multi-space migrations.
42
190
  static async migrate(space, targetVersion) {
43
- (0, import_invariant.invariant)(this.versionProperty, "Migrations namespace not set", {
44
- F: __dxlog_file,
45
- L: 40,
191
+ (0, import_invariant2.invariant)(!this.running(space), "Migration already running", {
192
+ F: __dxlog_file2,
193
+ L: 44,
194
+ S: this,
195
+ A: [
196
+ "!this.running(space)",
197
+ "'Migration already running'"
198
+ ]
199
+ });
200
+ (0, import_invariant2.invariant)(this.versionProperty, "Migrations namespace not set", {
201
+ F: __dxlog_file2,
202
+ L: 45,
46
203
  S: this,
47
204
  A: [
48
205
  "this.versionProperty",
49
206
  "'Migrations namespace not set'"
50
207
  ]
51
208
  });
52
- (0, import_invariant.invariant)(space.state.get() === import_echo.SpaceState.READY, "Space not ready", {
53
- F: __dxlog_file,
54
- L: 41,
209
+ (0, import_invariant2.invariant)(space.state.get() === import_echo.SpaceState.READY, "Space not ready", {
210
+ F: __dxlog_file2,
211
+ L: 46,
55
212
  S: this,
56
213
  A: [
57
214
  "space.state.get() === SpaceState.READY",
@@ -65,30 +222,37 @@ var Migrations = class {
65
222
  if (currentIndex === targetIndex) {
66
223
  return false;
67
224
  }
225
+ this._state.running.push(space.key.toHex());
68
226
  if (targetIndex > currentIndex) {
69
227
  const migrations = this.migrations.slice(currentIndex, targetIndex);
70
228
  for (const migration of migrations) {
71
- await migration.up({
72
- space
229
+ const builder = new MigrationBuilder(space);
230
+ await migration.next({
231
+ space,
232
+ builder
73
233
  });
74
- space.properties[this.versionProperty] = migration.version;
75
- }
76
- } else {
77
- const migrations = this.migrations.slice(targetIndex, currentIndex);
78
- migrations.reverse();
79
- for (const migration of migrations) {
80
- await migration.down({
81
- space
234
+ builder.changeProperties((propertiesStructure) => {
235
+ (0, import_invariant2.invariant)(this.versionProperty, "Migrations namespace not set", {
236
+ F: __dxlog_file2,
237
+ L: 62,
238
+ S: this,
239
+ A: [
240
+ "this.versionProperty",
241
+ "'Migrations namespace not set'"
242
+ ]
243
+ });
244
+ propertiesStructure.data[this.versionProperty] = migration.version;
82
245
  });
83
- const index = this.migrations.indexOf(migration);
84
- space.properties[this.versionProperty] = this.migrations[index - 1]?.version;
246
+ await builder._commit();
85
247
  }
86
248
  }
249
+ this._state.running.splice(this._state.running.indexOf(space.key.toHex()), 1);
87
250
  return true;
88
251
  }
89
252
  };
90
253
  // Annotate the CommonJS export names for ESM import in node:
91
254
  0 && (module.exports = {
255
+ MigrationBuilder,
92
256
  Migrations
93
257
  });
94
258
  //# sourceMappingURL=index.cjs.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
- "sources": ["../../../src/migrations.ts"],
4
- "sourcesContent": ["//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Space, SpaceState } from '@dxos/client/echo';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\n// TODO(burdon): Merge with successor to serialization mechanism in braneframe/types.\n\nexport type MigrationContext = {\n space: Space;\n};\n\nexport type Migration = {\n version: string | number;\n up: (context: MigrationContext) => MaybePromise<void>;\n down: (context: MigrationContext) => MaybePromise<void>;\n};\n\nexport class Migrations {\n static namespace?: string;\n static migrations: Migration[] = [];\n\n static get versionProperty() {\n return this.namespace && `${this.namespace}.version`;\n }\n\n static get targetVersion() {\n return this.migrations[this.migrations.length - 1].version;\n }\n\n static define(namespace: string, migrations: Migration[]) {\n this.namespace = namespace;\n this.migrations = migrations;\n }\n\n // TODO(wittjosiah): Multi-space migrations.\n static async migrate(space: Space, targetVersion?: string | number) {\n invariant(this.versionProperty, 'Migrations namespace not set');\n invariant(space.state.get() === SpaceState.READY, 'Space not ready');\n const currentVersion = space.properties[this.versionProperty];\n const currentIndex = this.migrations.findIndex((m) => m.version === currentVersion) + 1;\n const i = this.migrations.findIndex((m) => m.version === targetVersion);\n const targetIndex = i === -1 ? this.migrations.length : i + 1;\n if (currentIndex === targetIndex) {\n return false;\n }\n\n if (targetIndex > currentIndex) {\n const migrations = this.migrations.slice(currentIndex, targetIndex);\n for (const migration of migrations) {\n await migration.up({ space });\n space.properties[this.versionProperty] = migration.version;\n }\n } else {\n const migrations = this.migrations.slice(targetIndex, currentIndex);\n migrations.reverse();\n for (const migration of migrations) {\n await migration.down({ space });\n const index = this.migrations.indexOf(migration);\n space.properties[this.versionProperty] = this.migrations[index - 1]?.version;\n }\n }\n\n return true;\n }\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;AAIA,kBAAuC;AACvC,uBAA0B;;AAenB,IAAMA,aAAN,MAAMA;EAEX,OAAA;SAAOC,aAA0B,CAAA;;EAEjC,WAAWC,kBAAkB;AAC3B,WAAO,KAAKC,aAAa,GAAG,KAAKA,SAAS;EAC5C;EAEA,WAAWC,gBAAgB;AACzB,WAAO,KAAKH,WAAW,KAAKA,WAAWI,SAAS,CAAA,EAAGC;EACrD;EAEA,OAAOC,OAAOJ,WAAmBF,YAAyB;AACxD,SAAKE,YAAYA;AACjB,SAAKF,aAAaA;EACpB;;EAGA,aAAaO,QAAQC,OAAcL,eAAiC;AAClEM,oCAAU,KAAKR,iBAAiB,gCAAA;;;;;;;;;AAChCQ,oCAAUD,MAAME,MAAMC,IAAG,MAAOC,uBAAWC,OAAO,mBAAA;;;;;;;;;AAClD,UAAMC,iBAAiBN,MAAMO,WAAW,KAAKd,eAAe;AAC5D,UAAMe,eAAe,KAAKhB,WAAWiB,UAAU,CAACC,MAAMA,EAAEb,YAAYS,cAAAA,IAAkB;AACtF,UAAMK,IAAI,KAAKnB,WAAWiB,UAAU,CAACC,MAAMA,EAAEb,YAAYF,aAAAA;AACzD,UAAMiB,cAAcD,MAAM,KAAK,KAAKnB,WAAWI,SAASe,IAAI;AAC5D,QAAIH,iBAAiBI,aAAa;AAChC,aAAO;IACT;AAEA,QAAIA,cAAcJ,cAAc;AAC9B,YAAMhB,aAAa,KAAKA,WAAWqB,MAAML,cAAcI,WAAAA;AACvD,iBAAWE,aAAatB,YAAY;AAClC,cAAMsB,UAAUC,GAAG;UAAEf;QAAM,CAAA;AAC3BA,cAAMO,WAAW,KAAKd,eAAe,IAAIqB,UAAUjB;MACrD;IACF,OAAO;AACL,YAAML,aAAa,KAAKA,WAAWqB,MAAMD,aAAaJ,YAAAA;AACtDhB,iBAAWwB,QAAO;AAClB,iBAAWF,aAAatB,YAAY;AAClC,cAAMsB,UAAUG,KAAK;UAAEjB;QAAM,CAAA;AAC7B,cAAMkB,QAAQ,KAAK1B,WAAW2B,QAAQL,SAAAA;AACtCd,cAAMO,WAAW,KAAKd,eAAe,IAAI,KAAKD,WAAW0B,QAAQ,CAAA,GAAIrB;MACvE;IACF;AAEA,WAAO;EACT;AACF;",
6
- "names": ["Migrations", "migrations", "versionProperty", "namespace", "targetVersion", "length", "version", "define", "migrate", "space", "invariant", "state", "get", "SpaceState", "READY", "currentVersion", "properties", "currentIndex", "findIndex", "m", "i", "targetIndex", "slice", "migration", "up", "reverse", "down", "index", "indexOf"]
3
+ "sources": ["../../../src/migration-builder.ts", "../../../src/migrations.ts"],
4
+ "sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { getHeads, type Doc } from '@dxos/automerge/automerge';\nimport { type AnyDocumentId, type DocHandle, type Repo } from '@dxos/automerge/automerge-repo';\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { type AutomergeContext, ObjectCore } from '@dxos/echo-db';\nimport { type ObjectStructure, type SpaceDoc } from '@dxos/echo-protocol';\nimport { requireTypeReference, type S } from '@dxos/echo-schema';\nimport { invariant } from '@dxos/invariant';\nimport { type FlushRequest } from '@dxos/protocols/proto/dxos/echo/service';\n\nexport class MigrationBuilder {\n private readonly _repo: Repo;\n private readonly _automergeContext: AutomergeContext;\n private readonly _rootDoc: Doc<SpaceDoc>;\n\n // echoId -> automergeUrl\n private readonly _newLinks: Record<string, string> = {};\n private readonly _flushStates: FlushRequest.DocState[] = [];\n private readonly _deleteObjects: string[] = [];\n\n private _newRoot?: DocHandle<SpaceDoc> = undefined;\n\n constructor(private readonly _space: Space) {\n this._repo = this._space.db.coreDatabase.automerge.repo;\n this._automergeContext = this._space.db.coreDatabase.automerge;\n // TODO(wittjosiah): Accessing private API.\n this._rootDoc = (this._space.db.coreDatabase as any)._automergeDocLoader\n .getSpaceRootDocHandle()\n .docSync() as Doc<SpaceDoc>;\n }\n\n async findObject(id: string): Promise<ObjectStructure | undefined> {\n const documentId = (this._rootDoc.links?.[id] || this._newLinks[id]) as AnyDocumentId | undefined;\n const docHandle = documentId && this._repo.find(documentId);\n if (!docHandle) {\n return undefined;\n }\n\n await docHandle.whenReady();\n const doc = docHandle.docSync() as Doc<SpaceDoc>;\n return doc.objects?.[id];\n }\n\n async migrateObject(\n id: string,\n migrate: (objectStructure: ObjectStructure) => { schema: S.Schema<any>; props: any },\n ) {\n const objectStructure = await this.findObject(id);\n if (!objectStructure) {\n return;\n }\n\n const { schema, props } = migrate(objectStructure);\n this._createObject({ id, schema, props });\n }\n\n addObject(schema: S.Schema<any>, props: any) {\n const core = this._createObject({ schema, props });\n return core.id;\n }\n\n deleteObject(id: string) {\n this._deleteObjects.push(id);\n }\n\n changeProperties(changeFn: (properties: ObjectStructure) => void) {\n if (!this._newRoot) {\n this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n this._newRoot.change((doc: SpaceDoc) => {\n const propertiesStructure = doc.objects?.[this._space.properties.id];\n propertiesStructure && changeFn(propertiesStructure);\n });\n this._flushStates.push({\n documentId: this._newRoot.documentId,\n heads: getHeads(this._newRoot.docSync()),\n });\n }\n\n /**\n * @internal\n */\n async _commit() {\n if (!this._newRoot) {\n this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n await this._automergeContext.flush({\n states: this._flushStates,\n });\n\n // Create new epoch.\n await this._space.internal.createEpoch({\n migration: CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,\n automergeRootUrl: this._newRoot.url,\n });\n }\n\n private _buildNewRoot() {\n const previousLinks = { ...(this._rootDoc.links ?? {}) };\n for (const id of this._deleteObjects) {\n delete previousLinks[id];\n }\n\n this._newRoot = this._repo.create<SpaceDoc>({\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: this._rootDoc.objects,\n links: {\n ...previousLinks,\n ...this._newLinks,\n },\n });\n this._flushStates.push({\n documentId: this._newRoot.documentId,\n heads: getHeads(this._newRoot.docSync()),\n });\n }\n\n private _createObject({ id, schema, props }: { id?: string; schema: S.Schema<any>; props: any }) {\n const core = new ObjectCore();\n if (id) {\n core.id = id;\n }\n\n core.initNewObject(props);\n core.setType(requireTypeReference(schema));\n const newHandle = this._repo.create<SpaceDoc>({\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: {\n [core.id]: core.getDoc(),\n },\n });\n this._newLinks[core.id] = newHandle.url;\n this._flushStates.push({\n documentId: newHandle.documentId,\n heads: getHeads(newHandle.docSync()),\n });\n\n return core;\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Space, create, SpaceState } from '@dxos/client/echo';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\nimport { MigrationBuilder } from './migration-builder';\n\nexport type MigrationContext = {\n space: Space;\n builder: MigrationBuilder;\n};\n\nexport type Migration = {\n version: string;\n next: (context: MigrationContext) => MaybePromise<void>;\n};\n\nexport class Migrations {\n static namespace?: string;\n static migrations: Migration[] = [];\n private static _state = create<{ running: string[] }>({ running: [] });\n\n static get versionProperty() {\n return this.namespace && `${this.namespace}.version`;\n }\n\n static get targetVersion() {\n return this.migrations[this.migrations.length - 1].version;\n }\n\n static running(space: Space) {\n return this._state.running.includes(space.key.toHex());\n }\n\n static define(namespace: string, migrations: Migration[]) {\n this.namespace = namespace;\n this.migrations = migrations;\n }\n\n static async migrate(space: Space, targetVersion?: string | number) {\n invariant(!this.running(space), 'Migration already running');\n invariant(this.versionProperty, 'Migrations namespace not set');\n invariant(space.state.get() === SpaceState.READY, 'Space not ready');\n const currentVersion = space.properties[this.versionProperty];\n const currentIndex = this.migrations.findIndex((m) => m.version === currentVersion) + 1;\n const i = this.migrations.findIndex((m) => m.version === targetVersion);\n const targetIndex = i === -1 ? this.migrations.length : i + 1;\n if (currentIndex === targetIndex) {\n return false;\n }\n\n this._state.running.push(space.key.toHex());\n if (targetIndex > currentIndex) {\n const migrations = this.migrations.slice(currentIndex, targetIndex);\n for (const migration of migrations) {\n const builder = new MigrationBuilder(space);\n await migration.next({ space, builder });\n builder.changeProperties((propertiesStructure) => {\n invariant(this.versionProperty, 'Migrations namespace not set');\n propertiesStructure.data[this.versionProperty] = migration.version;\n });\n await builder._commit();\n }\n }\n this._state.running.splice(this._state.running.indexOf(space.key.toHex()), 1);\n\n return true;\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAIA,uBAAmC;AAGnC,kBAAmC;AACnC,qBAAkD;AAElD,yBAA6C;AAC7C,uBAA0B;ACP1B,kBAA+C;AAC/C,IAAAA,oBAA0B;;ADSnB,IAAMC,mBAAN,MAAMA;EAYXC,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,eAAwC,CAAA;SACxCC,iBAA2B,CAAA;SAEpCC,WAAiCC;AAGvC,SAAKC,QAAQ,KAAKN,OAAOO,GAAGC,aAAaC,UAAUC;AACnD,SAAKC,oBAAoB,KAAKX,OAAOO,GAAGC,aAAaC;AAErD,SAAKG,WAAY,KAAKZ,OAAOO,GAAGC,aAAqBK,oBAClDC,sBAAqB,EACrBC,QAAO;EACZ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A;AACvD,iBAAWvC,aAAamB,YAAY;AAClC,cAAMsB,UAAU,IAAI5F,iBAAiB8E,KAAAA;AACrC,cAAM3B,UAAU0C,KAAK;UAAEf;UAAOc;QAAQ,CAAA;AACtCA,gBAAQvD,iBAAiB,CAACK,wBAAAA;AACxBF,gCAAAA,WAAU,KAAKiC,iBAAiB,gCAAA;;;;;;;;;AAChC/B,8BAAoBoD,KAAK,KAAKrB,eAAe,IAAItB,UAAU0B;QAC7D,CAAA;AACA,cAAMe,QAAQ9C,QAAO;MACvB;IACF;AACA,SAAKyB,OAAOC,QAAQuB,OAAO,KAAKxB,OAAOC,QAAQwB,QAAQlB,MAAMjB,IAAIC,MAAK,CAAA,GAAK,CAAA;AAE3E,WAAO;EACT;AACF;",
6
+ "names": ["import_invariant", "MigrationBuilder", "constructor", "_space", "_newLinks", "_flushStates", "_deleteObjects", "_newRoot", "undefined", "_repo", "db", "coreDatabase", "automerge", "repo", "_automergeContext", "_rootDoc", "_automergeDocLoader", "getSpaceRootDocHandle", "docSync", "findObject", "id", "documentId", "links", "docHandle", "find", "whenReady", "doc", "objects", "migrateObject", "migrate", "objectStructure", "schema", "props", "_createObject", "addObject", "core", "deleteObject", "push", "changeProperties", "changeFn", "_buildNewRoot", "invariant", "change", "propertiesStructure", "properties", "heads", "getHeads", "_commit", "flush", "states", "internal", "createEpoch", "migration", "CreateEpochRequest", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "url", "previousLinks", "create", "access", "spaceKey", "key", "toHex", "ObjectCore", "initNewObject", "setType", "requireTypeReference", "newHandle", "getDoc", "Migrations", "migrations", "_state", "running", "versionProperty", "namespace", "targetVersion", "length", "version", "space", "includes", "define", "state", "get", "SpaceState", "READY", "currentVersion", "currentIndex", "findIndex", "m", "i", "targetIndex", "slice", "builder", "next", "data", "splice", "indexOf"]
7
7
  }
@@ -1 +1 @@
1
- {"inputs":{"packages/sdk/migrations/src/migrations.ts":{"bytes":8236,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/sdk/migrations/src/index.ts":{"bytes":501,"imports":[{"path":"packages/sdk/migrations/src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"packages/sdk/migrations/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":3895},"packages/sdk/migrations/dist/lib/node/index.cjs":{"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"exports":["Migrations"],"entryPoint":"packages/sdk/migrations/src/index.ts","inputs":{"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2163},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0}},"bytes":2268}}}
1
+ {"inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytes":16903,"imports":[{"path":"@dxos/automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","kind":"import-statement","external":true},{"path":"@dxos/echo-db","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"format":"esm"},"packages/sdk/migrations/src/migrations.ts":{"bytes":9817,"imports":[{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"}],"format":"esm"},"packages/sdk/migrations/src/index.ts":{"bytes":698,"imports":[{"path":"packages/sdk/migrations/src/migration-builder.ts","kind":"import-statement","original":"./migration-builder"},{"path":"packages/sdk/migrations/src/migrations.ts","kind":"import-statement","original":"./migrations"}],"format":"esm"}},"outputs":{"packages/sdk/migrations/dist/lib/node/index.cjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":12698},"packages/sdk/migrations/dist/lib/node/index.cjs":{"imports":[{"path":"@dxos/automerge/automerge","kind":"import-statement","external":true},{"path":"@dxos/client/halo","kind":"import-statement","external":true},{"path":"@dxos/echo-db","kind":"import-statement","external":true},{"path":"@dxos/echo-schema","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true},{"path":"@dxos/client/echo","kind":"import-statement","external":true},{"path":"@dxos/invariant","kind":"import-statement","external":true}],"exports":["MigrationBuilder","Migrations"],"entryPoint":"packages/sdk/migrations/src/index.ts","inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytesInOutput":3783},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2745}},"bytes":6706}}}
@@ -1,2 +1,4 @@
1
+ export type { ObjectStructure } from '@dxos/echo-protocol';
2
+ export * from './migration-builder';
1
3
  export * from './migrations';
2
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,cAAc,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AAIA,YAAY,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAE3D,cAAc,qBAAqB,CAAC;AACpC,cAAc,cAAc,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { type Space } from '@dxos/client/echo';
2
+ import { type ObjectStructure } from '@dxos/echo-protocol';
3
+ import { type S } from '@dxos/echo-schema';
4
+ export declare class MigrationBuilder {
5
+ private readonly _space;
6
+ private readonly _repo;
7
+ private readonly _automergeContext;
8
+ private readonly _rootDoc;
9
+ private readonly _newLinks;
10
+ private readonly _flushStates;
11
+ private readonly _deleteObjects;
12
+ private _newRoot?;
13
+ constructor(_space: Space);
14
+ findObject(id: string): Promise<ObjectStructure | undefined>;
15
+ migrateObject(id: string, migrate: (objectStructure: ObjectStructure) => {
16
+ schema: S.Schema<any>;
17
+ props: any;
18
+ }): Promise<void>;
19
+ addObject(schema: S.Schema<any>, props: any): string;
20
+ deleteObject(id: string): void;
21
+ changeProperties(changeFn: (properties: ObjectStructure) => void): void;
22
+ private _buildNewRoot;
23
+ private _createObject;
24
+ }
25
+ //# sourceMappingURL=migration-builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migration-builder.d.ts","sourceRoot":"","sources":["../../../src/migration-builder.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,KAAK,KAAK,EAAE,MAAM,mBAAmB,CAAC;AAG/C,OAAO,EAAE,KAAK,eAAe,EAAiB,MAAM,qBAAqB,CAAC;AAC1E,OAAO,EAAwB,KAAK,CAAC,EAAE,MAAM,mBAAmB,CAAC;AAIjE,qBAAa,gBAAgB;IAYf,OAAO,CAAC,QAAQ,CAAC,MAAM;IAXnC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAO;IAC7B,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAmB;IACrD,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgB;IAGzC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA8B;IACxD,OAAO,CAAC,QAAQ,CAAC,YAAY,CAA+B;IAC5D,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAgB;IAE/C,OAAO,CAAC,QAAQ,CAAC,CAAkC;gBAEtB,MAAM,EAAE,KAAK;IASpC,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,SAAS,CAAC;IAY5D,aAAa,CACjB,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,CAAC,eAAe,EAAE,eAAe,KAAK;QAAE,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAAC,KAAK,EAAE,GAAG,CAAA;KAAE;IAWtF,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,GAAG;IAK3C,YAAY,CAAC,EAAE,EAAE,MAAM;IAIvB,gBAAgB,CAAC,QAAQ,EAAE,CAAC,UAAU,EAAE,eAAe,KAAK,IAAI;IAoChE,OAAO,CAAC,aAAa;IAsBrB,OAAO,CAAC,aAAa;CAwBtB"}
@@ -1,18 +1,21 @@
1
1
  import { type Space } from '@dxos/client/echo';
2
2
  import { type MaybePromise } from '@dxos/util';
3
+ import { MigrationBuilder } from './migration-builder';
3
4
  export type MigrationContext = {
4
5
  space: Space;
6
+ builder: MigrationBuilder;
5
7
  };
6
8
  export type Migration = {
7
- version: string | number;
8
- up: (context: MigrationContext) => MaybePromise<void>;
9
- down: (context: MigrationContext) => MaybePromise<void>;
9
+ version: string;
10
+ next: (context: MigrationContext) => MaybePromise<void>;
10
11
  };
11
12
  export declare class Migrations {
12
13
  static namespace?: string;
13
14
  static migrations: Migration[];
15
+ private static _state;
14
16
  static get versionProperty(): string | undefined;
15
- static get targetVersion(): string | number;
17
+ static get targetVersion(): string;
18
+ static running(space: Space): boolean;
16
19
  static define(namespace: string, migrations: Migration[]): void;
17
20
  static migrate(space: Space, targetVersion?: string | number): Promise<boolean>;
18
21
  }
@@ -1 +1 @@
1
- {"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../../../src/migrations.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,KAAK,EAAc,MAAM,mBAAmB,CAAC;AAE3D,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAI/C,MAAM,MAAM,gBAAgB,GAAG;IAC7B,KAAK,EAAE,KAAK,CAAC;CACd,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,OAAO,EAAE,MAAM,GAAG,MAAM,CAAC;IACzB,EAAE,EAAE,CAAC,OAAO,EAAE,gBAAgB,KAAK,YAAY,CAAC,IAAI,CAAC,CAAC;IACtD,IAAI,EAAE,CAAC,OAAO,EAAE,gBAAgB,KAAK,YAAY,CAAC,IAAI,CAAC,CAAC;CACzD,CAAC;AAEF,qBAAa,UAAU;IACrB,MAAM,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC1B,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 -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/migrations",
3
- "version": "0.5.8",
3
+ "version": "0.5.9-main.0a0e87d",
4
4
  "description": "",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -20,10 +20,15 @@
20
20
  "src"
21
21
  ],
22
22
  "dependencies": {
23
- "@dxos/client": "0.5.8",
24
- "@dxos/echo-schema": "0.5.8",
25
- "@dxos/invariant": "0.5.8",
26
- "@dxos/util": "0.5.8"
23
+ "@dxos/client": "0.5.9-main.0a0e87d",
24
+ "@dxos/echo-db": "0.5.9-main.0a0e87d",
25
+ "@dxos/automerge": "0.5.9-main.0a0e87d",
26
+ "@dxos/echo-schema": "0.5.9-main.0a0e87d",
27
+ "@dxos/log": "0.5.9-main.0a0e87d",
28
+ "@dxos/protocols": "0.5.9-main.0a0e87d",
29
+ "@dxos/echo-protocol": "0.5.9-main.0a0e87d",
30
+ "@dxos/invariant": "0.5.9-main.0a0e87d",
31
+ "@dxos/util": "0.5.9-main.0a0e87d"
27
32
  },
28
33
  "devDependencies": {},
29
34
  "publishConfig": {
package/src/index.ts CHANGED
@@ -2,4 +2,7 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
+ export type { ObjectStructure } from '@dxos/echo-protocol';
6
+
7
+ export * from './migration-builder';
5
8
  export * from './migrations';
@@ -0,0 +1,152 @@
1
+ //
2
+ // Copyright 2024 DXOS.org
3
+ //
4
+
5
+ import { getHeads, type Doc } from '@dxos/automerge/automerge';
6
+ import { type AnyDocumentId, type DocHandle, type Repo } from '@dxos/automerge/automerge-repo';
7
+ import { type Space } from '@dxos/client/echo';
8
+ import { CreateEpochRequest } from '@dxos/client/halo';
9
+ import { type AutomergeContext, ObjectCore } from '@dxos/echo-db';
10
+ import { type ObjectStructure, type SpaceDoc } from '@dxos/echo-protocol';
11
+ import { requireTypeReference, type S } from '@dxos/echo-schema';
12
+ import { invariant } from '@dxos/invariant';
13
+ import { type FlushRequest } from '@dxos/protocols/proto/dxos/echo/service';
14
+
15
+ export class MigrationBuilder {
16
+ private readonly _repo: Repo;
17
+ private readonly _automergeContext: AutomergeContext;
18
+ private readonly _rootDoc: Doc<SpaceDoc>;
19
+
20
+ // echoId -> automergeUrl
21
+ private readonly _newLinks: Record<string, string> = {};
22
+ private readonly _flushStates: FlushRequest.DocState[] = [];
23
+ private readonly _deleteObjects: string[] = [];
24
+
25
+ private _newRoot?: DocHandle<SpaceDoc> = undefined;
26
+
27
+ constructor(private readonly _space: Space) {
28
+ this._repo = this._space.db.coreDatabase.automerge.repo;
29
+ this._automergeContext = this._space.db.coreDatabase.automerge;
30
+ // TODO(wittjosiah): Accessing private API.
31
+ this._rootDoc = (this._space.db.coreDatabase as any)._automergeDocLoader
32
+ .getSpaceRootDocHandle()
33
+ .docSync() as Doc<SpaceDoc>;
34
+ }
35
+
36
+ async findObject(id: string): Promise<ObjectStructure | undefined> {
37
+ const documentId = (this._rootDoc.links?.[id] || this._newLinks[id]) as AnyDocumentId | undefined;
38
+ const docHandle = documentId && this._repo.find(documentId);
39
+ if (!docHandle) {
40
+ return undefined;
41
+ }
42
+
43
+ await docHandle.whenReady();
44
+ const doc = docHandle.docSync() as Doc<SpaceDoc>;
45
+ return doc.objects?.[id];
46
+ }
47
+
48
+ async migrateObject(
49
+ id: string,
50
+ migrate: (objectStructure: ObjectStructure) => { schema: S.Schema<any>; props: any },
51
+ ) {
52
+ const objectStructure = await this.findObject(id);
53
+ if (!objectStructure) {
54
+ return;
55
+ }
56
+
57
+ const { schema, props } = migrate(objectStructure);
58
+ this._createObject({ id, schema, props });
59
+ }
60
+
61
+ addObject(schema: S.Schema<any>, props: any) {
62
+ const core = this._createObject({ schema, props });
63
+ return core.id;
64
+ }
65
+
66
+ deleteObject(id: string) {
67
+ this._deleteObjects.push(id);
68
+ }
69
+
70
+ changeProperties(changeFn: (properties: ObjectStructure) => void) {
71
+ if (!this._newRoot) {
72
+ this._buildNewRoot();
73
+ }
74
+ invariant(this._newRoot, 'New root not created');
75
+
76
+ this._newRoot.change((doc: SpaceDoc) => {
77
+ const propertiesStructure = doc.objects?.[this._space.properties.id];
78
+ propertiesStructure && changeFn(propertiesStructure);
79
+ });
80
+ this._flushStates.push({
81
+ documentId: this._newRoot.documentId,
82
+ heads: getHeads(this._newRoot.docSync()),
83
+ });
84
+ }
85
+
86
+ /**
87
+ * @internal
88
+ */
89
+ async _commit() {
90
+ if (!this._newRoot) {
91
+ this._buildNewRoot();
92
+ }
93
+ invariant(this._newRoot, 'New root not created');
94
+
95
+ await this._automergeContext.flush({
96
+ states: this._flushStates,
97
+ });
98
+
99
+ // Create new epoch.
100
+ await this._space.internal.createEpoch({
101
+ migration: CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,
102
+ automergeRootUrl: this._newRoot.url,
103
+ });
104
+ }
105
+
106
+ private _buildNewRoot() {
107
+ const previousLinks = { ...(this._rootDoc.links ?? {}) };
108
+ for (const id of this._deleteObjects) {
109
+ delete previousLinks[id];
110
+ }
111
+
112
+ this._newRoot = this._repo.create<SpaceDoc>({
113
+ access: {
114
+ spaceKey: this._space.key.toHex(),
115
+ },
116
+ objects: this._rootDoc.objects,
117
+ links: {
118
+ ...previousLinks,
119
+ ...this._newLinks,
120
+ },
121
+ });
122
+ this._flushStates.push({
123
+ documentId: this._newRoot.documentId,
124
+ heads: getHeads(this._newRoot.docSync()),
125
+ });
126
+ }
127
+
128
+ private _createObject({ id, schema, props }: { id?: string; schema: S.Schema<any>; props: any }) {
129
+ const core = new ObjectCore();
130
+ if (id) {
131
+ core.id = id;
132
+ }
133
+
134
+ core.initNewObject(props);
135
+ core.setType(requireTypeReference(schema));
136
+ const newHandle = this._repo.create<SpaceDoc>({
137
+ access: {
138
+ spaceKey: this._space.key.toHex(),
139
+ },
140
+ objects: {
141
+ [core.id]: core.getDoc(),
142
+ },
143
+ });
144
+ this._newLinks[core.id] = newHandle.url;
145
+ this._flushStates.push({
146
+ documentId: newHandle.documentId,
147
+ heads: getHeads(newHandle.docSync()),
148
+ });
149
+
150
+ return core;
151
+ }
152
+ }
@@ -5,7 +5,7 @@
5
5
  import { expect } from 'chai';
6
6
 
7
7
  import { Client } from '@dxos/client';
8
- import { type Space } from '@dxos/client/echo';
8
+ import { Filter, type Space } from '@dxos/client/echo';
9
9
  import { TestBuilder } from '@dxos/client/testing';
10
10
  import { Expando, create } from '@dxos/echo-schema';
11
11
  import { describe, test, beforeEach, beforeAll, afterAll } from '@dxos/test';
@@ -14,41 +14,32 @@ import { Migrations } from './migrations';
14
14
 
15
15
  Migrations.define('test', [
16
16
  {
17
- version: 1,
18
- up: async ({ space }) => {
19
- space.db.add(create(Expando, { namespace: 'test', count: 1 }));
20
- },
21
- down: async ({ space }) => {
22
- const { objects } = await space.db.query({ namespace: 'test' }).run();
23
- for (const object of objects) {
24
- space.db.remove(object);
25
- }
17
+ version: '1970-01-01',
18
+ next: async ({ builder }) => {
19
+ builder.addObject(Expando, { namespace: 'test', count: 1 });
26
20
  },
27
21
  },
28
22
  {
29
- version: 2,
30
- up: async ({ space }) => {
23
+ version: '1970-01-02',
24
+ next: async ({ space, builder }) => {
31
25
  const { objects } = await space.db.query({ namespace: 'test' }).run();
32
26
  for (const object of objects) {
33
- object.count = 2;
27
+ await builder.migrateObject(object.id, ({ data }) => ({
28
+ schema: Expando,
29
+ props: { namespace: data.namespace, count: 2 },
30
+ }));
34
31
  }
35
32
  },
36
- down: async () => {
37
- // No-op.
38
- },
39
33
  },
40
34
  {
41
- version: 3,
42
- up: async ({ space }) => {
43
- const { objects } = await space.db.query({ namespace: 'test' }).run();
44
- for (const object of objects) {
45
- object.count *= 3;
46
- }
47
- },
48
- down: async ({ space }) => {
35
+ version: '1970-01-03',
36
+ next: async ({ space, builder }) => {
49
37
  const { objects } = await space.db.query({ namespace: 'test' }).run();
50
38
  for (const object of objects) {
51
- object.count /= 3;
39
+ await builder.migrateObject(object.id, ({ data }) => ({
40
+ schema: Expando,
41
+ props: { namespace: data.namespace, count: data.count * 3 },
42
+ }));
52
43
  }
53
44
  },
54
45
  },
@@ -75,47 +66,34 @@ describe('Migrations', () => {
75
66
 
76
67
  test('if no migrations have been run before, runs all migrations', async () => {
77
68
  await Migrations.migrate(space);
78
- const { objects } = await space.db.query({ namespace: 'test' }).run();
69
+ const { objects } = await space.db.query(Filter.schema(Expando, { namespace: 'test' })).run();
79
70
  expect(objects).to.have.length(1);
80
71
  expect(objects[0].count).to.equal(6);
81
- expect(space.properties['test.version']).to.equal(3);
72
+ expect(space.properties['test.version']).to.equal('1970-01-03');
82
73
  });
83
74
 
84
75
  test('if some migrations have been run before, runs only the remaining migrations', async () => {
85
- space.properties['test.version'] = 2;
76
+ space.properties['test.version'] = '1970-01-02';
86
77
  space.db.add(create(Expando, { namespace: 'test', count: 5 }));
87
78
  await Migrations.migrate(space);
88
- const { objects } = await space.db.query({ namespace: 'test' }).run();
79
+ const { objects } = await space.db.query(Filter.schema(Expando, { namespace: 'test' })).run();
89
80
  expect(objects).to.have.length(1);
90
81
  expect(objects[0].count).to.equal(15);
91
- expect(space.properties['test.version']).to.equal(3);
82
+ expect(space.properties['test.version']).to.equal('1970-01-03');
92
83
  });
93
84
 
94
85
  test('if all migrations have been run before, does nothing', async () => {
95
- space.properties['test.version'] = 3;
86
+ space.properties['test.version'] = '1970-01-03';
96
87
  await Migrations.migrate(space);
97
- const { objects } = await space.db.query({ namespace: 'test' }).run();
88
+ const { objects } = await space.db.query(Filter.schema(Expando, { namespace: 'test' })).run();
98
89
  expect(objects).to.have.length(0);
99
90
  });
100
91
 
101
92
  test('if target version is specified, runs only the migrations up to that version', async () => {
102
- await Migrations.migrate(space, 2);
103
- const { objects } = await space.db.query({ namespace: 'test' }).run();
93
+ await Migrations.migrate(space, '1970-01-02');
94
+ const { objects } = await space.db.query(Filter.schema(Expando, { namespace: 'test' })).run();
104
95
  expect(objects).to.have.length(1);
105
96
  expect(objects[0].count).to.equal(2);
106
- expect(space.properties['test.version']).to.equal(2);
107
- });
108
-
109
- test('if target version is specified and is lower than current version, runs the down migrations', async () => {
110
- await Migrations.migrate(space);
111
- const beforeDowngrade = await space.db.query({ namespace: 'test' }).run();
112
- expect(beforeDowngrade.objects).to.have.length(1);
113
- expect(beforeDowngrade.objects[0].count).to.equal(6);
114
- expect(space.properties['test.version']).to.equal(3);
115
- await Migrations.migrate(space, 1);
116
- const afterDowngrade = await space.db.query({ namespace: 'test' }).run();
117
- expect(afterDowngrade.objects).to.have.length(1);
118
- expect(afterDowngrade.objects[0].count).to.equal(2);
119
- expect(space.properties['test.version']).to.equal(1);
97
+ expect(space.properties['test.version']).to.equal('1970-01-02');
120
98
  });
121
99
  });
package/src/migrations.ts CHANGED
@@ -2,25 +2,26 @@
2
2
  // Copyright 2023 DXOS.org
3
3
  //
4
4
 
5
- import { type Space, SpaceState } from '@dxos/client/echo';
5
+ import { type Space, create, SpaceState } from '@dxos/client/echo';
6
6
  import { invariant } from '@dxos/invariant';
7
7
  import { type MaybePromise } from '@dxos/util';
8
8
 
9
- // TODO(burdon): Merge with successor to serialization mechanism in braneframe/types.
9
+ import { MigrationBuilder } from './migration-builder';
10
10
 
11
11
  export type MigrationContext = {
12
12
  space: Space;
13
+ builder: MigrationBuilder;
13
14
  };
14
15
 
15
16
  export type Migration = {
16
- version: string | number;
17
- up: (context: MigrationContext) => MaybePromise<void>;
18
- down: (context: MigrationContext) => MaybePromise<void>;
17
+ version: string;
18
+ next: (context: MigrationContext) => MaybePromise<void>;
19
19
  };
20
20
 
21
21
  export class Migrations {
22
22
  static namespace?: string;
23
23
  static migrations: Migration[] = [];
24
+ private static _state = create<{ running: string[] }>({ running: [] });
24
25
 
25
26
  static get versionProperty() {
26
27
  return this.namespace && `${this.namespace}.version`;
@@ -30,13 +31,17 @@ export class Migrations {
30
31
  return this.migrations[this.migrations.length - 1].version;
31
32
  }
32
33
 
34
+ static running(space: Space) {
35
+ return this._state.running.includes(space.key.toHex());
36
+ }
37
+
33
38
  static define(namespace: string, migrations: Migration[]) {
34
39
  this.namespace = namespace;
35
40
  this.migrations = migrations;
36
41
  }
37
42
 
38
- // TODO(wittjosiah): Multi-space migrations.
39
43
  static async migrate(space: Space, targetVersion?: string | number) {
44
+ invariant(!this.running(space), 'Migration already running');
40
45
  invariant(this.versionProperty, 'Migrations namespace not set');
41
46
  invariant(space.state.get() === SpaceState.READY, 'Space not ready');
42
47
  const currentVersion = space.properties[this.versionProperty];
@@ -47,21 +52,20 @@ export class Migrations {
47
52
  return false;
48
53
  }
49
54
 
55
+ this._state.running.push(space.key.toHex());
50
56
  if (targetIndex > currentIndex) {
51
57
  const migrations = this.migrations.slice(currentIndex, targetIndex);
52
58
  for (const migration of migrations) {
53
- await migration.up({ space });
54
- space.properties[this.versionProperty] = migration.version;
55
- }
56
- } else {
57
- const migrations = this.migrations.slice(targetIndex, currentIndex);
58
- migrations.reverse();
59
- for (const migration of migrations) {
60
- await migration.down({ space });
61
- const index = this.migrations.indexOf(migration);
62
- space.properties[this.versionProperty] = this.migrations[index - 1]?.version;
59
+ const builder = new MigrationBuilder(space);
60
+ await migration.next({ space, builder });
61
+ builder.changeProperties((propertiesStructure) => {
62
+ invariant(this.versionProperty, 'Migrations namespace not set');
63
+ propertiesStructure.data[this.versionProperty] = migration.version;
64
+ });
65
+ await builder._commit();
63
66
  }
64
67
  }
68
+ this._state.running.splice(this._state.running.indexOf(space.key.toHex()), 1);
65
69
 
66
70
  return true;
67
71
  }