Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: support index on materialized view for PG SDL (#18061)
rebelicefd08bf8a1712
diff --git a/backend/plugin/schema/differ.go b/backend/plugin/schema/differ.goindex a91121567c..2d0ffb0bab 100644--- a/backend/plugin/schema/differ.go+++ b/backend/plugin/schema/differ.go@@ -191,8 +191,9 @@ type MaterializedViewDiff struct {MaterializedViewName stringOldMaterializedView *storepb.MaterializedViewMetadataNewMaterializedView *storepb.MaterializedViewMetadata- OldASTNode any // AST node for old materialized view- NewASTNode any // AST node for new materialized view+ OldASTNode any // AST node for old materialized view+ NewASTNode any // AST node for new materialized view+ IndexChanges []*IndexDiff // Index changes on materialized view}// FunctionDiff represents changes to a function.diff --git a/backend/plugin/schema/pg/generate_migration.go b/backend/plugin/schema/pg/generate_migration.goindex ad3f3f356f..5655f9c732 100644--- a/backend/plugin/schema/pg/generate_migration.go+++ b/backend/plugin/schema/pg/generate_migration.go@@ -268,12 +268,16 @@ func dropObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) {// Drop viewsfor _, viewDiff := range viewMap {- writeDropView(buf, viewDiff.SchemaName, viewDiff.ViewName)+ if viewDiff.Action == schema.MetadataDiffActionDrop {+ writeDropView(buf, viewDiff.SchemaName, viewDiff.ViewName)+ }}// Drop materialized viewsfor _, mvDiff := range materializedViewMap {- writeDropMaterializedView(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName)+ if mvDiff.Action == schema.MetadataDiffActionDrop {+ writeDropMaterializedView(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName)+ }}// Drop functions@@ -384,9 +388,13 @@ func dropObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) {// Drop the object itselfif viewDiff, ok := viewMap[objID]; ok {- writeDropView(buf, viewDiff.SchemaName, viewDiff.ViewName)+ if viewDiff.Action == schema.MetadataDiffActionDrop {+ writeDropView(buf, viewDiff.SchemaName, viewDiff.ViewName)+ }} else if mvDiff, ok := materializedViewMap[objID]; ok {- writeDropMaterializedView(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName)+ if mvDiff.Action == schema.MetadataDiffActionDrop {+ writeDropMaterializedView(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName)+ }} else if funcDiff, ok := functionMap[objID]; ok {definition := getFunctionDefinitionForDrop(funcDiff)writeDropFunction(buf, funcDiff.SchemaName, funcDiff.FunctionName, funcDiff.OldASTNode, definition)@@ -466,6 +474,30 @@ func dropObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) {}}}++ // Handle remaining ALTER materialized view drops (indexes)+ for _, mvDiff := range diff.MaterializedViewChanges {+ if mvDiff.Action == schema.MetadataDiffActionAlter {+ // Drop indexes+ for _, indexDiff := range mvDiff.IndexChanges {+ if indexDiff.Action == schema.MetadataDiffActionDrop {+ if indexDiff.OldIndex != nil {+ // Metadata mode: use index metadata+ if indexDiff.OldIndex.IsConstraint {+ writeDropConstraint(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName, indexDiff.OldIndex.Name)+ } else {+ writeDropIndex(buf, mvDiff.SchemaName, indexDiff.OldIndex.Name)+ }+ } else if indexDiff.OldASTNode != nil {+ // AST-only mode: extract from AST node+ if indexAST, ok := indexDiff.OldASTNode.(*pgparser.IndexstmtContext); ok {+ writeDropIndexFromAST(buf, mvDiff.SchemaName, indexAST)+ }+ }+ }+ }+ }+ }}// Drop enum types@@ -983,20 +1015,27 @@ func createObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) errorreturn errors.Errorf("materialized view diff for %s.%s has neither metadata nor AST node", mvDiff.SchemaName, mvDiff.MaterializedViewName)}case schema.MetadataDiffActionAlter:- // For PostgreSQL materialized views, we need to drop and recreate- // since ALTER MATERIALIZED VIEW doesn't support changing the definition- writeDropMaterializedView(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName)- // Support AST-only mode- if mvDiff.NewMaterializedView != nil {- if err := writeMigrationMaterializedView(buf, mvDiff.SchemaName, mvDiff.NewMaterializedView); err != nil {- return err- }- } else if mvDiff.NewASTNode != nil {- if err := writeMigrationMaterializedViewFromAST(buf, mvDiff.NewASTNode); err != nil {- return err+ // Check if this is an index-only change (no MV definition change)+ hasIndexOnlyChanges := len(mvDiff.IndexChanges) > 0 && mvDiff.NewMaterializedView == nil && mvDiff.NewASTNode == nil++ // For index-only changes, don't alter the MV itself+ // Index changes are processed separately later (see lines ~1107-1127)+ if !hasIndexOnlyChanges {+ // For PostgreSQL materialized views, we need to drop and recreate+ // since ALTER MATERIALIZED VIEW doesn't support changing the definition+ writeDropMaterializedView(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName)+ // Support AST-only mode+ if mvDiff.NewMaterializedView != nil {+ if err := writeMigrationMaterializedView(buf, mvDiff.SchemaName, mvDiff.NewMaterializedView); err != nil {+ return err+ }+ } else if mvDiff.NewASTNode != nil {+ if err := writeMigrationMaterializedViewFromAST(buf, mvDiff.NewASTNode); err != nil {+ return err+ }+ } else {+ return errors.Errorf("materialized view ALTER for %s.%s has neither metadata nor AST node", mvDiff.SchemaName, mvDiff.MaterializedViewName)}- } else {- return errors.Errorf("materialized view ALTER for %s.%s has neither metadata nor AST node", mvDiff.SchemaName, mvDiff.MaterializedViewName)}default:// No action needed for other operations@@ -1079,6 +1118,28 @@ func createObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error}}}++ // Add indexes for materialized view ALTER operations+ for _, mvDiff := range diff.MaterializedViewChanges {+ if mvDiff.Action == schema.MetadataDiffActionAlter {+ for _, indexDiff := range mvDiff.IndexChanges {+ if indexDiff.Action == schema.MetadataDiffActionCreate {+ if indexDiff.NewIndex != nil {+ // Metadata mode: use index metadata+ writeMigrationMaterializedViewIndex(buf, mvDiff.SchemaName, mvDiff.MaterializedViewName, indexDiff.NewIndex)+ } else if indexDiff.NewASTNode != nil {+ // AST-only mode: extract from AST node+ if indexAST, ok := indexDiff.NewASTNode.(*pgparser.IndexstmtContext); ok {+ if err := writeCreateIndexFromAST(buf, indexAST); err != nil {+ // If AST extraction fails, log error but continue (non-fatal)+ _, _ = fmt.Fprintf(buf, "-- Error creating index: %v\n", err)+ }+ }+ }+ }+ }+ }+ }}// ALTER table operations are now handled earlier in the topological order@@ -1116,6 +1177,15 @@ func createObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) errorreturn err}+ // Handle materialized view index comment changes+ for _, mvDiff := range diff.MaterializedViewChanges {+ if mvDiff.Action == schema.MetadataDiffActionAlter {+ if err := generateMaterializedViewIndexCommentChanges(buf, mvDiff); err != nil {+ return err+ }+ }+ }+// Handle function comment changesif err := generateFunctionCommentChanges(buf, diff); err != nil {return err@@ -2600,14 +2670,55 @@ func writeMigrationTrigger(out *strings.Builder, trigger *storepb.TriggerMetadat}// writeMigrationMaterializedViewIndex writes an index creation statement for a materialized view-func writeMigrationMaterializedViewIndex(out *strings.Builder, _ string, _ string, index *storepb.IndexMetadata) {- if index == nil || index.Definition == "" {+func writeMigrationMaterializedViewIndex(out *strings.Builder, schema, mvName string, index *storepb.IndexMetadata) {+ if index == nil {return}- _, _ = out.WriteString(index.Definition)- if !strings.HasSuffix(strings.TrimSpace(index.Definition), ";") {- _, _ = out.WriteString(";")++ // If index has a full definition, use it directly+ if index.Definition != "" {+ _, _ = out.WriteString(index.Definition)+ if !strings.HasSuffix(strings.TrimSpace(index.Definition), ";") {+ _, _ = out.WriteString(";")+ }+ _, _ = out.WriteString("\n")+ return+ }++ // Otherwise, construct the CREATE INDEX statement+ _, _ = out.WriteString(`CREATE `)+ if index.Unique {+ _, _ = out.WriteString(`UNIQUE `)+ }+ _, _ = out.WriteString(`INDEX "`)+ _, _ = out.WriteString(index.Name)+ _, _ = out.WriteString(`" ON "`)+ _, _ = out.WriteString(schema)+ _, _ = out.WriteString(`"."`)+ _, _ = out.WriteString(mvName)+ _, _ = out.WriteString(`" `)++ if index.Type != "" && index.Type != "BTREE" && index.Type != "btree" {+ _, _ = out.WriteString(`USING `)+ _, _ = out.WriteString(strings.ToUpper(index.Type))+ _, _ = out.WriteString(` `)+ }++ _, _ = out.WriteString(`(`)+ for i, expr := range index.Expressions {+ if i > 0 {+ _, _ = out.WriteString(`, `)+ }+ _, _ = out.WriteString(expr)++ // Handle descending order if specified+ if i < len(index.Descending) && index.Descending[i] {+ _, _ = out.WriteString(` DESC`)+ }}+ _, _ = out.WriteString(`)`)++ _, _ = out.WriteString(`;`)_, _ = out.WriteString("\n")}@@ -3046,6 +3157,26 @@ func generateIndexCommentChanges(buf *strings.Builder, tableDiff *schema.TableDireturn nil}+// generateMaterializedViewIndexCommentChanges generates COMMENT ON INDEX statements for index comment changes within materialized view diffs+func generateMaterializedViewIndexCommentChanges(buf *strings.Builder, mvDiff *schema.MaterializedViewDiff) error {+ for _, indexDiff := range mvDiff.IndexChanges {+ if indexDiff.Action == schema.MetadataDiffActionAlter {+ if indexDiff.OldIndex == nil || indexDiff.NewIndex == nil {+ continue+ }++ oldComment := indexDiff.OldIndex.Comment+ newComment := indexDiff.NewIndex.Comment++ // If comments are different, generate COMMENT ON INDEX statement+ if oldComment != newComment {+ writeCommentOnIndex(buf, mvDiff.SchemaName, indexDiff.NewIndex.Name, newComment)+ }+ }+ }+ return nil+}+// Helper functions to write comment statements for different object types// writeCommentOnSchema writes a COMMENT ON SCHEMA statementdiff --git a/backend/plugin/schema/pg/get_database_definition.go b/backend/plugin/schema/pg/get_database_definition.goindex 5bc275f36d..7e974421db 100644--- a/backend/plugin/schema/pg/get_database_definition.go+++ b/backend/plugin/schema/pg/get_database_definition.go@@ -2429,6 +2429,28 @@ func getSDLFormat(metadata *storepb.DatabaseSchemaMetadata) (string, error) {return "", err}}++ // Write indexes on materialized view+ for _, index := range materializedView.Indexes {+ // Skip constraint-based indexes as they are part of table definition+ if index.Primary || index.IsConstraint {+ continue+ }++ if err := writeIndexSDL(&buf, schema.Name, materializedView.Name, index); err != nil {+ return "", err+ }+ if _, err := buf.WriteString(";\n\n"); err != nil {+ return "", err+ }++ // Write index comment if present+ if len(index.Comment) > 0 {+ if err := writeIndexCommentSDL(&buf, schema.Name, index); err != nil {+ return "", err+ }+ }+ }}// Write functions and procedures after materialized views@@ -3378,6 +3400,28 @@ func GetMultiFileDatabaseDefinition(ctx schema.GetDefinitionContext, metadata *s}}+ // Write indexes on materialized view+ for _, index := range materializedView.Indexes {+ // Skip constraint-based indexes+ if index.Primary || index.IsConstraint {+ continue+ }++ buf.WriteString("\n")+ if err := writeIndexSDL(&buf, schemaName, materializedView.Name, index); err != nil {+ return nil, errors.Wrapf(err, "failed to generate index SDL for %s.%s", schemaName, index.Name)+ }+ buf.WriteString(";\n")++ // Write index comment if present+ if len(index.Comment) > 0 {+ buf.WriteString("\n")+ if err := writeIndexCommentSDL(&buf, schemaName, index); err != nil {+ return nil, errors.Wrapf(err, "failed to generate index comment for %s.%s", schemaName, index.Name)+ }+ }+ }+files = append(files, schema.File{Name: fmt.Sprintf("schemas/%s/materialized_views/%s.sql", schemaName, materializedView.Name),Content: buf.String(),diff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.goindex 947f343fc1..aa1a4c09fd 100644--- a/backend/plugin/schema/pg/get_sdl_diff.go+++ b/backend/plugin/schema/pg/get_sdl_diff.go@@ -1646,7 +1646,7 @@ func getIndexText(constraintAST parser.ITableconstraintContext) string {}// processStandaloneIndexChanges analyzes standalone CREATE INDEX statement changes-// and adds them to the appropriate table's IndexChanges+// and adds them to the appropriate table's or materialized view's IndexChangesfunc processStandaloneIndexChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {if currentChunks == nil || previousChunks == nil {return@@ -1660,13 +1660,23 @@ func processStandaloneIndexChanges(currentChunks, previousChunks *schema.SDLChunaffectedTables[qualifiedTableName] = tableDiff}+ // Initialize map with all existing materialized view diffs+ affectedMaterializedViews := make(map[string]*schema.MaterializedViewDiff, len(diff.MaterializedViewChanges))+ for _, mvDiff := range diff.MaterializedViewChanges {+ qualifiedMVName := mvDiff.SchemaName + "." + mvDiff.MaterializedViewName+ affectedMaterializedViews[qualifiedMVName] = mvDiff+ }+// Step 1: Process current indexes to find created and modified indexesfor _, currentChunk := range currentChunks.Indexes {- tableName := extractTableNameFromIndex(currentChunk.ASTNode)- if tableName == "" {- continue // Skip if we can't determine the table name+ targetObjectName := extractTableNameFromIndex(currentChunk.ASTNode)+ if targetObjectName == "" {+ continue // Skip if we can't determine the target object name}+ // Determine if this index is on a table or materialized view+ isOnMaterializedView := isIndexOnMaterializedView(targetObjectName, currentChunks, previousChunks)+if previousChunk, exists := previousChunks.Indexes[currentChunk.Identifier]; exists {// Index exists in both - check if modified by comparing text firstcurrentText := getStandaloneIndexText(currentChunk.ASTNode)@@ -1677,15 +1687,29 @@ func processStandaloneIndexChanges(currentChunks, previousChunks *schema.SDLChuncontinue}// Index was modified - use drop and recreate pattern (PostgreSQL standard)- tableDiff := getOrCreateTableDiff(diff, tableName, affectedTables)- tableDiff.IndexChanges = append(tableDiff.IndexChanges, &schema.IndexDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: previousChunk.ASTNode,- })- tableDiff.IndexChanges = append(tableDiff.IndexChanges, &schema.IndexDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: currentChunk.ASTNode,- })+ if isOnMaterializedView {+ // Index is on materialized view+ mvDiff := getOrCreateMaterializedViewDiff(diff, targetObjectName, affectedMaterializedViews)+ mvDiff.IndexChanges = append(mvDiff.IndexChanges, &schema.IndexDiff{+ Action: schema.MetadataDiffActionDrop,+ OldASTNode: previousChunk.ASTNode,+ })+ mvDiff.IndexChanges = append(mvDiff.IndexChanges, &schema.IndexDiff{+ Action: schema.MetadataDiffActionCreate,+ NewASTNode: currentChunk.ASTNode,+ })+ } else {+ // Index is on table+ tableDiff := getOrCreateTableDiff(diff, targetObjectName, affectedTables)+ tableDiff.IndexChanges = append(tableDiff.IndexChanges, &schema.IndexDiff{+ Action: schema.MetadataDiffActionDrop,+ OldASTNode: previousChunk.ASTNode,+ })+ tableDiff.IndexChanges = append(tableDiff.IndexChanges, &schema.IndexDiff{+ Action: schema.MetadataDiffActionCreate,+ NewASTNode: currentChunk.ASTNode,+ })+ }// Add COMMENT ON INDEX diffs if they exist in the new versionif len(currentChunk.CommentStatements) > 0 {schemaName, indexName := parseIdentifier(currentChunk.Identifier)@@ -1707,11 +1731,19 @@ func processStandaloneIndexChanges(currentChunks, previousChunks *schema.SDLChun// If text is identical, skip - no changes detected} else {// New index - store AST node only- tableDiff := getOrCreateTableDiff(diff, tableName, affectedTables)- tableDiff.IndexChanges = append(tableDiff.IndexChanges, &schema.IndexDiff{- Action: schema.MetadataDiffActionCreate,- NewASTNode: currentChunk.ASTNode,- })+ if isOnMaterializedView {+ mvDiff := getOrCreateMaterializedViewDiff(diff, targetObjectName, affectedMaterializedViews)+ mvDiff.IndexChanges = append(mvDiff.IndexChanges, &schema.IndexDiff{+ Action: schema.MetadataDiffActionCreate,+ NewASTNode: currentChunk.ASTNode,+ })+ } else {+ tableDiff := getOrCreateTableDiff(diff, targetObjectName, affectedTables)+ tableDiff.IndexChanges = append(tableDiff.IndexChanges, &schema.IndexDiff{+ Action: schema.MetadataDiffActionCreate,+ NewASTNode: currentChunk.ASTNode,+ })+ }// Add COMMENT ON INDEX diffs if they existif len(currentChunk.CommentStatements) > 0 {schemaName, indexName := parseIdentifier(currentChunk.Identifier)@@ -1736,16 +1768,27 @@ func processStandaloneIndexChanges(currentChunks, previousChunks *schema.SDLChunfor indexName, previousChunk := range previousChunks.Indexes {if _, exists := currentChunks.Indexes[indexName]; !exists {// Index was dropped - store AST node only- tableName := extractTableNameFromIndex(previousChunk.ASTNode)- if tableName == "" {- continue // Skip if we can't determine the table name+ targetObjectName := extractTableNameFromIndex(previousChunk.ASTNode)+ if targetObjectName == "" {+ continue // Skip if we can't determine the target object name}- tableDiff := getOrCreateTableDiff(diff, tableName, affectedTables)- tableDiff.IndexChanges = append(tableDiff.IndexChanges, &schema.IndexDiff{- Action: schema.MetadataDiffActionDrop,- OldASTNode: previousChunk.ASTNode,- })+ // Determine if this index was on a table or materialized view+ isOnMaterializedView := isIndexOnMaterializedView(targetObjectName, currentChunks, previousChunks)++ if isOnMaterializedView {+ mvDiff := getOrCreateMaterializedViewDiff(diff, targetObjectName, affectedMaterializedViews)+ mvDiff.IndexChanges = append(mvDiff.IndexChanges, &schema.IndexDiff{+ Action: schema.MetadataDiffActionDrop,+ OldASTNode: previousChunk.ASTNode,+ })+ } else {+ tableDiff := getOrCreateTableDiff(diff, targetObjectName, affectedTables)+ tableDiff.IndexChanges = append(tableDiff.IndexChanges, &schema.IndexDiff{+ Action: schema.MetadataDiffActionDrop,+ OldASTNode: previousChunk.ASTNode,+ })+ }}}}@@ -1836,6 +1879,52 @@ func getOrCreateTableDiff(diff *schema.MetadataDiff, tableName string, affectedTreturn newTableDiff}+// getOrCreateMaterializedViewDiff finds an existing materialized view diff or creates a new one+// mvName should be in schema.mv_name format+func getOrCreateMaterializedViewDiff(diff *schema.MetadataDiff, mvName string, affectedMaterializedViews map[string]*schema.MaterializedViewDiff) *schema.MaterializedViewDiff {+ // Check if we already have this materialized view in our map+ if mvDiff, exists := affectedMaterializedViews[mvName]; exists {+ return mvDiff+ }++ // Parse schema and MV name from mvName (format: schema.mv_name)+ schemaName, mvNameOnly := parseIdentifier(mvName)++ // Create a new materialized view diff for standalone index changes+ // We set Action to ALTER since we're modifying an existing MV by adding/removing indexes+ newMVDiff := &schema.MaterializedViewDiff{+ Action: schema.MetadataDiffActionAlter,+ SchemaName: schemaName,+ MaterializedViewName: mvNameOnly,+ OldMaterializedView: nil,+ NewMaterializedView: nil,+ OldASTNode: nil,+ NewASTNode: nil,+ IndexChanges: []*schema.IndexDiff{},+ }++ diff.MaterializedViewChanges = append(diff.MaterializedViewChanges, newMVDiff)+ affectedMaterializedViews[mvName] = newMVDiff+ return newMVDiff+}++// isIndexOnMaterializedView checks if the given object name refers to a materialized view+// rather than a table, by checking if it exists in the materialized view chunks+func isIndexOnMaterializedView(objectName string, currentChunks, previousChunks *schema.SDLChunks) bool {+ // Check if the object exists as a materialized view in current or previous chunks+ if currentChunks != nil && currentChunks.MaterializedViews != nil {+ if _, exists := currentChunks.MaterializedViews[objectName]; exists {+ return true+ }+ }+ if previousChunks != nil && previousChunks.MaterializedViews != nil {+ if _, exists := previousChunks.MaterializedViews[objectName]; exists {+ return true+ }+ }+ return false+}+// processViewChanges analyzes view changes between current and previous chunks// Following the text-first comparison pattern for performance optimizationfunc processViewChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {@@ -3828,7 +3917,8 @@ func generateUniqueKeyConstraintSDL(constraint *storepb.IndexMetadata) string {type extendedIndexMetadata struct {*storepb.IndexMetadataSchemaName string- TableName string+ TableName string // Table name or MaterializedView name+ TargetType string // "table" or "materialized_view"}// applyStandaloneIndexChangesToChunks applies minimal changes to standalone CREATE INDEX chunks@@ -3851,6 +3941,7 @@ func applyStandaloneIndexChangesToChunks(previousChunks *schema.SDLChunks, curre// Collect all standalone indexes from current schema (only non-constraint indexes)for _, schema := range currentMetadata.Schemas {+ // Collect indexes from tablesfor _, table := range schema.Tables {for _, index := range table.Indexes {// Only include standalone indexes (not constraints like PRIMARY KEY, UNIQUE CONSTRAINT)@@ -3861,6 +3952,25 @@ func applyStandaloneIndexChangesToChunks(previousChunks *schema.SDLChunks, curreIndexMetadata: index,SchemaName: schema.Name,TableName: table.Name,+ TargetType: "table",+ }+ currentIndexes[indexKey] = extendedIndex+ }+ }+ }++ // Collect indexes from materialized views+ for _, mv := range schema.MaterializedViews {+ for _, index := range mv.Indexes {+ // Only include standalone indexes (not constraints)+ if !index.IsConstraint && !index.Primary {+ indexKey := formatIndexKey(schema.Name, index.Name)+ // Store extended index metadata with materialized view/schema context+ extendedIndex := &extendedIndexMetadata{+ IndexMetadata: index,+ SchemaName: schema.Name,+ TableName: mv.Name,+ TargetType: "materialized_view",}currentIndexes[indexKey] = extendedIndex}@@ -3870,6 +3980,7 @@ func applyStandaloneIndexChangesToChunks(previousChunks *schema.SDLChunks, curre// Collect all standalone indexes from previous schemafor _, schema := range previousMetadata.Schemas {+ // Collect indexes from tablesfor _, table := range schema.Tables {for _, index := range table.Indexes {// Only include standalone indexes (not constraints)@@ -3879,6 +3990,24 @@ func applyStandaloneIndexChangesToChunks(previousChunks *schema.SDLChunks, curreIndexMetadata: index,SchemaName: schema.Name,TableName: table.Name,+ TargetType: "table",+ }+ previousIndexes[indexKey] = extendedIndex+ }+ }+ }++ // Collect indexes from materialized views+ for _, mv := range schema.MaterializedViews {+ for _, index := range mv.Indexes {+ // Only include standalone indexes (not constraints)+ if !index.IsConstraint && !index.Primary {+ indexKey := formatIndexKey(schema.Name, index.Name)+ extendedIndex := &extendedIndexMetadata{+ IndexMetadata: index,+ SchemaName: schema.Name,+ TableName: mv.Name,+ TargetType: "materialized_view",}previousIndexes[indexKey] = extendedIndex}diff --git a/backend/plugin/schema/pg/materialized_view_index_test.go b/backend/plugin/schema/pg/materialized_view_index_test.gonew file mode 100644index 0000000000..5b1151fa32--- /dev/null+++ b/backend/plugin/schema/pg/materialized_view_index_test.go@@ -0,0 +1,299 @@+package pg++import (+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/schema"+ "github.com/bytebase/bytebase/backend/store/model"+)++// TestMaterializedViewIndexInSDLOutput tests that materialized view indexes are included in SDL output+func TestMaterializedViewIndexInSDLOutput(t *testing.T) {+ metadata := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ MaterializedViews: []*storepb.MaterializedViewMetadata{+ {+ Name: "product_summary_mv",+ Definition: "SELECT id, name FROM products",+ Comment: "Product summary view",+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "idx_product_name",+ Expressions: []string{"name"},+ Type: "btree",+ Unique: false,+ Primary: false,+ IsConstraint: false,+ Comment: "Index on product name",+ },+ {+ Name: "idx_product_id_unique",+ Expressions: []string{"id"},+ Type: "btree",+ Unique: true,+ Primary: false,+ IsConstraint: false,+ },+ },+ },+ },+ },+ },+ }++ // Test single-file SDL format+ sdl, err := getSDLFormat(metadata)+ require.NoError(t, err)++ // Verify materialized view is present+ require.Contains(t, sdl, "CREATE MATERIALIZED VIEW")+ require.Contains(t, sdl, "product_summary_mv")++ // Verify indexes are present+ require.Contains(t, sdl, "CREATE INDEX \"idx_product_name\"")+ require.Contains(t, sdl, "CREATE UNIQUE INDEX \"idx_product_id_unique\"")++ // Verify index is on materialized view (using ON ONLY for SDL)+ require.Contains(t, sdl, "ON ONLY \"public\".\"product_summary_mv\"")++ // Verify comments+ require.Contains(t, sdl, "COMMENT ON MATERIALIZED VIEW")+ require.Contains(t, sdl, "Product summary view")+ require.Contains(t, sdl, "COMMENT ON INDEX")+ require.Contains(t, sdl, "Index on product name")+}++// TestMaterializedViewIndexInMultiFileSDL tests that materialized view indexes are included in multi-file SDL+func TestMaterializedViewIndexInMultiFileSDL(t *testing.T) {+ metadata := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ MaterializedViews: []*storepb.MaterializedViewMetadata{+ {+ Name: "user_stats_mv",+ Definition: "SELECT user_id, COUNT(*) as count FROM orders GROUP BY user_id",+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "idx_user_stats_user_id",+ Expressions: []string{"user_id"},+ Type: "btree",+ Unique: false,+ Primary: false,+ IsConstraint: false,+ },+ },+ },+ },+ },+ },+ }++ // Test multi-file SDL format+ result, err := GetMultiFileDatabaseDefinition(schema.GetDefinitionContext{}, metadata)+ require.NoError(t, err)+ require.NotNil(t, result)++ // Find the materialized view file+ var mvFile *schema.File+ for i := range result.Files {+ if result.Files[i].Name == "schemas/public/materialized_views/user_stats_mv.sql" {+ mvFile = &result.Files[i]+ break+ }+ }+ require.NotNil(t, mvFile, "Materialized view file not found")++ // Verify content includes both MV and index+ require.Contains(t, mvFile.Content, "CREATE MATERIALIZED VIEW")+ require.Contains(t, mvFile.Content, "user_stats_mv")+ require.Contains(t, mvFile.Content, "CREATE INDEX \"idx_user_stats_user_id\"")+ require.Contains(t, mvFile.Content, "ON ONLY \"public\".\"user_stats_mv\"")+}++// TestMaterializedViewIndexDependencyOrdering tests that materialized view indexes have correct dependency ordering in migrations+func TestMaterializedViewIndexDependencyOrdering(t *testing.T) {+ t.Run("CREATE: MV before Index", func(t *testing.T) {+ // Test that when creating a materialized view with indexes,+ // CREATE MATERIALIZED VIEW comes before CREATE INDEX++ oldMetadata := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ },+ },+ }++ newMetadata := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ MaterializedViews: []*storepb.MaterializedViewMetadata{+ {+ Name: "customer_segmentation_mv",+ Definition: "SELECT customer_id, SUM(amount) as total_spending FROM orders GROUP BY customer_id",+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "idx_customer_seg_spending",+ Expressions: []string{"total_spending"},+ Type: "btree",+ Unique: false,+ Primary: false,+ },+ },+ },+ },+ },+ },+ }++ // Convert to model.DatabaseSchema+ oldSchema := model.NewDatabaseSchema(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)+ newSchema := model.NewDatabaseSchema(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)++ // Get diff+ diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)+ require.NoError(t, err)++ // Generate migration+ migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)+ require.NoError(t, err)+ t.Logf("Generated migration:\n%s", migration)+ require.NotEmpty(t, migration)++ // Find positions of CREATE MATERIALIZED VIEW and CREATE INDEX+ mvPos := -1+ idxPos := -1+ for i := 0; i < len(migration); i++ {+ if mvPos == -1 && i+len("CREATE MATERIALIZED VIEW") <= len(migration) {+ if migration[i:i+len("CREATE MATERIALIZED VIEW")] == "CREATE MATERIALIZED VIEW" {+ mvPos = i+ }+ }+ if idxPos == -1 && i+len("CREATE INDEX") <= len(migration) {+ if migration[i:i+len("CREATE INDEX")] == "CREATE INDEX" {+ idxPos = i+ }+ }+ }++ require.NotEqual(t, -1, mvPos, "CREATE MATERIALIZED VIEW not found in migration")+ require.NotEqual(t, -1, idxPos, "CREATE INDEX not found in migration")+ require.Less(t, mvPos, idxPos, "CREATE MATERIALIZED VIEW should come before CREATE INDEX")+ })++ t.Run("DROP: Whole MV with indexes", func(t *testing.T) {+ // Test that when dropping an entire materialized view with indexes,+ // PostgreSQL automatically drops indexes so we don't need separate DROP INDEX statements++ oldMetadata := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ MaterializedViews: []*storepb.MaterializedViewMetadata{+ {+ Name: "customer_segmentation_mv",+ Definition: "SELECT customer_id, SUM(amount) as total_spending FROM orders GROUP BY customer_id",+ Indexes: []*storepb.IndexMetadata{+ {+ Name: "idx_customer_seg_spending",+ Expressions: []string{"total_spending"},+ Type: "btree",+ Unique: false,+ Primary: false,+ },+ },+ },+ },+ },+ },+ }++ newMetadata := &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ },+ },+ }++ // Convert to model.DatabaseSchema+ oldSchema := model.NewDatabaseSchema(oldMetadata, nil, nil, storepb.Engine_POSTGRES, false)+ newSchema := model.NewDatabaseSchema(newMetadata, nil, nil, storepb.Engine_POSTGRES, false)++ // Get diff+ diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_POSTGRES, oldSchema, newSchema)+ require.NoError(t, err)++ // Generate migration+ migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)+ require.NoError(t, err)+ t.Logf("Generated migration:\n%s", migration)+ require.NotEmpty(t, migration)++ // Verify DROP MATERIALIZED VIEW is present+ require.Contains(t, migration, "DROP MATERIALIZED VIEW")+ // Note: PostgreSQL automatically drops indexes when dropping a materialized view,+ // so we don't need (and shouldn't have) separate DROP INDEX statements+ require.NotContains(t, migration, "DROP INDEX")+ })++ t.Run("ADD Index to existing MV via SDL chunks", func(t *testing.T) {+ // This tests the exact scenario from the user's screenshot:+ // Adding an index to an existing materialized view should generate+ // CREATE INDEX statement without trying to recreate the MV++ // Previous SDL (MV without index)+ previousSDL := `CREATE MATERIALIZED VIEW "public"."customer_segmentation_mv" AS+ SELECT customer_id, SUM(amount) as total_spending+ FROM orders+ GROUP BY customer_id;`++ // Current SDL (MV with new index)+ currentSDL := `CREATE MATERIALIZED VIEW "public"."customer_segmentation_mv" AS+ SELECT customer_id, SUM(amount) as total_spending+ FROM orders+ GROUP BY customer_id;++ CREATE INDEX "idx_customer_seg_spending" ON "public"."customer_segmentation_mv" (total_spending);`++ // Get SDL diff (simulates bb rollout behavior)+ diff, err := GetSDLDiff(currentSDL, previousSDL, nil, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ // Debug: log all diffs+ t.Logf("MaterializedViewChanges: %d", len(diff.MaterializedViewChanges))+ for i, mvDiff := range diff.MaterializedViewChanges {+ t.Logf(" MV[%d]: Action=%v, Schema=%s, Name=%s, IndexChanges=%d, HasNewMV=%v, HasNewAST=%v",+ i, mvDiff.Action, mvDiff.SchemaName, mvDiff.MaterializedViewName,+ len(mvDiff.IndexChanges), mvDiff.NewMaterializedView != nil, mvDiff.NewASTNode != nil)+ }++ // Verify we have a MaterializedViewDiff with IndexChanges+ require.Equal(t, 1, len(diff.MaterializedViewChanges), "Should have 1 MV change")+ mvDiff := diff.MaterializedViewChanges[0]+ require.Equal(t, schema.MetadataDiffActionAlter, mvDiff.Action)+ require.Equal(t, "public", mvDiff.SchemaName)+ require.Equal(t, "customer_segmentation_mv", mvDiff.MaterializedViewName)+ require.Equal(t, 1, len(mvDiff.IndexChanges), "Should have 1 index change")+ require.Equal(t, schema.MetadataDiffActionCreate, mvDiff.IndexChanges[0].Action)++ // Generate migration+ migration, err := schema.GenerateMigration(storepb.Engine_POSTGRES, diff)+ require.NoError(t, err)+ t.Logf("Generated migration:\n%s", migration)++ // Verify migration only creates the index, doesn't recreate the MV+ require.Contains(t, migration, "CREATE INDEX")+ require.Contains(t, migration, "idx_customer_seg_spending")+ require.NotContains(t, migration, "DROP MATERIALIZED VIEW", "Should not drop MV for index-only changes")+ require.NotContains(t, migration, "CREATE MATERIALIZED VIEW", "Should not recreate MV for index-only changes")+ })+}