@dxos/migrations 0.8.4-main.fffef41 → 0.8.4-staging.60fe92afc8
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/LICENSE +102 -5
- package/README.md +3 -3
- package/dist/lib/browser/index.mjs +147 -109
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/node-esm/index.mjs +147 -109
- package/dist/lib/node-esm/index.mjs.map +4 -4
- package/dist/lib/node-esm/meta.json +1 -1
- package/dist/types/src/annotations.d.ts +4 -0
- package/dist/types/src/annotations.d.ts.map +1 -0
- package/dist/types/src/document-compaction.d.ts +19 -0
- package/dist/types/src/document-compaction.d.ts.map +1 -0
- package/dist/types/src/document-compaction.test.d.ts +2 -0
- package/dist/types/src/document-compaction.test.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +3 -1
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/migration-builder.d.ts +16 -8
- package/dist/types/src/migration-builder.d.ts.map +1 -1
- package/dist/types/src/migrations.d.ts +5 -1
- package/dist/types/src/migrations.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +18 -17
- package/src/annotations.ts +13 -0
- package/src/document-compaction.test.ts +46 -0
- package/src/document-compaction.ts +43 -0
- package/src/index.ts +3 -1
- package/src/migration-builder.ts +71 -38
- package/src/migrations.test.ts +36 -21
- package/src/migrations.ts +32 -17
package/LICENSE
CHANGED
|
@@ -1,8 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
Copyright (c) 2022 DXOS
|
|
1
|
+
# Functional Source License, Version 1.1, ALv2 Future License
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
## Abbreviation
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
FSL-1.1-Apache-2.0
|
|
7
6
|
|
|
8
|
-
|
|
7
|
+
## Notice
|
|
8
|
+
|
|
9
|
+
Copyright 2026 DXOS
|
|
10
|
+
|
|
11
|
+
## Terms and Conditions
|
|
12
|
+
|
|
13
|
+
### Licensor ("We")
|
|
14
|
+
|
|
15
|
+
The party offering the Software under these Terms and Conditions.
|
|
16
|
+
|
|
17
|
+
### The Software
|
|
18
|
+
|
|
19
|
+
The "Software" is each version of the software that we make available under
|
|
20
|
+
these Terms and Conditions, as indicated by our inclusion of these Terms and
|
|
21
|
+
Conditions with the Software.
|
|
22
|
+
|
|
23
|
+
### License Grant
|
|
24
|
+
|
|
25
|
+
Subject to your compliance with this License Grant and the Patents,
|
|
26
|
+
Redistribution and Trademark clauses below, we hereby grant you the right to
|
|
27
|
+
use, copy, modify, create derivative works, publicly perform, publicly display
|
|
28
|
+
and redistribute the Software for any Permitted Purpose identified below.
|
|
29
|
+
|
|
30
|
+
### Permitted Purpose
|
|
31
|
+
|
|
32
|
+
A Permitted Purpose is any purpose other than a Competing Use. A Competing Use
|
|
33
|
+
means making the Software available to others in a commercial product or
|
|
34
|
+
service that:
|
|
35
|
+
|
|
36
|
+
1. substitutes for the Software;
|
|
37
|
+
|
|
38
|
+
2. substitutes for any other product or service we offer using the Software
|
|
39
|
+
that exists as of the date we make the Software available; or
|
|
40
|
+
|
|
41
|
+
3. offers the same or substantially similar functionality as the Software.
|
|
42
|
+
|
|
43
|
+
Permitted Purposes specifically include using the Software:
|
|
44
|
+
|
|
45
|
+
1. for your internal use and access;
|
|
46
|
+
|
|
47
|
+
2. for non-commercial education;
|
|
48
|
+
|
|
49
|
+
3. for non-commercial research; and
|
|
50
|
+
|
|
51
|
+
4. in connection with professional services that you provide to a licensee
|
|
52
|
+
using the Software in accordance with these Terms and Conditions.
|
|
53
|
+
|
|
54
|
+
### Patents
|
|
55
|
+
|
|
56
|
+
To the extent your use for a Permitted Purpose would necessarily infringe our
|
|
57
|
+
patents, the license grant above includes a license under our patents. If you
|
|
58
|
+
make a claim against any party that the Software infringes or contributes to
|
|
59
|
+
the infringement of any patent, then your patent license to the Software ends
|
|
60
|
+
immediately.
|
|
61
|
+
|
|
62
|
+
### Redistribution
|
|
63
|
+
|
|
64
|
+
The Terms and Conditions apply to all copies, modifications and derivatives of
|
|
65
|
+
the Software.
|
|
66
|
+
|
|
67
|
+
If you redistribute any copies, modifications or derivatives of the Software,
|
|
68
|
+
you must include a copy of or a link to these Terms and Conditions and not
|
|
69
|
+
remove any copyright notices provided in or with the Software.
|
|
70
|
+
|
|
71
|
+
### Disclaimer
|
|
72
|
+
|
|
73
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTIES OF ANY KIND, EXPRESS OR
|
|
74
|
+
IMPLIED, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A PARTICULAR
|
|
75
|
+
PURPOSE, MERCHANTABILITY, TITLE OR NON-INFRINGEMENT.
|
|
76
|
+
|
|
77
|
+
IN NO EVENT WILL WE HAVE ANY LIABILITY TO YOU ARISING OUT OF OR RELATED TO THE
|
|
78
|
+
SOFTWARE, INCLUDING INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES,
|
|
79
|
+
EVEN IF WE HAVE BEEN INFORMED OF THEIR POSSIBILITY IN ADVANCE.
|
|
80
|
+
|
|
81
|
+
### Trademarks
|
|
82
|
+
|
|
83
|
+
Except for displaying the License Details and identifying us as the origin of
|
|
84
|
+
the Software, you have no right under these Terms and Conditions to use our
|
|
85
|
+
trademarks, trade names, service marks or product names.
|
|
86
|
+
|
|
87
|
+
## Grant of Future License
|
|
88
|
+
|
|
89
|
+
We hereby irrevocably grant you an additional license to use the Software under
|
|
90
|
+
the Apache License, Version 2.0 that is effective on the second anniversary of
|
|
91
|
+
the date we make the Software available. On or after that date, you may use the
|
|
92
|
+
Software under the Apache License, Version 2.0, in which case the following
|
|
93
|
+
will apply:
|
|
94
|
+
|
|
95
|
+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
|
96
|
+
this file except in compliance with the License.
|
|
97
|
+
|
|
98
|
+
You may obtain a copy of the License at
|
|
99
|
+
|
|
100
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
101
|
+
|
|
102
|
+
Unless required by applicable law or agreed to in writing, software distributed
|
|
103
|
+
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
|
+
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
|
+
specific language governing permissions and limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
# @dxos/
|
|
1
|
+
# @dxos/migrations
|
|
2
2
|
|
|
3
3
|
## Installation
|
|
4
4
|
|
|
5
5
|
```bash
|
|
6
|
-
pnpm i @dxos/
|
|
6
|
+
pnpm i @dxos/migrations
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
## DXOS Resources
|
|
@@ -16,4 +16,4 @@ pnpm i @dxos/echo-signals
|
|
|
16
16
|
|
|
17
17
|
Your ideas, issues, and code are most welcome. Please take a look at our [community code of conduct](https://github.com/dxos/dxos/blob/main/CODE_OF_CONDUCT.md), the [issue guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-issues), and the [PR contribution guide](https://github.com/dxos/dxos/blob/main/CONTRIBUTING.md#submitting-prs).
|
|
18
18
|
|
|
19
|
-
License: [
|
|
19
|
+
License: [FSL-1.1-Apache-2.0](./LICENSE) Copyright 2022 © DXOS
|
|
@@ -1,24 +1,40 @@
|
|
|
1
|
+
// src/annotations.ts
|
|
2
|
+
import * as Schema from "effect/Schema";
|
|
3
|
+
import { Annotation } from "@dxos/echo";
|
|
4
|
+
var MigrationVersionAnnotation = Annotation.make({
|
|
5
|
+
id: "org.dxos.migrations.version",
|
|
6
|
+
schema: Schema.String
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
// src/document-compaction.ts
|
|
10
|
+
import { SpaceState } from "@dxos/client/echo";
|
|
11
|
+
import { invariant as invariant2 } from "@dxos/invariant";
|
|
12
|
+
|
|
1
13
|
// src/migration-builder.ts
|
|
2
|
-
import { next as A } from "@automerge/automerge";
|
|
14
|
+
import { next as A, toJS } from "@automerge/automerge";
|
|
3
15
|
import { CreateEpochRequest } from "@dxos/client/halo";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
16
|
+
import { ObjectCore, migrateDocument } from "@dxos/echo-client/internal";
|
|
17
|
+
import { EncodedReference, SpaceDocVersion } from "@dxos/echo-protocol";
|
|
18
|
+
import { getSchemaURI } from "@dxos/echo/internal";
|
|
19
|
+
import * as Type from "@dxos/echo/Type";
|
|
7
20
|
import { invariant } from "@dxos/invariant";
|
|
21
|
+
import { EID, EntityId } from "@dxos/keys";
|
|
8
22
|
var __dxlog_file = "/__w/dxos/dxos/packages/sdk/migrations/src/migration-builder.ts";
|
|
9
23
|
var MigrationBuilder = class {
|
|
10
24
|
_space;
|
|
11
25
|
_repo;
|
|
12
26
|
_rootDoc;
|
|
13
|
-
//
|
|
27
|
+
// echoUri -> automergeUrl
|
|
14
28
|
_newLinks = {};
|
|
15
29
|
_flushIds = [];
|
|
16
30
|
_deleteObjects = [];
|
|
17
31
|
_newRoot = void 0;
|
|
18
32
|
constructor(_space) {
|
|
19
33
|
this._space = _space;
|
|
20
|
-
this._repo = this._space.db.
|
|
21
|
-
|
|
34
|
+
this._repo = this._space.internal.db._repo;
|
|
35
|
+
const rootDoc = this._space.internal.db._getSpaceRootDocHandle().doc();
|
|
36
|
+
invariant(rootDoc, "Space root document must be available when creating MigrationBuilder", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 41, S: this, A: ["rootDoc", "'Space root document must be available when creating MigrationBuilder'"] });
|
|
37
|
+
this._rootDoc = rootDoc;
|
|
22
38
|
}
|
|
23
39
|
async findObject(id) {
|
|
24
40
|
const documentId = (this._rootDoc.links?.[id] || this._newLinks[id])?.toString();
|
|
@@ -35,17 +51,10 @@ var MigrationBuilder = class {
|
|
|
35
51
|
if (!objectStructure) {
|
|
36
52
|
return;
|
|
37
53
|
}
|
|
38
|
-
const {
|
|
54
|
+
const { type, props } = await migrate(objectStructure);
|
|
55
|
+
const schema = Type.getSchema(type);
|
|
39
56
|
const oldHandle = await this._findObjectContainingHandle(id);
|
|
40
|
-
invariant(oldHandle, void 0, {
|
|
41
|
-
F: __dxlog_file,
|
|
42
|
-
L: 84,
|
|
43
|
-
S: this,
|
|
44
|
-
A: [
|
|
45
|
-
"oldHandle",
|
|
46
|
-
""
|
|
47
|
-
]
|
|
48
|
-
});
|
|
57
|
+
invariant(oldHandle, void 0, { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 62, S: this, A: ["oldHandle", ""] });
|
|
49
58
|
const newState = {
|
|
50
59
|
version: SpaceDocVersion.CURRENT,
|
|
51
60
|
access: {
|
|
@@ -54,7 +63,7 @@ var MigrationBuilder = class {
|
|
|
54
63
|
objects: {
|
|
55
64
|
[id]: {
|
|
56
65
|
system: {
|
|
57
|
-
type:
|
|
66
|
+
type: EncodedReference.fromURI(getSchemaURI(schema))
|
|
58
67
|
},
|
|
59
68
|
data: props,
|
|
60
69
|
meta: {
|
|
@@ -65,58 +74,78 @@ var MigrationBuilder = class {
|
|
|
65
74
|
};
|
|
66
75
|
const migratedDoc = migrateDocument(oldHandle.doc(), newState);
|
|
67
76
|
const newHandle = this._repo.import(A.save(migratedDoc));
|
|
77
|
+
await newHandle.whenReady();
|
|
78
|
+
invariant(newHandle.url, "Migrated document URL not available after whenReady", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 83, S: this, A: ["newHandle.url", "'Migrated document URL not available after whenReady'"] });
|
|
68
79
|
this._newLinks[id] = newHandle.url;
|
|
69
|
-
this._addHandleToFlushList(newHandle);
|
|
80
|
+
this._addHandleToFlushList(newHandle.documentId);
|
|
70
81
|
}
|
|
71
|
-
async addObject(
|
|
72
|
-
const
|
|
73
|
-
|
|
82
|
+
async addObject(type, props) {
|
|
83
|
+
const resolved = Type.getSchema(type);
|
|
84
|
+
const core = await this._createObject({
|
|
85
|
+
schema: resolved,
|
|
74
86
|
props
|
|
75
87
|
});
|
|
76
88
|
return core.id;
|
|
77
89
|
}
|
|
78
90
|
createReference(id) {
|
|
79
|
-
|
|
91
|
+
invariant(EntityId.isValid(id), "Invalid EntityId.", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 96, S: this, A: ["EntityId.isValid(id)", "'Invalid EntityId.'"] });
|
|
92
|
+
return EncodedReference.fromURI(EID.make({
|
|
93
|
+
entityId: id
|
|
94
|
+
}));
|
|
80
95
|
}
|
|
81
96
|
deleteObject(id) {
|
|
82
97
|
this._deleteObjects.push(id);
|
|
83
98
|
}
|
|
84
|
-
|
|
99
|
+
/**
|
|
100
|
+
* Re-materializes linked object documents into fresh Automerge docs without history.
|
|
101
|
+
* Call {@link _commit} to publish a new space epoch with updated root links.
|
|
102
|
+
*/
|
|
103
|
+
async compactLinkedDocuments(objectIds) {
|
|
104
|
+
const linkIds = objectIds ?? Object.keys(this._rootDoc.links ?? {});
|
|
105
|
+
const compacted = [];
|
|
106
|
+
const skipped = [];
|
|
107
|
+
for (const id of linkIds) {
|
|
108
|
+
const oldHandle = await this._findObjectContainingHandle(id);
|
|
109
|
+
if (!oldHandle) {
|
|
110
|
+
skipped.push(id);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
await oldHandle.whenReady();
|
|
114
|
+
const materialized = toJS(oldHandle.doc());
|
|
115
|
+
const newHandle = this._repo.create(materialized);
|
|
116
|
+
await newHandle.whenReady();
|
|
117
|
+
invariant(newHandle.url, "Compacted document URL not available after whenReady", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 121, S: this, A: ["newHandle.url", "'Compacted document URL not available after whenReady'"] });
|
|
118
|
+
this._newLinks[id] = newHandle.url;
|
|
119
|
+
this._addHandleToFlushList(newHandle.documentId);
|
|
120
|
+
compacted.push(id);
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
compacted,
|
|
124
|
+
skipped
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
async changeProperties(changeFn) {
|
|
85
128
|
if (!this._newRoot) {
|
|
86
|
-
this._buildNewRoot();
|
|
129
|
+
await this._buildNewRoot();
|
|
87
130
|
}
|
|
88
|
-
invariant(this._newRoot, "New root not created", {
|
|
89
|
-
F: __dxlog_file,
|
|
90
|
-
L: 126,
|
|
91
|
-
S: this,
|
|
92
|
-
A: [
|
|
93
|
-
"this._newRoot",
|
|
94
|
-
"'New root not created'"
|
|
95
|
-
]
|
|
96
|
-
});
|
|
131
|
+
invariant(this._newRoot, "New root not created", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 135, S: this, A: ["this._newRoot", "'New root not created'"] });
|
|
97
132
|
this._newRoot.change((doc) => {
|
|
98
133
|
const propertiesStructure = doc.objects?.[this._space.properties.id];
|
|
99
134
|
propertiesStructure && changeFn(propertiesStructure);
|
|
100
135
|
});
|
|
101
|
-
this.
|
|
136
|
+
await this._newRoot.whenReady();
|
|
137
|
+
this._addHandleToFlushList(this._newRoot.documentId);
|
|
102
138
|
}
|
|
103
139
|
/**
|
|
104
140
|
* @internal
|
|
105
141
|
*/
|
|
106
142
|
async _commit() {
|
|
107
143
|
if (!this._newRoot) {
|
|
108
|
-
this._buildNewRoot();
|
|
144
|
+
await this._buildNewRoot();
|
|
109
145
|
}
|
|
110
|
-
invariant(this._newRoot, "New root not created", {
|
|
111
|
-
F: __dxlog_file,
|
|
112
|
-
L: 142,
|
|
113
|
-
S: this,
|
|
114
|
-
A: [
|
|
115
|
-
"this._newRoot",
|
|
116
|
-
"'New root not created'"
|
|
117
|
-
]
|
|
118
|
-
});
|
|
146
|
+
invariant(this._newRoot, "New root not created", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 149, S: this, A: ["this._newRoot", "'New root not created'"] });
|
|
119
147
|
await this._space.db.flush();
|
|
148
|
+
invariant(this._newRoot.url, "New root URL not available", { "~LogMeta": "~LogMeta", F: __dxlog_file, L: 152, S: this, A: ["this._newRoot.url", "'New root URL not available'"] });
|
|
120
149
|
await this._space.internal.createEpoch({
|
|
121
150
|
migration: CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT,
|
|
122
151
|
automergeRootUrl: this._newRoot.url
|
|
@@ -131,7 +160,7 @@ var MigrationBuilder = class {
|
|
|
131
160
|
await docHandle.whenReady();
|
|
132
161
|
return docHandle;
|
|
133
162
|
}
|
|
134
|
-
_buildNewRoot() {
|
|
163
|
+
async _buildNewRoot() {
|
|
135
164
|
const links = {
|
|
136
165
|
...this._rootDoc.links ?? {}
|
|
137
166
|
};
|
|
@@ -149,15 +178,16 @@ var MigrationBuilder = class {
|
|
|
149
178
|
objects: this._rootDoc.objects,
|
|
150
179
|
links
|
|
151
180
|
});
|
|
152
|
-
this.
|
|
181
|
+
await this._newRoot.whenReady();
|
|
182
|
+
this._addHandleToFlushList(this._newRoot.documentId);
|
|
153
183
|
}
|
|
154
|
-
_createObject({ id, schema, props }) {
|
|
184
|
+
async _createObject({ id, schema, props }) {
|
|
155
185
|
const core = new ObjectCore();
|
|
156
186
|
if (id) {
|
|
157
187
|
core.id = id;
|
|
158
188
|
}
|
|
159
189
|
core.initNewObject(props);
|
|
160
|
-
core.setType(
|
|
190
|
+
core.setType(EncodedReference.fromURI(getSchemaURI(schema)));
|
|
161
191
|
const newHandle = this._repo.create({
|
|
162
192
|
version: SpaceDocVersion.CURRENT,
|
|
163
193
|
access: {
|
|
@@ -167,25 +197,51 @@ var MigrationBuilder = class {
|
|
|
167
197
|
[core.id]: core.getDoc()
|
|
168
198
|
}
|
|
169
199
|
});
|
|
200
|
+
await newHandle.whenReady();
|
|
170
201
|
this._newLinks[core.id] = newHandle.url;
|
|
171
|
-
this._addHandleToFlushList(newHandle);
|
|
202
|
+
this._addHandleToFlushList(newHandle.documentId);
|
|
172
203
|
return core;
|
|
173
204
|
}
|
|
174
|
-
_addHandleToFlushList(
|
|
175
|
-
this._flushIds.push(
|
|
205
|
+
_addHandleToFlushList(id) {
|
|
206
|
+
this._flushIds.push(id);
|
|
176
207
|
}
|
|
177
208
|
};
|
|
178
209
|
|
|
210
|
+
// src/document-compaction.ts
|
|
211
|
+
var __dxlog_file2 = "/__w/dxos/dxos/packages/sdk/migrations/src/document-compaction.ts";
|
|
212
|
+
var compactDocumentsEpochMigration = async (space, options = {}) => {
|
|
213
|
+
invariant2(space.state.get() === SpaceState.SPACE_READY, "Space must be open and ready before compaction.", { "~LogMeta": "~LogMeta", F: __dxlog_file2, L: 11, S: void 0, A: ["space.state.get() === SpaceState.SPACE_READY", "'Space must be open and ready before compaction.'"] });
|
|
214
|
+
const builder = new MigrationBuilder(space);
|
|
215
|
+
const { compacted, skipped } = await builder.compactLinkedDocuments(options.objectIds);
|
|
216
|
+
await builder._commit();
|
|
217
|
+
const epochs = await space.internal.getEpochs();
|
|
218
|
+
const lastEpoch = epochs[epochs.length - 1];
|
|
219
|
+
const epochNumber = lastEpoch?.subject.assertion.number ?? 0;
|
|
220
|
+
return {
|
|
221
|
+
compacted,
|
|
222
|
+
skipped,
|
|
223
|
+
epochNumber
|
|
224
|
+
};
|
|
225
|
+
};
|
|
226
|
+
|
|
179
227
|
// src/migrations.ts
|
|
180
|
-
import {
|
|
181
|
-
import
|
|
182
|
-
|
|
228
|
+
import { Atom } from "@effect-atom/atom";
|
|
229
|
+
import * as Registry from "@effect-atom/atom/Registry";
|
|
230
|
+
import * as Option from "effect/Option";
|
|
231
|
+
import { SpaceState as SpaceState2 } from "@dxos/client/echo";
|
|
232
|
+
import { Annotation as Annotation2, Obj } from "@dxos/echo";
|
|
233
|
+
import { invariant as invariant3 } from "@dxos/invariant";
|
|
234
|
+
var __dxlog_file3 = "/__w/dxos/dxos/packages/sdk/migrations/src/migrations.ts";
|
|
183
235
|
var Migrations = class {
|
|
184
236
|
static namespace;
|
|
185
237
|
static migrations = [];
|
|
186
|
-
static
|
|
238
|
+
static _registry = Registry.make();
|
|
239
|
+
static _stateAtom = Atom.make({
|
|
187
240
|
running: []
|
|
188
|
-
});
|
|
241
|
+
}).pipe(Atom.keepAlive);
|
|
242
|
+
/**
|
|
243
|
+
* @deprecated Use `MigrationVersionAnnotation` via `Annotation.get/set` on space properties.
|
|
244
|
+
*/
|
|
189
245
|
static get versionProperty() {
|
|
190
246
|
return this.namespace && `${this.namespace}.version`;
|
|
191
247
|
}
|
|
@@ -193,77 +249,59 @@ var Migrations = class {
|
|
|
193
249
|
return this.migrations[this.migrations.length - 1]?.version;
|
|
194
250
|
}
|
|
195
251
|
static running(space) {
|
|
196
|
-
|
|
252
|
+
const state = this._registry.get(this._stateAtom);
|
|
253
|
+
return state.running.includes(space.key.toHex());
|
|
197
254
|
}
|
|
198
255
|
static define(namespace, migrations) {
|
|
199
256
|
this.namespace = namespace;
|
|
200
257
|
this.migrations = migrations;
|
|
201
258
|
}
|
|
202
259
|
static async migrate(space, targetVersion) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
S: this,
|
|
207
|
-
A: [
|
|
208
|
-
"!this.running(space)",
|
|
209
|
-
"'Migration already running'"
|
|
210
|
-
]
|
|
211
|
-
});
|
|
212
|
-
invariant2(this.versionProperty, "Migrations namespace not set", {
|
|
213
|
-
F: __dxlog_file2,
|
|
214
|
-
L: 45,
|
|
215
|
-
S: this,
|
|
216
|
-
A: [
|
|
217
|
-
"this.versionProperty",
|
|
218
|
-
"'Migrations namespace not set'"
|
|
219
|
-
]
|
|
220
|
-
});
|
|
221
|
-
invariant2(space.state.get() === SpaceState.SPACE_READY, "Space not ready", {
|
|
222
|
-
F: __dxlog_file2,
|
|
223
|
-
L: 46,
|
|
224
|
-
S: this,
|
|
225
|
-
A: [
|
|
226
|
-
"space.state.get() === SpaceState.SPACE_READY",
|
|
227
|
-
"'Space not ready'"
|
|
228
|
-
]
|
|
229
|
-
});
|
|
230
|
-
const currentVersion = space.properties[this.versionProperty];
|
|
260
|
+
invariant3(!this.running(space), "Migration already running", { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 36, S: this, A: ["!this.running(space)", "'Migration already running'"] });
|
|
261
|
+
invariant3(space.state.get() === SpaceState2.SPACE_READY, "Space not ready", { "~LogMeta": "~LogMeta", F: __dxlog_file3, L: 37, S: this, A: ["space.state.get() === SpaceState.SPACE_READY", "'Space not ready'"] });
|
|
262
|
+
const currentVersion = Annotation2.get(space.properties, MigrationVersionAnnotation).pipe(Option.getOrUndefined);
|
|
231
263
|
const currentIndex = this.migrations.findIndex((m) => m.version === currentVersion) + 1;
|
|
232
264
|
const i = this.migrations.findIndex((m) => m.version === targetVersion);
|
|
233
265
|
const targetIndex = i === -1 ? this.migrations.length : i + 1;
|
|
234
266
|
if (currentIndex === targetIndex) {
|
|
235
267
|
return false;
|
|
236
268
|
}
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
269
|
+
const spaceKey = space.key.toHex();
|
|
270
|
+
const currentState = this._registry.get(this._stateAtom);
|
|
271
|
+
this._registry.set(this._stateAtom, {
|
|
272
|
+
running: [
|
|
273
|
+
...currentState.running,
|
|
274
|
+
spaceKey
|
|
275
|
+
]
|
|
276
|
+
});
|
|
277
|
+
try {
|
|
278
|
+
if (targetIndex > currentIndex) {
|
|
279
|
+
const migrations = this.migrations.slice(currentIndex, targetIndex);
|
|
280
|
+
for (const migration of migrations) {
|
|
281
|
+
const builder = new MigrationBuilder(space);
|
|
282
|
+
await migration.next({
|
|
283
|
+
space,
|
|
284
|
+
builder
|
|
285
|
+
});
|
|
286
|
+
await builder._commit();
|
|
287
|
+
Obj.update(space.properties, (properties) => {
|
|
288
|
+
Annotation2.set(properties, MigrationVersionAnnotation, migration.version);
|
|
255
289
|
});
|
|
256
|
-
|
|
257
|
-
});
|
|
258
|
-
await builder._commit();
|
|
290
|
+
}
|
|
259
291
|
}
|
|
292
|
+
} finally {
|
|
293
|
+
const finalState = this._registry.get(this._stateAtom);
|
|
294
|
+
this._registry.set(this._stateAtom, {
|
|
295
|
+
running: finalState.running.filter((key) => key !== spaceKey)
|
|
296
|
+
});
|
|
260
297
|
}
|
|
261
|
-
this._state.running.splice(this._state.running.indexOf(space.key.toHex()), 1);
|
|
262
298
|
return true;
|
|
263
299
|
}
|
|
264
300
|
};
|
|
265
301
|
export {
|
|
266
302
|
MigrationBuilder,
|
|
267
|
-
|
|
303
|
+
MigrationVersionAnnotation,
|
|
304
|
+
Migrations,
|
|
305
|
+
compactDocumentsEpochMigration
|
|
268
306
|
};
|
|
269
307
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../../src/migration-builder.ts", "../../../src/migrations.ts"],
|
|
4
|
-
"sourcesContent": ["//\n// Copyright 2024 DXOS.org\n//\n\nimport { next as A, type Doc } from '@automerge/automerge';\nimport { type AnyDocumentId, type DocumentId } from '@automerge/automerge-repo';\nimport type * as Schema from 'effect/Schema';\n\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { requireTypeReference } from '@dxos/echo/internal';\nimport { type DocHandleProxy, ObjectCore, type RepoProxy, migrateDocument } from '@dxos/echo-db';\nimport {\n type DatabaseDirectory,\n type ObjectStructure,\n Reference,\n SpaceDocVersion,\n encodeReference,\n} from '@dxos/echo-protocol';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\n/*\n\nConsidering a better API for this:\n\n```ts\nconst migration = space.db.beginMigration(); // all actions are not visible to queries and are only applied once you call `apply`\n\nmigration.applyObjectMigration(defineMigration(From, To, { ... }));\n\nmigration.delete(id);\nmigration.add(obj);\n\nawait migration.apply(); // Will create new epoch.\n```\n\n*/\n\n// TODO(dmaretskyi): We no longer need to hook into ECHO internals, with the changes to echo APIs.\nexport class MigrationBuilder {\n private readonly _repo: RepoProxy;\n private readonly _rootDoc: Doc<DatabaseDirectory>;\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<DatabaseDirectory> = 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 .doc() as Doc<DatabaseDirectory>;\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.doc() as Doc<DatabaseDirectory>;\n return doc.objects?.[id];\n }\n\n async migrateObject(\n id: string,\n migrate: (objectStructure: ObjectStructure) => MaybePromise<{ schema: Schema.Schema.AnyNoContext; props: any }>,\n ): Promise<void> {\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: DatabaseDirectory = {\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.doc() as Doc<DatabaseDirectory>, newState);\n const newHandle = this._repo.import<DatabaseDirectory>(A.save(migratedDoc));\n this._newLinks[id] = newHandle.url;\n this._addHandleToFlushList(newHandle);\n }\n\n async addObject(schema: Schema.Schema.AnyNoContext, props: any): Promise<string> {\n const core = this._createObject({ schema, props });\n return core.id;\n }\n\n createReference(id: string) {\n return encodeReference(Reference.localObjectReference(id));\n }\n\n deleteObject(id: string): void {\n this._deleteObjects.push(id);\n }\n\n changeProperties(changeFn: (properties: ObjectStructure) => void): void {\n if (!this._newRoot) {\n this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n this._newRoot.change((doc: DatabaseDirectory) => {\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(): Promise<void> {\n if (!this._newRoot) {\n this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n await this._space.db.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<DatabaseDirectory> | 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(): void {\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 A.RawString(url);\n }\n\n this._newRoot = this._repo.create<DatabaseDirectory>({\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({\n id,\n schema,\n props,\n }: {\n id?: string;\n schema: Schema.Schema.AnyNoContext;\n props: any;\n }): ObjectCore {\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<DatabaseDirectory>({\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>): void {\n this._flushIds.push(handle.documentId);\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { type Space, SpaceState, live } 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 = live<{ 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): boolean {\n return this._state.running.includes(space.key.toHex());\n }\n\n static define(namespace: string, migrations: Migration[]): void {\n this.namespace = namespace;\n this.migrations = migrations;\n }\n\n static async migrate(space: Space, targetVersion?: string | number): Promise<boolean> {\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,
|
|
6
|
-
"names": ["
|
|
3
|
+
"sources": ["../../../src/annotations.ts", "../../../src/document-compaction.ts", "../../../src/migration-builder.ts", "../../../src/migrations.ts"],
|
|
4
|
+
"sourcesContent": ["//\n// Copyright 2026 DXOS.org\n//\n\nimport * as Schema from 'effect/Schema';\n\nimport { Annotation } from '@dxos/echo';\n\n/** Migration version stored on space properties meta. */\nexport const MigrationVersionAnnotation = Annotation.make({\n id: 'org.dxos.migrations.version',\n schema: Schema.String,\n});\n", "//\n// Copyright 2026 DXOS.org\n//\n\nimport { type Space, SpaceState } from '@dxos/client/echo';\nimport { invariant } from '@dxos/invariant';\n\nimport { MigrationBuilder } from './migration-builder';\n\nexport type CompactDocumentsOptions = {\n /**\n * Entity ids whose linked Automerge documents should be compacted.\n * Defaults to all ids in the space root `links` map.\n */\n objectIds?: string[];\n};\n\nexport type CompactDocumentsResult = {\n compacted: string[];\n skipped: string[];\n epochNumber: number;\n};\n\n/**\n * Re-materializes linked object documents into fresh Automerge docs (no history) and commits\n * a new space epoch with {@link CreateEpochRequest.Migration.REPLACE_AUTOMERGE_ROOT}.\n */\nexport const compactDocumentsEpochMigration = async (\n space: Space,\n options: CompactDocumentsOptions = {},\n): Promise<CompactDocumentsResult> => {\n invariant(space.state.get() === SpaceState.SPACE_READY, 'Space must be open and ready before compaction.');\n\n const builder = new MigrationBuilder(space);\n const { compacted, skipped } = await builder.compactLinkedDocuments(options.objectIds);\n await builder._commit();\n\n const epochs = await space.internal.getEpochs();\n const lastEpoch = epochs[epochs.length - 1];\n const epochNumber = lastEpoch?.subject.assertion.number ?? 0;\n\n return { compacted, skipped, epochNumber };\n};\n", "//\n// Copyright 2024 DXOS.org\n//\n\nimport { next as A, type Doc, toJS } from '@automerge/automerge';\nimport { type AnyDocumentId, type DocumentId } from '@automerge/automerge-repo';\nimport type * as Schema from 'effect/Schema';\n\nimport { type Space } from '@dxos/client/echo';\nimport { CreateEpochRequest } from '@dxos/client/halo';\nimport { type DocHandleProxy, ObjectCore, type RepoProxy, migrateDocument } from '@dxos/echo-client/internal';\nimport { type DatabaseDirectory, EncodedReference, type EntityStructure, SpaceDocVersion } from '@dxos/echo-protocol';\nimport { getSchemaURI } from '@dxos/echo/internal';\nimport * as Type from '@dxos/echo/Type';\nimport { invariant } from '@dxos/invariant';\nimport { EID, EntityId } from '@dxos/keys';\nimport { type MaybePromise } from '@dxos/util';\n\n/*\n\nConsidering a better API for this:\n\n```ts\nconst migration = space.db.beginMigration(); // all actions are not visible to queries and are only applied once you call `apply`\n\nmigration.applyObjectMigration(defineMigration(From, To, { ... }));\n\nmigration.delete(id);\nmigration.add(obj);\n\nawait migration.apply(); // Will create new epoch.\n```\n\n*/\n\n// TODO(dmaretskyi): We no longer need to hook into ECHO internals, with the changes to echo APIs.\nexport class MigrationBuilder {\n private readonly _repo: RepoProxy;\n private readonly _rootDoc: Doc<DatabaseDirectory>;\n\n // echoUri -> automergeUrl\n private readonly _newLinks: Record<string, string> = {};\n private readonly _flushIds: DocumentId[] = [];\n private readonly _deleteObjects: string[] = [];\n\n private _newRoot?: DocHandleProxy<DatabaseDirectory> = undefined;\n\n constructor(private readonly _space: Space) {\n this._repo = this._space.internal.db._repo;\n const rootDoc = this._space.internal.db._getSpaceRootDocHandle().doc();\n invariant(rootDoc, 'Space root document must be available when creating MigrationBuilder');\n this._rootDoc = rootDoc;\n }\n\n async findObject(id: string): Promise<EntityStructure | 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.doc() as Doc<DatabaseDirectory>;\n return doc.objects?.[id];\n }\n\n async migrateObject(\n id: string,\n migrate: (objectStructure: EntityStructure) => MaybePromise<{ type: Type.AnyEntity; props: any }>,\n ): Promise<void> {\n const objectStructure = await this.findObject(id);\n if (!objectStructure) {\n return;\n }\n\n const { type, props } = await migrate(objectStructure);\n const schema = Type.getSchema(type);\n\n const oldHandle = await this._findObjectContainingHandle(id);\n invariant(oldHandle);\n\n const newState: DatabaseDirectory = {\n version: SpaceDocVersion.CURRENT,\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: {\n [id]: {\n system: {\n type: EncodedReference.fromURI(getSchemaURI(schema)!),\n },\n data: props,\n meta: {\n keys: [],\n },\n },\n },\n };\n const migratedDoc = migrateDocument(oldHandle.doc() as Doc<DatabaseDirectory>, newState);\n const newHandle = this._repo.import<DatabaseDirectory>(A.save(migratedDoc));\n await newHandle.whenReady();\n invariant(newHandle.url, 'Migrated document URL not available after whenReady');\n this._newLinks[id] = newHandle.url;\n this._addHandleToFlushList(newHandle.documentId!);\n }\n\n async addObject(type: Type.AnyEntity, props: any): Promise<string> {\n const resolved = Type.getSchema(type);\n const core = await this._createObject({ schema: resolved, props });\n return core.id;\n }\n\n createReference(id: string) {\n invariant(EntityId.isValid(id), 'Invalid EntityId.');\n return EncodedReference.fromURI(EID.make({ entityId: id }));\n }\n\n deleteObject(id: string): void {\n this._deleteObjects.push(id);\n }\n\n /**\n * Re-materializes linked object documents into fresh Automerge docs without history.\n * Call {@link _commit} to publish a new space epoch with updated root links.\n */\n async compactLinkedDocuments(objectIds?: string[]): Promise<{ compacted: string[]; skipped: string[] }> {\n const linkIds = objectIds ?? Object.keys(this._rootDoc.links ?? {});\n const compacted: string[] = [];\n const skipped: string[] = [];\n\n for (const id of linkIds) {\n const oldHandle = await this._findObjectContainingHandle(id);\n if (!oldHandle) {\n skipped.push(id);\n continue;\n }\n\n await oldHandle.whenReady();\n const materialized = toJS(oldHandle.doc()!) as DatabaseDirectory;\n const newHandle = this._repo.create<DatabaseDirectory>(materialized);\n await newHandle.whenReady();\n invariant(newHandle.url, 'Compacted document URL not available after whenReady');\n this._newLinks[id] = newHandle.url;\n this._addHandleToFlushList(newHandle.documentId!);\n compacted.push(id);\n }\n\n return { compacted, skipped };\n }\n\n async changeProperties(changeFn: (properties: EntityStructure) => void): Promise<void> {\n if (!this._newRoot) {\n await this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n this._newRoot.change((doc: DatabaseDirectory) => {\n const propertiesStructure = doc.objects?.[this._space.properties.id];\n propertiesStructure && changeFn(propertiesStructure);\n });\n await this._newRoot.whenReady();\n this._addHandleToFlushList(this._newRoot.documentId!);\n }\n\n /**\n * @internal\n */\n async _commit(): Promise<void> {\n if (!this._newRoot) {\n await this._buildNewRoot();\n }\n invariant(this._newRoot, 'New root not created');\n\n await this._space.db.flush();\n\n // Create new epoch.\n invariant(this._newRoot.url, 'New root URL not available');\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<DatabaseDirectory> | 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 async _buildNewRoot(): Promise<void> {\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 A.RawString(url);\n }\n\n this._newRoot = this._repo.create<DatabaseDirectory>({\n version: SpaceDocVersion.CURRENT,\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: this._rootDoc.objects,\n links,\n });\n await this._newRoot.whenReady();\n this._addHandleToFlushList(this._newRoot.documentId!);\n }\n\n private async _createObject({\n id,\n schema,\n props,\n }: {\n id?: string;\n schema: Schema.Schema.AnyNoContext;\n props: any;\n }): Promise<ObjectCore> {\n const core = new ObjectCore();\n if (id) {\n core.id = id;\n }\n\n core.initNewObject(props);\n core.setType(EncodedReference.fromURI(getSchemaURI(schema)!));\n const newHandle = this._repo.create<DatabaseDirectory>({\n version: SpaceDocVersion.CURRENT,\n access: {\n spaceKey: this._space.key.toHex(),\n },\n objects: {\n [core.id]: core.getDoc() as EntityStructure,\n },\n });\n await newHandle.whenReady();\n this._newLinks[core.id] = newHandle.url!;\n this._addHandleToFlushList(newHandle.documentId!);\n\n return core;\n }\n\n private _addHandleToFlushList(id: DocumentId): void {\n this._flushIds.push(id);\n }\n}\n", "//\n// Copyright 2023 DXOS.org\n//\n\nimport { Atom } from '@effect-atom/atom';\nimport * as Registry from '@effect-atom/atom/Registry';\nimport * as Option from 'effect/Option';\n\nimport { type Space, SpaceState } from '@dxos/client/echo';\nimport { Annotation, Obj } from '@dxos/echo';\nimport { invariant } from '@dxos/invariant';\nimport { type MaybePromise } from '@dxos/util';\n\nimport { MigrationVersionAnnotation } from './annotations';\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 _registry = Registry.make();\n private static _stateAtom = Atom.make<{ running: string[] }>({ running: [] }).pipe(Atom.keepAlive);\n\n /**\n * @deprecated Use `MigrationVersionAnnotation` via `Annotation.get/set` on space properties.\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): boolean {\n const state = this._registry.get(this._stateAtom);\n return state.running.includes(space.key.toHex());\n }\n\n static define(namespace: string, migrations: Migration[]): void {\n this.namespace = namespace;\n this.migrations = migrations;\n }\n\n static async migrate(space: Space, targetVersion?: string | number): Promise<boolean> {\n invariant(!this.running(space), 'Migration already running');\n invariant(space.state.get() === SpaceState.SPACE_READY, 'Space not ready');\n const currentVersion = Annotation.get(space.properties, MigrationVersionAnnotation).pipe(Option.getOrUndefined);\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 const spaceKey = space.key.toHex();\n const currentState = this._registry.get(this._stateAtom);\n this._registry.set(this._stateAtom, { running: [...currentState.running, spaceKey] });\n try {\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 await builder._commit();\n Obj.update(space.properties, (properties) => {\n Annotation.set(properties, MigrationVersionAnnotation, migration.version);\n });\n }\n }\n } finally {\n const finalState = this._registry.get(this._stateAtom);\n this._registry.set(this._stateAtom, { running: finalState.running.filter((key) => key !== spaceKey) });\n }\n\n return true;\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAIA,YAAYA,YAAY;AAExB,SAASC,kBAAkB;AAGpB,IAAMC,6BAA6BD,WAAWE,KAAK;EACxDC,IAAI;EACJC,QAAeC;AACjB,CAAA;;;ACRA,SAAqBC,kBAAkB;AACvC,SAASC,aAAAA,kBAAiB;;;ACD1B,SAASC,QAAQC,GAAaC,YAAY;AAK1C,SAASC,0BAA0B;AACnC,SAA8BC,YAA4BC,uBAAuB;AACjF,SAAiCC,kBAAwCC,uBAAuB;AAChG,SAASC,oBAAoB;AAC7B,YAAYC,UAAU;AACtB,SAASC,iBAAiB;AAC1B,SAASC,KAAKC,gBAAgB;AAG9B,IAAA,eAAA;AAmBmBC,IAAiB,mBAAjBA,MAAiB;EACjBC;EAEjB;EACiBC;;EAEAC,YAAAA,CAAAA;EAETC,YAA+CC,CAAAA;EAEvD,iBAA6BC,CAAa;aAAbA;cACtBN,QAAQ;AACb,SAAA,SAAMO;AACNV,SAAAA,QAAUU,KAAAA,OAAS,SAAA,GAAA;AACnB,UAAKN,UAAWM,KAAAA,OAAAA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GAAA,IAAA,GAAA,MAAA,GAAA,CAAA,gDAAA,mBAAA,EAAA,CAAA;AACtF,UAAMwB,iBAAStB,YAAWoB,IAAWG,MAAQF,YAAYf,0BAAAA,EAAAA,KAAAA,qBAAAA;AACzD,UAAMkB,eAAcF,KAAM,WAAUtB,UAAWyB,CAAAA,MAAAA,EAAM,YAAO,cAAA,IAAA;AAC5D,UAAIN,IAAAA,KAAAA,WAAiBK,UAAa,CAAA,MAAA,EAAA,YAAA,aAAA;UAChC,cAAO,MAAA,KAAA,KAAA,WAAA,SAAA,IAAA;AACT,QAAA,iBAAA,aAAA;AAEA,aAAME;IACN;AACA,UAAKzB,WAAU0B,MAAQ,IAACzB,MAAAA;UAAcE,eAAS,KAAA,UAAA,IAAA,KAAA,UAAA;mBAAIwB,IAAAA,KAAaxB,YAAO;eAAEsB;QAAS,GAAA,aAAA;QAAC;MAC/E;;;UAGA,cAAWG,cAAa7B;cACtB,aAAM8B,KAAcC,WAAAA,MAAiBxB,cAAAA,WAAAA;mBAC/BsB,aAAc,YAAC;gBAAEtB,UAAAA,IAAAA,iBAAAA,KAAAA;gBAAOuB,UAAAA,KAAAA;YAAQ;YACtC;UACAE,CAAAA;gBACEjB,QAAAA,QAAekB;AACjB,cAAA,OAAA,MAAA,YAAA,CAAA,eAAA;AACF,YAAAlB,YAAA,IAAA,YAAA,4BAAA,UAAA,OAAA;UACF,CAAA;QACF;MACE;;YACsCX,aAAS8B,KAAW9B,UAAQ+B,IAAM,KAAEC,UAAQA;AAAkB,WAAA,UAAA,IAAA,KAAA,YAAA;QACtG,SAAA,WAAA,QAAA,OAAA,CAAA,QAAA,QAAA,QAAA;MAEA,CAAA;IACF;AACF,WAAA;;;",
|
|
6
|
+
"names": ["Schema", "Annotation", "MigrationVersionAnnotation", "make", "id", "schema", "String", "SpaceState", "invariant", "next", "A", "toJS", "CreateEpochRequest", "ObjectCore", "migrateDocument", "EncodedReference", "SpaceDocVersion", "getSchemaURI", "Type", "invariant", "EID", "EntityId", "_repo", "_rootDoc", "_newLinks", "_deleteObjects", "_newRoot", "undefined", "_space", "rootDoc", "findObject", "docHandle", "objects", "migrateObject", "migrate", "objectStructure", "oldHandle", "_findObjectContainingHandle", "newState", "version", "access", "spaceKey", "system", "data", "props", "meta", "keys", "newHandle", "whenReady", "id", "_addHandleToFlushList", "addObject", "core", "_createObject", "schema", "resolved", "createReference", "entityId", "deleteObject", "skipped", "linkIds", "compacted", "_buildNewRoot", "propertiesStructure", "properties", "changeFn", "migration", "REPLACE_AUTOMERGE_ROOT", "automergeRootUrl", "documentId", "url", "create", "links", "initNewObject", "CURRENT", "__dxlog_file", "builder", "space", "invariant", "compacted", "skipped", "compactLinkedDocuments", "_commit", "epochs", "internal", "getEpochs", "lastEpoch", "epochNumber", "subject", "assertion", "Atom", "Registry", "Option", "SpaceState", "Annotation", "Obj", "invariant", "__dxlog_file", "Migrations", "migrations", "_registry", "_stateAtom", "Atom", "running", "keepAlive", "targetVersion", "space", "state", "define", "namespace", "invariant", "get", "SpaceState", "currentVersion", "Annotation", "MigrationVersionAnnotation", "pipe", "getOrUndefined", "currentIndex", "findIndex", "version", "i", "m", "targetIndex", "length", "spaceKey", "set", "currentState", "migration", "builder", "MigrationBuilder", "Obj", "properties", "finalState", "filter", "key"]
|
|
7
7
|
}
|