@dxos/migrations 0.5.8 → 0.5.9-main.079a532
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/dist/lib/browser/index.mjs +225 -22
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +223 -21
- package/dist/lib/node/index.cjs.map +4 -4
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/index.d.ts +2 -0
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/migration-builder.d.ts +29 -0
- package/dist/types/src/migration-builder.d.ts.map +1 -0
- package/dist/types/src/migrations.d.ts +7 -4
- package/dist/types/src/migrations.d.ts.map +1 -1
- package/package.json +10 -5
- package/src/index.ts +3 -0
- package/src/migration-builder.ts +192 -0
- package/src/migrations.test.ts +26 -48
- package/src/migrations.ts +20 -16
|
@@ -1,35 +1,231 @@
|
|
|
1
|
-
// packages/sdk/migrations/src/
|
|
2
|
-
import {
|
|
1
|
+
// packages/sdk/migrations/src/migration-builder.ts
|
|
2
|
+
import { next as am } from "@dxos/automerge/automerge";
|
|
3
|
+
import { CreateEpochRequest } from "@dxos/client/halo";
|
|
4
|
+
import { ObjectCore, migrateDocument } from "@dxos/echo-db";
|
|
5
|
+
import { SpaceDocVersion, encodeReference, Reference } from "@dxos/echo-protocol";
|
|
6
|
+
import { requireTypeReference } from "@dxos/echo-schema";
|
|
3
7
|
import { invariant } from "@dxos/invariant";
|
|
4
|
-
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/
|
|
8
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migration-builder.ts";
|
|
9
|
+
var MigrationBuilder = class {
|
|
10
|
+
constructor(_space) {
|
|
11
|
+
this._space = _space;
|
|
12
|
+
this._newLinks = {};
|
|
13
|
+
this._flushStates = [];
|
|
14
|
+
this._deleteObjects = [];
|
|
15
|
+
this._newRoot = void 0;
|
|
16
|
+
this._repo = this._space.db.coreDatabase.automerge.repo;
|
|
17
|
+
this._automergeContext = this._space.db.coreDatabase.automerge;
|
|
18
|
+
this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().docSync();
|
|
19
|
+
}
|
|
20
|
+
async findObject(id) {
|
|
21
|
+
const documentId = this._rootDoc.links?.[id] || this._newLinks[id];
|
|
22
|
+
const docHandle = documentId && this._repo.find(documentId);
|
|
23
|
+
if (!docHandle) {
|
|
24
|
+
return void 0;
|
|
25
|
+
}
|
|
26
|
+
await docHandle.whenReady();
|
|
27
|
+
const doc = docHandle.docSync();
|
|
28
|
+
return doc.objects?.[id];
|
|
29
|
+
}
|
|
30
|
+
async migrateObject(id, migrate) {
|
|
31
|
+
const objectStructure = await this.findObject(id);
|
|
32
|
+
if (!objectStructure) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const { schema, props } = await migrate(objectStructure);
|
|
36
|
+
const oldHandle = await this._findObjectContainingHandle(id);
|
|
37
|
+
invariant(oldHandle, void 0, {
|
|
38
|
+
F: __dxlog_file,
|
|
39
|
+
L: 61,
|
|
40
|
+
S: this,
|
|
41
|
+
A: [
|
|
42
|
+
"oldHandle",
|
|
43
|
+
""
|
|
44
|
+
]
|
|
45
|
+
});
|
|
46
|
+
const newState = {
|
|
47
|
+
version: SpaceDocVersion.CURRENT,
|
|
48
|
+
access: {
|
|
49
|
+
spaceKey: this._space.key.toHex()
|
|
50
|
+
},
|
|
51
|
+
objects: {
|
|
52
|
+
[id]: {
|
|
53
|
+
system: {
|
|
54
|
+
type: encodeReference(requireTypeReference(schema))
|
|
55
|
+
},
|
|
56
|
+
data: props,
|
|
57
|
+
meta: {
|
|
58
|
+
keys: []
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
const migratedDoc = migrateDocument(oldHandle.docSync(), newState);
|
|
64
|
+
const newHandle = this._repo.import(am.save(migratedDoc));
|
|
65
|
+
this._newLinks[id] = newHandle.url;
|
|
66
|
+
this._addHandleToFlushList(newHandle);
|
|
67
|
+
}
|
|
68
|
+
async addObject(schema, props) {
|
|
69
|
+
const core = this._createObject({
|
|
70
|
+
schema,
|
|
71
|
+
props
|
|
72
|
+
});
|
|
73
|
+
return core.id;
|
|
74
|
+
}
|
|
75
|
+
createReference(id) {
|
|
76
|
+
return encodeReference(new Reference(id));
|
|
77
|
+
}
|
|
78
|
+
deleteObject(id) {
|
|
79
|
+
this._deleteObjects.push(id);
|
|
80
|
+
}
|
|
81
|
+
changeProperties(changeFn) {
|
|
82
|
+
if (!this._newRoot) {
|
|
83
|
+
this._buildNewRoot();
|
|
84
|
+
}
|
|
85
|
+
invariant(this._newRoot, "New root not created", {
|
|
86
|
+
F: __dxlog_file,
|
|
87
|
+
L: 103,
|
|
88
|
+
S: this,
|
|
89
|
+
A: [
|
|
90
|
+
"this._newRoot",
|
|
91
|
+
"'New root not created'"
|
|
92
|
+
]
|
|
93
|
+
});
|
|
94
|
+
this._newRoot.change((doc) => {
|
|
95
|
+
const propertiesStructure = doc.objects?.[this._space.properties.id];
|
|
96
|
+
propertiesStructure && changeFn(propertiesStructure);
|
|
97
|
+
});
|
|
98
|
+
this._addHandleToFlushList(this._newRoot);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* @internal
|
|
102
|
+
*/
|
|
103
|
+
async _commit() {
|
|
104
|
+
if (!this._newRoot) {
|
|
105
|
+
this._buildNewRoot();
|
|
106
|
+
}
|
|
107
|
+
invariant(this._newRoot, "New root not created", {
|
|
108
|
+
F: __dxlog_file,
|
|
109
|
+
L: 119,
|
|
110
|
+
S: this,
|
|
111
|
+
A: [
|
|
112
|
+
"this._newRoot",
|
|
113
|
+
"'New root not created'"
|
|
114
|
+
]
|
|
115
|
+
});
|
|
116
|
+
await this._automergeContext.flush({
|
|
117
|
+
states: this._flushStates
|
|
118
|
+
});
|
|
119
|
+
await this._space.internal.createEpoch({
|
|
120
|
+
migration: CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,
|
|
121
|
+
automergeRootUrl: this._newRoot.url
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
async _findObjectContainingHandle(id) {
|
|
125
|
+
const documentId = this._rootDoc.links?.[id] || this._newLinks[id];
|
|
126
|
+
const docHandle = documentId && this._repo.find(documentId);
|
|
127
|
+
if (!docHandle) {
|
|
128
|
+
return void 0;
|
|
129
|
+
}
|
|
130
|
+
await docHandle.whenReady();
|
|
131
|
+
return docHandle;
|
|
132
|
+
}
|
|
133
|
+
_buildNewRoot() {
|
|
134
|
+
const previousLinks = {
|
|
135
|
+
...this._rootDoc.links ?? {}
|
|
136
|
+
};
|
|
137
|
+
for (const id of this._deleteObjects) {
|
|
138
|
+
delete previousLinks[id];
|
|
139
|
+
}
|
|
140
|
+
this._newRoot = this._repo.create({
|
|
141
|
+
version: SpaceDocVersion.CURRENT,
|
|
142
|
+
access: {
|
|
143
|
+
spaceKey: this._space.key.toHex()
|
|
144
|
+
},
|
|
145
|
+
objects: this._rootDoc.objects,
|
|
146
|
+
links: {
|
|
147
|
+
...previousLinks,
|
|
148
|
+
...this._newLinks
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
this._addHandleToFlushList(this._newRoot);
|
|
152
|
+
}
|
|
153
|
+
_createObject({ id, schema, props }) {
|
|
154
|
+
const core = new ObjectCore();
|
|
155
|
+
if (id) {
|
|
156
|
+
core.id = id;
|
|
157
|
+
}
|
|
158
|
+
core.initNewObject(props);
|
|
159
|
+
core.setType(requireTypeReference(schema));
|
|
160
|
+
const newHandle = this._repo.create({
|
|
161
|
+
version: SpaceDocVersion.CURRENT,
|
|
162
|
+
access: {
|
|
163
|
+
spaceKey: this._space.key.toHex()
|
|
164
|
+
},
|
|
165
|
+
objects: {
|
|
166
|
+
[core.id]: core.getDoc()
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
this._newLinks[core.id] = newHandle.url;
|
|
170
|
+
this._addHandleToFlushList(newHandle);
|
|
171
|
+
return core;
|
|
172
|
+
}
|
|
173
|
+
_addHandleToFlushList(handle) {
|
|
174
|
+
this._flushStates.push({
|
|
175
|
+
documentId: handle.documentId,
|
|
176
|
+
heads: am.getHeads(handle.docSync())
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// packages/sdk/migrations/src/migrations.ts
|
|
182
|
+
import { create, SpaceState } from "@dxos/client/echo";
|
|
183
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
184
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
|
|
5
185
|
var Migrations = class {
|
|
6
186
|
static {
|
|
7
187
|
this.migrations = [];
|
|
8
188
|
}
|
|
189
|
+
static {
|
|
190
|
+
this._state = create({
|
|
191
|
+
running: []
|
|
192
|
+
});
|
|
193
|
+
}
|
|
9
194
|
static get versionProperty() {
|
|
10
195
|
return this.namespace && `${this.namespace}.version`;
|
|
11
196
|
}
|
|
12
197
|
static get targetVersion() {
|
|
13
198
|
return this.migrations[this.migrations.length - 1].version;
|
|
14
199
|
}
|
|
200
|
+
static running(space) {
|
|
201
|
+
return this._state.running.includes(space.key.toHex());
|
|
202
|
+
}
|
|
15
203
|
static define(namespace, migrations) {
|
|
16
204
|
this.namespace = namespace;
|
|
17
205
|
this.migrations = migrations;
|
|
18
206
|
}
|
|
19
|
-
// TODO(wittjosiah): Multi-space migrations.
|
|
20
207
|
static async migrate(space, targetVersion) {
|
|
21
|
-
|
|
22
|
-
F:
|
|
23
|
-
L:
|
|
208
|
+
invariant2(!this.running(space), "Migration already running", {
|
|
209
|
+
F: __dxlog_file2,
|
|
210
|
+
L: 44,
|
|
211
|
+
S: this,
|
|
212
|
+
A: [
|
|
213
|
+
"!this.running(space)",
|
|
214
|
+
"'Migration already running'"
|
|
215
|
+
]
|
|
216
|
+
});
|
|
217
|
+
invariant2(this.versionProperty, "Migrations namespace not set", {
|
|
218
|
+
F: __dxlog_file2,
|
|
219
|
+
L: 45,
|
|
24
220
|
S: this,
|
|
25
221
|
A: [
|
|
26
222
|
"this.versionProperty",
|
|
27
223
|
"'Migrations namespace not set'"
|
|
28
224
|
]
|
|
29
225
|
});
|
|
30
|
-
|
|
31
|
-
F:
|
|
32
|
-
L:
|
|
226
|
+
invariant2(space.state.get() === SpaceState.READY, "Space not ready", {
|
|
227
|
+
F: __dxlog_file2,
|
|
228
|
+
L: 46,
|
|
33
229
|
S: this,
|
|
34
230
|
A: [
|
|
35
231
|
"space.state.get() === SpaceState.READY",
|
|
@@ -43,29 +239,36 @@ var Migrations = class {
|
|
|
43
239
|
if (currentIndex === targetIndex) {
|
|
44
240
|
return false;
|
|
45
241
|
}
|
|
242
|
+
this._state.running.push(space.key.toHex());
|
|
46
243
|
if (targetIndex > currentIndex) {
|
|
47
244
|
const migrations = this.migrations.slice(currentIndex, targetIndex);
|
|
48
245
|
for (const migration of migrations) {
|
|
49
|
-
|
|
50
|
-
|
|
246
|
+
const builder = new MigrationBuilder(space);
|
|
247
|
+
await migration.next({
|
|
248
|
+
space,
|
|
249
|
+
builder
|
|
51
250
|
});
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
251
|
+
builder.changeProperties((propertiesStructure) => {
|
|
252
|
+
invariant2(this.versionProperty, "Migrations namespace not set", {
|
|
253
|
+
F: __dxlog_file2,
|
|
254
|
+
L: 62,
|
|
255
|
+
S: this,
|
|
256
|
+
A: [
|
|
257
|
+
"this.versionProperty",
|
|
258
|
+
"'Migrations namespace not set'"
|
|
259
|
+
]
|
|
260
|
+
});
|
|
261
|
+
propertiesStructure.data[this.versionProperty] = migration.version;
|
|
60
262
|
});
|
|
61
|
-
|
|
62
|
-
space.properties[this.versionProperty] = this.migrations[index - 1]?.version;
|
|
263
|
+
await builder._commit();
|
|
63
264
|
}
|
|
64
265
|
}
|
|
266
|
+
this._state.running.splice(this._state.running.indexOf(space.key.toHex()), 1);
|
|
65
267
|
return true;
|
|
66
268
|
}
|
|
67
269
|
};
|
|
68
270
|
export {
|
|
271
|
+
MigrationBuilder,
|
|
69
272
|
Migrations
|
|
70
273
|
};
|
|
71
274
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/migrations.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright
|
|
5
|
-
"mappings": ";AAIA,
|
|
6
|
-
"names": ["SpaceState", "invariant", "Migrations", "migrations", "versionProperty", "namespace", "targetVersion", "length", "version", "define", "migrate", "
|
|
3
|
+
"sources": ["../../../src/migration-builder.ts", "../../../src/migrations.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Doc, next as am } 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, migrateDocument } from '@dxos/echo-db';\nimport { SpaceDocVersion, encodeReference, type ObjectStructure, type SpaceDoc, Reference } 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';\nimport { type MaybePromise } from '@dxos/util';\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) => MaybePromise<{ 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 } = await migrate(objectStructure);\n\n const oldHandle = await this._findObjectContainingHandle(id);\n invariant(oldHandle);\n\n const newState: SpaceDoc = {\n version: SpaceDocVersion.CURRENT,\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: {\n [id]: {\n system: {\n type: encodeReference(requireTypeReference(schema)),\n },\n data: props,\n meta: {\n keys: [],\n },\n },\n },\n };\n const migratedDoc = migrateDocument(oldHandle.docSync() as Doc<SpaceDoc>, newState);\n const newHandle = this._repo.import<SpaceDoc>(am.save(migratedDoc));\n this._newLinks[id] = newHandle.url;\n this._addHandleToFlushList(newHandle);\n }\n\n async addObject(schema: S.Schema<any>, props: any) {\n const core = this._createObject({ schema, props });\n return core.id;\n }\n\n createReference(id: string) {\n return encodeReference(new Reference(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._addHandleToFlushList(this._newRoot);\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 async _findObjectContainingHandle(id: string): Promise<DocHandle<SpaceDoc> | 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 return docHandle;\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 version: SpaceDocVersion.CURRENT,\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._addHandleToFlushList(this._newRoot);\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 version: SpaceDocVersion.CURRENT,\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._addHandleToFlushList(newHandle);\n\n return core;\n }\n\n private _addHandleToFlushList(handle: DocHandle<any>) {\n this._flushStates.push({\n documentId: handle.documentId,\n heads: am.getHeads(handle.docSync()),\n });\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,SAAmBA,QAAQC,UAAU;AAGrC,SAASC,0BAA0B;AACnC,SAAgCC,YAAYC,uBAAuB;AACnE,SAASC,iBAAiBC,iBAAsDC,iBAAiB;AACjG,SAASC,4BAAoC;AAC7C,SAASC,iBAAiB;;AAInB,IAAMC,mBAAN,MAAMA;EAYXC,YAA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M8B,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": ["next", "am", "CreateEpochRequest", "ObjectCore", "migrateDocument", "SpaceDocVersion", "encodeReference", "Reference", "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", "oldHandle", "_findObjectContainingHandle", "newState", "version", "CURRENT", "access", "spaceKey", "key", "toHex", "system", "type", "data", "meta", "keys", "migratedDoc", "newHandle", "import", "save", "url", "_addHandleToFlushList", "addObject", "core", "_createObject", "createReference", "deleteObject", "push", "changeProperties", "changeFn", "_buildNewRoot", "change", "propertiesStructure", "properties", "_commit", "flush", "states", "internal", "createEpoch", "migration", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "previousLinks", "create", "initNewObject", "setType", "getDoc", "handle", "heads", "getHeads", "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":
|
|
1
|
+
{"inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytes":21641,"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-protocol","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":15103},"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-protocol","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":4976},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2745}},"bytes":7899}}}
|
package/dist/lib/node/index.cjs
CHANGED
|
@@ -18,40 +18,235 @@ 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
|
|
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_protocol = require("@dxos/echo-protocol");
|
|
29
|
+
var import_echo_schema = require("@dxos/echo-schema");
|
|
25
30
|
var import_invariant = require("@dxos/invariant");
|
|
26
|
-
var
|
|
31
|
+
var import_echo = require("@dxos/client/echo");
|
|
32
|
+
var import_invariant2 = require("@dxos/invariant");
|
|
33
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migration-builder.ts";
|
|
34
|
+
var MigrationBuilder = class {
|
|
35
|
+
constructor(_space) {
|
|
36
|
+
this._space = _space;
|
|
37
|
+
this._newLinks = {};
|
|
38
|
+
this._flushStates = [];
|
|
39
|
+
this._deleteObjects = [];
|
|
40
|
+
this._newRoot = void 0;
|
|
41
|
+
this._repo = this._space.db.coreDatabase.automerge.repo;
|
|
42
|
+
this._automergeContext = this._space.db.coreDatabase.automerge;
|
|
43
|
+
this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().docSync();
|
|
44
|
+
}
|
|
45
|
+
async findObject(id) {
|
|
46
|
+
const documentId = this._rootDoc.links?.[id] || this._newLinks[id];
|
|
47
|
+
const docHandle = documentId && this._repo.find(documentId);
|
|
48
|
+
if (!docHandle) {
|
|
49
|
+
return void 0;
|
|
50
|
+
}
|
|
51
|
+
await docHandle.whenReady();
|
|
52
|
+
const doc = docHandle.docSync();
|
|
53
|
+
return doc.objects?.[id];
|
|
54
|
+
}
|
|
55
|
+
async migrateObject(id, migrate) {
|
|
56
|
+
const objectStructure = await this.findObject(id);
|
|
57
|
+
if (!objectStructure) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const { schema, props } = await migrate(objectStructure);
|
|
61
|
+
const oldHandle = await this._findObjectContainingHandle(id);
|
|
62
|
+
(0, import_invariant.invariant)(oldHandle, void 0, {
|
|
63
|
+
F: __dxlog_file,
|
|
64
|
+
L: 61,
|
|
65
|
+
S: this,
|
|
66
|
+
A: [
|
|
67
|
+
"oldHandle",
|
|
68
|
+
""
|
|
69
|
+
]
|
|
70
|
+
});
|
|
71
|
+
const newState = {
|
|
72
|
+
version: import_echo_protocol.SpaceDocVersion.CURRENT,
|
|
73
|
+
access: {
|
|
74
|
+
spaceKey: this._space.key.toHex()
|
|
75
|
+
},
|
|
76
|
+
objects: {
|
|
77
|
+
[id]: {
|
|
78
|
+
system: {
|
|
79
|
+
type: (0, import_echo_protocol.encodeReference)((0, import_echo_schema.requireTypeReference)(schema))
|
|
80
|
+
},
|
|
81
|
+
data: props,
|
|
82
|
+
meta: {
|
|
83
|
+
keys: []
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
const migratedDoc = (0, import_echo_db.migrateDocument)(oldHandle.docSync(), newState);
|
|
89
|
+
const newHandle = this._repo.import(import_automerge.next.save(migratedDoc));
|
|
90
|
+
this._newLinks[id] = newHandle.url;
|
|
91
|
+
this._addHandleToFlushList(newHandle);
|
|
92
|
+
}
|
|
93
|
+
async addObject(schema, props) {
|
|
94
|
+
const core = this._createObject({
|
|
95
|
+
schema,
|
|
96
|
+
props
|
|
97
|
+
});
|
|
98
|
+
return core.id;
|
|
99
|
+
}
|
|
100
|
+
createReference(id) {
|
|
101
|
+
return (0, import_echo_protocol.encodeReference)(new import_echo_protocol.Reference(id));
|
|
102
|
+
}
|
|
103
|
+
deleteObject(id) {
|
|
104
|
+
this._deleteObjects.push(id);
|
|
105
|
+
}
|
|
106
|
+
changeProperties(changeFn) {
|
|
107
|
+
if (!this._newRoot) {
|
|
108
|
+
this._buildNewRoot();
|
|
109
|
+
}
|
|
110
|
+
(0, import_invariant.invariant)(this._newRoot, "New root not created", {
|
|
111
|
+
F: __dxlog_file,
|
|
112
|
+
L: 103,
|
|
113
|
+
S: this,
|
|
114
|
+
A: [
|
|
115
|
+
"this._newRoot",
|
|
116
|
+
"'New root not created'"
|
|
117
|
+
]
|
|
118
|
+
});
|
|
119
|
+
this._newRoot.change((doc) => {
|
|
120
|
+
const propertiesStructure = doc.objects?.[this._space.properties.id];
|
|
121
|
+
propertiesStructure && changeFn(propertiesStructure);
|
|
122
|
+
});
|
|
123
|
+
this._addHandleToFlushList(this._newRoot);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* @internal
|
|
127
|
+
*/
|
|
128
|
+
async _commit() {
|
|
129
|
+
if (!this._newRoot) {
|
|
130
|
+
this._buildNewRoot();
|
|
131
|
+
}
|
|
132
|
+
(0, import_invariant.invariant)(this._newRoot, "New root not created", {
|
|
133
|
+
F: __dxlog_file,
|
|
134
|
+
L: 119,
|
|
135
|
+
S: this,
|
|
136
|
+
A: [
|
|
137
|
+
"this._newRoot",
|
|
138
|
+
"'New root not created'"
|
|
139
|
+
]
|
|
140
|
+
});
|
|
141
|
+
await this._automergeContext.flush({
|
|
142
|
+
states: this._flushStates
|
|
143
|
+
});
|
|
144
|
+
await this._space.internal.createEpoch({
|
|
145
|
+
migration: import_halo.CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,
|
|
146
|
+
automergeRootUrl: this._newRoot.url
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
async _findObjectContainingHandle(id) {
|
|
150
|
+
const documentId = this._rootDoc.links?.[id] || this._newLinks[id];
|
|
151
|
+
const docHandle = documentId && this._repo.find(documentId);
|
|
152
|
+
if (!docHandle) {
|
|
153
|
+
return void 0;
|
|
154
|
+
}
|
|
155
|
+
await docHandle.whenReady();
|
|
156
|
+
return docHandle;
|
|
157
|
+
}
|
|
158
|
+
_buildNewRoot() {
|
|
159
|
+
const previousLinks = {
|
|
160
|
+
...this._rootDoc.links ?? {}
|
|
161
|
+
};
|
|
162
|
+
for (const id of this._deleteObjects) {
|
|
163
|
+
delete previousLinks[id];
|
|
164
|
+
}
|
|
165
|
+
this._newRoot = this._repo.create({
|
|
166
|
+
version: import_echo_protocol.SpaceDocVersion.CURRENT,
|
|
167
|
+
access: {
|
|
168
|
+
spaceKey: this._space.key.toHex()
|
|
169
|
+
},
|
|
170
|
+
objects: this._rootDoc.objects,
|
|
171
|
+
links: {
|
|
172
|
+
...previousLinks,
|
|
173
|
+
...this._newLinks
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
this._addHandleToFlushList(this._newRoot);
|
|
177
|
+
}
|
|
178
|
+
_createObject({ id, schema, props }) {
|
|
179
|
+
const core = new import_echo_db.ObjectCore();
|
|
180
|
+
if (id) {
|
|
181
|
+
core.id = id;
|
|
182
|
+
}
|
|
183
|
+
core.initNewObject(props);
|
|
184
|
+
core.setType((0, import_echo_schema.requireTypeReference)(schema));
|
|
185
|
+
const newHandle = this._repo.create({
|
|
186
|
+
version: import_echo_protocol.SpaceDocVersion.CURRENT,
|
|
187
|
+
access: {
|
|
188
|
+
spaceKey: this._space.key.toHex()
|
|
189
|
+
},
|
|
190
|
+
objects: {
|
|
191
|
+
[core.id]: core.getDoc()
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
this._newLinks[core.id] = newHandle.url;
|
|
195
|
+
this._addHandleToFlushList(newHandle);
|
|
196
|
+
return core;
|
|
197
|
+
}
|
|
198
|
+
_addHandleToFlushList(handle) {
|
|
199
|
+
this._flushStates.push({
|
|
200
|
+
documentId: handle.documentId,
|
|
201
|
+
heads: import_automerge.next.getHeads(handle.docSync())
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
|
|
27
206
|
var Migrations = class {
|
|
28
207
|
static {
|
|
29
208
|
this.migrations = [];
|
|
30
209
|
}
|
|
210
|
+
static {
|
|
211
|
+
this._state = (0, import_echo.create)({
|
|
212
|
+
running: []
|
|
213
|
+
});
|
|
214
|
+
}
|
|
31
215
|
static get versionProperty() {
|
|
32
216
|
return this.namespace && `${this.namespace}.version`;
|
|
33
217
|
}
|
|
34
218
|
static get targetVersion() {
|
|
35
219
|
return this.migrations[this.migrations.length - 1].version;
|
|
36
220
|
}
|
|
221
|
+
static running(space) {
|
|
222
|
+
return this._state.running.includes(space.key.toHex());
|
|
223
|
+
}
|
|
37
224
|
static define(namespace, migrations) {
|
|
38
225
|
this.namespace = namespace;
|
|
39
226
|
this.migrations = migrations;
|
|
40
227
|
}
|
|
41
|
-
// TODO(wittjosiah): Multi-space migrations.
|
|
42
228
|
static async migrate(space, targetVersion) {
|
|
43
|
-
(0,
|
|
44
|
-
F:
|
|
45
|
-
L:
|
|
229
|
+
(0, import_invariant2.invariant)(!this.running(space), "Migration already running", {
|
|
230
|
+
F: __dxlog_file2,
|
|
231
|
+
L: 44,
|
|
232
|
+
S: this,
|
|
233
|
+
A: [
|
|
234
|
+
"!this.running(space)",
|
|
235
|
+
"'Migration already running'"
|
|
236
|
+
]
|
|
237
|
+
});
|
|
238
|
+
(0, import_invariant2.invariant)(this.versionProperty, "Migrations namespace not set", {
|
|
239
|
+
F: __dxlog_file2,
|
|
240
|
+
L: 45,
|
|
46
241
|
S: this,
|
|
47
242
|
A: [
|
|
48
243
|
"this.versionProperty",
|
|
49
244
|
"'Migrations namespace not set'"
|
|
50
245
|
]
|
|
51
246
|
});
|
|
52
|
-
(0,
|
|
53
|
-
F:
|
|
54
|
-
L:
|
|
247
|
+
(0, import_invariant2.invariant)(space.state.get() === import_echo.SpaceState.READY, "Space not ready", {
|
|
248
|
+
F: __dxlog_file2,
|
|
249
|
+
L: 46,
|
|
55
250
|
S: this,
|
|
56
251
|
A: [
|
|
57
252
|
"space.state.get() === SpaceState.READY",
|
|
@@ -65,30 +260,37 @@ var Migrations = class {
|
|
|
65
260
|
if (currentIndex === targetIndex) {
|
|
66
261
|
return false;
|
|
67
262
|
}
|
|
263
|
+
this._state.running.push(space.key.toHex());
|
|
68
264
|
if (targetIndex > currentIndex) {
|
|
69
265
|
const migrations = this.migrations.slice(currentIndex, targetIndex);
|
|
70
266
|
for (const migration of migrations) {
|
|
71
|
-
|
|
72
|
-
|
|
267
|
+
const builder = new MigrationBuilder(space);
|
|
268
|
+
await migration.next({
|
|
269
|
+
space,
|
|
270
|
+
builder
|
|
73
271
|
});
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
272
|
+
builder.changeProperties((propertiesStructure) => {
|
|
273
|
+
(0, import_invariant2.invariant)(this.versionProperty, "Migrations namespace not set", {
|
|
274
|
+
F: __dxlog_file2,
|
|
275
|
+
L: 62,
|
|
276
|
+
S: this,
|
|
277
|
+
A: [
|
|
278
|
+
"this.versionProperty",
|
|
279
|
+
"'Migrations namespace not set'"
|
|
280
|
+
]
|
|
281
|
+
});
|
|
282
|
+
propertiesStructure.data[this.versionProperty] = migration.version;
|
|
82
283
|
});
|
|
83
|
-
|
|
84
|
-
space.properties[this.versionProperty] = this.migrations[index - 1]?.version;
|
|
284
|
+
await builder._commit();
|
|
85
285
|
}
|
|
86
286
|
}
|
|
287
|
+
this._state.running.splice(this._state.running.indexOf(space.key.toHex()), 1);
|
|
87
288
|
return true;
|
|
88
289
|
}
|
|
89
290
|
};
|
|
90
291
|
// Annotate the CommonJS export names for ESM import in node:
|
|
91
292
|
0 && (module.exports = {
|
|
293
|
+
MigrationBuilder,
|
|
92
294
|
Migrations
|
|
93
295
|
});
|
|
94
296
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/migrations.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright
|
|
5
|
-
"mappings": "
|
|
6
|
-
"names": ["
|
|
3
|
+
"sources": ["../../../src/migration-builder.ts", "../../../src/migrations.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Doc, next as am } 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, migrateDocument } from '@dxos/echo-db';\nimport { SpaceDocVersion, encodeReference, type ObjectStructure, type SpaceDoc, Reference } 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';\nimport { type MaybePromise } from '@dxos/util';\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) => MaybePromise<{ 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 } = await migrate(objectStructure);\n\n const oldHandle = await this._findObjectContainingHandle(id);\n invariant(oldHandle);\n\n const newState: SpaceDoc = {\n version: SpaceDocVersion.CURRENT,\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: {\n [id]: {\n system: {\n type: encodeReference(requireTypeReference(schema)),\n },\n data: props,\n meta: {\n keys: [],\n },\n },\n },\n };\n const migratedDoc = migrateDocument(oldHandle.docSync() as Doc<SpaceDoc>, newState);\n const newHandle = this._repo.import<SpaceDoc>(am.save(migratedDoc));\n this._newLinks[id] = newHandle.url;\n this._addHandleToFlushList(newHandle);\n }\n\n async addObject(schema: S.Schema<any>, props: any) {\n const core = this._createObject({ schema, props });\n return core.id;\n }\n\n createReference(id: string) {\n return encodeReference(new Reference(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._addHandleToFlushList(this._newRoot);\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 async _findObjectContainingHandle(id: string): Promise<DocHandle<SpaceDoc> | 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 return docHandle;\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 version: SpaceDocVersion.CURRENT,\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._addHandleToFlushList(this._newRoot);\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 version: SpaceDocVersion.CURRENT,\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._addHandleToFlushList(newHandle);\n\n return core;\n }\n\n private _addHandleToFlushList(handle: DocHandle<any>) {\n this._flushStates.push({\n documentId: handle.documentId,\n heads: am.getHeads(handle.docSync()),\n });\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,uBAAqC;AAGrC,kBAAmC;AACnC,qBAAmE;AACnE,2BAAiG;AACjG,yBAA6C;AAC7C,uBAA0B;ACP1B,kBAA+C;AAC/C,IAAAA,oBAA0B;;ADUnB,IAAMC,mBAAN,MAAMA;EAYXC,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,eAAwC,CAAA;SACxCC,iBAA2B,CAAA;SAEpCC,WAAi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gBAAQ/C,iBAAiB,CAACI,wBAAAA;AACxBnC,gCAAAA,WAAU,KAAK4D,iBAAiB,gCAAA;;;;;;;;;AAChCzB,8BAAoBtB,KAAK,KAAK+C,eAAe,IAAIlB,UAAUxC;QAC7D,CAAA;AACA,cAAM4E,QAAQzC,QAAO;MACvB;IACF;AACA,SAAKqB,OAAOC,QAAQqB,OAAO,KAAKtB,OAAOC,QAAQsB,QAAQjB,MAAMzD,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", "oldHandle", "_findObjectContainingHandle", "invariant", "newState", "version", "SpaceDocVersion", "CURRENT", "access", "spaceKey", "key", "toHex", "system", "type", "encodeReference", "requireTypeReference", "data", "meta", "keys", "migratedDoc", "migrateDocument", "newHandle", "import", "am", "save", "url", "_addHandleToFlushList", "addObject", "core", "_createObject", "createReference", "Reference", "deleteObject", "push", "changeProperties", "changeFn", "_buildNewRoot", "change", "propertiesStructure", "properties", "_commit", "flush", "states", "internal", "createEpoch", "migration", "CreateEpochRequest", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "previousLinks", "create", "ObjectCore", "initNewObject", "setType", "getDoc", "handle", "heads", "getHeads", "Migrations", "migrations", "_state", "running", "versionProperty", "namespace", "targetVersion", "length", "space", "includes", "define", "state", "get", "SpaceState", "READY", "currentVersion", "currentIndex", "findIndex", "m", "i", "targetIndex", "slice", "builder", "next", "splice", "indexOf"]
|
|
7
7
|
}
|
package/dist/lib/node/meta.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"inputs":{"packages/sdk/migrations/src/migrations.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytes":21641,"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-protocol","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":15103},"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-protocol","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":4976},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2745}},"bytes":7899}}}
|
|
@@ -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,29 @@
|
|
|
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
|
+
import { type MaybePromise } from '@dxos/util';
|
|
5
|
+
export declare class MigrationBuilder {
|
|
6
|
+
private readonly _space;
|
|
7
|
+
private readonly _repo;
|
|
8
|
+
private readonly _automergeContext;
|
|
9
|
+
private readonly _rootDoc;
|
|
10
|
+
private readonly _newLinks;
|
|
11
|
+
private readonly _flushStates;
|
|
12
|
+
private readonly _deleteObjects;
|
|
13
|
+
private _newRoot?;
|
|
14
|
+
constructor(_space: Space);
|
|
15
|
+
findObject(id: string): Promise<ObjectStructure | undefined>;
|
|
16
|
+
migrateObject(id: string, migrate: (objectStructure: ObjectStructure) => MaybePromise<{
|
|
17
|
+
schema: S.Schema<any>;
|
|
18
|
+
props: any;
|
|
19
|
+
}>): Promise<void>;
|
|
20
|
+
addObject(schema: S.Schema<any>, props: any): Promise<string>;
|
|
21
|
+
createReference(id: string): import("@dxos/echo-protocol").EncodedReferenceObject;
|
|
22
|
+
deleteObject(id: string): void;
|
|
23
|
+
changeProperties(changeFn: (properties: ObjectStructure) => void): void;
|
|
24
|
+
private _findObjectContainingHandle;
|
|
25
|
+
private _buildNewRoot;
|
|
26
|
+
private _createObject;
|
|
27
|
+
private _addHandleToFlushList;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=migration-builder.d.ts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,EAAE,eAAe,KAAK,IAAI;YAiClD,2BAA2B;IAWzC,OAAO,CAAC,aAAa;IAoBrB,OAAO,CAAC,aAAa;IAuBrB,OAAO,CAAC,qBAAqB;CAM9B"}
|
|
@@ -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
|
|
8
|
-
|
|
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
|
|
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,
|
|
1
|
+
{"version":3,"file":"migrations.d.ts","sourceRoot":"","sources":["../../../src/migrations.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,KAAK,KAAK,EAAsB,MAAM,mBAAmB,CAAC;AAEnE,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAEvD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,KAAK,EAAE,KAAK,CAAC;IACb,OAAO,EAAE,gBAAgB,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,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,MAAM,CAAC,UAAU,EAAE,SAAS,EAAE,CAAM;IACpC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAkD;IAEvE,MAAM,KAAK,eAAe,uBAEzB;IAED,MAAM,KAAK,aAAa,WAEvB;IAED,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK;IAI3B,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE;WAK3C,OAAO,CAAC,KAAK,EAAE,KAAK,EAAE,aAAa,CAAC,EAAE,MAAM,GAAG,MAAM;CA6BnE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/migrations",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.9-main.079a532",
|
|
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/
|
|
24
|
-
"@dxos/echo-
|
|
25
|
-
"@dxos/
|
|
26
|
-
"@dxos/
|
|
23
|
+
"@dxos/automerge": "0.5.9-main.079a532",
|
|
24
|
+
"@dxos/echo-db": "0.5.9-main.079a532",
|
|
25
|
+
"@dxos/client": "0.5.9-main.079a532",
|
|
26
|
+
"@dxos/echo-protocol": "0.5.9-main.079a532",
|
|
27
|
+
"@dxos/echo-schema": "0.5.9-main.079a532",
|
|
28
|
+
"@dxos/invariant": "0.5.9-main.079a532",
|
|
29
|
+
"@dxos/log": "0.5.9-main.079a532",
|
|
30
|
+
"@dxos/util": "0.5.9-main.079a532",
|
|
31
|
+
"@dxos/protocols": "0.5.9-main.079a532"
|
|
27
32
|
},
|
|
28
33
|
"devDependencies": {},
|
|
29
34
|
"publishConfig": {
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2024 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { type Doc, next as am } 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, migrateDocument } from '@dxos/echo-db';
|
|
10
|
+
import { SpaceDocVersion, encodeReference, type ObjectStructure, type SpaceDoc, Reference } 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
|
+
import { type MaybePromise } from '@dxos/util';
|
|
15
|
+
|
|
16
|
+
export class MigrationBuilder {
|
|
17
|
+
private readonly _repo: Repo;
|
|
18
|
+
private readonly _automergeContext: AutomergeContext;
|
|
19
|
+
private readonly _rootDoc: Doc<SpaceDoc>;
|
|
20
|
+
|
|
21
|
+
// echoId -> automergeUrl
|
|
22
|
+
private readonly _newLinks: Record<string, string> = {};
|
|
23
|
+
private readonly _flushStates: FlushRequest.DocState[] = [];
|
|
24
|
+
private readonly _deleteObjects: string[] = [];
|
|
25
|
+
|
|
26
|
+
private _newRoot?: DocHandle<SpaceDoc> = undefined;
|
|
27
|
+
|
|
28
|
+
constructor(private readonly _space: Space) {
|
|
29
|
+
this._repo = this._space.db.coreDatabase.automerge.repo;
|
|
30
|
+
this._automergeContext = this._space.db.coreDatabase.automerge;
|
|
31
|
+
// TODO(wittjosiah): Accessing private API.
|
|
32
|
+
this._rootDoc = (this._space.db.coreDatabase as any)._automergeDocLoader
|
|
33
|
+
.getSpaceRootDocHandle()
|
|
34
|
+
.docSync() as Doc<SpaceDoc>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async findObject(id: string): Promise<ObjectStructure | undefined> {
|
|
38
|
+
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id]) as AnyDocumentId | undefined;
|
|
39
|
+
const docHandle = documentId && this._repo.find(documentId);
|
|
40
|
+
if (!docHandle) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
await docHandle.whenReady();
|
|
45
|
+
const doc = docHandle.docSync() as Doc<SpaceDoc>;
|
|
46
|
+
return doc.objects?.[id];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async migrateObject(
|
|
50
|
+
id: string,
|
|
51
|
+
migrate: (objectStructure: ObjectStructure) => MaybePromise<{ schema: S.Schema<any>; props: any }>,
|
|
52
|
+
) {
|
|
53
|
+
const objectStructure = await this.findObject(id);
|
|
54
|
+
if (!objectStructure) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const { schema, props } = await migrate(objectStructure);
|
|
59
|
+
|
|
60
|
+
const oldHandle = await this._findObjectContainingHandle(id);
|
|
61
|
+
invariant(oldHandle);
|
|
62
|
+
|
|
63
|
+
const newState: SpaceDoc = {
|
|
64
|
+
version: SpaceDocVersion.CURRENT,
|
|
65
|
+
access: {
|
|
66
|
+
spaceKey: this._space.key.toHex(),
|
|
67
|
+
},
|
|
68
|
+
objects: {
|
|
69
|
+
[id]: {
|
|
70
|
+
system: {
|
|
71
|
+
type: encodeReference(requireTypeReference(schema)),
|
|
72
|
+
},
|
|
73
|
+
data: props,
|
|
74
|
+
meta: {
|
|
75
|
+
keys: [],
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
const migratedDoc = migrateDocument(oldHandle.docSync() as Doc<SpaceDoc>, newState);
|
|
81
|
+
const newHandle = this._repo.import<SpaceDoc>(am.save(migratedDoc));
|
|
82
|
+
this._newLinks[id] = newHandle.url;
|
|
83
|
+
this._addHandleToFlushList(newHandle);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async addObject(schema: S.Schema<any>, props: any) {
|
|
87
|
+
const core = this._createObject({ schema, props });
|
|
88
|
+
return core.id;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
createReference(id: string) {
|
|
92
|
+
return encodeReference(new Reference(id));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
deleteObject(id: string) {
|
|
96
|
+
this._deleteObjects.push(id);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
changeProperties(changeFn: (properties: ObjectStructure) => void) {
|
|
100
|
+
if (!this._newRoot) {
|
|
101
|
+
this._buildNewRoot();
|
|
102
|
+
}
|
|
103
|
+
invariant(this._newRoot, 'New root not created');
|
|
104
|
+
|
|
105
|
+
this._newRoot.change((doc: SpaceDoc) => {
|
|
106
|
+
const propertiesStructure = doc.objects?.[this._space.properties.id];
|
|
107
|
+
propertiesStructure && changeFn(propertiesStructure);
|
|
108
|
+
});
|
|
109
|
+
this._addHandleToFlushList(this._newRoot);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* @internal
|
|
114
|
+
*/
|
|
115
|
+
async _commit() {
|
|
116
|
+
if (!this._newRoot) {
|
|
117
|
+
this._buildNewRoot();
|
|
118
|
+
}
|
|
119
|
+
invariant(this._newRoot, 'New root not created');
|
|
120
|
+
|
|
121
|
+
await this._automergeContext.flush({
|
|
122
|
+
states: this._flushStates,
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
// Create new epoch.
|
|
126
|
+
await this._space.internal.createEpoch({
|
|
127
|
+
migration: CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,
|
|
128
|
+
automergeRootUrl: this._newRoot.url,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private async _findObjectContainingHandle(id: string): Promise<DocHandle<SpaceDoc> | undefined> {
|
|
133
|
+
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id]) as AnyDocumentId | undefined;
|
|
134
|
+
const docHandle = documentId && this._repo.find(documentId);
|
|
135
|
+
if (!docHandle) {
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
await docHandle.whenReady();
|
|
140
|
+
return docHandle;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
private _buildNewRoot() {
|
|
144
|
+
const previousLinks = { ...(this._rootDoc.links ?? {}) };
|
|
145
|
+
for (const id of this._deleteObjects) {
|
|
146
|
+
delete previousLinks[id];
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
this._newRoot = this._repo.create<SpaceDoc>({
|
|
150
|
+
version: SpaceDocVersion.CURRENT,
|
|
151
|
+
access: {
|
|
152
|
+
spaceKey: this._space.key.toHex(),
|
|
153
|
+
},
|
|
154
|
+
objects: this._rootDoc.objects,
|
|
155
|
+
links: {
|
|
156
|
+
...previousLinks,
|
|
157
|
+
...this._newLinks,
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
this._addHandleToFlushList(this._newRoot);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private _createObject({ id, schema, props }: { id?: string; schema: S.Schema<any>; props: any }) {
|
|
164
|
+
const core = new ObjectCore();
|
|
165
|
+
if (id) {
|
|
166
|
+
core.id = id;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
core.initNewObject(props);
|
|
170
|
+
core.setType(requireTypeReference(schema));
|
|
171
|
+
const newHandle = this._repo.create<SpaceDoc>({
|
|
172
|
+
version: SpaceDocVersion.CURRENT,
|
|
173
|
+
access: {
|
|
174
|
+
spaceKey: this._space.key.toHex(),
|
|
175
|
+
},
|
|
176
|
+
objects: {
|
|
177
|
+
[core.id]: core.getDoc(),
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
this._newLinks[core.id] = newHandle.url;
|
|
181
|
+
this._addHandleToFlushList(newHandle);
|
|
182
|
+
|
|
183
|
+
return core;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
private _addHandleToFlushList(handle: DocHandle<any>) {
|
|
187
|
+
this._flushStates.push({
|
|
188
|
+
documentId: handle.documentId,
|
|
189
|
+
heads: am.getHeads(handle.docSync()),
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
}
|
package/src/migrations.test.ts
CHANGED
|
@@ -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:
|
|
18
|
-
|
|
19
|
-
|
|
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
|
+
await builder.addObject(Expando, { namespace: 'test', count: 1 });
|
|
26
20
|
},
|
|
27
21
|
},
|
|
28
22
|
{
|
|
29
|
-
version:
|
|
30
|
-
|
|
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.
|
|
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:
|
|
42
|
-
|
|
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.
|
|
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(
|
|
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'] =
|
|
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(
|
|
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'] =
|
|
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,
|
|
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(
|
|
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
|
-
|
|
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
|
|
17
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
}
|