Tldraw/packages/store/src/lib/test/validateMigrations.test.ts

166 wiersze
3.1 KiB
TypeScript
Czysty Zwykły widok Historia

New migrations again (#3220) Describe what your pull request does. If appropriate, add GIFs or images showing the before and after. ### Change Type - [x] `sdk` — Changes the tldraw SDK - [x] `galaxy brain` — Architectural changes ### Test Plan 1. Add a step-by-step description of how to test your PR here. 2. - [ ] Unit Tests - [ ] End to end tests ### Release Notes #### BREAKING CHANGES - The `Migrations` type is now called `LegacyMigrations`. - The serialized schema format (e.g. returned by `StoreSchema.serialize()` and `Store.getSnapshot()`) has changed. You don't need to do anything about it unless you were reading data directly from the schema for some reason. In which case it'd be best to avoid that in the future! We have no plans to change the schema format again (this time was traumatic enough) but you never know. - `compareRecordVersions` and the `RecordVersion` type have both disappeared. There is no replacement. These were public by mistake anyway, so hopefully nobody had been using it. - `compareSchemas` is a bit less useful now. Our migrations system has become a little fuzzy to allow for simpler UX when adding/removing custom extensions and 3rd party dependencies, and as a result we can no longer compare serialized schemas in any rigorous manner. You can rely on this function to return `0` if the schemas are the same. Otherwise it will return `-1` if the schema on the right _seems_ to be newer than the schema on the left, but it cannot guarantee that in situations where migration sequences have been removed over time (e.g. if you remove one of the builtin tldraw shapes). Generally speaking, the best way to check schema compatibility now is to call `store.schema.getMigrationsSince(persistedSchema)`. This will throw an error if there is no upgrade path from the `persistedSchema` to the current version. - `defineMigrations` has been deprecated and will be removed in a future release. For upgrade instructions see https://tldraw.dev/docs/persistence#Updating-legacy-shape-migrations-defineMigrations - `migrate` has been removed. Nobody should have been using this but if you were you'll need to find an alternative. For migrating tldraw data, you should stick to using `schema.migrateStoreSnapshot` and, if you are building a nuanced sync engine that supports some amount of backwards compatibility, also feel free to use `schema.migratePersistedRecord`. - the `Migration` type has changed. If you need the old one for some reason it has been renamed to `LegacyMigration`. It will be removed in a future release. - the `Migrations` type has been renamed to `LegacyMigrations` and will be removed in a future release. - the `SerializedSchema` type has been augmented. If you need the old version specifically you can use `SerializedSchemaV1` --------- Co-authored-by: Steve Ruiz <steveruizok@gmail.com>
2024-04-15 12:53:42 +00:00
import { validateMigrations } from '../migrate'
describe(validateMigrations, () => {
it('should throw if a migration id is invalid', () => {
expect(() =>
validateMigrations({
retroactive: false,
sequence: [
{
id: 'foo.1' as any,
scope: 'record',
up() {
// noop
},
},
],
sequenceId: 'foo',
})
).toThrowErrorMatchingInlineSnapshot(
`"Every migration in sequence 'foo' must have an id starting with 'foo/'. Got invalid id: 'foo.1'"`
)
expect(() =>
validateMigrations({
retroactive: false,
sequence: [
{
id: 'foo/one' as any,
scope: 'record',
up() {
// noop
},
},
],
sequenceId: 'foo',
})
).toThrowErrorMatchingInlineSnapshot(`"Invalid migration id: 'foo/one'"`)
expect(() =>
validateMigrations({
retroactive: false,
sequence: [
{
id: 'foo/1' as any,
scope: 'record',
up() {
// noop
},
},
{
id: 'foo.2' as any,
scope: 'record',
up() {
// noop
},
},
],
sequenceId: 'foo',
})
).toThrowErrorMatchingInlineSnapshot(
`"Every migration in sequence 'foo' must have an id starting with 'foo/'. Got invalid id: 'foo.2'"`
)
expect(() =>
validateMigrations({
retroactive: false,
sequence: [
{
id: 'foo/1' as any,
scope: 'record',
up() {
// noop
},
},
{
id: 'foo/two' as any,
scope: 'record',
up() {
// noop
},
},
],
sequenceId: 'foo',
})
).toThrowErrorMatchingInlineSnapshot(`"Invalid migration id: 'foo/two'"`)
})
it('should throw if the sequenceId is invalid', () => {
expect(() =>
validateMigrations({
retroactive: false,
sequence: [],
sequenceId: 'foo/bar',
})
).toThrowErrorMatchingInlineSnapshot(`"sequenceId cannot contain a '/', got foo/bar"`)
expect(() =>
validateMigrations({
retroactive: false,
sequence: [],
sequenceId: '',
})
).toThrowErrorMatchingInlineSnapshot(`"sequenceId must be a non-empty string"`)
})
it('should throw if the version numbers do not start at 1', () => {
expect(() =>
validateMigrations({
retroactive: false,
sequence: [
{
id: 'foo/2',
scope: 'record',
up() {
// noop
},
},
],
sequenceId: 'foo',
})
).toThrowErrorMatchingInlineSnapshot(
`"Expected the first migrationId to be 'foo/1' but got 'foo/2'"`
)
})
it('should throw if the version numbers do not increase monotonically', () => {
expect(() =>
validateMigrations({
retroactive: false,
sequence: [
{
id: 'foo/1',
scope: 'record',
up() {
// noop
},
},
{
id: 'foo/2',
scope: 'record',
up() {
// noop
},
},
{
id: 'foo/4',
scope: 'record',
up() {
// noop
},
},
],
sequenceId: 'foo',
})
).toThrowErrorMatchingInlineSnapshot(
`"Migration id numbers must increase in increments of 1, expected foo/3 but got 'foo/4'"`
)
})
})