@dxos/migrations 0.6.1 → 0.6.2-main.8a232a5
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 +6 -9
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +6 -9
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/types/src/migration-builder.d.ts +1 -1
- package/dist/types/src/migration-builder.d.ts.map +1 -1
- package/package.json +10 -10
- package/src/migration-builder.ts +10 -14
|
@@ -10,7 +10,7 @@ var MigrationBuilder = class {
|
|
|
10
10
|
constructor(_space) {
|
|
11
11
|
this._space = _space;
|
|
12
12
|
this._newLinks = {};
|
|
13
|
-
this.
|
|
13
|
+
this._flushIds = [];
|
|
14
14
|
this._deleteObjects = [];
|
|
15
15
|
this._newRoot = void 0;
|
|
16
16
|
this._repo = this._space.db.coreDatabase.automerge.repo;
|
|
@@ -36,7 +36,7 @@ var MigrationBuilder = class {
|
|
|
36
36
|
const oldHandle = await this._findObjectContainingHandle(id);
|
|
37
37
|
invariant(oldHandle, void 0, {
|
|
38
38
|
F: __dxlog_file,
|
|
39
|
-
L:
|
|
39
|
+
L: 60,
|
|
40
40
|
S: this,
|
|
41
41
|
A: [
|
|
42
42
|
"oldHandle",
|
|
@@ -84,7 +84,7 @@ var MigrationBuilder = class {
|
|
|
84
84
|
}
|
|
85
85
|
invariant(this._newRoot, "New root not created", {
|
|
86
86
|
F: __dxlog_file,
|
|
87
|
-
L:
|
|
87
|
+
L: 102,
|
|
88
88
|
S: this,
|
|
89
89
|
A: [
|
|
90
90
|
"this._newRoot",
|
|
@@ -106,7 +106,7 @@ var MigrationBuilder = class {
|
|
|
106
106
|
}
|
|
107
107
|
invariant(this._newRoot, "New root not created", {
|
|
108
108
|
F: __dxlog_file,
|
|
109
|
-
L:
|
|
109
|
+
L: 118,
|
|
110
110
|
S: this,
|
|
111
111
|
A: [
|
|
112
112
|
"this._newRoot",
|
|
@@ -114,7 +114,7 @@ var MigrationBuilder = class {
|
|
|
114
114
|
]
|
|
115
115
|
});
|
|
116
116
|
await this._automergeContext.flush({
|
|
117
|
-
|
|
117
|
+
documentIds: this._flushIds
|
|
118
118
|
});
|
|
119
119
|
await this._space.internal.createEpoch({
|
|
120
120
|
migration: CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,
|
|
@@ -171,10 +171,7 @@ var MigrationBuilder = class {
|
|
|
171
171
|
return core;
|
|
172
172
|
}
|
|
173
173
|
_addHandleToFlushList(handle) {
|
|
174
|
-
this.
|
|
175
|
-
documentId: handle.documentId,
|
|
176
|
-
heads: am.getHeads(handle.docSync())
|
|
177
|
-
});
|
|
174
|
+
this._flushIds.push(handle.documentId);
|
|
178
175
|
}
|
|
179
176
|
};
|
|
180
177
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
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
|
|
5
|
-
"mappings": ";AAIA,SAAmBA,QAAQC,UAAU;AAGrC,SAASC,0BAA0B;AACnC,SAAgCC,YAAYC,
|
|
6
|
-
"names": ["next", "am", "CreateEpochRequest", "ObjectCore", "migrateDocument", "SpaceDocVersion", "encodeReference", "Reference", "requireTypeReference", "invariant", "MigrationBuilder", "constructor", "_space", "_newLinks", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Doc, next as am } from '@dxos/automerge/automerge';\nimport { type AnyDocumentId, type DocumentId } from '@dxos/automerge/automerge-repo';\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { type AutomergeContext, ObjectCore, migrateDocument, type RepoProxy, type DocHandleProxy } 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 MaybePromise } from '@dxos/util';\n\nexport class MigrationBuilder {\n private readonly _repo: RepoProxy;\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 _flushIds: DocumentId[] = [];\n private readonly _deleteObjects: string[] = [];\n\n private _newRoot?: DocHandleProxy<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 documentIds: this._flushIds,\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<DocHandleProxy<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() as ObjectStructure,\n },\n });\n this._newLinks[core.id] = newHandle.url;\n this._addHandleToFlushList(newHandle);\n\n return core;\n }\n\n private _addHandleToFlushList(handle: DocHandleProxy<any>) {\n this._flushIds.push(handle.documentId);\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,uBAA4D;AACxG,SAASC,iBAAiBC,iBAAsDC,iBAAiB;AACjG,SAASC,4BAAoC;AAC7C,SAASC,iBAAiB;;AAGnB,IAAMC,mBAAN,MAAMA;EAYXC,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,YAA0B,CAAA;SAC1BC,iBAA2B,CAAA;SAEpCC,WAAsCC;AAG5C,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;EAEA,MAAMC,WAAWC,IAAkD;AACjE,UAAMC,aAAc,KAAKN,SAASO,QAAQF,EAAAA,KAAO,KAAKhB,UAAUgB,EAAAA;AAChE,UAAMG,YAAYF,cAAc,KAAKZ,MAAMe,KAAKH,UAAAA;AAChD,QAAI,CAACE,WAAW;AACd,aAAOf;IACT;AAEA,UAAMe,UAAUE,UAAS;AACzB,UAAMC,MAAMH,UAAUL,QAAO;AAC7B,WAAOQ,IAAIC,UAAUP,EAAAA;EACvB;EAEA,MAAMQ,cACJR,IACAS,SACA;AACA,UAAMC,kBAAkB,MAAM,KAAKX,WAAWC,EAAAA;AAC9C,QAAI,CAACU,iBAAiB;AACpB;IACF;AAEA,UAAM,EAAEC,QAAQC,MAAK,IAAK,MAAMH,QAAQC,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,IAAkB;AACtF,UAAMK,IAAI,KAAKzB,WAAWuB,UAAU,CAACC,MAAMA,EAAEhB,YAAYF,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": ["next", "am", "CreateEpochRequest", "ObjectCore", "migrateDocument", "SpaceDocVersion", "encodeReference", "Reference", "requireTypeReference", "invariant", "MigrationBuilder", "constructor", "_space", "_newLinks", "_flushIds", "_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", "documentIds", "internal", "createEpoch", "migration", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "previousLinks", "create", "initNewObject", "setType", "getDoc", "handle", "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/migration-builder.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytes":21308,"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":14921},"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":4902},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2745}},"bytes":7825}}}
|
package/dist/lib/node/index.cjs
CHANGED
|
@@ -35,7 +35,7 @@ var MigrationBuilder = class {
|
|
|
35
35
|
constructor(_space) {
|
|
36
36
|
this._space = _space;
|
|
37
37
|
this._newLinks = {};
|
|
38
|
-
this.
|
|
38
|
+
this._flushIds = [];
|
|
39
39
|
this._deleteObjects = [];
|
|
40
40
|
this._newRoot = void 0;
|
|
41
41
|
this._repo = this._space.db.coreDatabase.automerge.repo;
|
|
@@ -61,7 +61,7 @@ var MigrationBuilder = class {
|
|
|
61
61
|
const oldHandle = await this._findObjectContainingHandle(id);
|
|
62
62
|
(0, import_invariant.invariant)(oldHandle, void 0, {
|
|
63
63
|
F: __dxlog_file,
|
|
64
|
-
L:
|
|
64
|
+
L: 60,
|
|
65
65
|
S: this,
|
|
66
66
|
A: [
|
|
67
67
|
"oldHandle",
|
|
@@ -109,7 +109,7 @@ var MigrationBuilder = class {
|
|
|
109
109
|
}
|
|
110
110
|
(0, import_invariant.invariant)(this._newRoot, "New root not created", {
|
|
111
111
|
F: __dxlog_file,
|
|
112
|
-
L:
|
|
112
|
+
L: 102,
|
|
113
113
|
S: this,
|
|
114
114
|
A: [
|
|
115
115
|
"this._newRoot",
|
|
@@ -131,7 +131,7 @@ var MigrationBuilder = class {
|
|
|
131
131
|
}
|
|
132
132
|
(0, import_invariant.invariant)(this._newRoot, "New root not created", {
|
|
133
133
|
F: __dxlog_file,
|
|
134
|
-
L:
|
|
134
|
+
L: 118,
|
|
135
135
|
S: this,
|
|
136
136
|
A: [
|
|
137
137
|
"this._newRoot",
|
|
@@ -139,7 +139,7 @@ var MigrationBuilder = class {
|
|
|
139
139
|
]
|
|
140
140
|
});
|
|
141
141
|
await this._automergeContext.flush({
|
|
142
|
-
|
|
142
|
+
documentIds: this._flushIds
|
|
143
143
|
});
|
|
144
144
|
await this._space.internal.createEpoch({
|
|
145
145
|
migration: import_halo.CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,
|
|
@@ -196,10 +196,7 @@ var MigrationBuilder = class {
|
|
|
196
196
|
return core;
|
|
197
197
|
}
|
|
198
198
|
_addHandleToFlushList(handle) {
|
|
199
|
-
this.
|
|
200
|
-
documentId: handle.documentId,
|
|
201
|
-
heads: import_automerge.next.getHeads(handle.docSync())
|
|
202
|
-
});
|
|
199
|
+
this._flushIds.push(handle.documentId);
|
|
203
200
|
}
|
|
204
201
|
};
|
|
205
202
|
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
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
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAIA,uBAAqC;AAGrC,kBAAmC;AACnC,
|
|
6
|
-
"names": ["import_invariant", "MigrationBuilder", "constructor", "_space", "_newLinks", "
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { type Doc, next as am } from '@dxos/automerge/automerge';\nimport { type AnyDocumentId, type DocumentId } from '@dxos/automerge/automerge-repo';\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { type AutomergeContext, ObjectCore, migrateDocument, type RepoProxy, type DocHandleProxy } 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 MaybePromise } from '@dxos/util';\n\nexport class MigrationBuilder {\n private readonly _repo: RepoProxy;\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 _flushIds: DocumentId[] = [];\n private readonly _deleteObjects: string[] = [];\n\n private _newRoot?: DocHandleProxy<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 documentIds: this._flushIds,\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<DocHandleProxy<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() as ObjectStructure,\n },\n });\n this._newLinks[core.id] = newHandle.url;\n this._addHandleToFlushList(newHandle);\n\n return core;\n }\n\n private _addHandleToFlushList(handle: DocHandleProxy<any>) {\n this._flushIds.push(handle.documentId);\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,qBAAwG;AACxG,2BAAiG;AACjG,yBAA6C;AAC7C,uBAA0B;ACP1B,kBAA+C;AAC/C,IAAAA,oBAA0B;;ADSnB,IAAMC,mBAAN,MAAMA;EAYXC,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,YAA0B,CAAA;SAC1BC,iBAA2B,CAAA;SAEpCC,WAAsCC;AAG5C,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;EAEA,MAAMC,WAAWC,IAAkD;AACjE,UAAMC,aAAc,KAAKN,SAASO,QAAQF,EAAAA,KAAO,KAAKhB,UAAUgB,EAAAA;AAChE,UAAMG,YAAYF,cAAc,KAAKZ,MAAMe,KAAKH,UAAAA;AAChD,QAAI,CAACE,WAAW;AACd,aAAOf;IACT;AAEA,UAAMe,UAAUE,UAAS;AACzB,UAAMC,MAAMH,UAAUL,QAAO;AAC7B,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A,WAAU8D,MAAMG,MAAMC,IAAG,MAAOC,uBAAWC,OAAO,mBAAA;;;;;;;;;AAClD,UAAMC,iBAAiBP,MAAM1B,WAAW,KAAKsB,eAAe;AAC5D,UAAMY,eAAe,KAAKf,WAAWgB,UAAU,CAACC,MAAMA,EAAEtE,YAAYmE,cAAAA,IAAkB;AACtF,UAAMI,IAAI,KAAKlB,WAAWgB,UAAU,CAACC,MAAMA,EAAEtE,YAAY0D,aAAAA;AACzD,UAAMc,cAAcD,MAAM,KAAK,KAAKlB,WAAWM,SAASY,IAAI;AAC5D,QAAIH,iBAAiBI,aAAa;AAChC,aAAO;IACT;AAEA,SAAKlB,OAAOC,QAAQ3B,KAAKgC,MAAMvD,IAAIC,MAAK,CAAA;AACxC,QAAIkE,cAAcJ,cAAc;AAC9B,YAAMf,aAAa,KAAKA,WAAWoB,MAAML,cAAcI,WAAAA;AACvD,iBAAWhC,aAAaa,YAAY;AAClC,cAAMqB,UAAU,IAAI9G,iBAAiBgG,KAAAA;AACrC,cAAMpB,UAAUmC,KAAK;UAAEf;UAAOc;QAAQ,CAAA;AACtCA,gBAAQ7C,iBAAiB,CAACI,wBAAAA;AACxBnC,gCAAAA,WAAU,KAAK0D,iBAAiB,gCAAA;;;;;;;;;AAChCvB,8BAAoBtB,KAAK,KAAK6C,eAAe,IAAIhB,UAAUxC;QAC7D,CAAA;AACA,cAAM0E,QAAQvC,QAAO;MACvB;IACF;AACA,SAAKmB,OAAOC,QAAQqB,OAAO,KAAKtB,OAAOC,QAAQsB,QAAQjB,MAAMvD,IAAIC,MAAK,CAAA,GAAK,CAAA;AAE3E,WAAO;EACT;AACF;",
|
|
6
|
+
"names": ["import_invariant", "MigrationBuilder", "constructor", "_space", "_newLinks", "_flushIds", "_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", "documentIds", "internal", "createEpoch", "migration", "CreateEpochRequest", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "previousLinks", "create", "ObjectCore", "initNewObject", "setType", "getDoc", "handle", "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/migration-builder.ts":{"bytes":
|
|
1
|
+
{"inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytes":21308,"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":14921},"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":4902},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2745}},"bytes":7825}}}
|
|
@@ -8,7 +8,7 @@ export declare class MigrationBuilder {
|
|
|
8
8
|
private readonly _automergeContext;
|
|
9
9
|
private readonly _rootDoc;
|
|
10
10
|
private readonly _newLinks;
|
|
11
|
-
private readonly
|
|
11
|
+
private readonly _flushIds;
|
|
12
12
|
private readonly _deleteObjects;
|
|
13
13
|
private _newRoot?;
|
|
14
14
|
constructor(_space: Space);
|
|
@@ -1 +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,EAAoC,KAAK,eAAe,EAA4B,MAAM,qBAAqB,CAAC;AACvH,OAAO,EAAwB,KAAK,CAAC,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"migration-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AC,aAAa;IAoBrB,OAAO,CAAC,aAAa;IAuBrB,OAAO,CAAC,qBAAqB;CAG9B"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/migrations",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2-main.8a232a5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
@@ -20,15 +20,15 @@
|
|
|
20
20
|
"src"
|
|
21
21
|
],
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@dxos/
|
|
24
|
-
"@dxos/
|
|
25
|
-
"@dxos/echo-
|
|
26
|
-
"@dxos/
|
|
27
|
-
"@dxos/echo-schema": "0.6.
|
|
28
|
-
"@dxos/
|
|
29
|
-
"@dxos/log": "0.6.
|
|
30
|
-
"@dxos/
|
|
31
|
-
"@dxos/
|
|
23
|
+
"@dxos/automerge": "0.6.2-main.8a232a5",
|
|
24
|
+
"@dxos/echo-protocol": "0.6.2-main.8a232a5",
|
|
25
|
+
"@dxos/echo-db": "0.6.2-main.8a232a5",
|
|
26
|
+
"@dxos/invariant": "0.6.2-main.8a232a5",
|
|
27
|
+
"@dxos/echo-schema": "0.6.2-main.8a232a5",
|
|
28
|
+
"@dxos/client": "0.6.2-main.8a232a5",
|
|
29
|
+
"@dxos/log": "0.6.2-main.8a232a5",
|
|
30
|
+
"@dxos/protocols": "0.6.2-main.8a232a5",
|
|
31
|
+
"@dxos/util": "0.6.2-main.8a232a5"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {},
|
|
34
34
|
"publishConfig": {
|
package/src/migration-builder.ts
CHANGED
|
@@ -3,27 +3,26 @@
|
|
|
3
3
|
//
|
|
4
4
|
|
|
5
5
|
import { type Doc, next as am } from '@dxos/automerge/automerge';
|
|
6
|
-
import { type AnyDocumentId, type
|
|
6
|
+
import { type AnyDocumentId, type DocumentId } from '@dxos/automerge/automerge-repo';
|
|
7
7
|
import { type Space } from '@dxos/client/echo';
|
|
8
8
|
import { CreateEpochRequest } from '@dxos/client/halo';
|
|
9
|
-
import { type AutomergeContext, ObjectCore, migrateDocument } from '@dxos/echo-db';
|
|
9
|
+
import { type AutomergeContext, ObjectCore, migrateDocument, type RepoProxy, type DocHandleProxy } from '@dxos/echo-db';
|
|
10
10
|
import { SpaceDocVersion, encodeReference, type ObjectStructure, type SpaceDoc, Reference } from '@dxos/echo-protocol';
|
|
11
11
|
import { requireTypeReference, type S } from '@dxos/echo-schema';
|
|
12
12
|
import { invariant } from '@dxos/invariant';
|
|
13
|
-
import { type FlushRequest } from '@dxos/protocols/proto/dxos/echo/service';
|
|
14
13
|
import { type MaybePromise } from '@dxos/util';
|
|
15
14
|
|
|
16
15
|
export class MigrationBuilder {
|
|
17
|
-
private readonly _repo:
|
|
16
|
+
private readonly _repo: RepoProxy;
|
|
18
17
|
private readonly _automergeContext: AutomergeContext;
|
|
19
18
|
private readonly _rootDoc: Doc<SpaceDoc>;
|
|
20
19
|
|
|
21
20
|
// echoId -> automergeUrl
|
|
22
21
|
private readonly _newLinks: Record<string, string> = {};
|
|
23
|
-
private readonly
|
|
22
|
+
private readonly _flushIds: DocumentId[] = [];
|
|
24
23
|
private readonly _deleteObjects: string[] = [];
|
|
25
24
|
|
|
26
|
-
private _newRoot?:
|
|
25
|
+
private _newRoot?: DocHandleProxy<SpaceDoc> = undefined;
|
|
27
26
|
|
|
28
27
|
constructor(private readonly _space: Space) {
|
|
29
28
|
this._repo = this._space.db.coreDatabase.automerge.repo;
|
|
@@ -119,7 +118,7 @@ export class MigrationBuilder {
|
|
|
119
118
|
invariant(this._newRoot, 'New root not created');
|
|
120
119
|
|
|
121
120
|
await this._automergeContext.flush({
|
|
122
|
-
|
|
121
|
+
documentIds: this._flushIds,
|
|
123
122
|
});
|
|
124
123
|
|
|
125
124
|
// Create new epoch.
|
|
@@ -129,7 +128,7 @@ export class MigrationBuilder {
|
|
|
129
128
|
});
|
|
130
129
|
}
|
|
131
130
|
|
|
132
|
-
private async _findObjectContainingHandle(id: string): Promise<
|
|
131
|
+
private async _findObjectContainingHandle(id: string): Promise<DocHandleProxy<SpaceDoc> | undefined> {
|
|
133
132
|
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id]) as AnyDocumentId | undefined;
|
|
134
133
|
const docHandle = documentId && this._repo.find(documentId);
|
|
135
134
|
if (!docHandle) {
|
|
@@ -174,7 +173,7 @@ export class MigrationBuilder {
|
|
|
174
173
|
spaceKey: this._space.key.toHex(),
|
|
175
174
|
},
|
|
176
175
|
objects: {
|
|
177
|
-
[core.id]: core.getDoc(),
|
|
176
|
+
[core.id]: core.getDoc() as ObjectStructure,
|
|
178
177
|
},
|
|
179
178
|
});
|
|
180
179
|
this._newLinks[core.id] = newHandle.url;
|
|
@@ -183,10 +182,7 @@ export class MigrationBuilder {
|
|
|
183
182
|
return core;
|
|
184
183
|
}
|
|
185
184
|
|
|
186
|
-
private _addHandleToFlushList(handle:
|
|
187
|
-
this.
|
|
188
|
-
documentId: handle.documentId,
|
|
189
|
-
heads: am.getHeads(handle.docSync()),
|
|
190
|
-
});
|
|
185
|
+
private _addHandleToFlushList(handle: DocHandleProxy<any>) {
|
|
186
|
+
this._flushIds.push(handle.documentId);
|
|
191
187
|
}
|
|
192
188
|
}
|