@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.
@@ -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
- })