Skip to content

Commit 127e28f

Browse files
Revert "Mat views indexdes" (#238)
This reverts commit ba0e9dc.
1 parent ce93f35 commit 127e28f

File tree

2 files changed

+14
-119
lines changed

2 files changed

+14
-119
lines changed

pkg/diff/materialized_view_sql_generator_test.go

Lines changed: 0 additions & 68 deletions
This file was deleted.

pkg/diff/sql_generator.go

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -225,24 +225,15 @@ func buildSchemaDiff(old, new schema.Schema) (schemaDiff, bool, error) {
225225
if err != nil {
226226
return schemaDiff{}, false, fmt.Errorf("diffing tables: %w", err)
227227
}
228+
228229
newSchemaTablesByName := buildSchemaObjByNameMap(new.Tables)
229230
addedTablesByName := buildSchemaObjByNameMap(tableDiffs.adds)
230231
deletedTablesByName := buildSchemaObjByNameMap(tableDiffs.deletes)
231232
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-
241233
indexesDiff, err := diffLists(old.Indexes, new.Indexes, func(oldIndex, newIndex schema.Index, _, _ int) (indexDiff, bool, error) {
242234
return buildIndexDiff(indexDiffConfig{
243235
newSchemaTablesByName: newSchemaTablesByName,
244236
addedTablesByName: addedTablesByName,
245-
addedMatViewsByName: addedMatViewsByName,
246237
oldSchemaIndexesByName: buildSchemaObjByNameMap(old.Indexes),
247238
newSchemaIndexesByName: buildSchemaObjByNameMap(new.Indexes),
248239
}, oldIndex, newIndex)
@@ -328,6 +319,13 @@ func buildSchemaDiff(old, new schema.Schema) (schemaDiff, bool, error) {
328319
return schemaDiff{}, false, fmt.Errorf("diffing views: %w", err)
329320
}
330321

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+
331329
return schemaDiff{
332330
oldAndNew: oldAndNew[schema.Schema]{
333331
old: old,
@@ -432,7 +430,6 @@ func buildTableDiff(oldTable, newTable schema.Table, _, _ int) (diff tableDiff,
432430
type indexDiffConfig struct {
433431
newSchemaTablesByName map[string]schema.Table
434432
addedTablesByName map[string]schema.Table
435-
addedMatViewsByName map[string]schema.MaterializedView
436433

437434
// oldSchemaIndexesByName and newSchemaIndexesByName by name are hackaround because the diff function does not yet support hierarchies
438435
oldSchemaIndexesByName map[string]schema.Index
@@ -459,10 +456,6 @@ func buildIndexDiff(deps indexDiffConfig, old, new schema.Index) (diff indexDiff
459456
// re-created). In other words, an index must be re-created if the owning table is re-created
460457
return indexDiff{}, true, nil
461458
}
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-
}
466459

467460
if old.ParentIdx == nil {
468461
// If the old index didn't belong to a partitioned index (and the new index does), we can resolve the parent
@@ -538,8 +531,6 @@ func (s schemaSQLGenerator) Alter(diff schemaDiff) ([]Statement, error) {
538531
tablesInNewSchemaByName := buildSchemaObjByNameMap(diff.new.Tables)
539532
deletedTablesByName := buildSchemaObjByNameMap(diff.tableDiffs.deletes)
540533
addedTablesByName := buildSchemaObjByNameMap(diff.tableDiffs.adds)
541-
deletedMatViewsByName := buildSchemaObjByNameMap(diff.materializedViewDiffs.deletes)
542-
addedMatViewsByName := buildSchemaObjByNameMap(diff.materializedViewDiffs.adds)
543534
functionsInNewSchemaByName := buildSchemaObjByNameMap(diff.new.Functions)
544535

545536
namedSchemaStatements, err := diff.namedSchemaDiffs.resolveToSQLGroupedByEffect(&namedSchemaSQLGenerator{})
@@ -585,13 +576,9 @@ func (s schemaSQLGenerator) Alter(diff schemaDiff) ([]Statement, error) {
585576
partialGraph = concatPartialGraphs(partialGraph, renameConflictingIndexesPartialGraph)
586577

587578
indexGenerator := legacyToNewSqlVertexGenerator[schema.Index, indexDiff](&indexSQLVertexGenerator{
588-
deletedTablesByName: deletedTablesByName,
589-
addedTablesByName: addedTablesByName,
590-
tablesInNewSchemaByName: tablesInNewSchemaByName,
591-
592-
deletedMatViewsByName: deletedMatViewsByName,
593-
addedMatViewsByName: addedMatViewsByName,
594-
579+
deletedTablesByName: deletedTablesByName,
580+
addedTablesByName: addedTablesByName,
581+
tablesInNewSchemaByName: tablesInNewSchemaByName,
595582
indexesInNewSchemaByName: buildSchemaObjByNameMap(diff.new.Indexes),
596583

597584
renameSQLVertexGenerator: renameConflictingIndexesGenerator,
@@ -1580,12 +1567,6 @@ type indexSQLVertexGenerator struct {
15801567
// tablesInNewSchemaByName is a map of table name to tables (and partitions) in the new schema.
15811568
// These tables are not necessarily new. This is used to identify if the table is partitioned
15821569
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-
15891570
// indexesInNewSchemaByName is a map of index name to the index
15901571
// This is used to identify the parent index is a primary key
15911572
indexesInNewSchemaByName map[string]schema.Index
@@ -1605,9 +1586,6 @@ func (isg *indexSQLVertexGenerator) Add(index schema.Index) ([]Statement, error)
16051586
if _, isNewTable := isg.addedTablesByName[index.OwningTable.GetName()]; isNewTable {
16061587
stmts = stripMigrationHazards(stmts...)
16071588
}
1608-
if _, isNewMatView := isg.addedMatViewsByName[index.OwningTable.GetName()]; isNewMatView {
1609-
stmts = stripMigrationHazards(stmts...)
1610-
}
16111589
return stmts, nil
16121590
}
16131591

@@ -1678,12 +1656,9 @@ func (isg *indexSQLVertexGenerator) addIdxStmtsWithHazards(index schema.Index) (
16781656
}
16791657

16801658
func (isg *indexSQLVertexGenerator) Delete(index schema.Index) ([]Statement, error) {
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.
1659+
_, tableWasDeleted := isg.deletedTablesByName[index.OwningTable.GetName()]
1660+
// An index will be dropped if its owning table is dropped.
1661+
if tableWasDeleted {
16871662
return nil, nil
16881663
}
16891664

@@ -1857,11 +1832,7 @@ func (isg *indexSQLVertexGenerator) GetAddAlterDependencies(index, _ schema.Inde
18571832

18581833
func (isg *indexSQLVertexGenerator) GetDeleteDependencies(index schema.Index) ([]dependency, error) {
18591834
dependencies := []dependency{
1860-
// Technically, only the table -or- materialized view will exist, but there is no harm in having a dependency
1861-
// on both.
18621835
mustRun(isg.GetSQLVertexId(index, diffTypeDelete)).after(buildTableVertexId(index.OwningTable, diffTypeDelete)),
1863-
mustRun(isg.GetSQLVertexId(index, diffTypeDelete)).after(buildMaterializedViewVertexId(index.OwningTable, diffTypeDelete)),
1864-
18651836
// Drop the index after it has been potentially renamed
18661837
mustRun(isg.GetSQLVertexId(index, diffTypeDelete)).after(buildRenameConflictingIndexVertexId(index.GetSchemaQualifiedName(), diffTypeAddAlter)),
18671838
}
@@ -1879,20 +1850,12 @@ func (isg *indexSQLVertexGenerator) GetDeleteDependencies(index schema.Index) ([
18791850

18801851
func (isg *indexSQLVertexGenerator) addDepsOnTableAddAlterIfNecessary(index schema.Index) []dependency {
18811852
// 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
18831853
parentTable, ok := isg.tablesInNewSchemaByName[index.OwningTable.GetName()]
18841854
if !ok {
18851855
// If the parent table is deleted, we don't need to worry about making the index statement come
18861856
// before any alters
18871857
return nil
18881858
}
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
18961859

18971860
// These dependencies will force the index deletion statement to come before the table AddAlter
18981861
addAlterColumnDeps := []dependency{

0 commit comments

Comments
 (0)