@dxos/migrations 0.6.13 → 0.6.14-main.69511f5
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 +8 -8
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node/index.cjs +8 -8
- package/dist/lib/node/index.cjs.map +3 -3
- package/dist/lib/node/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +270 -0
- package/dist/lib/node-esm/index.mjs.map +7 -0
- package/dist/lib/node-esm/meta.json +1 -0
- package/dist/types/src/migration-builder.d.ts.map +1 -1
- package/package.json +13 -14
- package/src/migration-builder.ts +9 -8
- package/src/migrations.test.ts +1 -2
|
@@ -17,7 +17,7 @@ var MigrationBuilder = class {
|
|
|
17
17
|
this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().docSync();
|
|
18
18
|
}
|
|
19
19
|
async findObject(id) {
|
|
20
|
-
const documentId = this._rootDoc.links?.[id] || this._newLinks[id];
|
|
20
|
+
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString();
|
|
21
21
|
const docHandle = documentId && this._repo.find(documentId);
|
|
22
22
|
if (!docHandle) {
|
|
23
23
|
return void 0;
|
|
@@ -119,7 +119,7 @@ var MigrationBuilder = class {
|
|
|
119
119
|
});
|
|
120
120
|
}
|
|
121
121
|
async _findObjectContainingHandle(id) {
|
|
122
|
-
const documentId = this._rootDoc.links?.[id] || this._newLinks[id];
|
|
122
|
+
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString();
|
|
123
123
|
const docHandle = documentId && this._repo.find(documentId);
|
|
124
124
|
if (!docHandle) {
|
|
125
125
|
return void 0;
|
|
@@ -128,11 +128,14 @@ var MigrationBuilder = class {
|
|
|
128
128
|
return docHandle;
|
|
129
129
|
}
|
|
130
130
|
_buildNewRoot() {
|
|
131
|
-
const
|
|
131
|
+
const links = {
|
|
132
132
|
...this._rootDoc.links ?? {}
|
|
133
133
|
};
|
|
134
134
|
for (const id of this._deleteObjects) {
|
|
135
|
-
delete
|
|
135
|
+
delete links[id];
|
|
136
|
+
}
|
|
137
|
+
for (const [id, url] of Object.entries(this._newLinks)) {
|
|
138
|
+
links[id] = new am.RawString(url);
|
|
136
139
|
}
|
|
137
140
|
this._newRoot = this._repo.create({
|
|
138
141
|
version: SpaceDocVersion.CURRENT,
|
|
@@ -140,10 +143,7 @@ var MigrationBuilder = class {
|
|
|
140
143
|
spaceKey: this._space.key.toHex()
|
|
141
144
|
},
|
|
142
145
|
objects: this._rootDoc.objects,
|
|
143
|
-
links
|
|
144
|
-
...previousLinks,
|
|
145
|
-
...this._newLinks
|
|
146
|
-
}
|
|
146
|
+
links
|
|
147
147
|
});
|
|
148
148
|
this._addHandleToFlushList(this._newRoot);
|
|
149
149
|
}
|
|
@@ -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 DocumentId } from '@dxos/automerge/automerge-repo';\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { 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 _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._repo;\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._space.db.coreDatabase.flush();\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
|
|
5
|
-
"mappings": ";AAIA,SAAmBA,QAAQC,UAAU;AAGrC,SAASC,0BAA0B;AACnC,SAASC,YAAYC,uBAA4D;AACjF,SAASC,iBAAiBC,iBAAsDC,iBAAiB;AACjG,SAASC,4BAAoC;AAC7C,SAASC,iBAAiB;;AAGnB,IAAMC,mBAAN,MAAMA;EAWXC,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,YAA0B,CAAA;SAC1BC,iBAA2B,CAAA;SAEpCC,WAAsCC;AAG5C,SAAKC,QAAQ,KAAKN,OAAOO,GAAGC,aAAaF;AAEzC,SAAKG,WAAY,KAAKT,OAAOO,GAAGC,aAAqBE,oBAClDC,sBAAqB,EACrBC,QAAO;EACZ;EAEA,MAAMC,WAAWC,IAAkD;AACjE,UAAMC,
|
|
6
|
-
"names": ["next", "am", "CreateEpochRequest", "ObjectCore", "migrateDocument", "SpaceDocVersion", "encodeReference", "Reference", "requireTypeReference", "invariant", "MigrationBuilder", "constructor", "_space", "_newLinks", "_flushIds", "_deleteObjects", "_newRoot", "undefined", "_repo", "db", "coreDatabase", "_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", "internal", "createEpoch", "migration", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "
|
|
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 { 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 _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._repo;\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])?.toString() 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._space.db.coreDatabase.flush();\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])?.toString() 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 links = { ...(this._rootDoc.links ?? {}) };\n for (const id of this._deleteObjects) {\n delete links[id];\n }\n\n for (const [id, url] of Object.entries(this._newLinks)) {\n links[id] = new am.RawString(url);\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 });\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.SPACE_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,SAASC,YAAYC,uBAA4D;AACjF,SAASC,iBAAiBC,iBAAsDC,iBAAiB;AACjG,SAASC,4BAAoC;AAC7C,SAASC,iBAAiB;;AAGnB,IAAMC,mBAAN,MAAMA;EAWXC,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,YAA0B,CAAA;SAC1BC,iBAA2B,CAAA;SAEpCC,WAAsCC;AAG5C,SAAKC,QAAQ,KAAKN,OAAOO,GAAGC,aAAaF;AAEzC,SAAKG,WAAY,KAAKT,OAAOO,GAAGC,aAAqBE,oBAClDC,sBAAqB,EACrBC,QA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;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", "_rootDoc", "_automergeDocLoader", "getSpaceRootDocHandle", "docSync", "findObject", "id", "documentId", "links", "toString", "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", "internal", "createEpoch", "migration", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "Object", "entries", "RawString", "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", "SPACE_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":20959,"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":9845,"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":14793},"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":4847},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2757}},"bytes":7782}}}
|
package/dist/lib/node/index.cjs
CHANGED
|
@@ -42,7 +42,7 @@ var MigrationBuilder = class {
|
|
|
42
42
|
this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().docSync();
|
|
43
43
|
}
|
|
44
44
|
async findObject(id) {
|
|
45
|
-
const documentId = this._rootDoc.links?.[id] || this._newLinks[id];
|
|
45
|
+
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString();
|
|
46
46
|
const docHandle = documentId && this._repo.find(documentId);
|
|
47
47
|
if (!docHandle) {
|
|
48
48
|
return void 0;
|
|
@@ -144,7 +144,7 @@ var MigrationBuilder = class {
|
|
|
144
144
|
});
|
|
145
145
|
}
|
|
146
146
|
async _findObjectContainingHandle(id) {
|
|
147
|
-
const documentId = this._rootDoc.links?.[id] || this._newLinks[id];
|
|
147
|
+
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString();
|
|
148
148
|
const docHandle = documentId && this._repo.find(documentId);
|
|
149
149
|
if (!docHandle) {
|
|
150
150
|
return void 0;
|
|
@@ -153,11 +153,14 @@ var MigrationBuilder = class {
|
|
|
153
153
|
return docHandle;
|
|
154
154
|
}
|
|
155
155
|
_buildNewRoot() {
|
|
156
|
-
const
|
|
156
|
+
const links = {
|
|
157
157
|
...this._rootDoc.links ?? {}
|
|
158
158
|
};
|
|
159
159
|
for (const id of this._deleteObjects) {
|
|
160
|
-
delete
|
|
160
|
+
delete links[id];
|
|
161
|
+
}
|
|
162
|
+
for (const [id, url] of Object.entries(this._newLinks)) {
|
|
163
|
+
links[id] = new import_automerge.next.RawString(url);
|
|
161
164
|
}
|
|
162
165
|
this._newRoot = this._repo.create({
|
|
163
166
|
version: import_echo_protocol.SpaceDocVersion.CURRENT,
|
|
@@ -165,10 +168,7 @@ var MigrationBuilder = class {
|
|
|
165
168
|
spaceKey: this._space.key.toHex()
|
|
166
169
|
},
|
|
167
170
|
objects: this._rootDoc.objects,
|
|
168
|
-
links
|
|
169
|
-
...previousLinks,
|
|
170
|
-
...this._newLinks
|
|
171
|
-
}
|
|
171
|
+
links
|
|
172
172
|
});
|
|
173
173
|
this._addHandleToFlushList(this._newRoot);
|
|
174
174
|
}
|
|
@@ -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 DocumentId } from '@dxos/automerge/automerge-repo';\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { 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 _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._repo;\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._space.db.coreDatabase.flush();\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
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAIA,uBAAqC;AAGrC,kBAAmC;AACnC,qBAAiF;AACjF,2BAAiG;AACjG,yBAA6C;AAC7C,uBAA0B;ACP1B,kBAA+C;AAC/C,IAAAA,oBAA0B;;ADSnB,IAAMC,mBAAN,MAAMA;EAWXC,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,YAA0B,CAAA;SAC1BC,iBAA2B,CAAA;SAEpCC,WAAsCC;AAG5C,SAAKC,QAAQ,KAAKN,OAAOO,GAAGC,aAAaF;AAEzC,SAAKG,WAAY,KAAKT,OAAOO,GAAGC,aAAqBE,oBAClDC,sBAAqB,EACrBC,QAAO;EACZ;EAEA,MAAMC,WAAWC,IAAkD;AACjE,UAAMC,
|
|
6
|
-
"names": ["import_invariant", "MigrationBuilder", "constructor", "_space", "_newLinks", "_flushIds", "_deleteObjects", "_newRoot", "undefined", "_repo", "db", "coreDatabase", "_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", "internal", "createEpoch", "migration", "CreateEpochRequest", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "
|
|
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 { 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 _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._repo;\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])?.toString() 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._space.db.coreDatabase.flush();\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])?.toString() 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 links = { ...(this._rootDoc.links ?? {}) };\n for (const id of this._deleteObjects) {\n delete links[id];\n }\n\n for (const [id, url] of Object.entries(this._newLinks)) {\n links[id] = new am.RawString(url);\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 });\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.SPACE_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,qBAAiF;AACjF,2BAAiG;AACjG,yBAA6C;AAC7C,uBAA0B;ACP1B,kBAA+C;AAC/C,IAAAA,oBAA0B;;ADSnB,IAAMC,mBAAN,MAAMA;EAWXC,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,YAA0B,CAAA;SAC1BC,iBAA2B,CAAA;SAEpCC,WAAsCC;AAG5C,SAAKC,QAAQ,KAAKN,OAAOO,GAAGC,aAAaF;AAEzC,SAAKG,WAAY,KAAKT,OAAOO,GAAGC,aAAqBE,oBAClDC,sBAAqB,EACrBC,QAAO;EACZ;EAEA,MAAMC,WAAWC,IAAkD;AACjE,UAAMC,cAAc,KAAKN,SAASO,QAAQF,EAAAA,KAAO,KAAKb,UAAUa,EAAAA,IAAMG,SAAAA;AACtE,UAAMC,YAAYH,cAAc,KAAKT,MAAMa,KAAKJ,UAAAA;AAChD,QAAI,CAACG,WAAW;AACd,aAAOb;IACT;AAEA,UAAMa,UAAUE,UAAS;AACzB,UAAMC,MAAMH,UAAUN,QAAO;AAC7B,WAAOS,IAAIC,UAAUR,EAAAA;EACvB;EAEA,MAAMS,cACJT,IACAU,SACA;AACA,UAAMC,kBAAkB,MAAM,KAAKZ,WAAWC,EAAAA;AAC9C,QAAI,CAACW,iBAAiB;AACpB;IACF;AAEA,UAAM,EAAEC,QAAQC,MAAK,IAAK,MAAMH,QAAQC,eAAAA;AAExC,UAAMG,YAAY,MAAM,KAAKC,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,WAAWgB,UAAU,CAACC,MAAMA,EAAEvE,YAAYoE,cAAAA,IAAkB;AACtF,UAAMI,IAAI,KAAKlB,WAAWgB,UAAU,CAACC,MAAMA,EAAEvE,YAAY2D,aAAAA;AACzD,UAAMc,cAAcD,MAAM,KAAK,KAAKlB,WAAWM,SAASY,IAAI;AAC5D,QAAIH,iBAAiBI,aAAa;AAChC,aAAO;IACT;AAEA,SAAKlB,OAAOC,QAAQ5B,KAAKiC,MAAMxD,IAAIC,MAAK,CAAA;AACxC,QAAImE,cAAcJ,cAAc;AAC9B,YAAMf,aAAa,KAAKA,WAAWoB,MAAML,cAAcI,WAAAA;AACvD,iBAAWlC,aAAae,YAAY;AAClC,cAAMqB,UAAU,IAAI7G,iBAAiB+F,KAAAA;AACrC,cAAMtB,UAAUqC,KAAK;UAAEf;UAAOc;QAAQ,CAAA;AACtCA,gBAAQ9C,iBAAiB,CAACI,wBAAAA;AACxBnC,gCAAAA,WAAU,KAAK2D,iBAAiB,gCAAA;;;;;;;;;AAChCxB,8BAAoBtB,KAAK,KAAK8C,eAAe,IAAIlB,UAAUvC;QAC7D,CAAA;AACA,cAAM2E,QAAQxC,QAAO;MACvB;IACF;AACA,SAAKoB,OAAOC,QAAQqB,OAAO,KAAKtB,OAAOC,QAAQsB,QAAQjB,MAAMxD,IAAIC,MAAK,CAAA,GAAK,CAAA;AAE3E,WAAO;EACT;AACF;",
|
|
6
|
+
"names": ["import_invariant", "MigrationBuilder", "constructor", "_space", "_newLinks", "_flushIds", "_deleteObjects", "_newRoot", "undefined", "_repo", "db", "coreDatabase", "_rootDoc", "_automergeDocLoader", "getSpaceRootDocHandle", "docSync", "findObject", "id", "documentId", "links", "toString", "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", "internal", "createEpoch", "migration", "CreateEpochRequest", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "Object", "entries", "RawString", "create", "ObjectCore", "initNewObject", "setType", "getDoc", "handle", "Migrations", "migrations", "_state", "running", "versionProperty", "namespace", "targetVersion", "length", "space", "includes", "define", "state", "get", "SpaceState", "SPACE_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":20959,"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":9845,"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":14793},"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":4847},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2757}},"bytes":7782}}}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';const require = createRequire(import.meta.url);
|
|
2
|
+
|
|
3
|
+
// packages/sdk/migrations/src/migration-builder.ts
|
|
4
|
+
import { next as am } from "@dxos/automerge/automerge";
|
|
5
|
+
import { CreateEpochRequest } from "@dxos/client/halo";
|
|
6
|
+
import { ObjectCore, migrateDocument } from "@dxos/echo-db";
|
|
7
|
+
import { SpaceDocVersion, encodeReference, Reference } from "@dxos/echo-protocol";
|
|
8
|
+
import { requireTypeReference } from "@dxos/echo-schema";
|
|
9
|
+
import { invariant } from "@dxos/invariant";
|
|
10
|
+
var __dxlog_file = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migration-builder.ts";
|
|
11
|
+
var MigrationBuilder = class {
|
|
12
|
+
constructor(_space) {
|
|
13
|
+
this._space = _space;
|
|
14
|
+
this._newLinks = {};
|
|
15
|
+
this._flushIds = [];
|
|
16
|
+
this._deleteObjects = [];
|
|
17
|
+
this._newRoot = void 0;
|
|
18
|
+
this._repo = this._space.db.coreDatabase._repo;
|
|
19
|
+
this._rootDoc = this._space.db.coreDatabase._automergeDocLoader.getSpaceRootDocHandle().docSync();
|
|
20
|
+
}
|
|
21
|
+
async findObject(id) {
|
|
22
|
+
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString();
|
|
23
|
+
const docHandle = documentId && this._repo.find(documentId);
|
|
24
|
+
if (!docHandle) {
|
|
25
|
+
return void 0;
|
|
26
|
+
}
|
|
27
|
+
await docHandle.whenReady();
|
|
28
|
+
const doc = docHandle.docSync();
|
|
29
|
+
return doc.objects?.[id];
|
|
30
|
+
}
|
|
31
|
+
async migrateObject(id, migrate) {
|
|
32
|
+
const objectStructure = await this.findObject(id);
|
|
33
|
+
if (!objectStructure) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const { schema, props } = await migrate(objectStructure);
|
|
37
|
+
const oldHandle = await this._findObjectContainingHandle(id);
|
|
38
|
+
invariant(oldHandle, void 0, {
|
|
39
|
+
F: __dxlog_file,
|
|
40
|
+
L: 58,
|
|
41
|
+
S: this,
|
|
42
|
+
A: [
|
|
43
|
+
"oldHandle",
|
|
44
|
+
""
|
|
45
|
+
]
|
|
46
|
+
});
|
|
47
|
+
const newState = {
|
|
48
|
+
version: SpaceDocVersion.CURRENT,
|
|
49
|
+
access: {
|
|
50
|
+
spaceKey: this._space.key.toHex()
|
|
51
|
+
},
|
|
52
|
+
objects: {
|
|
53
|
+
[id]: {
|
|
54
|
+
system: {
|
|
55
|
+
type: encodeReference(requireTypeReference(schema))
|
|
56
|
+
},
|
|
57
|
+
data: props,
|
|
58
|
+
meta: {
|
|
59
|
+
keys: []
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
const migratedDoc = migrateDocument(oldHandle.docSync(), newState);
|
|
65
|
+
const newHandle = this._repo.import(am.save(migratedDoc));
|
|
66
|
+
this._newLinks[id] = newHandle.url;
|
|
67
|
+
this._addHandleToFlushList(newHandle);
|
|
68
|
+
}
|
|
69
|
+
async addObject(schema, props) {
|
|
70
|
+
const core = this._createObject({
|
|
71
|
+
schema,
|
|
72
|
+
props
|
|
73
|
+
});
|
|
74
|
+
return core.id;
|
|
75
|
+
}
|
|
76
|
+
createReference(id) {
|
|
77
|
+
return encodeReference(new Reference(id));
|
|
78
|
+
}
|
|
79
|
+
deleteObject(id) {
|
|
80
|
+
this._deleteObjects.push(id);
|
|
81
|
+
}
|
|
82
|
+
changeProperties(changeFn) {
|
|
83
|
+
if (!this._newRoot) {
|
|
84
|
+
this._buildNewRoot();
|
|
85
|
+
}
|
|
86
|
+
invariant(this._newRoot, "New root not created", {
|
|
87
|
+
F: __dxlog_file,
|
|
88
|
+
L: 100,
|
|
89
|
+
S: this,
|
|
90
|
+
A: [
|
|
91
|
+
"this._newRoot",
|
|
92
|
+
"'New root not created'"
|
|
93
|
+
]
|
|
94
|
+
});
|
|
95
|
+
this._newRoot.change((doc) => {
|
|
96
|
+
const propertiesStructure = doc.objects?.[this._space.properties.id];
|
|
97
|
+
propertiesStructure && changeFn(propertiesStructure);
|
|
98
|
+
});
|
|
99
|
+
this._addHandleToFlushList(this._newRoot);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* @internal
|
|
103
|
+
*/
|
|
104
|
+
async _commit() {
|
|
105
|
+
if (!this._newRoot) {
|
|
106
|
+
this._buildNewRoot();
|
|
107
|
+
}
|
|
108
|
+
invariant(this._newRoot, "New root not created", {
|
|
109
|
+
F: __dxlog_file,
|
|
110
|
+
L: 116,
|
|
111
|
+
S: this,
|
|
112
|
+
A: [
|
|
113
|
+
"this._newRoot",
|
|
114
|
+
"'New root not created'"
|
|
115
|
+
]
|
|
116
|
+
});
|
|
117
|
+
await this._space.db.coreDatabase.flush();
|
|
118
|
+
await this._space.internal.createEpoch({
|
|
119
|
+
migration: CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,
|
|
120
|
+
automergeRootUrl: this._newRoot.url
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
async _findObjectContainingHandle(id) {
|
|
124
|
+
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString();
|
|
125
|
+
const docHandle = documentId && this._repo.find(documentId);
|
|
126
|
+
if (!docHandle) {
|
|
127
|
+
return void 0;
|
|
128
|
+
}
|
|
129
|
+
await docHandle.whenReady();
|
|
130
|
+
return docHandle;
|
|
131
|
+
}
|
|
132
|
+
_buildNewRoot() {
|
|
133
|
+
const links = {
|
|
134
|
+
...this._rootDoc.links ?? {}
|
|
135
|
+
};
|
|
136
|
+
for (const id of this._deleteObjects) {
|
|
137
|
+
delete links[id];
|
|
138
|
+
}
|
|
139
|
+
for (const [id, url] of Object.entries(this._newLinks)) {
|
|
140
|
+
links[id] = new am.RawString(url);
|
|
141
|
+
}
|
|
142
|
+
this._newRoot = this._repo.create({
|
|
143
|
+
version: SpaceDocVersion.CURRENT,
|
|
144
|
+
access: {
|
|
145
|
+
spaceKey: this._space.key.toHex()
|
|
146
|
+
},
|
|
147
|
+
objects: this._rootDoc.objects,
|
|
148
|
+
links
|
|
149
|
+
});
|
|
150
|
+
this._addHandleToFlushList(this._newRoot);
|
|
151
|
+
}
|
|
152
|
+
_createObject({ id, schema, props }) {
|
|
153
|
+
const core = new ObjectCore();
|
|
154
|
+
if (id) {
|
|
155
|
+
core.id = id;
|
|
156
|
+
}
|
|
157
|
+
core.initNewObject(props);
|
|
158
|
+
core.setType(requireTypeReference(schema));
|
|
159
|
+
const newHandle = this._repo.create({
|
|
160
|
+
version: SpaceDocVersion.CURRENT,
|
|
161
|
+
access: {
|
|
162
|
+
spaceKey: this._space.key.toHex()
|
|
163
|
+
},
|
|
164
|
+
objects: {
|
|
165
|
+
[core.id]: core.getDoc()
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
this._newLinks[core.id] = newHandle.url;
|
|
169
|
+
this._addHandleToFlushList(newHandle);
|
|
170
|
+
return core;
|
|
171
|
+
}
|
|
172
|
+
_addHandleToFlushList(handle) {
|
|
173
|
+
this._flushIds.push(handle.documentId);
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
// packages/sdk/migrations/src/migrations.ts
|
|
178
|
+
import { create, SpaceState } from "@dxos/client/echo";
|
|
179
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
180
|
+
var __dxlog_file2 = "/home/runner/work/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
|
|
181
|
+
var Migrations = class {
|
|
182
|
+
static {
|
|
183
|
+
this.migrations = [];
|
|
184
|
+
}
|
|
185
|
+
static {
|
|
186
|
+
this._state = create({
|
|
187
|
+
running: []
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
static get versionProperty() {
|
|
191
|
+
return this.namespace && `${this.namespace}.version`;
|
|
192
|
+
}
|
|
193
|
+
static get targetVersion() {
|
|
194
|
+
return this.migrations[this.migrations.length - 1].version;
|
|
195
|
+
}
|
|
196
|
+
static running(space) {
|
|
197
|
+
return this._state.running.includes(space.key.toHex());
|
|
198
|
+
}
|
|
199
|
+
static define(namespace, migrations) {
|
|
200
|
+
this.namespace = namespace;
|
|
201
|
+
this.migrations = migrations;
|
|
202
|
+
}
|
|
203
|
+
static async migrate(space, targetVersion) {
|
|
204
|
+
invariant2(!this.running(space), "Migration already running", {
|
|
205
|
+
F: __dxlog_file2,
|
|
206
|
+
L: 44,
|
|
207
|
+
S: this,
|
|
208
|
+
A: [
|
|
209
|
+
"!this.running(space)",
|
|
210
|
+
"'Migration already running'"
|
|
211
|
+
]
|
|
212
|
+
});
|
|
213
|
+
invariant2(this.versionProperty, "Migrations namespace not set", {
|
|
214
|
+
F: __dxlog_file2,
|
|
215
|
+
L: 45,
|
|
216
|
+
S: this,
|
|
217
|
+
A: [
|
|
218
|
+
"this.versionProperty",
|
|
219
|
+
"'Migrations namespace not set'"
|
|
220
|
+
]
|
|
221
|
+
});
|
|
222
|
+
invariant2(space.state.get() === SpaceState.SPACE_READY, "Space not ready", {
|
|
223
|
+
F: __dxlog_file2,
|
|
224
|
+
L: 46,
|
|
225
|
+
S: this,
|
|
226
|
+
A: [
|
|
227
|
+
"space.state.get() === SpaceState.SPACE_READY",
|
|
228
|
+
"'Space not ready'"
|
|
229
|
+
]
|
|
230
|
+
});
|
|
231
|
+
const currentVersion = space.properties[this.versionProperty];
|
|
232
|
+
const currentIndex = this.migrations.findIndex((m) => m.version === currentVersion) + 1;
|
|
233
|
+
const i = this.migrations.findIndex((m) => m.version === targetVersion);
|
|
234
|
+
const targetIndex = i === -1 ? this.migrations.length : i + 1;
|
|
235
|
+
if (currentIndex === targetIndex) {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
this._state.running.push(space.key.toHex());
|
|
239
|
+
if (targetIndex > currentIndex) {
|
|
240
|
+
const migrations = this.migrations.slice(currentIndex, targetIndex);
|
|
241
|
+
for (const migration of migrations) {
|
|
242
|
+
const builder = new MigrationBuilder(space);
|
|
243
|
+
await migration.next({
|
|
244
|
+
space,
|
|
245
|
+
builder
|
|
246
|
+
});
|
|
247
|
+
builder.changeProperties((propertiesStructure) => {
|
|
248
|
+
invariant2(this.versionProperty, "Migrations namespace not set", {
|
|
249
|
+
F: __dxlog_file2,
|
|
250
|
+
L: 62,
|
|
251
|
+
S: this,
|
|
252
|
+
A: [
|
|
253
|
+
"this.versionProperty",
|
|
254
|
+
"'Migrations namespace not set'"
|
|
255
|
+
]
|
|
256
|
+
});
|
|
257
|
+
propertiesStructure.data[this.versionProperty] = migration.version;
|
|
258
|
+
});
|
|
259
|
+
await builder._commit();
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
this._state.running.splice(this._state.running.indexOf(space.key.toHex()), 1);
|
|
263
|
+
return true;
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
export {
|
|
267
|
+
MigrationBuilder,
|
|
268
|
+
Migrations
|
|
269
|
+
};
|
|
270
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 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 DocumentId } from '@dxos/automerge/automerge-repo';\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { 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 _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._repo;\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])?.toString() 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._space.db.coreDatabase.flush();\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])?.toString() 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 links = { ...(this._rootDoc.links ?? {}) };\n for (const id of this._deleteObjects) {\n delete links[id];\n }\n\n for (const [id, url] of Object.entries(this._newLinks)) {\n links[id] = new am.RawString(url);\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 });\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.SPACE_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,SAASC,YAAYC,uBAA4D;AACjF,SAASC,iBAAiBC,iBAAsDC,iBAAiB;AACjG,SAASC,4BAAoC;AAC7C,SAASC,iBAAiB;;AAGnB,IAAMC,mBAAN,MAAMA;EAWXC,YAA6BC,QAAe;SAAfA,SAAAA;SANZC,YAAoC,CAAC;SACrCC,YAA0B,CAAA;SAC1BC,iBAA2B,CAAA;SAEpCC,WAAsCC;AAG5C,SAAKC,QAAQ,KAAKN,OAAOO,GAAGC,aAAaF;AAEzC,SAAKG,WAAY,KAAKT,OAAOO,GAAGC,aAAqBE,oBAClDC,sBAAqB,EACrBC,QAAO;EACZ;EAEA,MAAMC,WAAWC,IAAkD;AACjE,UAAMC,cAAc,KAAKN,SAASO,QAAQF,EAAAA,KAAO,KAAKb,UAAUa,EAAAA,IAAMG,SAAAA;AACtE,UAAMC,YAAYH,cAAc,KAAKT,MAAMa,KAAKJ,UAAAA;AAChD,QAAI,CAACG,WAAW;AACd,aAAOb;IACT;AAEA,UAAMa,UAAUE,UAAS;AACzB,UAAMC,MAAMH,UAAUN,QAAO;AAC7B,WAAOS,IAAIC,UAAUR,EAAAA;EACvB;EAEA,MAAMS,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AAAA,WAAU,KAAKX,iBAAiB,gCAAA;;;;;;;;;AAChCW,IAAAA,WAAUN,MAAMO,MAAMC,IAAG,MAAOC,WAAWC,aAAa,mBAAA;;;;;;;;;AACxD,UAAMC,iBAAiBX,MAAMY,WAAW,KAAKjB,eAAe;AAC5D,UAAMkB,eAAe,KAAKtB,WAAWuB,UAAU,CAACC,MAAMA,EAAEhB,YAAYY,cAAAA,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", "_rootDoc", "_automergeDocLoader", "getSpaceRootDocHandle", "docSync", "findObject", "id", "documentId", "links", "toString", "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", "internal", "createEpoch", "migration", "Migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "Object", "entries", "RawString", "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", "SPACE_READY", "currentVersion", "properties", "currentIndex", "findIndex", "m", "i", "targetIndex", "push", "slice", "migration", "builder", "MigrationBuilder", "next", "changeProperties", "propertiesStructure", "data", "_commit", "splice", "indexOf"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"inputs":{"packages/sdk/migrations/src/migration-builder.ts":{"bytes":20959,"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":9845,"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-esm/index.mjs.map":{"imports":[],"exports":[],"inputs":{},"bytes":14795},"packages/sdk/migrations/dist/lib/node-esm/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":4847},"packages/sdk/migrations/src/index.ts":{"bytesInOutput":0},"packages/sdk/migrations/src/migrations.ts":{"bytesInOutput":2757}},"bytes":7875}}}
|
|
@@ -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;AAEjE,OAAO,EAAE,KAAK,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/C,qBAAa,gBAAgB;IAWf,OAAO,CAAC,QAAQ,CAAC,MAAM;IAVnC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAY;IAClC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAgB;IAGz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O,CAAC,QAAQ,CAAC,SAAS,CAA8B;IACxD,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAoB;IAC9C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAgB;IAE/C,OAAO,CAAC,QAAQ,CAAC,CAAuC;gBAE3B,MAAM,EAAE,KAAK;IAQpC,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,GAAG,SAAS,CAAC;IAY5D,aAAa,CACjB,EAAE,EAAE,MAAM,EACV,OAAO,EAAE,CAAC,eAAe,EAAE,eAAe,KAAK,YAAY,CAAC;QAAE,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QAAC,KAAK,EAAE,GAAG,CAAA;KAAE,CAAC;IAmC9F,SAAS,CAAC,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,GAAG;IAKjD,eAAe,CAAC,EAAE,EAAE,MAAM;IAI1B,YAAY,CAAC,EAAE,EAAE,MAAM;IAIvB,gBAAgB,CAAC,QAAQ,EAAE,CAAC,UAAU,EAAE,eAAe,KAAK,IAAI;YA+BlD,2BAA2B;IAWzC,OAAO,CAAC,aAAa;IAqBrB,OAAO,CAAC,aAAa;IAuBrB,OAAO,CAAC,qBAAqB;CAG9B"}
|
package/package.json
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dxos/migrations",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.14-main.69511f5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"homepage": "https://dxos.org",
|
|
6
6
|
"bugs": "https://github.com/dxos/dxos/issues",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"author": "info@dxos.org",
|
|
9
|
+
"sideEffects": true,
|
|
9
10
|
"exports": {
|
|
10
11
|
".": {
|
|
12
|
+
"types": "./dist/types/src/index.d.ts",
|
|
11
13
|
"browser": "./dist/lib/browser/index.mjs",
|
|
12
|
-
"node":
|
|
13
|
-
"default": "./dist/lib/node/index.cjs"
|
|
14
|
-
},
|
|
15
|
-
"types": "./dist/types/src/index.d.ts"
|
|
14
|
+
"node": "./dist/lib/node-esm/index.mjs"
|
|
16
15
|
}
|
|
17
16
|
},
|
|
18
17
|
"types": "dist/types/src/index.d.ts",
|
|
@@ -24,15 +23,15 @@
|
|
|
24
23
|
"src"
|
|
25
24
|
],
|
|
26
25
|
"dependencies": {
|
|
27
|
-
"@dxos/automerge": "0.6.
|
|
28
|
-
"@dxos/client": "0.6.
|
|
29
|
-
"@dxos/echo-db": "0.6.
|
|
30
|
-
"@dxos/echo-
|
|
31
|
-
"@dxos/echo-
|
|
32
|
-
"@dxos/invariant": "0.6.
|
|
33
|
-
"@dxos/log": "0.6.
|
|
34
|
-
"@dxos/protocols": "0.6.
|
|
35
|
-
"@dxos/util": "0.6.
|
|
26
|
+
"@dxos/automerge": "0.6.14-main.69511f5",
|
|
27
|
+
"@dxos/client": "0.6.14-main.69511f5",
|
|
28
|
+
"@dxos/echo-db": "0.6.14-main.69511f5",
|
|
29
|
+
"@dxos/echo-schema": "0.6.14-main.69511f5",
|
|
30
|
+
"@dxos/echo-protocol": "0.6.14-main.69511f5",
|
|
31
|
+
"@dxos/invariant": "0.6.14-main.69511f5",
|
|
32
|
+
"@dxos/log": "0.6.14-main.69511f5",
|
|
33
|
+
"@dxos/protocols": "0.6.14-main.69511f5",
|
|
34
|
+
"@dxos/util": "0.6.14-main.69511f5"
|
|
36
35
|
},
|
|
37
36
|
"devDependencies": {},
|
|
38
37
|
"publishConfig": {
|
package/src/migration-builder.ts
CHANGED
|
@@ -32,7 +32,7 @@ export class MigrationBuilder {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
async findObject(id: string): Promise<ObjectStructure | undefined> {
|
|
35
|
-
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id]) as AnyDocumentId | undefined;
|
|
35
|
+
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString() as AnyDocumentId | undefined;
|
|
36
36
|
const docHandle = documentId && this._repo.find(documentId);
|
|
37
37
|
if (!docHandle) {
|
|
38
38
|
return undefined;
|
|
@@ -125,7 +125,7 @@ export class MigrationBuilder {
|
|
|
125
125
|
}
|
|
126
126
|
|
|
127
127
|
private async _findObjectContainingHandle(id: string): Promise<DocHandleProxy<SpaceDoc> | undefined> {
|
|
128
|
-
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id]) as AnyDocumentId | undefined;
|
|
128
|
+
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString() as AnyDocumentId | undefined;
|
|
129
129
|
const docHandle = documentId && this._repo.find(documentId);
|
|
130
130
|
if (!docHandle) {
|
|
131
131
|
return undefined;
|
|
@@ -136,9 +136,13 @@ export class MigrationBuilder {
|
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
private _buildNewRoot() {
|
|
139
|
-
const
|
|
139
|
+
const links = { ...(this._rootDoc.links ?? {}) };
|
|
140
140
|
for (const id of this._deleteObjects) {
|
|
141
|
-
delete
|
|
141
|
+
delete links[id];
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
for (const [id, url] of Object.entries(this._newLinks)) {
|
|
145
|
+
links[id] = new am.RawString(url);
|
|
142
146
|
}
|
|
143
147
|
|
|
144
148
|
this._newRoot = this._repo.create<SpaceDoc>({
|
|
@@ -147,10 +151,7 @@ export class MigrationBuilder {
|
|
|
147
151
|
spaceKey: this._space.key.toHex(),
|
|
148
152
|
},
|
|
149
153
|
objects: this._rootDoc.objects,
|
|
150
|
-
links
|
|
151
|
-
...previousLinks,
|
|
152
|
-
...this._newLinks,
|
|
153
|
-
},
|
|
154
|
+
links,
|
|
154
155
|
});
|
|
155
156
|
this._addHandleToFlushList(this._newRoot);
|
|
156
157
|
}
|
package/src/migrations.test.ts
CHANGED
|
@@ -2,13 +2,12 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { expect } from '
|
|
5
|
+
import { afterAll, beforeEach, beforeAll, describe, expect, test } from 'vitest';
|
|
6
6
|
|
|
7
7
|
import { Client } from '@dxos/client';
|
|
8
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
|
-
import { describe, test, beforeEach, beforeAll, afterAll } from '@dxos/test';
|
|
12
11
|
|
|
13
12
|
import { Migrations } from './migrations';
|
|
14
13
|
|