Skip to content

Commit

Permalink
WIP - wrong order fix
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Feb 5, 2024
1 parent 83978dd commit e012e0a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 13 deletions.
7 changes: 7 additions & 0 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@ func (db *db) updateSchema(
// Make sure that this collection is the parent of the current [col], and not part of
// another collection set that happens to be using the same schema.
if source.SourceCollectionID == previousID {
if existingCol.RootID == client.OrphanRootID {
existingCol.RootID = col.RootID
existingCol, err = description.SaveCollection(ctx, txn, existingCol)
if err != nil {
return err
}

Check warning on line 242 in db/collection.go

View check run for this annotation

Codecov / codecov/patch

db/collection.go#L241-L242

Added lines #L241 - L242 were not covered by tests
}
isExistingCol = true
break existingColLoop
}
Expand Down
23 changes: 10 additions & 13 deletions db/lens.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,19 @@ func (db *db) setMigration(ctx context.Context, txn datastore.Txn, cfg client.Le
for _, sourceCol := range sourceCols {
isDstCollectionFound := false
dstColsLoop:
for _, dstCol := range dstCols {
for i, dstCol := range dstCols {
collectionSources := dstCol.CollectionSources()
if len(collectionSources) == 0 {
dstCol.Sources = append(dstCol.Sources, &client.CollectionSource{
SourceCollectionID: sourceCol.ID,
})
dstCols[i] = dstCol

isDstCollectionFound = true
break dstColsLoop
}
for _, source := range collectionSources {
if source.SourceCollectionID == sourceCol.ID {
if dstCol.RootID == client.OrphanRootID && sourceCol.RootID != client.OrphanRootID {
// If the destination is marked as orphaned, and the source is not, then update the
// destination RootID to be the actual known RootID of the source. This will happen
// when the destination collection was created before the source.
dstCol.RootID = sourceCol.RootID

_, err := description.SaveCollection(ctx, txn, dstCol)
if err != nil {
return err
}
}

isDstCollectionFound = true
break dstColsLoop
}
Expand Down
87 changes: 87 additions & 0 deletions tests/integration/schema/migrations/query/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,93 @@ func TestSchemaMigrationQueryMigratesAcrossMultipleVersionsBeforePatches(t *test
testUtils.ExecuteTestCase(t, test)
}

func TestSchemaMigrationQueryMigratesAcrossMultipleVersionsBeforePatchesWrongOrder(t *testing.T) {
test := testUtils.TestCase{
Description: "Test schema migration, multiple migrations before patch",
Actions: []any{
testUtils.SchemaUpdate{
Schema: `
type Users {
name: String
}
`,
},
testUtils.CreateDoc{
Doc: `{
"name": "John"
}`,
},
testUtils.ConfigureMigration{
// Declare the migration from v2=>v3 before declaring the migration from v1=>v2
LensConfig: client.LensConfig{
SourceSchemaVersionID: "bafkreia4m6sn2rfypj2velvwpyude22fcb5jyfzum2eh3cdzg4a3myj5nu",
DestinationSchemaVersionID: "bafkreifiai7ukztmfavmicyq6hummnwpueq475ddn6m5wsbjhhpjtp6fcy",
Lens: model.Lens{
Lenses: []model.LensModule{
{
Path: lenses.SetDefaultModulePath,
Arguments: map[string]any{
"dst": "email",
"value": "[email protected]",
},
},
},
},
},
},
testUtils.ConfigureMigration{
LensConfig: client.LensConfig{
SourceSchemaVersionID: "bafkreig3zt63qt7bkji47etyu2sqtzroa3tcfdxgwqc3ka2ijy63refq3a",
DestinationSchemaVersionID: "bafkreia4m6sn2rfypj2velvwpyude22fcb5jyfzum2eh3cdzg4a3myj5nu",
Lens: model.Lens{
Lenses: []model.LensModule{
{
Path: lenses.SetDefaultModulePath,
Arguments: map[string]any{
"dst": "verified",
"value": true,
},
},
},
},
},
},
testUtils.SchemaPatch{
Patch: `
[
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "verified", "Kind": "Boolean"} }
]
`,
},
testUtils.SchemaPatch{
Patch: `
[
{ "op": "add", "path": "/Users/Fields/-", "value": {"Name": "email", "Kind": "String"} }
]
`,
},
testUtils.Request{
Request: `query {
Users {
name
verified
email
}
}`,
Results: []map[string]any{
{
"name": "John",
"verified": true,
"email": "[email protected]",
},
},
},
},
}

testUtils.ExecuteTestCase(t, test)
}

// This test is important as it tests that orphan migrations do not block the fetcher(s)
// from functioning.
//
Expand Down

0 comments on commit e012e0a

Please sign in to comment.