@tldraw/store 3.16.0-internal.51e99e128bd4 → 3.16.0-internal.a478398270c6
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/README.md +2 -2
- package/dist-cjs/index.d.ts +2 -8
- package/dist-cjs/index.js +1 -1
- package/dist-cjs/lib/RecordsDiff.js +3 -3
- package/dist-cjs/lib/RecordsDiff.js.map +2 -2
- package/dist-cjs/lib/StoreSchema.js +8 -23
- package/dist-cjs/lib/StoreSchema.js.map +3 -3
- package/dist-esm/index.d.mts +2 -8
- package/dist-esm/index.mjs +1 -1
- package/dist-esm/lib/RecordsDiff.mjs +3 -3
- package/dist-esm/lib/RecordsDiff.mjs.map +2 -2
- package/dist-esm/lib/StoreSchema.mjs +8 -23
- package/dist-esm/lib/StoreSchema.mjs.map +3 -3
- package/package.json +19 -12
- package/src/lib/RecordsDiff.ts +3 -9
- package/src/lib/StoreSchema.ts +8 -32
- package/src/lib/test/AtomMap.test.ts +1 -2
- package/src/lib/test/dependsOn.test.ts +2 -2
- package/src/lib/test/recordStore.test.ts +37 -40
- package/src/lib/test/sortMigrations.test.ts +4 -4
- package/src/lib/test/validateMigrations.test.ts +8 -8
- package/src/lib/test/migrationCaching.test.ts +0 -209
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-deprecated */
|
|
2
|
-
import { assert } from '@tldraw/utils'
|
|
3
|
-
import {
|
|
4
|
-
BaseRecord,
|
|
5
|
-
Migration,
|
|
6
|
-
RecordId,
|
|
7
|
-
createMigrationIds,
|
|
8
|
-
createMigrationSequence,
|
|
9
|
-
createRecordType,
|
|
10
|
-
} from '../../'
|
|
11
|
-
import { StoreSchema } from '../StoreSchema'
|
|
12
|
-
|
|
13
|
-
interface TestRecord extends BaseRecord<'test', RecordId<TestRecord>> {
|
|
14
|
-
name: string
|
|
15
|
-
version: number
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
describe('StoreSchema migration caching', () => {
|
|
19
|
-
// Create migration IDs
|
|
20
|
-
const TestVersions = createMigrationIds('com.tldraw.test', {
|
|
21
|
-
AddVersion: 1,
|
|
22
|
-
UpdateVersion: 2,
|
|
23
|
-
})
|
|
24
|
-
|
|
25
|
-
// Create a simple schema with migrations
|
|
26
|
-
const createTestSchema = (version: number) => {
|
|
27
|
-
const TestRecordType = createRecordType<TestRecord>('test', {
|
|
28
|
-
scope: 'document',
|
|
29
|
-
})
|
|
30
|
-
|
|
31
|
-
const sequence: Migration[] = []
|
|
32
|
-
|
|
33
|
-
if (version > 1) {
|
|
34
|
-
sequence.push({
|
|
35
|
-
id: TestVersions.AddVersion,
|
|
36
|
-
scope: 'record',
|
|
37
|
-
up: (record: any) => {
|
|
38
|
-
// Mutate the record in place
|
|
39
|
-
record.version = 2
|
|
40
|
-
// Don't return anything
|
|
41
|
-
},
|
|
42
|
-
down: (record: any) => {
|
|
43
|
-
record.version = 1
|
|
44
|
-
// Don't return anything
|
|
45
|
-
},
|
|
46
|
-
})
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (version > 2) {
|
|
50
|
-
sequence.push({
|
|
51
|
-
id: TestVersions.UpdateVersion,
|
|
52
|
-
scope: 'record',
|
|
53
|
-
up: (record: any) => {
|
|
54
|
-
record.version = 3
|
|
55
|
-
// Don't return anything
|
|
56
|
-
},
|
|
57
|
-
down: (record: any) => {
|
|
58
|
-
record.version = 2
|
|
59
|
-
// Don't return anything
|
|
60
|
-
},
|
|
61
|
-
})
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const schema = StoreSchema.create(
|
|
65
|
-
{
|
|
66
|
-
test: TestRecordType,
|
|
67
|
-
},
|
|
68
|
-
{
|
|
69
|
-
migrations: [createMigrationSequence({ sequenceId: 'com.tldraw.test', sequence })],
|
|
70
|
-
}
|
|
71
|
-
)
|
|
72
|
-
|
|
73
|
-
return schema
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
it('should cache migration results and return same array reference', () => {
|
|
77
|
-
const schema = createTestSchema(3)
|
|
78
|
-
const oldSchema = schema.serializeEarliestVersion()
|
|
79
|
-
|
|
80
|
-
// First call should create the migrations array
|
|
81
|
-
const migrations1 = schema.getMigrationsSince(oldSchema)
|
|
82
|
-
assert(migrations1.ok)
|
|
83
|
-
expect(migrations1.value).toHaveLength(2)
|
|
84
|
-
|
|
85
|
-
// Second call should return the same array reference (cached)
|
|
86
|
-
const migrations2 = schema.getMigrationsSince(oldSchema)
|
|
87
|
-
assert(migrations2.ok)
|
|
88
|
-
expect(migrations2.value).toBe(migrations1.value) // Same array reference
|
|
89
|
-
|
|
90
|
-
// Third call should also return the same array reference
|
|
91
|
-
const migrations3 = schema.getMigrationsSince(oldSchema)
|
|
92
|
-
assert(migrations3.ok)
|
|
93
|
-
expect(migrations3.value).toBe(migrations1.value)
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
it('should not cache when schema versions are different', () => {
|
|
97
|
-
const schema = createTestSchema(3)
|
|
98
|
-
const oldSchema = schema.serializeEarliestVersion()
|
|
99
|
-
|
|
100
|
-
// Call with original schema
|
|
101
|
-
const migrations1 = schema.getMigrationsSince(oldSchema)
|
|
102
|
-
expect(migrations1.ok).toBe(true)
|
|
103
|
-
if (!migrations1.ok) throw new Error('Expected migrations1 to be ok')
|
|
104
|
-
|
|
105
|
-
// Create a different schema version by using a schema with version 2
|
|
106
|
-
const schemaV2 = createTestSchema(2)
|
|
107
|
-
const schemaV2Serialized = schemaV2.serializeEarliestVersion()
|
|
108
|
-
const migrations2 = schema.getMigrationsSince(schemaV2Serialized)
|
|
109
|
-
expect(migrations2.ok).toBe(true)
|
|
110
|
-
if (!migrations2.ok) throw new Error('Expected migrations2 to be ok')
|
|
111
|
-
|
|
112
|
-
// Should be different arrays (no cache hit)
|
|
113
|
-
expect(migrations2.value).not.toBe(migrations1.value)
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
it('should handle mutateInputStore: true with migrators that return void', () => {
|
|
117
|
-
const schema = createTestSchema(3)
|
|
118
|
-
const oldSchema = schema.serializeEarliestVersion()
|
|
119
|
-
|
|
120
|
-
const store = {
|
|
121
|
-
test1: {
|
|
122
|
-
id: 'test1',
|
|
123
|
-
name: 'Test 1',
|
|
124
|
-
version: 1,
|
|
125
|
-
typeName: 'test',
|
|
126
|
-
},
|
|
127
|
-
test2: {
|
|
128
|
-
id: 'test2',
|
|
129
|
-
name: 'Test 2',
|
|
130
|
-
version: 1,
|
|
131
|
-
typeName: 'test',
|
|
132
|
-
},
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Test with mutateInputStore: true
|
|
136
|
-
const result1 = schema.migrateStoreSnapshot(
|
|
137
|
-
{ store, schema: oldSchema },
|
|
138
|
-
{ mutateInputStore: true }
|
|
139
|
-
)
|
|
140
|
-
|
|
141
|
-
assert(result1.type === 'success')
|
|
142
|
-
expect((result1.value as any).test1.version).toBe(3)
|
|
143
|
-
expect((result1.value as any).test2.version).toBe(3)
|
|
144
|
-
|
|
145
|
-
// The input store should be mutated in place
|
|
146
|
-
expect(result1.value).toBe(store)
|
|
147
|
-
})
|
|
148
|
-
|
|
149
|
-
it('should handle mutateInputStore: false with migrators that return void', () => {
|
|
150
|
-
const schema = createTestSchema(3)
|
|
151
|
-
const oldSchema = schema.serializeEarliestVersion()
|
|
152
|
-
|
|
153
|
-
const store = {
|
|
154
|
-
test1: {
|
|
155
|
-
id: 'test1',
|
|
156
|
-
name: 'Test 1',
|
|
157
|
-
version: 1,
|
|
158
|
-
typeName: 'test',
|
|
159
|
-
},
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
// Test with mutateInputStore: false (default)
|
|
163
|
-
const result = schema.migrateStoreSnapshot({ store, schema: oldSchema })
|
|
164
|
-
|
|
165
|
-
assert(result.type === 'success')
|
|
166
|
-
expect((result.value as any).test1.version).toBe(3)
|
|
167
|
-
|
|
168
|
-
// The input store should NOT be mutated
|
|
169
|
-
expect(store.test1.version).toBe(1)
|
|
170
|
-
})
|
|
171
|
-
|
|
172
|
-
it('should handle empty migration list caching', () => {
|
|
173
|
-
const schema = createTestSchema(1) // No migrations
|
|
174
|
-
const oldSchema = schema.serializeEarliestVersion()
|
|
175
|
-
|
|
176
|
-
// First call
|
|
177
|
-
const migrations1 = schema.getMigrationsSince(oldSchema)
|
|
178
|
-
assert(migrations1.ok)
|
|
179
|
-
|
|
180
|
-
expect(migrations1.value).toHaveLength(0)
|
|
181
|
-
|
|
182
|
-
// Second call should return same array reference
|
|
183
|
-
const migrations2 = schema.getMigrationsSince(oldSchema)
|
|
184
|
-
assert(migrations2.ok)
|
|
185
|
-
expect(migrations2.value).toBe(migrations1.value)
|
|
186
|
-
expect(migrations2.value).toHaveLength(0)
|
|
187
|
-
})
|
|
188
|
-
|
|
189
|
-
it('should handle incompatible schema caching', () => {
|
|
190
|
-
const schema = createTestSchema(3)
|
|
191
|
-
const incompatibleSchema = {
|
|
192
|
-
schemaVersion: 1 as const,
|
|
193
|
-
storeVersion: 1,
|
|
194
|
-
recordVersions: {
|
|
195
|
-
test: {
|
|
196
|
-
version: 999, // Much higher version than what we support
|
|
197
|
-
},
|
|
198
|
-
},
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// First call should fail
|
|
202
|
-
const migrations1 = schema.getMigrationsSince(incompatibleSchema)
|
|
203
|
-
expect(migrations1.ok).toBe(false)
|
|
204
|
-
|
|
205
|
-
// Second call should also fail (but might be cached)
|
|
206
|
-
const migrations2 = schema.getMigrationsSince(incompatibleSchema)
|
|
207
|
-
expect(migrations2.ok).toBe(false)
|
|
208
|
-
})
|
|
209
|
-
})
|