@@ -225,15 +225,24 @@ func buildSchemaDiff(old, new schema.Schema) (schemaDiff, bool, error) {
225
225
if err != nil {
226
226
return schemaDiff {}, false , fmt .Errorf ("diffing tables: %w" , err )
227
227
}
228
-
229
228
newSchemaTablesByName := buildSchemaObjByNameMap (new .Tables )
230
229
addedTablesByName := buildSchemaObjByNameMap (tableDiffs .adds )
231
230
deletedTablesByName := buildSchemaObjByNameMap (tableDiffs .deletes )
232
231
tableDiffsByName := buildDiffByNameMap [schema.Table , tableDiff ](tableDiffs .alters )
232
+
233
+ materializedViewDiffs , err := diffLists (old .MaterializedViews , new .MaterializedViews , func (old , new schema.MaterializedView , _ , _ int ) (diff materializedViewDiff , requiresRecreation bool , error error ) {
234
+ return buildMaterializedViewDiff (deletedTablesByName , tableDiffsByName , old , new )
235
+ })
236
+ if err != nil {
237
+ return schemaDiff {}, false , fmt .Errorf ("diffing materialized views: %w" , err )
238
+ }
239
+ addedMatViewsByName := buildSchemaObjByNameMap (materializedViewDiffs .adds )
240
+
233
241
indexesDiff , err := diffLists (old .Indexes , new .Indexes , func (oldIndex , newIndex schema.Index , _ , _ int ) (indexDiff , bool , error ) {
234
242
return buildIndexDiff (indexDiffConfig {
235
243
newSchemaTablesByName : newSchemaTablesByName ,
236
244
addedTablesByName : addedTablesByName ,
245
+ addedMatViewsByName : addedMatViewsByName ,
237
246
oldSchemaIndexesByName : buildSchemaObjByNameMap (old .Indexes ),
238
247
newSchemaIndexesByName : buildSchemaObjByNameMap (new .Indexes ),
239
248
}, oldIndex , newIndex )
@@ -319,13 +328,6 @@ func buildSchemaDiff(old, new schema.Schema) (schemaDiff, bool, error) {
319
328
return schemaDiff {}, false , fmt .Errorf ("diffing views: %w" , err )
320
329
}
321
330
322
- materializedViewDiffs , err := diffLists (old .MaterializedViews , new .MaterializedViews , func (old , new schema.MaterializedView , _ , _ int ) (diff materializedViewDiff , requiresRecreation bool , error error ) {
323
- return buildMaterializedViewDiff (deletedTablesByName , tableDiffsByName , old , new )
324
- })
325
- if err != nil {
326
- return schemaDiff {}, false , fmt .Errorf ("diffing materialized views: %w" , err )
327
- }
328
-
329
331
return schemaDiff {
330
332
oldAndNew : oldAndNew [schema.Schema ]{
331
333
old : old ,
@@ -430,6 +432,7 @@ func buildTableDiff(oldTable, newTable schema.Table, _, _ int) (diff tableDiff,
430
432
type indexDiffConfig struct {
431
433
newSchemaTablesByName map [string ]schema.Table
432
434
addedTablesByName map [string ]schema.Table
435
+ addedMatViewsByName map [string ]schema.MaterializedView
433
436
434
437
// oldSchemaIndexesByName and newSchemaIndexesByName by name are hackaround because the diff function does not yet support hierarchies
435
438
oldSchemaIndexesByName map [string ]schema.Index
@@ -456,6 +459,10 @@ func buildIndexDiff(deps indexDiffConfig, old, new schema.Index) (diff indexDiff
456
459
// re-created). In other words, an index must be re-created if the owning table is re-created
457
460
return indexDiff {}, true , nil
458
461
}
462
+ if _ , isOnNewMatView := deps .addedMatViewsByName [new .OwningTable .GetName ()]; isOnNewMatView {
463
+ // If the materialized view is new, then the index must be re-created
464
+ return indexDiff {}, true , nil
465
+ }
459
466
460
467
if old .ParentIdx == nil {
461
468
// If the old index didn't belong to a partitioned index (and the new index does), we can resolve the parent
@@ -531,6 +538,8 @@ func (s schemaSQLGenerator) Alter(diff schemaDiff) ([]Statement, error) {
531
538
tablesInNewSchemaByName := buildSchemaObjByNameMap (diff .new .Tables )
532
539
deletedTablesByName := buildSchemaObjByNameMap (diff .tableDiffs .deletes )
533
540
addedTablesByName := buildSchemaObjByNameMap (diff .tableDiffs .adds )
541
+ deletedMatViewsByName := buildSchemaObjByNameMap (diff .materializedViewDiffs .deletes )
542
+ addedMatViewsByName := buildSchemaObjByNameMap (diff .materializedViewDiffs .adds )
534
543
functionsInNewSchemaByName := buildSchemaObjByNameMap (diff .new .Functions )
535
544
536
545
namedSchemaStatements , err := diff .namedSchemaDiffs .resolveToSQLGroupedByEffect (& namedSchemaSQLGenerator {})
@@ -576,9 +585,13 @@ func (s schemaSQLGenerator) Alter(diff schemaDiff) ([]Statement, error) {
576
585
partialGraph = concatPartialGraphs (partialGraph , renameConflictingIndexesPartialGraph )
577
586
578
587
indexGenerator := legacyToNewSqlVertexGenerator [schema.Index , indexDiff ](& indexSQLVertexGenerator {
579
- deletedTablesByName : deletedTablesByName ,
580
- addedTablesByName : addedTablesByName ,
581
- tablesInNewSchemaByName : tablesInNewSchemaByName ,
588
+ deletedTablesByName : deletedTablesByName ,
589
+ addedTablesByName : addedTablesByName ,
590
+ tablesInNewSchemaByName : tablesInNewSchemaByName ,
591
+
592
+ deletedMatViewsByName : deletedMatViewsByName ,
593
+ addedMatViewsByName : addedMatViewsByName ,
594
+
582
595
indexesInNewSchemaByName : buildSchemaObjByNameMap (diff .new .Indexes ),
583
596
584
597
renameSQLVertexGenerator : renameConflictingIndexesGenerator ,
@@ -1567,6 +1580,12 @@ type indexSQLVertexGenerator struct {
1567
1580
// tablesInNewSchemaByName is a map of table name to tables (and partitions) in the new schema.
1568
1581
// These tables are not necessarily new. This is used to identify if the table is partitioned
1569
1582
tablesInNewSchemaByName map [string ]schema.Table
1583
+
1584
+ // deletedMatViewsByName is a map of materialized view name to the delete materialized view
1585
+ deletedMatViewsByName map [string ]schema.MaterializedView
1586
+ // addedMatViewsByName is a map of materialiezd view name to added materialized view.
1587
+ addedMatViewsByName map [string ]schema.MaterializedView
1588
+
1570
1589
// indexesInNewSchemaByName is a map of index name to the index
1571
1590
// This is used to identify the parent index is a primary key
1572
1591
indexesInNewSchemaByName map [string ]schema.Index
@@ -1586,6 +1605,9 @@ func (isg *indexSQLVertexGenerator) Add(index schema.Index) ([]Statement, error)
1586
1605
if _ , isNewTable := isg .addedTablesByName [index .OwningTable .GetName ()]; isNewTable {
1587
1606
stmts = stripMigrationHazards (stmts ... )
1588
1607
}
1608
+ if _ , isNewMatView := isg .addedMatViewsByName [index .OwningTable .GetName ()]; isNewMatView {
1609
+ stmts = stripMigrationHazards (stmts ... )
1610
+ }
1589
1611
return stmts , nil
1590
1612
}
1591
1613
@@ -1656,9 +1678,12 @@ func (isg *indexSQLVertexGenerator) addIdxStmtsWithHazards(index schema.Index) (
1656
1678
}
1657
1679
1658
1680
func (isg * indexSQLVertexGenerator ) Delete (index schema.Index ) ([]Statement , error ) {
1659
- _ , tableWasDeleted := isg .deletedTablesByName [index .OwningTable .GetName ()]
1660
- // An index will be dropped if its owning table is dropped.
1661
- if tableWasDeleted {
1681
+ if _ , tableWasDeleted := isg .deletedTablesByName [index .OwningTable .GetName ()]; tableWasDeleted {
1682
+ // An index will be dropped if its owning table is dropped.
1683
+ return nil , nil
1684
+ }
1685
+ if _ , matViewWasDeleted := isg .deletedMatViewsByName [index .OwningTable .GetName ()]; matViewWasDeleted {
1686
+ // An index will be dropped if its owning materialized view is dropped.
1662
1687
return nil , nil
1663
1688
}
1664
1689
@@ -1832,7 +1857,11 @@ func (isg *indexSQLVertexGenerator) GetAddAlterDependencies(index, _ schema.Inde
1832
1857
1833
1858
func (isg * indexSQLVertexGenerator ) GetDeleteDependencies (index schema.Index ) ([]dependency , error ) {
1834
1859
dependencies := []dependency {
1860
+ // Technically, only the table -or- materialized view will exist, but there is no harm in having a dependency
1861
+ // on both.
1835
1862
mustRun (isg .GetSQLVertexId (index , diffTypeDelete )).after (buildTableVertexId (index .OwningTable , diffTypeDelete )),
1863
+ mustRun (isg .GetSQLVertexId (index , diffTypeDelete )).after (buildMaterializedViewVertexId (index .OwningTable , diffTypeDelete )),
1864
+
1836
1865
// Drop the index after it has been potentially renamed
1837
1866
mustRun (isg .GetSQLVertexId (index , diffTypeDelete )).after (buildRenameConflictingIndexVertexId (index .GetSchemaQualifiedName (), diffTypeAddAlter )),
1838
1867
}
@@ -1850,12 +1879,20 @@ func (isg *indexSQLVertexGenerator) GetDeleteDependencies(index schema.Index) ([
1850
1879
1851
1880
func (isg * indexSQLVertexGenerator ) addDepsOnTableAddAlterIfNecessary (index schema.Index ) []dependency {
1852
1881
// This could be cleaner if start sorting columns separately in the graph
1882
+ // TODO(bplunkett) - can below just be switched to deleted tables by name
1853
1883
parentTable , ok := isg .tablesInNewSchemaByName [index .OwningTable .GetName ()]
1854
1884
if ! ok {
1855
1885
// If the parent table is deleted, we don't need to worry about making the index statement come
1856
1886
// before any alters
1857
1887
return nil
1858
1888
}
1889
+ if _ , deletedMatView := isg .deletedMatViewsByName [index .OwningTable .GetName ()]; deletedMatView {
1890
+ // If the parent materialized view is deleted, we don't need to worry about making the index statement come
1891
+ // before any alters.
1892
+ return nil
1893
+ }
1894
+
1895
+ // TODO(bplunkett) - add conditinoal switches for materialized views
1859
1896
1860
1897
// These dependencies will force the index deletion statement to come before the table AddAlter
1861
1898
addAlterColumnDeps := []dependency {
0 commit comments