Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: support materialized view for pg SDL (#18054)

rebelice788c00049316
diff --git a/backend/plugin/schema/differ.go b/backend/plugin/schema/differ.go
index 29471206bd..a91121567c 100644
--- a/backend/plugin/schema/differ.go
+++ b/backend/plugin/schema/differ.go
@@ -191,6 +191,8 @@ type MaterializedViewDiff struct {
MaterializedViewName string
OldMaterializedView *storepb.MaterializedViewMetadata
NewMaterializedView *storepb.MaterializedViewMetadata
+ OldASTNode any // AST node for old materialized view
+ NewASTNode any // AST node for new materialized view
}
// FunctionDiff represents changes to a function.
@@ -254,13 +256,14 @@ type EventDiff struct {
type CommentObjectType string
const (
- CommentObjectTypeSchema CommentObjectType = "SCHEMA"
- CommentObjectTypeTable CommentObjectType = "TABLE"
- CommentObjectTypeColumn CommentObjectType = "COLUMN"
- CommentObjectTypeView CommentObjectType = "VIEW"
- CommentObjectTypeFunction CommentObjectType = "FUNCTION"
- CommentObjectTypeSequence CommentObjectType = "SEQUENCE"
- CommentObjectTypeIndex CommentObjectType = "INDEX"
+ CommentObjectTypeSchema CommentObjectType = "SCHEMA"
+ CommentObjectTypeTable CommentObjectType = "TABLE"
+ CommentObjectTypeColumn CommentObjectType = "COLUMN"
+ CommentObjectTypeView CommentObjectType = "VIEW"
+ CommentObjectTypeMaterializedView CommentObjectType = "MATERIALIZED VIEW"
+ CommentObjectTypeFunction CommentObjectType = "FUNCTION"
+ CommentObjectTypeSequence CommentObjectType = "SEQUENCE"
+ CommentObjectTypeIndex CommentObjectType = "INDEX"
)
// CommentDiff represents changes to database object comments.
diff --git a/backend/plugin/schema/pg/generate_migration.go b/backend/plugin/schema/pg/generate_migration.go
index dfe7134bb2..ad3f3f356f 100644
--- a/backend/plugin/schema/pg/generate_migration.go
+++ b/backend/plugin/schema/pg/generate_migration.go
@@ -168,13 +168,23 @@ func dropObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) {
// For materialized views depending on tables/views
for mvID, mvDiff := range materializedViewMap {
+ var dependencies []*storepb.DependencyColumn
+
if mvDiff.OldMaterializedView != nil {
- for _, dep := range mvDiff.OldMaterializedView.DependencyColumns {
- depID := getMigrationObjectID(dep.Schema, dep.Table)
- if allObjects[depID] {
- // Edge from dependent to dependency
- graph.AddEdge(mvID, depID)
- }
+ // Use metadata if available
+ dependencies = mvDiff.OldMaterializedView.DependencyColumns
+ } else if mvDiff.OldASTNode != nil {
+ // Extract dependencies from AST node for AST-only mode
+ // Use the temporary metadata containing objects being dropped
+ dependencies = getMaterializedViewDependenciesFromAST(mvDiff.OldASTNode, mvDiff.SchemaName, tempMetadata)
+ }
+
+ for _, dep := range dependencies {
+ depID := getMigrationObjectID(dep.Schema, dep.Table)
+ if allObjects[depID] {
+ // Edge from dependent to dependency (materialized view depends on table/view)
+ // For DROP: mv -> view/table means mv should be dropped before view/table
+ graph.AddEdge(mvID, depID)
}
}
}
@@ -725,10 +735,21 @@ func createObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error
// For materialized views depending on tables/views
for mvID, mvDiff := range materializedViewMap {
+ var dependencies []*storepb.DependencyColumn
+
if mvDiff.NewMaterializedView != nil {
- for _, dep := range mvDiff.NewMaterializedView.DependencyColumns {
- depID := getMigrationObjectID(dep.Schema, dep.Table)
- // Edge from dependency to dependent
+ // Use metadata if available
+ dependencies = mvDiff.NewMaterializedView.DependencyColumns
+ } else if mvDiff.NewASTNode != nil {
+ // Extract dependencies from AST node for AST-only mode
+ // Use the temporary metadata containing objects being created
+ dependencies = getMaterializedViewDependenciesFromAST(mvDiff.NewASTNode, mvDiff.SchemaName, tempMetadata)
+ }
+
+ for _, dep := range dependencies {
+ depID := getMigrationObjectID(dep.Schema, dep.Table)
+ if allObjects[depID] {
+ // Edge from dependency to dependent (table/view to materialized view)
graph.AddEdge(depID, mvID)
}
}
@@ -824,8 +845,17 @@ func createObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error
for _, mvDiff := range materializedViewMap {
switch mvDiff.Action {
case schema.MetadataDiffActionCreate, schema.MetadataDiffActionAlter:
- if err := writeMigrationMaterializedView(buf, mvDiff.SchemaName, mvDiff.NewMaterializedView); err != nil {
- return err
+ // Support AST-only mode: if metadata is nil but AST node exists, use AST
+ 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 diff for %s.%s has neither metadata nor AST node", mvDiff.SchemaName, mvDiff.MaterializedViewName)
}
default:
// No action needed
@@ -940,15 +970,33 @@ func createObjectsInOrder(diff *schema.MetadataDiff, buf *strings.Builder) error
} else if mvDiff, ok := materializedViewMap[objID]; ok {
switch mvDiff.Action {
case schema.MetadataDiffActionCreate:
- if err := writeMigrationMaterializedView(buf, mvDiff.SchemaName, mvDiff.NewMaterializedView); err != nil {
- return err
+ // Support AST-only mode: if metadata is nil but AST node exists, use AST
+ 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 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)
- if err := writeMigrationMaterializedView(buf, mvDiff.SchemaName, mvDiff.NewMaterializedView); err != nil {
- return err
+ // 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)
}
default:
// No action needed for other operations
@@ -2431,6 +2479,49 @@ func writeMigrationFunctionFromASTWithReplace(out *strings.Builder, astNode any)
return nil
}
+// writeMigrationMaterializedViewFromAST writes a CREATE MATERIALIZED VIEW statement from AST node
+func writeMigrationMaterializedViewFromAST(out *strings.Builder, astNode any) error {
+ if astNode == nil {
+ return errors.New("AST node is nil")
+ }
+
+ // Extract the original SQL text from the AST node
+ var mvSQL string
+
+ // Try to cast to PostgreSQL CreatematviewstmtContext first
+ if ctx, ok := astNode.(*pgparser.CreatematviewstmtContext); ok {
+ // First try to get text using token stream
+ if tokenStream := ctx.GetParser().GetTokenStream(); tokenStream != nil {
+ start := ctx.GetStart()
+ stop := ctx.GetStop()
+ if start != nil && stop != nil {
+ mvSQL = tokenStream.GetTextFromTokens(start, stop)
+ }
+ }
+
+ // Fallback to GetText() if token stream approach failed
+ if mvSQL == "" {
+ mvSQL = ctx.GetText()
+ }
+ } else {
+ // Generic fallback - try to get text using token approach first
+ if tree, ok := astNode.(antlr.ParseTree); ok {
+ mvSQL = getTextFromAST(tree)
+ }
+ }
+
+ if mvSQL != "" {
+ _, _ = out.WriteString(mvSQL)
+ if !strings.HasSuffix(strings.TrimSpace(mvSQL), ";") {
+ _, _ = out.WriteString(`;`)
+ }
+ _, _ = out.WriteString("\n\n")
+ return nil
+ }
+
+ return errors.New("failed to extract SQL from materialized view AST node")
+}
+
// writeMaterializedView writes a CREATE MATERIALIZED VIEW statement
func writeMigrationMaterializedView(out *strings.Builder, schema string, view *storepb.MaterializedViewMetadata) error {
_, _ = out.WriteString(`CREATE MATERIALIZED VIEW "`)
@@ -3189,6 +3280,9 @@ func generateCommentChangesFromSDL(buf *strings.Builder, diff *schema.MetadataDi
case schema.CommentObjectTypeView:
writeCommentOnView(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment)
+ case schema.CommentObjectTypeMaterializedView:
+ writeCommentOnMaterializedView(buf, commentDiff.SchemaName, commentDiff.ObjectName, newComment)
+
case schema.CommentObjectTypeFunction:
// For functions, ObjectName contains the function signature
// Try to find the function definition and AST node to determine if it's a FUNCTION or PROCEDURE
@@ -3500,6 +3594,77 @@ func getViewDependenciesFromAST(astNode any, schemaName string, _ *storepb.Datab
return dependencies
}
+// getMaterializedViewDependenciesFromAST extracts table/view dependencies from a materialized view's AST node
+func getMaterializedViewDependenciesFromAST(astNode any, schemaName string, _ *storepb.DatabaseSchemaMetadata) []*storepb.DependencyColumn {
+ if astNode == nil {
+ return []*storepb.DependencyColumn{}
+ }
+
+ var selectStatement string
+
+ if ctx, ok := astNode.(*pgparser.CreatematviewstmtContext); ok {
+ if ctx.Selectstmt() != nil {
+ // Try to get text using token stream first
+ if tokenStream := ctx.GetParser().GetTokenStream(); tokenStream != nil {
+ start := ctx.Selectstmt().GetStart()
+ stop := ctx.Selectstmt().GetStop()
+ if start != nil && stop != nil {
+ selectStatement = tokenStream.GetTextFromTokens(start, stop)
+ }
+ }
+
+ // Fallback to token-based approach if failed
+ if selectStatement == "" {
+ selectStatement = getTextFromAST(ctx.Selectstmt())
+ }
+ }
+ }
+
+ if selectStatement == "" {
+ return []*storepb.DependencyColumn{}
+ }
+
+ queryStatement := strings.TrimSpace(selectStatement)
+
+ accessTables, err := pgpluginparser.ExtractAccessTables(queryStatement, pgpluginparser.ExtractAccessTablesOption{
+ DefaultDatabase: "",
+ DefaultSchema: schemaName,
+ SkipMetadataValidation: true,
+ })
+ if err != nil {
+ return []*storepb.DependencyColumn{}
+ }
+
+ // The caller will filter against allObjects
+ dependencyMap := make(map[string]*storepb.DependencyColumn)
+ for _, resource := range accessTables {
+ if resource.Schema == "pg_catalog" || resource.Schema == "information_schema" {
+ continue
+ }
+
+ resourceSchema := resource.Schema
+ if resourceSchema == "" {
+ resourceSchema = schemaName
+ }
+
+ key := fmt.Sprintf("%s.%s", resourceSchema, resource.Table)
+ if _, exists := dependencyMap[key]; !exists {
+ dependencyMap[key] = &storepb.DependencyColumn{
+ Schema: resourceSchema,
+ Table: resource.Table,
+ Column: "*", // Table-level dependencies
+ }
+ }
+ }
+
+ var dependencies []*storepb.DependencyColumn
+ for _, dep := range dependencyMap {
+ dependencies = append(dependencies, dep)
+ }
+
+ return dependencies
+}
+
// extractColumnMetadataFromAST extracts column metadata from AST node without normalization (except column name)
// This is used in AST-only mode where we need the raw values for comparison
func extractColumnMetadataFromAST(columnDef pgparser.IColumnDefContext) *columnMetadataAST {
diff --git a/backend/plugin/schema/pg/get_database_definition.go b/backend/plugin/schema/pg/get_database_definition.go
index e18f2aa239..5bc275f36d 100644
--- a/backend/plugin/schema/pg/get_database_definition.go
+++ b/backend/plugin/schema/pg/get_database_definition.go
@@ -2409,7 +2409,29 @@ func getSDLFormat(metadata *storepb.DatabaseSchemaMetadata) (string, error) {
}
}
- // Write functions and procedures after views
+ // Write materialized views after views
+ for _, materializedView := range schema.MaterializedViews {
+ if materializedView.SkipDump {
+ continue
+ }
+
+ if err := writeMaterializedViewSDL(&buf, schema.Name, materializedView); err != nil {
+ return "", err
+ }
+
+ if _, err := buf.WriteString(";\n\n"); err != nil {
+ return "", err
+ }
+
+ // Write materialized view comment if present
+ if len(materializedView.Comment) > 0 {
+ if err := writeMaterializedViewCommentSDL(&buf, schema.Name, materializedView); err != nil {
+ return "", err
+ }
+ }
+ }
+
+ // Write functions and procedures after materialized views
for _, function := range schema.Functions {
if function.SkipDump {
continue
@@ -2942,6 +2964,36 @@ func writeViewSDL(out io.Writer, schemaName string, view *storepb.ViewMetadata)
return err
}
+func writeMaterializedViewSDL(out io.Writer, schemaName string, view *storepb.MaterializedViewMetadata) error {
+ if _, err := io.WriteString(out, `CREATE MATERIALIZED VIEW "`); err != nil {
+ return err
+ }
+
+ if _, err := io.WriteString(out, schemaName); err != nil {
+ return err
+ }
+
+ if _, err := io.WriteString(out, `"."`); err != nil {
+ return err
+ }
+
+ if _, err := io.WriteString(out, view.Name); err != nil {
+ return err
+ }
+
+ if _, err := io.WriteString(out, `" AS `); err != nil {
+ return err
+ }
+
+ // The materialized view definition should already include the SELECT statement
+ definition := strings.TrimSpace(view.Definition)
+ // Remove trailing semicolon if present
+ definition = strings.TrimSuffix(definition, ";")
+
+ _, err := io.WriteString(out, definition)
+ return err
+}
+
func writeFunctionSDL(out io.Writer, _ string, function *storepb.FunctionMetadata) error {
// The function definition should already include the complete CREATE FUNCTION statement
definition := strings.TrimSpace(function.Definition)
@@ -3306,6 +3358,32 @@ func GetMultiFileDatabaseDefinition(ctx schema.GetDefinitionContext, metadata *s
})
}
+ // Generate materialized view files
+ for _, materializedView := range schemaMetadata.MaterializedViews {
+ if materializedView.SkipDump {
+ continue
+ }
+
+ var buf strings.Builder
+ if err := writeMaterializedViewSDL(&buf, schemaName, materializedView); err != nil {
+ return nil, errors.Wrapf(err, "failed to generate materialized view SDL for %s.%s", schemaName, materializedView.Name)
+ }
+ buf.WriteString(";\n")
+
+ // Write materialized view comment if present
+ if len(materializedView.Comment) > 0 {
+ buf.WriteString("\n")
+ if err := writeMaterializedViewCommentSDL(&buf, schemaName, materializedView); err != nil {
+ return nil, errors.Wrapf(err, "failed to generate materialized view comment for %s.%s", schemaName, materializedView.Name)
+ }
+ }
+
+ files = append(files, schema.File{
+ Name: fmt.Sprintf("schemas/%s/materialized_views/%s.sql", schemaName, materializedView.Name),
+ Content: buf.String(),
+ })
+ }
+
// Generate function files
for _, function := range schemaMetadata.Functions {
if function.SkipDump {
@@ -3542,6 +3620,33 @@ func writeViewCommentSDL(out io.Writer, schemaName string, view *storepb.ViewMet
return err
}
+// nolint:unused
+func writeMaterializedViewCommentSDL(out io.Writer, schemaName string, view *storepb.MaterializedViewMetadata) error {
+ if _, err := io.WriteString(out, `COMMENT ON MATERIALIZED VIEW "`); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, schemaName); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, `"."`); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, view.Name); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, `" IS '`); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, escapeSingleQuote(view.Comment)); err != nil {
+ return err
+ }
+ if _, err := io.WriteString(out, `';`); err != nil {
+ return err
+ }
+ _, err := io.WriteString(out, "\n\n")
+ return err
+}
+
func writeFunctionCommentSDL(out io.Writer, schemaName string, function *storepb.FunctionMetadata) error {
// Determine if this is a PROCEDURE or FUNCTION by checking the definition
objectType := "FUNCTION"
diff --git a/backend/plugin/schema/pg/get_database_definition_sdl_test.go b/backend/plugin/schema/pg/get_database_definition_sdl_test.go
index 701f030725..9fe929cae5 100644
--- a/backend/plugin/schema/pg/get_database_definition_sdl_test.go
+++ b/backend/plugin/schema/pg/get_database_definition_sdl_test.go
@@ -1564,6 +1564,13 @@ func TestGetMultiFileDatabaseDefinition_WithComments(t *testing.T) {
Comment: "View of active products",
},
},
+ MaterializedViews: []*storepb.MaterializedViewMetadata{
+ {
+ Name: "product_summary_mv",
+ Definition: "SELECT id, name FROM app_schema.products",
+ Comment: "Materialized view of product summary",
+ },
+ },
Functions: []*storepb.FunctionMetadata{
{
Name: "count_products",
@@ -1624,6 +1631,12 @@ func TestGetMultiFileDatabaseDefinition_WithComments(t *testing.T) {
assert.Contains(t, viewFile, `CREATE VIEW "app_schema"."active_products"`)
assert.Contains(t, viewFile, `COMMENT ON VIEW "app_schema"."active_products" IS 'View of active products';`)
+ // Verify materialized view file with comment
+ materializedViewFile, ok := fileMap["schemas/app_schema/materialized_views/product_summary_mv.sql"]
+ require.True(t, ok, "materialized view file should exist")
+ assert.Contains(t, materializedViewFile, `CREATE MATERIALIZED VIEW "app_schema"."product_summary_mv"`)
+ assert.Contains(t, materializedViewFile, `COMMENT ON MATERIALIZED VIEW "app_schema"."product_summary_mv" IS 'Materialized view of product summary';`)
+
// Verify function file with comment
functionFile, ok := fileMap["schemas/app_schema/functions/count_products.sql"]
require.True(t, ok, "function file should exist")
diff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.go
index f7bfd79c71..947f343fc1 100644
--- a/backend/plugin/schema/pg/get_sdl_diff.go
+++ b/backend/plugin/schema/pg/get_sdl_diff.go
@@ -91,6 +91,9 @@ func GetSDLDiff(currentSDLText, previousUserSDLText string, currentSchema, previ
// Process view changes
processViewChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)
+ // Process materialized view changes
+ processMaterializedViewChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)
+
// Process function changes
processFunctionChanges(currentChunks, previousChunks, currentDBSDLChunks, diff)
@@ -113,14 +116,15 @@ func GetSDLDiff(currentSDLText, previousUserSDLText string, currentSchema, previ
func ChunkSDLText(sdlText string) (*schema.SDLChunks, error) {
if strings.TrimSpace(sdlText) == "" {
return &schema.SDLChunks{
- Tables: make(map[string]*schema.SDLChunk),
- Views: make(map[string]*schema.SDLChunk),
- Functions: make(map[string]*schema.SDLChunk),
- Indexes: make(map[string]*schema.SDLChunk),
- Sequences: make(map[string]*schema.SDLChunk),
- Schemas: make(map[string]*schema.SDLChunk),
- ColumnComments: make(map[string]map[string]antlr.ParserRuleContext),
- IndexComments: make(map[string]map[string]antlr.ParserRuleContext),
+ Tables: make(map[string]*schema.SDLChunk),
+ Views: make(map[string]*schema.SDLChunk),
+ MaterializedViews: make(map[string]*schema.SDLChunk),
+ Functions: make(map[string]*schema.SDLChunk),
+ Indexes: make(map[string]*schema.SDLChunk),
+ Sequences: make(map[string]*schema.SDLChunk),
+ Schemas: make(map[string]*schema.SDLChunk),
+ ColumnComments: make(map[string]map[string]antlr.ParserRuleContext),
+ IndexComments: make(map[string]map[string]antlr.ParserRuleContext),
}, nil
}
@@ -132,14 +136,15 @@ func ChunkSDLText(sdlText string) (*schema.SDLChunks, error) {
extractor := &sdlChunkExtractor{
sdlText: sdlText,
chunks: &schema.SDLChunks{
- Tables: make(map[string]*schema.SDLChunk),
- Views: make(map[string]*schema.SDLChunk),
- Functions: make(map[string]*schema.SDLChunk),
- Indexes: make(map[string]*schema.SDLChunk),
- Sequences: make(map[string]*schema.SDLChunk),
- Schemas: make(map[string]*schema.SDLChunk),
- ColumnComments: make(map[string]map[string]antlr.ParserRuleContext),
- IndexComments: make(map[string]map[string]antlr.ParserRuleContext),
+ Tables: make(map[string]*schema.SDLChunk),
+ Views: make(map[string]*schema.SDLChunk),
+ MaterializedViews: make(map[string]*schema.SDLChunk),
+ Functions: make(map[string]*schema.SDLChunk),
+ Indexes: make(map[string]*schema.SDLChunk),
+ Sequences: make(map[string]*schema.SDLChunk),
+ Schemas: make(map[string]*schema.SDLChunk),
+ ColumnComments: make(map[string]map[string]antlr.ParserRuleContext),
+ IndexComments: make(map[string]map[string]antlr.ParserRuleContext),
},
tokens: parseResult.Tokens,
}
@@ -336,6 +341,32 @@ func (l *sdlChunkExtractor) EnterViewstmt(ctx *parser.ViewstmtContext) {
l.chunks.Views[schemaQualifiedName] = chunk
}
+// EnterCreatematviewstmt handles CREATE MATERIALIZED VIEW statements
+func (l *sdlChunkExtractor) EnterCreatematviewstmt(ctx *parser.CreatematviewstmtContext) {
+ // Extract materialized view name from create_mv_target
+ if ctx.Create_mv_target() == nil || ctx.Create_mv_target().Qualified_name() == nil {
+ return
+ }
+
+ identifier := pgparser.NormalizePostgreSQLQualifiedName(ctx.Create_mv_target().Qualified_name())
+ identifierStr := strings.Join(identifier, ".")
+
+ // Ensure schema.materializedViewName format
+ var schemaQualifiedName string
+ if strings.Contains(identifierStr, ".") {
+ schemaQualifiedName = identifierStr
+ } else {
+ schemaQualifiedName = "public." + identifierStr
+ }
+
+ chunk := &schema.SDLChunk{
+ Identifier: schemaQualifiedName,
+ ASTNode: ctx,
+ }
+
+ l.chunks.MaterializedViews[schemaQualifiedName] = chunk
+}
+
func (l *sdlChunkExtractor) EnterCreateschemastmt(ctx *parser.CreateschemastmtContext) {
// Extract schema name
var schemaName string
@@ -457,6 +488,10 @@ func (l *sdlChunkExtractor) EnterCommentstmt(ctx *parser.CommentstmtContext) {
objectType := ctx.Object_type_any_name().GetText()
anyName := pgparser.NormalizePostgreSQLAnyName(ctx.Any_name())
+ // Debug: log the object type to understand what we're getting
+ // This will help us identify the issue with MATERIALIZED VIEW comments
+ _ = objectType // Prevent unused variable error in production
+
var identifier string
if len(anyName) >= 2 {
// Format: schema.object
@@ -469,7 +504,9 @@ func (l *sdlChunkExtractor) EnterCommentstmt(ctx *parser.CommentstmtContext) {
}
// Route to appropriate chunk map based on object type
- switch strings.ToUpper(objectType) {
+ // Note: For "MATERIALIZED VIEW", the parser may return "MATERIALIZEDVIEW" (no space)
+ objectTypeUpper := strings.ToUpper(objectType)
+ switch objectTypeUpper {
case "TABLE":
if chunk, exists := l.chunks.Tables[identifier]; exists {
chunk.CommentStatements = append(chunk.CommentStatements, ctx)
@@ -492,6 +529,19 @@ func (l *sdlChunkExtractor) EnterCommentstmt(ctx *parser.CommentstmtContext) {
}
l.chunks.Views[identifier] = chunk
}
+ case "MATERIALIZED VIEW", "MATERIALIZEDVIEW":
+ // Handle both "MATERIALIZED VIEW" (with space) and "MATERIALIZEDVIEW" (no space)
+ // The parser may return either depending on the grammar definition
+ if chunk, exists := l.chunks.MaterializedViews[identifier]; exists {
+ chunk.CommentStatements = append(chunk.CommentStatements, ctx)
+ } else {
+ chunk := &schema.SDLChunk{
+ Identifier: identifier,
+ ASTNode: nil,
+ CommentStatements: []antlr.ParserRuleContext{ctx},
+ }
+ l.chunks.MaterializedViews[identifier] = chunk
+ }
case "SEQUENCE":
if chunk, exists := l.chunks.Sequences[identifier]; exists {
chunk.CommentStatements = append(chunk.CommentStatements, ctx)
@@ -1887,6 +1937,108 @@ func processViewChanges(currentChunks, previousChunks *schema.SDLChunks, current
}
}
+// processMaterializedViewChanges analyzes materialized view changes between current and previous chunks
+// Following the same pattern as processViewChanges
+// Note: Materialized views use DROP + CREATE pattern (no ALTER support in PostgreSQL)
+func processMaterializedViewChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {
+ // Process current materialized views to find created and modified materialized views
+ for _, currentChunk := range currentChunks.MaterializedViews {
+ if previousChunk, exists := previousChunks.MaterializedViews[currentChunk.Identifier]; exists {
+ // Materialized view exists in both - check if modified by comparing text first (excluding comments)
+ currentText := currentChunk.GetTextWithoutComments()
+ previousText := previousChunk.GetTextWithoutComments()
+ if currentText != previousText {
+ // Apply usability check: skip diff if current chunk matches database metadata SDL
+ if currentDBSDLChunks.shouldSkipChunkDiffForUsability(currentText, currentChunk.Identifier) {
+ continue
+ }
+ // Materialized view was modified - use drop and recreate pattern (PostgreSQL doesn't support ALTER MATERIALIZED VIEW definition)
+ schemaName, mvName := parseIdentifier(currentChunk.Identifier)
+ diff.MaterializedViewChanges = append(diff.MaterializedViewChanges, &schema.MaterializedViewDiff{
+ Action: schema.MetadataDiffActionDrop,
+ SchemaName: schemaName,
+ MaterializedViewName: mvName,
+ OldMaterializedView: nil, // Will be populated when SDL drift detection is implemented
+ NewMaterializedView: nil,
+ OldASTNode: previousChunk.ASTNode,
+ NewASTNode: nil,
+ })
+ diff.MaterializedViewChanges = append(diff.MaterializedViewChanges, &schema.MaterializedViewDiff{
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: schemaName,
+ MaterializedViewName: mvName,
+ OldMaterializedView: nil, // Will be populated when SDL drift detection is implemented
+ NewMaterializedView: nil, // Will be populated when SDL drift detection is implemented
+ OldASTNode: nil,
+ NewASTNode: currentChunk.ASTNode,
+ })
+ // Add COMMENT ON MATERIALIZED VIEW diffs if they exist
+ if len(currentChunk.CommentStatements) > 0 {
+ for _, commentNode := range currentChunk.CommentStatements {
+ commentText := extractCommentTextFromNode(commentNode)
+ diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{
+ Action: schema.MetadataDiffActionCreate,
+ ObjectType: schema.CommentObjectTypeMaterializedView,
+ SchemaName: schemaName,
+ ObjectName: mvName,
+ OldComment: "",
+ NewComment: commentText,
+ OldASTNode: nil,
+ NewASTNode: commentNode,
+ })
+ }
+ }
+ }
+ // If text is identical, skip - no changes detected
+ } else {
+ // New materialized view
+ schemaName, mvName := parseIdentifier(currentChunk.Identifier)
+ diff.MaterializedViewChanges = append(diff.MaterializedViewChanges, &schema.MaterializedViewDiff{
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: schemaName,
+ MaterializedViewName: mvName,
+ OldMaterializedView: nil,
+ NewMaterializedView: nil, // Will be populated when SDL drift detection is implemented
+ OldASTNode: nil,
+ NewASTNode: currentChunk.ASTNode,
+ })
+ // Add COMMENT ON MATERIALIZED VIEW diffs if they exist
+ if len(currentChunk.CommentStatements) > 0 {
+ for _, commentNode := range currentChunk.CommentStatements {
+ commentText := extractCommentTextFromNode(commentNode)
+ diff.CommentChanges = append(diff.CommentChanges, &schema.CommentDiff{
+ Action: schema.MetadataDiffActionCreate,
+ ObjectType: schema.CommentObjectTypeMaterializedView,
+ SchemaName: schemaName,
+ ObjectName: mvName,
+ OldComment: "",
+ NewComment: commentText,
+ OldASTNode: nil,
+ NewASTNode: commentNode,
+ })
+ }
+ }
+ }
+ }
+
+ // Process previous materialized views to find dropped ones
+ for identifier, previousChunk := range previousChunks.MaterializedViews {
+ if _, exists := currentChunks.MaterializedViews[identifier]; !exists {
+ // Materialized view was dropped
+ schemaName, mvName := parseIdentifier(identifier)
+ diff.MaterializedViewChanges = append(diff.MaterializedViewChanges, &schema.MaterializedViewDiff{
+ Action: schema.MetadataDiffActionDrop,
+ SchemaName: schemaName,
+ MaterializedViewName: mvName,
+ OldMaterializedView: nil, // Will be populated when SDL drift detection is implemented
+ NewMaterializedView: nil,
+ OldASTNode: previousChunk.ASTNode,
+ NewASTNode: nil,
+ })
+ }
+ }
+}
+
// processFunctionChanges analyzes function changes between current and previous chunks
// Following the text-first comparison pattern for performance optimization
func processFunctionChanges(currentChunks, previousChunks *schema.SDLChunks, currentDBSDLChunks *currentDatabaseSDLChunks, diff *schema.MetadataDiff) {
@@ -2348,6 +2500,12 @@ func applyMinimalChangesToChunks(previousChunks *schema.SDLChunks, currentSchema
return errors.Wrap(err, "failed to apply view changes")
}
+ // Process materialized view changes: apply minimal changes to materialized view chunks
+ err = applyMaterializedViewChangesToChunks(previousChunks, currentSchema, previousSchema)
+ if err != nil {
+ return errors.Wrap(err, "failed to apply materialized view changes")
+ }
+
// Process column comment changes: sync column comments based on metadata
err = applyColumnCommentChanges(previousChunks, currentSchema, previousSchema)
if err != nil {
@@ -2977,6 +3135,14 @@ func buildCurrentDatabaseSDLChunks(currentSchema *model.DatabaseSchema) (*curren
sdlChunks.comments[identifier] = []string{commentText}
}
}
+ for identifier, chunk := range currentSDLChunks.MaterializedViews {
+ sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetTextWithoutComments())
+ // Store comment text for usability check
+ commentText := extractCommentTextFromChunk(chunk)
+ if commentText != "" {
+ sdlChunks.comments[identifier] = []string{commentText}
+ }
+ }
for identifier, chunk := range currentSDLChunks.Functions {
sdlChunks.chunks[identifier] = strings.TrimSpace(chunk.GetTextWithoutComments())
// Store comment text for usability check
@@ -4828,6 +4994,7 @@ func processCommentChanges(currentChunks, previousChunks *schema.SDLChunks, curr
// Process object-level comments
processObjectComments(currentChunks.Tables, previousChunks.Tables, schema.CommentObjectTypeTable, createdObjects, droppedObjects, currentDBSDLChunks, diff)
processObjectComments(currentChunks.Views, previousChunks.Views, schema.CommentObjectTypeView, createdObjects, droppedObjects, currentDBSDLChunks, diff)
+ processObjectComments(currentChunks.MaterializedViews, previousChunks.MaterializedViews, schema.CommentObjectTypeMaterializedView, createdObjects, droppedObjects, currentDBSDLChunks, diff)
processObjectComments(currentChunks.Functions, previousChunks.Functions, schema.CommentObjectTypeFunction, createdObjects, droppedObjects, currentDBSDLChunks, diff)
processObjectComments(currentChunks.Sequences, previousChunks.Sequences, schema.CommentObjectTypeSequence, createdObjects, droppedObjects, currentDBSDLChunks, diff)
processObjectComments(currentChunks.Indexes, previousChunks.Indexes, schema.CommentObjectTypeIndex, createdObjects, droppedObjects, currentDBSDLChunks, diff)
@@ -4855,6 +5022,13 @@ func buildCreatedObjectsSet(diff *schema.MetadataDiff) map[string]bool {
}
}
+ for _, mvDiff := range diff.MaterializedViewChanges {
+ if mvDiff.Action == schema.MetadataDiffActionCreate {
+ identifier := mvDiff.SchemaName + "." + mvDiff.MaterializedViewName
+ created[identifier] = true
+ }
+ }
+
for _, funcDiff := range diff.FunctionChanges {
if funcDiff.Action == schema.MetadataDiffActionCreate {
identifier := funcDiff.SchemaName + "." + funcDiff.FunctionName
@@ -4890,6 +5064,13 @@ func buildDroppedObjectsSet(diff *schema.MetadataDiff) map[string]bool {
}
}
+ for _, mvDiff := range diff.MaterializedViewChanges {
+ if mvDiff.Action == schema.MetadataDiffActionDrop {
+ identifier := mvDiff.SchemaName + "." + mvDiff.MaterializedViewName
+ dropped[identifier] = true
+ }
+ }
+
for _, funcDiff := range diff.FunctionChanges {
if funcDiff.Action == schema.MetadataDiffActionDrop {
identifier := funcDiff.SchemaName + "." + funcDiff.FunctionName
@@ -5332,6 +5513,248 @@ func (e *viewExtractor) EnterViewstmt(ctx *parser.ViewstmtContext) {
}
}
+// applyMaterializedViewChangesToChunks applies minimal changes to materialized view chunks based on schema metadata
+func applyMaterializedViewChangesToChunks(previousChunks *schema.SDLChunks, currentSchema, previousSchema *model.DatabaseSchema) error {
+ if currentSchema == nil || previousSchema == nil || previousChunks == nil {
+ return nil
+ }
+
+ // Get materialized view differences by comparing schema metadata
+ currentMetadata := currentSchema.GetMetadata()
+ previousMetadata := previousSchema.GetMetadata()
+ if currentMetadata == nil || previousMetadata == nil {
+ return nil
+ }
+
+ // Build materialized view maps for current and previous schemas
+ currentMaterializedViews := make(map[string]*storepb.MaterializedViewMetadata)
+ previousMaterializedViews := make(map[string]*storepb.MaterializedViewMetadata)
+
+ // Collect all materialized views from current schema
+ for _, schema := range currentMetadata.Schemas {
+ for _, mv := range schema.MaterializedViews {
+ mvKey := formatViewKey(schema.Name, mv.Name)
+ currentMaterializedViews[mvKey] = mv
+ }
+ }
+
+ // Collect all materialized views from previous schema
+ for _, schema := range previousMetadata.Schemas {
+ for _, mv := range schema.MaterializedViews {
+ mvKey := formatViewKey(schema.Name, mv.Name)
+ previousMaterializedViews[mvKey] = mv
+ }
+ }
+
+ // Process materialized view additions: create new materialized view chunks
+ for mvKey, currentMV := range currentMaterializedViews {
+ if _, exists := previousMaterializedViews[mvKey]; !exists {
+ // New materialized view - create a chunk for it
+ err := createMaterializedViewChunk(previousChunks, currentMV, mvKey)
+ if err != nil {
+ return errors.Wrapf(err, "failed to create materialized view chunk for %s", mvKey)
+ }
+ }
+ }
+
+ // Process materialized view modifications: update existing chunks
+ for mvKey, currentMV := range currentMaterializedViews {
+ if previousMV, exists := previousMaterializedViews[mvKey]; exists {
+ // Materialized view exists in both metadata
+ // Only update if chunk exists in SDL (user explicitly defined it)
+ if _, chunkExists := previousChunks.MaterializedViews[mvKey]; chunkExists {
+ // Chunk exists - update if needed
+ err := updateMaterializedViewChunkIfNeeded(previousChunks, currentMV, previousMV, mvKey)
+ if err != nil {
+ return errors.Wrapf(err, "failed to update materialized view chunk for %s", mvKey)
+ }
+ }
+ // If chunk doesn't exist, skip - user didn't define this materialized view in SDL
+ }
+ }
+
+ // Process materialized view deletions: remove dropped materialized view chunks
+ for mvKey := range previousMaterializedViews {
+ if _, exists := currentMaterializedViews[mvKey]; !exists {
+ // Materialized view was dropped - remove it from chunks
+ deleteMaterializedViewChunk(previousChunks, mvKey)
+ }
+ }
+
+ return nil
+}
+
+// createMaterializedViewChunk creates a new CREATE MATERIALIZED VIEW chunk and adds it to the chunks
+func createMaterializedViewChunk(chunks *schema.SDLChunks, mv *storepb.MaterializedViewMetadata, mvKey string) error {
+ if mv == nil || chunks == nil {
+ return nil
+ }
+
+ // Generate SDL text for the materialized view
+ schemaName, _ := parseIdentifier(mvKey)
+ mvSDL := generateCreateMaterializedViewSDL(schemaName, mv)
+ if mvSDL == "" {
+ return errors.New("failed to generate SDL for materialized view")
+ }
+
+ // Parse the SDL to get AST node
+ parseResult, err := pgparser.ParsePostgreSQL(mvSDL)
+ if err != nil {
+ return errors.Wrapf(err, "failed to parse generated materialized view SDL: %s", mvSDL)
+ }
+
+ // Extract the CREATE MATERIALIZED VIEW AST node
+ var mvASTNode *parser.CreatematviewstmtContext
+ antlr.ParseTreeWalkerDefault.Walk(&materializedViewExtractor{
+ result: &mvASTNode,
+ }, parseResult.Tree)
+
+ if mvASTNode == nil {
+ return errors.New("failed to extract CREATE MATERIALIZED VIEW AST node")
+ }
+
+ // Create and add the chunk
+ chunk := &schema.SDLChunk{
+ Identifier: mvKey,
+ ASTNode: mvASTNode,
+ }
+
+ // Add comment if the materialized view has one
+ if mv.Comment != "" {
+ commentSQL := generateCommentOnMaterializedViewSQL(schemaName, mv.Name, mv.Comment)
+ commentParseResult, err := pgparser.ParsePostgreSQL(commentSQL)
+ if err == nil && commentParseResult.Tree != nil {
+ // Extract COMMENT ON MATERIALIZED VIEW AST node
+ var commentASTNode *parser.CommentstmtContext
+ antlr.ParseTreeWalkerDefault.Walk(&commentExtractor{
+ result: &commentASTNode,
+ }, commentParseResult.Tree)
+
+ if commentASTNode != nil {
+ chunk.CommentStatements = []antlr.ParserRuleContext{commentASTNode}
+ }
+ }
+ }
+
+ if chunks.MaterializedViews == nil {
+ chunks.MaterializedViews = make(map[string]*schema.SDLChunk)
+ }
+ chunks.MaterializedViews[mvKey] = chunk
+
+ return nil
+}
+
+// updateMaterializedViewChunkIfNeeded updates an existing materialized view chunk if the definition has changed
+func updateMaterializedViewChunkIfNeeded(chunks *schema.SDLChunks, currentMV, previousMV *storepb.MaterializedViewMetadata, mvKey string) error {
+ if currentMV == nil || previousMV == nil || chunks == nil {
+ return nil
+ }
+
+ // Get the existing chunk
+ chunk, exists := chunks.MaterializedViews[mvKey]
+ if !exists {
+ return errors.Errorf("materialized view chunk not found for key %s", mvKey)
+ }
+
+ // Check if the materialized view definition has changed
+ if currentMV.Definition != previousMV.Definition {
+ // Materialized view definition changed - regenerate the chunk
+ schemaName, _ := parseIdentifier(mvKey)
+ mvSDL := generateCreateMaterializedViewSDL(schemaName, currentMV)
+ if mvSDL == "" {
+ return errors.New("failed to generate SDL for materialized view")
+ }
+
+ // Parse the new SDL to get a fresh AST node
+ parseResult, err := pgparser.ParsePostgreSQL(mvSDL)
+ if err != nil {
+ return errors.Wrapf(err, "failed to parse generated materialized view SDL: %s", mvSDL)
+ }
+
+ // Extract the CREATE MATERIALIZED VIEW AST node
+ var mvASTNode *parser.CreatematviewstmtContext
+ antlr.ParseTreeWalkerDefault.Walk(&materializedViewExtractor{
+ result: &mvASTNode,
+ }, parseResult.Tree)
+
+ if mvASTNode == nil {
+ return errors.New("failed to extract CREATE MATERIALIZED VIEW AST node")
+ }
+
+ // Update the chunk's AST node
+ chunk.ASTNode = mvASTNode
+ }
+
+ // Handle comment changes independently of definition changes
+ if currentMV.Comment != previousMV.Comment {
+ schemaName, _ := parseIdentifier(mvKey)
+ if currentMV.Comment != "" {
+ // New or updated comment
+ commentSQL := generateCommentOnMaterializedViewSQL(schemaName, currentMV.Name, currentMV.Comment)
+ commentParseResult, err := pgparser.ParsePostgreSQL(commentSQL)
+ if err == nil && commentParseResult.Tree != nil {
+ // Extract COMMENT ON MATERIALIZED VIEW AST node
+ var commentASTNode *parser.CommentstmtContext
+ antlr.ParseTreeWalkerDefault.Walk(&commentExtractor{
+ result: &commentASTNode,
+ }, commentParseResult.Tree)
+
+ if commentASTNode != nil {
+ chunk.CommentStatements = []antlr.ParserRuleContext{commentASTNode}
+ }
+ }
+ } else {
+ // Comment was removed
+ chunk.CommentStatements = nil
+ }
+ }
+
+ return nil
+}
+
+// deleteMaterializedViewChunk removes a materialized view chunk from the chunks
+func deleteMaterializedViewChunk(chunks *schema.SDLChunks, mvKey string) {
+ if chunks != nil && chunks.MaterializedViews != nil {
+ delete(chunks.MaterializedViews, mvKey)
+ }
+}
+
+// generateCreateMaterializedViewSDL generates the SDL text for a CREATE MATERIALIZED VIEW statement
+func generateCreateMaterializedViewSDL(schemaName string, mv *storepb.MaterializedViewMetadata) string {
+ if mv == nil {
+ return ""
+ }
+
+ var buf strings.Builder
+ if err := writeMaterializedViewSDL(&buf, schemaName, mv); err != nil {
+ return ""
+ }
+
+ return buf.String()
+}
+
+// generateCommentOnMaterializedViewSQL generates a COMMENT ON MATERIALIZED VIEW statement
+func generateCommentOnMaterializedViewSQL(schemaName, mvName, comment string) string {
+ if schemaName == "" {
+ schemaName = "public"
+ }
+ // Escape single quotes in comment
+ escapedComment := strings.ReplaceAll(comment, "'", "''")
+ return fmt.Sprintf("COMMENT ON MATERIALIZED VIEW \"%s\".\"%s\" IS '%s';", schemaName, mvName, escapedComment)
+}
+
+// materializedViewExtractor is a walker to extract CREATE MATERIALIZED VIEW AST nodes
+type materializedViewExtractor struct {
+ parser.BasePostgreSQLParserListener
+ result **parser.CreatematviewstmtContext
+}
+
+func (e *materializedViewExtractor) EnterCreatematviewstmt(ctx *parser.CreatematviewstmtContext) {
+ if e.result != nil && *e.result == nil {
+ *e.result = ctx
+ }
+}
+
// applyColumnCommentChanges applies minimal changes to column comments based on schema metadata
// This function only updates COMMENT ON COLUMN statements without modifying CREATE TABLE statements
func applyColumnCommentChanges(previousChunks *schema.SDLChunks, currentSchema, previousSchema *model.DatabaseSchema) error {
diff --git a/backend/plugin/schema/pg/get_sdl_diff_test.go b/backend/plugin/schema/pg/get_sdl_diff_test.go
index e9769de5ee..78accd3eb1 100644
--- a/backend/plugin/schema/pg/get_sdl_diff_test.go
+++ b/backend/plugin/schema/pg/get_sdl_diff_test.go
@@ -227,6 +227,54 @@ func TestConvertDatabaseSchemaToSDL(t *testing.T) {
expectedSDL: "CREATE TABLE users (\n id integer NOT NULL,\n name text NOT NULL\n);",
expectedError: false,
},
+ {
+ name: "schema_with_materialized_view",
+ dbSchema: model.NewDatabaseSchema(
+ &storepb.DatabaseSchemaMetadata{
+ Name: "test_db",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "products",
+ Columns: []*storepb.ColumnMetadata{
+ {
+ Name: "id",
+ Type: "integer",
+ Nullable: false,
+ },
+ {
+ Name: "name",
+ Type: "text",
+ Nullable: false,
+ },
+ {
+ Name: "price",
+ Type: "numeric",
+ Nullable: false,
+ },
+ },
+ },
+ },
+ MaterializedViews: []*storepb.MaterializedViewMetadata{
+ {
+ Name: "product_summary_mv",
+ Definition: "SELECT id, name, price FROM products",
+ Comment: "Summary of products",
+ },
+ },
+ },
+ },
+ },
+ nil,
+ nil,
+ storepb.Engine_POSTGRES,
+ false,
+ ),
+ expectedSDL: "CREATE MATERIALIZED VIEW",
+ expectedError: false,
+ },
}
for _, tt := range tests {
@@ -237,11 +285,18 @@ func TestConvertDatabaseSchemaToSDL(t *testing.T) {
require.Error(t, err)
} else {
require.NoError(t, err)
- if tt.expectedSDL != "" {
- // For non-empty expected SDL, check that result contains expected structure
+ switch tt.expectedSDL {
+ case "":
+ require.Equal(t, tt.expectedSDL, result)
+ case "CREATE TABLE users (\n id integer NOT NULL,\n name text NOT NULL\n);":
require.Contains(t, result, "CREATE TABLE")
require.Contains(t, result, "users")
- } else {
+ case "CREATE MATERIALIZED VIEW":
+ require.Contains(t, result, "CREATE MATERIALIZED VIEW")
+ require.Contains(t, result, "product_summary_mv")
+ require.Contains(t, result, "COMMENT ON MATERIALIZED VIEW")
+ require.Contains(t, result, "Summary of products")
+ default:
require.Equal(t, tt.expectedSDL, result)
}
}
@@ -4355,6 +4410,295 @@ COMMENT ON VIEW public.active_users IS 'Old comment';`,
}
}
+func TestApplyMaterializedViewChangesToChunks_CommentDrift(t *testing.T) {
+ tests := []struct {
+ name string
+ previousUserSDLText string
+ currentSchema *model.DatabaseSchema
+ previousSchema *model.DatabaseSchema
+ expectedCommentStatements int
+ expectedCommentText string
+ }{
+ {
+ name: "materialized_view_comment_changed_but_definition_unchanged",
+ previousUserSDLText: `CREATE TABLE public.users (
+ id INTEGER PRIMARY KEY,
+ email TEXT NOT NULL
+);
+
+CREATE MATERIALIZED VIEW public.user_emails_mv AS
+SELECT id, email FROM users;
+
+COMMENT ON MATERIALIZED VIEW public.user_emails_mv IS 'Old comment';`,
+ currentSchema: model.NewDatabaseSchema(
+ &storepb.DatabaseSchemaMetadata{
+ Name: "test_db",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "email", Type: "text", Nullable: false},
+ },
+ },
+ },
+ MaterializedViews: []*storepb.MaterializedViewMetadata{
+ {
+ Name: "user_emails_mv",
+ Definition: "SELECT id, email FROM users",
+ Comment: "New comment", // Changed from "Old comment"
+ },
+ },
+ },
+ },
+ },
+ nil,
+ nil,
+ storepb.Engine_POSTGRES,
+ false,
+ ),
+ previousSchema: model.NewDatabaseSchema(
+ &storepb.DatabaseSchemaMetadata{
+ Name: "test_db",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "email", Type: "text", Nullable: false},
+ },
+ },
+ },
+ MaterializedViews: []*storepb.MaterializedViewMetadata{
+ {
+ Name: "user_emails_mv",
+ Definition: "SELECT id, email FROM users",
+ Comment: "Old comment",
+ },
+ },
+ },
+ },
+ },
+ nil,
+ nil,
+ storepb.Engine_POSTGRES,
+ false,
+ ),
+ expectedCommentStatements: 1,
+ expectedCommentText: "New comment",
+ },
+ {
+ name: "materialized_view_comment_added_when_previously_none",
+ previousUserSDLText: `CREATE TABLE public.users (
+ id INTEGER PRIMARY KEY,
+ role TEXT
+);
+
+CREATE MATERIALIZED VIEW public.admin_users_mv AS
+SELECT * FROM users WHERE role = 'admin';`,
+ currentSchema: model.NewDatabaseSchema(
+ &storepb.DatabaseSchemaMetadata{
+ Name: "test_db",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "role", Type: "text"},
+ },
+ },
+ },
+ MaterializedViews: []*storepb.MaterializedViewMetadata{
+ {
+ Name: "admin_users_mv",
+ Definition: "SELECT * FROM users WHERE role = 'admin'",
+ Comment: "Materialized view of admin users", // Added
+ },
+ },
+ },
+ },
+ },
+ nil,
+ nil,
+ storepb.Engine_POSTGRES,
+ false,
+ ),
+ previousSchema: model.NewDatabaseSchema(
+ &storepb.DatabaseSchemaMetadata{
+ Name: "test_db",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "role", Type: "text"},
+ },
+ },
+ },
+ MaterializedViews: []*storepb.MaterializedViewMetadata{
+ {
+ Name: "admin_users_mv",
+ Definition: "SELECT * FROM users WHERE role = 'admin'",
+ Comment: "", // No comment before
+ },
+ },
+ },
+ },
+ },
+ nil,
+ nil,
+ storepb.Engine_POSTGRES,
+ false,
+ ),
+ expectedCommentStatements: 1,
+ expectedCommentText: "Materialized view of admin users",
+ },
+ {
+ name: "materialized_view_comment_removed",
+ previousUserSDLText: `CREATE TABLE public.users (
+ id INTEGER PRIMARY KEY,
+ active BOOLEAN
+);
+
+CREATE MATERIALIZED VIEW public.active_users_mv AS
+SELECT * FROM users WHERE active = true;
+
+COMMENT ON MATERIALIZED VIEW public.active_users_mv IS 'Old comment';`,
+ currentSchema: model.NewDatabaseSchema(
+ &storepb.DatabaseSchemaMetadata{
+ Name: "test_db",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "active", Type: "boolean"},
+ },
+ },
+ },
+ MaterializedViews: []*storepb.MaterializedViewMetadata{
+ {
+ Name: "active_users_mv",
+ Definition: "SELECT * FROM users WHERE active = true",
+ Comment: "", // Comment removed
+ },
+ },
+ },
+ },
+ },
+ nil,
+ nil,
+ storepb.Engine_POSTGRES,
+ false,
+ ),
+ previousSchema: model.NewDatabaseSchema(
+ &storepb.DatabaseSchemaMetadata{
+ Name: "test_db",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "users",
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "id", Type: "integer"},
+ {Name: "active", Type: "boolean"},
+ },
+ },
+ },
+ MaterializedViews: []*storepb.MaterializedViewMetadata{
+ {
+ Name: "active_users_mv",
+ Definition: "SELECT * FROM users WHERE active = true",
+ Comment: "Old comment",
+ },
+ },
+ },
+ },
+ },
+ nil,
+ nil,
+ storepb.Engine_POSTGRES,
+ false,
+ ),
+ expectedCommentStatements: 0,
+ expectedCommentText: "",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ // Parse previous SDL
+ previousChunks, err := ChunkSDLText(tt.previousUserSDLText)
+ require.NoError(t, err)
+ require.NotNil(t, previousChunks)
+
+ // Log original chunks
+ t.Log("=== Previous SDL Chunks (Before Drift Handling) ===")
+ for identifier, chunk := range previousChunks.MaterializedViews {
+ t.Logf("Materialized View %s:", identifier)
+ t.Logf(" Text: %s", chunk.GetText())
+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))
+ }
+
+ // Apply drift handling
+ err = applyMaterializedViewChangesToChunks(previousChunks, tt.currentSchema, tt.previousSchema)
+ require.NoError(t, err)
+
+ // Log chunks after drift handling
+ t.Log("=== Previous SDL Chunks (After Drift Handling) ===")
+ for identifier, chunk := range previousChunks.MaterializedViews {
+ t.Logf("Materialized View %s:", identifier)
+ t.Logf(" Text: %s", chunk.GetText())
+ t.Logf(" CommentStatements count: %d", len(chunk.CommentStatements))
+ }
+
+ // Get the materialized view chunk
+ var mvChunk *schema.SDLChunk
+ for _, chunk := range previousChunks.MaterializedViews {
+ mvChunk = chunk
+ break
+ }
+ require.NotNil(t, mvChunk, "Materialized view chunk should exist after drift handling")
+
+ // Get full text
+ fullText := mvChunk.GetText()
+ t.Logf("Full materialized view text:\n%s", fullText)
+
+ // Verify COMMENT statements count
+ assert.Equal(t, tt.expectedCommentStatements, len(mvChunk.CommentStatements),
+ "COMMENT statements count should match expected")
+
+ // Verify COMMENT content
+ if tt.expectedCommentStatements > 0 {
+ assert.Contains(t, fullText, "COMMENT ON MATERIALIZED VIEW",
+ "Full text should contain COMMENT ON MATERIALIZED VIEW statement")
+ assert.Contains(t, fullText, tt.expectedCommentText,
+ "COMMENT should contain the expected text: %s", tt.expectedCommentText)
+ } else {
+ // Should NOT contain COMMENT ON MATERIALIZED VIEW
+ assert.NotContains(t, fullText, "COMMENT ON MATERIALIZED VIEW",
+ "Full text should NOT contain COMMENT ON MATERIALIZED VIEW when comment is removed")
+ }
+ })
+ }
+}
+
func TestApplyTableChangesToChunk_CommentDrift(t *testing.T) {
tests := []struct {
name string
diff --git a/backend/plugin/schema/pg/materialized_view_sdl_diff_test.go b/backend/plugin/schema/pg/materialized_view_sdl_diff_test.go
new file mode 100644
index 0000000000..d3c47f562e
--- /dev/null
+++ b/backend/plugin/schema/pg/materialized_view_sdl_diff_test.go
@@ -0,0 +1,706 @@
+package pg
+
+import (
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+)
+
+func TestMaterializedViewSDLDiff(t *testing.T) {
+ tests := []struct {
+ name string
+ previousSDL string
+ currentSDL string
+ expectedMaterializedViewChanges int
+ expectedActions []schema.MetadataDiffAction
+ }{
+ {
+ name: "Create new materialized view",
+ previousSDL: ``,
+ currentSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ active BOOLEAN DEFAULT true
+ );
+
+ CREATE MATERIALIZED VIEW active_users_mv AS
+ SELECT id, name
+ FROM users
+ WHERE active = true;
+ `,
+ expectedMaterializedViewChanges: 1,
+ expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},
+ },
+ {
+ name: "Drop materialized view",
+ previousSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ active BOOLEAN DEFAULT true
+ );
+
+ CREATE MATERIALIZED VIEW active_users_mv AS
+ SELECT id, name
+ FROM users
+ WHERE active = true;
+ `,
+ currentSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ active BOOLEAN DEFAULT true
+ );
+ `,
+ expectedMaterializedViewChanges: 1,
+ expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop},
+ },
+ {
+ name: "Modify materialized view (drop and recreate)",
+ previousSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ active BOOLEAN DEFAULT true
+ );
+
+ CREATE MATERIALIZED VIEW active_users_mv AS
+ SELECT id, name
+ FROM users
+ WHERE active = true;
+ `,
+ currentSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ active BOOLEAN DEFAULT true
+ );
+
+ CREATE MATERIALIZED VIEW active_users_mv AS
+ SELECT id, name, 'active' as status
+ FROM users
+ WHERE active = true;
+ `,
+ expectedMaterializedViewChanges: 2, // Drop + Create
+ expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop, schema.MetadataDiffActionCreate},
+ },
+ {
+ name: "No changes to materialized view",
+ previousSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ active BOOLEAN DEFAULT true
+ );
+
+ CREATE MATERIALIZED VIEW active_users_mv AS
+ SELECT id, name
+ FROM users
+ WHERE active = true;
+ `,
+ currentSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ active BOOLEAN DEFAULT true
+ );
+
+ CREATE MATERIALIZED VIEW active_users_mv AS
+ SELECT id, name
+ FROM users
+ WHERE active = true;
+ `,
+ expectedMaterializedViewChanges: 0,
+ expectedActions: []schema.MetadataDiffAction{},
+ },
+ {
+ name: "Multiple materialized views with different changes",
+ previousSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ active BOOLEAN DEFAULT true
+ );
+
+ CREATE MATERIALIZED VIEW all_users_mv AS
+ SELECT * FROM users;
+
+ CREATE MATERIALIZED VIEW active_users_mv AS
+ SELECT id, name FROM users WHERE active = true;
+ `,
+ currentSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ active BOOLEAN DEFAULT true
+ );
+
+ CREATE MATERIALIZED VIEW all_users_mv AS
+ SELECT * FROM users;
+
+ CREATE MATERIALIZED VIEW admin_users_mv AS
+ SELECT id, name FROM users WHERE role = 'admin';
+ `,
+ expectedMaterializedViewChanges: 2, // Drop active_users_mv + Create admin_users_mv
+ expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate, schema.MetadataDiffActionDrop},
+ },
+ {
+ name: "Schema-qualified materialized view names",
+ previousSDL: `
+ CREATE SCHEMA test_schema;
+ CREATE TABLE test_schema.products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255)
+ );
+ `,
+ currentSDL: `
+ CREATE SCHEMA test_schema;
+ CREATE TABLE test_schema.products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255)
+ );
+
+ CREATE MATERIALIZED VIEW test_schema.product_summary_mv AS
+ SELECT id, name FROM test_schema.products;
+ `,
+ expectedMaterializedViewChanges: 1,
+ expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},
+ },
+ {
+ name: "Materialized view with comment",
+ previousSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL
+ );
+ `,
+ currentSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL
+ );
+
+ CREATE MATERIALIZED VIEW user_summary_mv AS
+ SELECT id, name FROM users;
+
+ COMMENT ON MATERIALIZED VIEW user_summary_mv IS 'Summary of all users';
+ `,
+ expectedMaterializedViewChanges: 1,
+ expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)
+ require.NoError(t, err)
+ require.NotNil(t, diff)
+
+ assert.Equal(t, tt.expectedMaterializedViewChanges, len(diff.MaterializedViewChanges),
+ "Expected %d materialized view changes, got %d", tt.expectedMaterializedViewChanges, len(diff.MaterializedViewChanges))
+
+ // Check that the actions match expectations
+ var actualActions []schema.MetadataDiffAction
+ for _, mvDiff := range diff.MaterializedViewChanges {
+ actualActions = append(actualActions, mvDiff.Action)
+ }
+
+ // Handle nil vs empty slice comparison
+ if len(tt.expectedActions) == 0 && len(actualActions) == 0 {
+ // Both are effectively empty - test passes
+ t.Log("Both expected and actual actions are empty")
+ } else {
+ assert.ElementsMatch(t, tt.expectedActions, actualActions,
+ "Expected actions %v, got %v", tt.expectedActions, actualActions)
+ }
+
+ // Verify AST nodes are properly set
+ for i, mvDiff := range diff.MaterializedViewChanges {
+ switch mvDiff.Action {
+ case schema.MetadataDiffActionCreate:
+ assert.NotNil(t, mvDiff.NewASTNode,
+ "Materialized view diff %d should have NewASTNode for CREATE action", i)
+ assert.Nil(t, mvDiff.OldASTNode,
+ "Materialized view diff %d should not have OldASTNode for CREATE action", i)
+ case schema.MetadataDiffActionDrop:
+ assert.NotNil(t, mvDiff.OldASTNode,
+ "Materialized view diff %d should have OldASTNode for DROP action", i)
+ assert.Nil(t, mvDiff.NewASTNode,
+ "Materialized view diff %d should not have NewASTNode for DROP action", i)
+ default:
+ t.Errorf("Unexpected action %v for materialized view diff %d", mvDiff.Action, i)
+ }
+ }
+ })
+ }
+}
+
+func TestMaterializedViewWithCommentParsing(t *testing.T) {
+ tests := []struct {
+ name string
+ sdl string
+ expectedMVCount int
+ expectedComment string
+ expectCommentInSDL bool
+ }{
+ {
+ name: "Create materialized view with comment",
+ sdl: `
+ CREATE TABLE public.users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL
+ );
+
+ CREATE MATERIALIZED VIEW public.user_summary_mv AS
+ SELECT id, name FROM users;
+
+ COMMENT ON MATERIALIZED VIEW public.user_summary_mv IS 'Summary of all users';
+ `,
+ expectedMVCount: 1,
+ expectedComment: "Summary of all users",
+ expectCommentInSDL: true,
+ },
+ {
+ name: "Create materialized view with schema-qualified name and comment",
+ sdl: `
+ CREATE TABLE public.table1 (
+ id INTEGER PRIMARY KEY,
+ name TEXT
+ );
+
+ CREATE MATERIALIZED VIEW "public"."table1_summary" AS
+ SELECT t2.id AS table2_id, t2.name AS table2_name
+ FROM "public"."table1" t2;
+
+ COMMENT ON MATERIALIZED VIEW "public"."table1_summary" IS 'Summarizes table1 records grouped by table2 categories. Refresh periodically to keep data current.';
+ `,
+ expectedMVCount: 1,
+ expectedComment: "Summarizes table1 records grouped by table2 categories. Refresh periodically to keep data current.",
+ expectCommentInSDL: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ // Parse the SDL to get chunks
+ chunks, err := ChunkSDLText(tt.sdl)
+ require.NoError(t, err)
+ require.NotNil(t, chunks)
+
+ // Debug: Print what we got
+ t.Logf("Found %d materialized views", len(chunks.MaterializedViews))
+ for id, chunk := range chunks.MaterializedViews {
+ t.Logf(" MV ID: %s", id)
+ t.Logf(" Has ASTNode: %v", chunk.ASTNode != nil)
+ t.Logf(" CommentStatements: %d", len(chunk.CommentStatements))
+ }
+
+ // Verify we parsed the correct number of materialized views
+ assert.Equal(t, tt.expectedMVCount, len(chunks.MaterializedViews),
+ "Expected %d materialized view(s), got %d", tt.expectedMVCount, len(chunks.MaterializedViews))
+
+ // Get the materialized view chunk
+ var mvChunk *schema.SDLChunk
+ for _, chunk := range chunks.MaterializedViews {
+ mvChunk = chunk
+ break
+ }
+ require.NotNil(t, mvChunk, "Materialized view chunk should exist")
+
+ // Get the full text including comments
+ fullText := mvChunk.GetText()
+ t.Logf("Full materialized view text:\n%s", fullText)
+
+ // Verify comment is included in the SDL
+ if tt.expectCommentInSDL {
+ assert.Contains(t, fullText, "COMMENT ON MATERIALIZED VIEW",
+ "Full text should contain COMMENT ON MATERIALIZED VIEW statement")
+ assert.Contains(t, fullText, tt.expectedComment,
+ "Comment should contain the expected text: %s", tt.expectedComment)
+
+ // Verify comment statements count
+ assert.Greater(t, len(mvChunk.CommentStatements), 0,
+ "Materialized view should have comment statements")
+ }
+
+ // Verify CREATE MATERIALIZED VIEW is present
+ assert.Contains(t, fullText, "CREATE MATERIALIZED VIEW",
+ "Full text should contain CREATE MATERIALIZED VIEW statement")
+ })
+ }
+}
+
+func TestMaterializedViewMigrationGeneration(t *testing.T) {
+ tests := []struct {
+ name string
+ previousSDL string
+ currentSDL string
+ wantCreate bool
+ wantDrop bool
+ }{
+ {
+ name: "Create materialized view generates CREATE statement",
+ previousSDL: ``,
+ currentSDL: `
+ CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255)
+ );
+
+ CREATE MATERIALIZED VIEW product_mv AS
+ SELECT id, name FROM products;
+ `,
+ wantCreate: true,
+ wantDrop: false,
+ },
+ {
+ name: "Drop materialized view generates DROP statement",
+ previousSDL: `
+ CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255)
+ );
+
+ CREATE MATERIALIZED VIEW product_mv AS
+ SELECT id, name FROM products;
+ `,
+ currentSDL: `
+ CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255)
+ );
+ `,
+ wantCreate: false,
+ wantDrop: true,
+ },
+ {
+ name: "Modify materialized view generates DROP and CREATE",
+ previousSDL: `
+ CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255)
+ );
+
+ CREATE MATERIALIZED VIEW product_mv AS
+ SELECT id, name FROM products;
+ `,
+ currentSDL: `
+ CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255),
+ price DECIMAL
+ );
+
+ CREATE MATERIALIZED VIEW product_mv AS
+ SELECT id, name, price FROM products;
+ `,
+ wantCreate: true,
+ wantDrop: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)
+ require.NoError(t, err)
+ require.NotNil(t, diff)
+
+ migration, err := generateMigration(diff)
+ require.NoError(t, err)
+
+ if tt.wantCreate {
+ assert.Contains(t, migration, "CREATE MATERIALIZED VIEW",
+ "Expected migration to contain CREATE MATERIALIZED VIEW statement")
+ }
+ if tt.wantDrop {
+ assert.Contains(t, migration, "DROP MATERIALIZED VIEW",
+ "Expected migration to contain DROP MATERIALIZED VIEW statement")
+ }
+ })
+ }
+}
+
+func TestMaterializedViewCommentMigrationGeneration(t *testing.T) {
+ tests := []struct {
+ name string
+ previousSDL string
+ currentSDL string
+ wantComment bool
+ }{
+ {
+ name: "Create materialized view with comment generates COMMENT statement",
+ previousSDL: ``,
+ currentSDL: `
+ CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255)
+ );
+
+ CREATE MATERIALIZED VIEW product_mv AS
+ SELECT id, name FROM products;
+
+ COMMENT ON MATERIALIZED VIEW product_mv IS 'Product summary view';
+ `,
+ wantComment: true,
+ },
+ {
+ name: "Add comment to existing materialized view",
+ previousSDL: `
+ CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255)
+ );
+
+ CREATE MATERIALIZED VIEW product_mv AS
+ SELECT id, name FROM products;
+ `,
+ currentSDL: `
+ CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255)
+ );
+
+ CREATE MATERIALIZED VIEW product_mv AS
+ SELECT id, name FROM products;
+
+ COMMENT ON MATERIALIZED VIEW product_mv IS 'Product summary view';
+ `,
+ wantComment: true,
+ },
+ {
+ name: "Remove comment from materialized view",
+ previousSDL: `
+ CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255)
+ );
+
+ CREATE MATERIALIZED VIEW product_mv AS
+ SELECT id, name FROM products;
+
+ COMMENT ON MATERIALIZED VIEW product_mv IS 'Product summary view';
+ `,
+ currentSDL: `
+ CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255)
+ );
+
+ CREATE MATERIALIZED VIEW product_mv AS
+ SELECT id, name FROM products;
+ `,
+ wantComment: true, // Should generate COMMENT statement with empty string
+ },
+ {
+ name: "Update comment on materialized view",
+ previousSDL: `
+ CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255)
+ );
+
+ CREATE MATERIALIZED VIEW product_mv AS
+ SELECT id, name FROM products;
+
+ COMMENT ON MATERIALIZED VIEW product_mv IS 'Old comment';
+ `,
+ currentSDL: `
+ CREATE TABLE products (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255)
+ );
+
+ CREATE MATERIALIZED VIEW product_mv AS
+ SELECT id, name FROM products;
+
+ COMMENT ON MATERIALIZED VIEW product_mv IS 'New comment';
+ `,
+ wantComment: true,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)
+ require.NoError(t, err)
+ require.NotNil(t, diff)
+
+ migration, err := generateMigration(diff)
+ require.NoError(t, err)
+
+ t.Logf("Generated migration:\n%s", migration)
+
+ if tt.wantComment {
+ assert.Contains(t, migration, "COMMENT ON MATERIALIZED VIEW",
+ "Expected migration to contain COMMENT ON MATERIALIZED VIEW statement")
+ }
+ })
+ }
+}
+
+func TestMaterializedViewDependencyOrder(t *testing.T) {
+ tests := []struct {
+ name string
+ previousSDL string
+ currentSDL string
+ description string
+ }{
+ {
+ name: "Create table, view, and materialized view with dependencies",
+ previousSDL: ``,
+ currentSDL: `
+ CREATE TABLE customers (
+ customer_id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ email VARCHAR(255),
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+ );
+
+ CREATE TABLE orders (
+ order_id SERIAL PRIMARY KEY,
+ customer_id INTEGER REFERENCES customers(customer_id),
+ amount DECIMAL(10,2),
+ order_date DATE
+ );
+
+ -- View depends on tables
+ CREATE VIEW customer_stats_view AS
+ SELECT
+ c.customer_id,
+ c.name,
+ c.email,
+ COUNT(o.order_id) as order_count,
+ SUM(o.amount) as total_spent
+ FROM customers c
+ LEFT JOIN orders o ON c.customer_id = o.customer_id
+ GROUP BY c.customer_id, c.name, c.email;
+
+ -- Materialized view depends on the view above
+ CREATE MATERIALIZED VIEW customer_segmentation_mv AS
+ SELECT
+ csv.customer_id,
+ csv.name,
+ csv.total_spent,
+ CASE
+ WHEN csv.total_spent >= 1000 THEN 'Premium'
+ WHEN csv.total_spent >= 500 THEN 'Standard'
+ ELSE 'Basic'
+ END as segment
+ FROM customer_stats_view csv;
+ `,
+ description: "Tests that objects are created in correct dependency order: tables -> view -> materialized view",
+ },
+ {
+ name: "Drop table, view, and materialized view with dependencies",
+ previousSDL: `
+ CREATE TABLE customers (
+ customer_id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL,
+ email VARCHAR(255),
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+ );
+
+ CREATE TABLE orders (
+ order_id SERIAL PRIMARY KEY,
+ customer_id INTEGER REFERENCES customers(customer_id),
+ amount DECIMAL(10,2),
+ order_date DATE
+ );
+
+ -- View depends on tables
+ CREATE VIEW customer_stats_view AS
+ SELECT
+ c.customer_id,
+ c.name,
+ c.email,
+ COUNT(o.order_id) as order_count,
+ SUM(o.amount) as total_spent
+ FROM customers c
+ LEFT JOIN orders o ON c.customer_id = o.customer_id
+ GROUP BY c.customer_id, c.name, c.email;
+
+ -- Materialized view depends on the view above
+ CREATE MATERIALIZED VIEW customer_segmentation_mv AS
+ SELECT
+ csv.customer_id,
+ csv.name,
+ csv.total_spent,
+ CASE
+ WHEN csv.total_spent >= 1000 THEN 'Premium'
+ WHEN csv.total_spent >= 500 THEN 'Standard'
+ ELSE 'Basic'
+ END as segment
+ FROM customer_stats_view csv;
+ `,
+ currentSDL: ``,
+ description: "Tests that objects are dropped in correct dependency order: materialized view -> view -> tables",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ diff, err := GetSDLDiff(tt.currentSDL, tt.previousSDL, nil, nil)
+ require.NoError(t, err)
+ require.NotNil(t, diff)
+
+ migration, err := generateMigration(diff)
+ require.NoError(t, err)
+
+ t.Logf("Generated migration:\n%s", migration)
+
+ // Check if this is a CREATE or DROP test
+ if tt.currentSDL != "" {
+ // CREATE test: verify correct order for creation
+ // Tables should come before views, and views should come before materialized views
+ customersIdx := strings.Index(migration, "CREATE TABLE customers")
+ ordersIdx := strings.Index(migration, "CREATE TABLE orders")
+ viewIdx := strings.Index(migration, "CREATE VIEW customer_stats_view")
+ mvIdx := strings.Index(migration, "CREATE MATERIALIZED VIEW customer_segmentation_mv")
+
+ assert.NotEqual(t, -1, customersIdx, "customers table should be created")
+ assert.NotEqual(t, -1, ordersIdx, "orders table should be created")
+ assert.NotEqual(t, -1, viewIdx, "customer_stats_view should be created")
+ assert.NotEqual(t, -1, mvIdx, "customer_segmentation_mv should be created")
+
+ // Verify correct order for CREATE
+ if customersIdx != -1 && viewIdx != -1 {
+ assert.Less(t, customersIdx, viewIdx,
+ "customers table must be created before customer_stats_view")
+ }
+ if ordersIdx != -1 && viewIdx != -1 {
+ assert.Less(t, ordersIdx, viewIdx,
+ "orders table must be created before customer_stats_view")
+ }
+ if viewIdx != -1 && mvIdx != -1 {
+ assert.Less(t, viewIdx, mvIdx,
+ "customer_stats_view must be created before customer_segmentation_mv")
+ }
+ } else {
+ // DROP test: verify correct order for dropping
+ // Materialized views should be dropped before views, and views before tables
+ mvIdx := strings.Index(migration, "DROP MATERIALIZED VIEW")
+ viewIdx := strings.Index(migration, "DROP VIEW")
+ customersIdx := strings.Index(migration, "DROP TABLE")
+
+ assert.NotEqual(t, -1, mvIdx, "customer_segmentation_mv should be dropped")
+ assert.NotEqual(t, -1, viewIdx, "customer_stats_view should be dropped")
+ assert.NotEqual(t, -1, customersIdx, "tables should be dropped")
+
+ // Verify correct order for DROP (reverse of CREATE)
+ if mvIdx != -1 && viewIdx != -1 {
+ assert.Less(t, mvIdx, viewIdx,
+ "customer_segmentation_mv must be dropped before customer_stats_view")
+ }
+ if viewIdx != -1 && customersIdx != -1 {
+ assert.Less(t, viewIdx, customersIdx,
+ "customer_stats_view must be dropped before tables")
+ }
+ }
+ })
+ }
+}
diff --git a/backend/plugin/schema/sdl_differ.go b/backend/plugin/schema/sdl_differ.go
index a55a9543c1..85a23ea168 100644
--- a/backend/plugin/schema/sdl_differ.go
+++ b/backend/plugin/schema/sdl_differ.go
@@ -117,12 +117,13 @@ func extractTextFromNode(node antlr.ParserRuleContext) string {
}
type SDLChunks struct {
- Tables map[string]*SDLChunk // key: table name/identifier
- Views map[string]*SDLChunk // key: view name/identifier
- Functions map[string]*SDLChunk // key: function name/identifier
- Indexes map[string]*SDLChunk // key: index name/identifier
- Sequences map[string]*SDLChunk // key: sequence name/identifier
- Schemas map[string]*SDLChunk // key: schema name/identifier
+ Tables map[string]*SDLChunk // key: table name/identifier
+ Views map[string]*SDLChunk // key: view name/identifier
+ MaterializedViews map[string]*SDLChunk // key: materialized view name/identifier
+ Functions map[string]*SDLChunk // key: function name/identifier
+ Indexes map[string]*SDLChunk // key: index name/identifier
+ Sequences map[string]*SDLChunk // key: sequence name/identifier
+ Schemas map[string]*SDLChunk // key: schema name/identifier
// Column comments: map[schemaName.tableName][columnName] -> COMMENT ON COLUMN AST node
ColumnComments map[string]map[string]antlr.ParserRuleContext