Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: print serial and identity column instead of create sequence for SDL (#17703)
rebelice233c71f4cdd2
diff --git a/backend/plugin/schema/pg/column_rewrite_test.go b/backend/plugin/schema/pg/column_rewrite_test.goindex 2365a2418f..c1f6b12602 100644--- a/backend/plugin/schema/pg/column_rewrite_test.go+++ b/backend/plugin/schema/pg/column_rewrite_test.go@@ -315,7 +315,7 @@ func TestColumnRewriteOperations(t *testing.T) {}// Apply column changes- err = applyTableChangesToChunk(chunk, tc.currentTable, tc.previousTable)+ err = applyTableChangesToChunk(chunk, tc.currentTable, tc.previousTable, nil)if tc.expectedSDL == "" {// For cases where we don't specify expected SDL, we just want to ensure no errorassert.NoError(t, err, "applyTableChangesToChunk should not return error")@@ -453,7 +453,7 @@ func TestColumnRewriteWithCollation(t *testing.T) {ASTNode: createTableNode,}- err = applyTableChangesToChunk(chunk, currentTable, previousTable)+ err = applyTableChangesToChunk(chunk, currentTable, previousTable, nil)assert.NoError(t, err, "Should handle collation changes without error")}@@ -503,7 +503,7 @@ func TestGenerateColumnSDLFunction(t *testing.T) {for _, tc := range testCases {t.Run(tc.name, func(t *testing.T) {- result := generateColumnSDL(tc.column)+ result := generateColumnSDL(tc.column, "test_table", nil)assert.Equal(t, tc.expected, result)})}diff --git a/backend/plugin/schema/pg/constraint_rewrite_test.go b/backend/plugin/schema/pg/constraint_rewrite_test.goindex 2f92024b67..78542a71ab 100644--- a/backend/plugin/schema/pg/constraint_rewrite_test.go+++ b/backend/plugin/schema/pg/constraint_rewrite_test.go@@ -685,7 +685,7 @@ func TestConstraintRewriteOperations(t *testing.T) {}// Apply constraint changes- err = applyTableChangesToChunk(chunk, tc.currentTable, tc.previousTable)+ err = applyTableChangesToChunk(chunk, tc.currentTable, tc.previousTable, nil)if tc.expectedSDL == "" {// For cases where we don't specify expected SDL, we just want to ensure no errorassert.NoError(t, err, "applyTableChangesToChunk should not return error")@@ -762,7 +762,7 @@ func TestConstraintModificationOperations(t *testing.T) {}// Apply constraint changes- err = applyTableChangesToChunk(chunk, tc.currentTable, tc.previousTable)+ err = applyTableChangesToChunk(chunk, tc.currentTable, tc.previousTable, nil)assert.NoError(t, err, "applyTableChangesToChunk should not return error")assert.NotNil(t, chunk.ASTNode, "Chunk AST node should not be nil after modification")t.Logf("Test case '%s' passed: %s", tc.name, tc.description)diff --git a/backend/plugin/schema/pg/get_database_definition.go b/backend/plugin/schema/pg/get_database_definition.goindex bca3595c73..b9e01a2564 100644--- a/backend/plugin/schema/pg/get_database_definition.go+++ b/backend/plugin/schema/pg/get_database_definition.go@@ -2138,7 +2138,48 @@ func getSDLFormat(metadata *storepb.DatabaseSchemaMetadata) (string, error) {}}+ // Build a map of serial and identity sequences that should be skipped+ // Serial sequences are identified by checking if their owner columns match the serial pattern+ // Identity sequences are identified by checking if their owner columns have IdentityGeneration set+ skipSequences := make(map[string]bool)+ for _, schema := range metadata.Schemas {+ if schema.SkipDump {+ continue+ }+ for _, table := range schema.Tables {+ if table.SkipDump {+ continue+ }+ for _, column := range table.Columns {+ // Check for serial columns+ isSerial, _ := isSerialColumn(column, table.Name, schema.Sequences)+ if isSerial {+ // Find the sequence that belongs to this serial column+ for _, sequence := range schema.Sequences {+ if sequence.OwnerTable == table.Name && sequence.OwnerColumn == column.Name {+ sequenceKey := schema.Name + "." + sequence.Name+ skipSequences[sequenceKey] = true+ break+ }+ }+ }+ // Check for identity columns+ if isIdentityColumn(column) {+ // Find the sequence that belongs to this identity column+ for _, sequence := range schema.Sequences {+ if sequence.OwnerTable == table.Name && sequence.OwnerColumn == column.Name {+ sequenceKey := schema.Name + "." + sequence.Name+ skipSequences[sequenceKey] = true+ break+ }+ }+ }+ }+ }+ }+// Write all sequences before tables to ensure they exist before any table references them.+ // Skip sequences that belong to serial or identity columns as they will be implicitly created.sequenceOwnershipMap := make(map[string][]*storepb.SequenceMetadata)for _, schema := range metadata.Schemas {@@ -2150,6 +2191,11 @@ func getSDLFormat(metadata *storepb.DatabaseSchemaMetadata) (string, error) {if sequence.SkipDump {continue}+ // Skip sequences that belong to serial or identity columns+ sequenceKey := schema.Name + "." + sequence.Name+ if skipSequences[sequenceKey] {+ continue+ }if err := writeSequenceSDL(&buf, schema.Name, sequence); err != nil {return "", err}@@ -2164,6 +2210,23 @@ func getSDLFormat(metadata *storepb.DatabaseSchemaMetadata) (string, error) {}}+ // Build a map of sequences by table for easy lookup during table creation+ tableSequencesMap := make(map[string][]*storepb.SequenceMetadata)+ for _, schema := range metadata.Schemas {+ if schema.SkipDump {+ continue+ }+ for _, sequence := range schema.Sequences {+ if sequence.SkipDump {+ continue+ }+ if sequence.OwnerTable != "" {+ tableKey := schema.Name + "." + sequence.OwnerTable+ tableSequencesMap[tableKey] = append(tableSequencesMap[tableKey], sequence)+ }+ }+ }+for _, schema := range metadata.Schemas {if schema.SkipDump {continue@@ -2175,8 +2238,12 @@ func getSDLFormat(metadata *storepb.DatabaseSchemaMetadata) (string, error) {continue}+ // Get sequences for this table+ tableKey := schema.Name + "." + table.Name+ tableSequences := tableSequencesMap[tableKey]+// Write CREATE TABLE statement- if err := writeCreateTableSDL(&buf, schema.Name, table); err != nil {+ if err := writeCreateTableSDL(&buf, schema.Name, table, tableSequences); err != nil {return "", err}@@ -2224,9 +2291,163 @@ func getSDLFormat(metadata *storepb.DatabaseSchemaMetadata) (string, error) {return buf.String(), nil}+// isSerialColumn checks if a column is a serial/bigserial/smallserial type based on its properties+// Serial pattern: integer/bigint/smallint + NOT NULL + nextval(sequence) default+// extractSequenceNameFromNextval extracts the sequence name from a nextval() default value.+// It handles various PostgreSQL formats including:+// - nextval('sequence_name')+// - nextval('schema.sequence_name')+// - nextval('"Schema"."Sequence"')+// - nextval('public."MixedCase"')+// - nextval('sequence_name'::regclass)+// Returns the unquoted sequence name without schema qualification.+func extractSequenceNameFromNextval(defaultValue string) string {+ if defaultValue == "" {+ return ""+ }++ // Find nextval( - case insensitive+ defaultLower := strings.ToLower(defaultValue)+ nextvalIdx := strings.Index(defaultLower, "nextval(")+ if nextvalIdx == -1 {+ return ""+ }++ // Find the opening quote after nextval(+ startIdx := nextvalIdx + len("nextval(")+ if startIdx >= len(defaultValue) {+ return ""+ }++ // Determine quote type (' or ")+ quoteChar := byte(0)+ for startIdx < len(defaultValue) && (defaultValue[startIdx] == ' ' || defaultValue[startIdx] == '\t') {+ startIdx+++ }+ if startIdx >= len(defaultValue) {+ return ""+ }++ switch defaultValue[startIdx] {+ case '\'':+ quoteChar = '\''+ case '"':+ quoteChar = '"'+ default:+ return ""+ }++ startIdx++ // Skip opening quote++ // Find the closing quote+ endIdx := startIdx+ for endIdx < len(defaultValue) && defaultValue[endIdx] != quoteChar {+ endIdx+++ }+ if endIdx >= len(defaultValue) {+ return ""+ }++ // Extract the full sequence reference (may include schema and ::regclass)+ sequenceRef := defaultValue[startIdx:endIdx]++ // Remove ::regclass suffix if present+ if idx := strings.Index(sequenceRef, "::"); idx != -1 {+ sequenceRef = sequenceRef[:idx]+ }++ // Parse schema-qualified name: schema.sequence or "schema"."sequence"+ // We need to extract just the sequence name, handling quoted identifiers+ sequenceName := extractIdentifierFromQualifiedName(sequenceRef)++ // Remove surrounding quotes from the sequence name if present+ sequenceName = strings.TrimSpace(sequenceName)+ if len(sequenceName) >= 2 && sequenceName[0] == '"' && sequenceName[len(sequenceName)-1] == '"' {+ sequenceName = sequenceName[1 : len(sequenceName)-1]+ }++ return sequenceName+}++// extractIdentifierFromQualifiedName extracts the identifier (sequence name) from a qualified name.+// Handles cases like:+// - sequence_name -> sequence_name+// - schema.sequence_name -> sequence_name+// - "Schema"."Sequence" -> "Sequence"+// - public."MixedCase" -> "MixedCase"+func extractIdentifierFromQualifiedName(qualifiedName string) string {+ if qualifiedName == "" {+ return ""+ }++ // Look for the last dot that's not inside quotes+ inQuotes := false+ lastDotIdx := -1++ for i := 0; i < len(qualifiedName); i++ {+ if qualifiedName[i] == '"' {+ inQuotes = !inQuotes+ } else if qualifiedName[i] == '.' && !inQuotes {+ lastDotIdx = i+ }+ }++ // If we found a dot outside quotes, take everything after it+ if lastDotIdx != -1 {+ return qualifiedName[lastDotIdx+1:]+ }++ // No schema qualification found+ return qualifiedName+}++func isSerialColumn(column *storepb.ColumnMetadata, tableName string, sequences []*storepb.SequenceMetadata) (isSerial bool, serialType string) {+ // Serial columns must be NOT NULL+ if column.Nullable {+ return false, ""+ }++ // Serial columns must have a nextval() default+ if !strings.Contains(strings.ToLower(column.Default), "nextval(") {+ return false, ""+ }++ // Check type and map to serial type+ var expectedSerialType string+ switch strings.ToLower(column.Type) {+ case "integer":+ expectedSerialType = "serial"+ case "bigint":+ expectedSerialType = "bigserial"+ case "smallint":+ expectedSerialType = "smallserial"+ default:+ return false, ""+ }++ // IMPORTANT: Only treat as serial if the sequence is owned by this column+ // This prevents converting columns that use independent sequences to serial type+ sequenceName := extractSequenceNameFromNextval(column.Default)+ if sequenceName == "" {+ return false, ""+ }++ // Check if any sequence with this name is owned by this column+ for _, seq := range sequences {+ if seq.Name == sequenceName && seq.OwnerTable == tableName && seq.OwnerColumn == column.Name {+ return true, expectedSerialType+ }+ }++ // Sequence is not owned by this column, so don't convert to serial+ return false, ""+}+// writeColumnSDL writes a single column definition to the output writer// This function is extracted from writeCreateTableSDL to enable code reuse-func writeColumnSDL(out io.Writer, column *storepb.ColumnMetadata) error {+// sequences parameter is optional and used to find identity sequences for the column+// tableName is used to verify sequence ownership for serial column detection+func writeColumnSDL(out io.Writer, column *storepb.ColumnMetadata, tableName string, sequences []*storepb.SequenceMetadata) error {if _, err := io.WriteString(out, `"`); err != nil {return err}@@ -2239,23 +2460,111 @@ func writeColumnSDL(out io.Writer, column *storepb.ColumnMetadata) error {return err}- if _, err := io.WriteString(out, column.Type); err != nil {- return err- }+ // Check if this is an identity column+ if isIdentityColumn(column) {+ // Find the sequence for this identity column+ var identitySequence *storepb.SequenceMetadata+ for _, seq := range sequences {+ if seq.OwnerColumn == column.Name {+ identitySequence = seq+ break+ }+ }++ // Write type+ if _, err := io.WriteString(out, column.Type); err != nil {+ return err+ }- if column.Default != "" {- if _, err := io.WriteString(out, ` DEFAULT `); err != nil {+ // Write GENERATED ... AS IDENTITY+ if _, err := io.WriteString(out, " GENERATED "); err != nil {return err}- if _, err := io.WriteString(out, column.Default); err != nil {+ if column.IdentityGeneration == storepb.ColumnMetadata_ALWAYS {+ if _, err := io.WriteString(out, "ALWAYS"); err != nil {+ return err+ }+ } else {+ if _, err := io.WriteString(out, "BY DEFAULT"); err != nil {+ return err+ }+ }+ if _, err := io.WriteString(out, " AS IDENTITY"); err != nil {return err}++ // Write identity options if we have the sequence+ if identitySequence != nil {+ hasOptions := false+ // Check if we need to write options (non-default values)+ if identitySequence.Start != "" && identitySequence.Start != "1" {+ hasOptions = true+ }+ if identitySequence.Increment != "" && identitySequence.Increment != "1" {+ hasOptions = true+ }++ if hasOptions {+ if _, err := io.WriteString(out, " ("); err != nil {+ return err+ }++ first := true+ if identitySequence.Start != "" && identitySequence.Start != "1" {+ if _, err := fmt.Fprintf(out, "START WITH %s", identitySequence.Start); err != nil {+ return err+ }+ first = false+ }+ if identitySequence.Increment != "" && identitySequence.Increment != "1" {+ if !first {+ if _, err := io.WriteString(out, " "); err != nil {+ return err+ }+ }+ if _, err := fmt.Fprintf(out, "INCREMENT BY %s", identitySequence.Increment); err != nil {+ return err+ }+ }++ if _, err := io.WriteString(out, ")"); err != nil {+ return err+ }+ }+ }++ // Identity columns are implicitly NOT NULL, skip that+ return nil}- if !column.Nullable {- if _, err := io.WriteString(out, ` NOT NULL`); err != nil {+ // Check if this is a serial column and convert it back to serial type+ isSerial, serialType := isSerialColumn(column, tableName, sequences)+ if isSerial {+ // Use serial type instead of integer + nextval + NOT NULL+ if _, err := io.WriteString(out, serialType); err != nil {+ return err+ }+ // Skip DEFAULT and NOT NULL as they are implicit in serial+ } else {+ // Normal column handling+ if _, err := io.WriteString(out, column.Type); err != nil {return err}++ if column.Default != "" {+ if _, err := io.WriteString(out, ` DEFAULT `); err != nil {+ return err+ }+ if _, err := io.WriteString(out, column.Default); err != nil {+ return err+ }+ }++ if !column.Nullable {+ if _, err := io.WriteString(out, ` NOT NULL`); err != nil {+ return err+ }+ }}if column.Collation != "" {@@ -2273,7 +2582,7 @@ func writeColumnSDL(out io.Writer, column *storepb.ColumnMetadata) error {return nil}-func writeCreateTableSDL(out io.Writer, schemaName string, table *storepb.TableMetadata) error {+func writeCreateTableSDL(out io.Writer, schemaName string, table *storepb.TableMetadata, sequences []*storepb.SequenceMetadata) error {if _, err := io.WriteString(out, `CREATE TABLE "`); err != nil {return err}@@ -2307,7 +2616,7 @@ func writeCreateTableSDL(out io.Writer, schemaName string, table *storepb.TableM}// Use the extracted writeColumnSDL function- if err := writeColumnSDL(out, column); err != nil {+ if err := writeColumnSDL(out, column, table.Name, sequences); err != nil {return err}}diff --git a/backend/plugin/schema/pg/get_database_definition_sdl_test.go b/backend/plugin/schema/pg/get_database_definition_sdl_test.goindex b7c40bdd5f..57faa8b48e 100644--- a/backend/plugin/schema/pg/get_database_definition_sdl_test.go+++ b/backend/plugin/schema/pg/get_database_definition_sdl_test.go@@ -637,12 +637,10 @@ $$;},expected: `CREATE SEQUENCE "public"."independent_seq";-CREATE SEQUENCE "public"."user_id_seq";-CREATE SEQUENCE "public"."order_seq" AS integer START WITH 1000 INCREMENT BY 10 MINVALUE 1000 MAXVALUE 999999 CYCLE;CREATE TABLE "public"."users" (- "id" INTEGER DEFAULT nextval('user_id_seq'::regclass) NOT NULL,+ "id" serial,"name" VARCHAR(255) NOT NULL);@@ -655,6 +653,196 @@ CREATE TABLE "public"."users" (},expected: "",},+ {+ name: "Serial columns should use serial types",+ metadata: &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "users_id_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ OwnerTable: "users",+ OwnerColumn: "id",+ },+ {+ Name: "products_id_seq",+ DataType: "integer",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "2147483647",+ Cycle: false,+ OwnerTable: "products",+ OwnerColumn: "id",+ },+ {+ Name: "orders_id_seq",+ DataType: "smallint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "32767",+ Cycle: false,+ OwnerTable: "orders",+ OwnerColumn: "id",+ },+ },+ Tables: []*storepb.TableMetadata{+ {+ Name: "users",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "bigint",+ Nullable: false,+ Default: "nextval('users_id_seq'::regclass)",+ },+ {+ Name: "name",+ Type: "VARCHAR(255)",+ Nullable: false,+ },+ },+ },+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ Default: "nextval('products_id_seq'::regclass)",+ },+ {+ Name: "name",+ Type: "VARCHAR(255)",+ Nullable: false,+ },+ },+ },+ {+ Name: "orders",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "smallint",+ Nullable: false,+ Default: "nextval('orders_id_seq'::regclass)",+ },+ {+ Name: "user_id",+ Type: "INTEGER",+ Nullable: false,+ },+ },+ },+ },+ },+ },+ },+ expected: `CREATE TABLE "public"."users" (+ "id" bigserial,+ "name" VARCHAR(255) NOT NULL+);++CREATE TABLE "public"."products" (+ "id" serial,+ "name" VARCHAR(255) NOT NULL+);++CREATE TABLE "public"."orders" (+ "id" smallserial,+ "user_id" INTEGER NOT NULL+);++`,+ },+ {+ name: "Identity columns should use GENERATED AS IDENTITY syntax",+ metadata: &storepb.DatabaseSchemaMetadata{+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "users_id_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ OwnerTable: "users",+ OwnerColumn: "id",+ },+ {+ Name: "products_id_seq",+ DataType: "integer",+ Start: "100",+ Increment: "5",+ MinValue: "1",+ MaxValue: "2147483647",+ Cycle: false,+ OwnerTable: "products",+ OwnerColumn: "id",+ },+ },+ Tables: []*storepb.TableMetadata{+ {+ Name: "users",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "bigint",+ Nullable: false,+ IdentityGeneration: storepb.ColumnMetadata_ALWAYS,+ },+ {+ Name: "name",+ Type: "VARCHAR(255)",+ Nullable: false,+ },+ },+ },+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ IdentityGeneration: storepb.ColumnMetadata_BY_DEFAULT,+ },+ {+ Name: "name",+ Type: "VARCHAR(255)",+ Nullable: false,+ },+ },+ },+ },+ },+ },+ },+ expected: `CREATE TABLE "public"."users" (+ "id" bigint GENERATED ALWAYS AS IDENTITY,+ "name" VARCHAR(255) NOT NULL+);++CREATE TABLE "public"."products" (+ "id" integer GENERATED BY DEFAULT AS IDENTITY (START WITH 100 INCREMENT BY 5),+ "name" VARCHAR(255) NOT NULL+);++`,+ },}for _, tt := range tests {diff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.goindex 507539e164..851926db49 100644--- a/backend/plugin/schema/pg/get_sdl_diff.go+++ b/backend/plugin/schema/pg/get_sdl_diff.go@@ -21,16 +21,25 @@ func init() {}func GetSDLDiff(currentSDLText, previousUserSDLText string, currentSchema, previousSchema *model.DatabaseSchema) (*schema.MetadataDiff, error) {+ generatedSDL, err := convertDatabaseSchemaToSDL(currentSchema)+ if err != nil {+ return nil, errors.Wrap(err, "failed to convert current schema to SDL format for initialization")+ }+// Check for initialization scenario: previousUserSDLText is emptyif strings.TrimSpace(previousUserSDLText) == "" && currentSchema != nil {// Initialization scenario: convert currentSchema to SDL format as baseline- generatedSDL, err := convertDatabaseSchemaToSDL(currentSchema)- if err != nil {- return nil, errors.Wrap(err, "failed to convert current schema to SDL format for initialization")- }previousUserSDLText = generatedSDL}+ // Only skip processing if both current SDL and generated SDL match+ // AND there is actually a current schema to compare against.+ // If currentSchema is nil, we must process the diff to detect drops from previous SDL.+ if currentSchema != nil && strings.TrimSpace(currentSDLText) == strings.TrimSpace(generatedSDL) {+ // No changes detected between current SDL and database schema+ return &schema.MetadataDiff{}, nil+ }+currentChunks, err := ChunkSDLText(currentSDLText)if err != nil {return nil, errors.Wrap(err, "failed to chunk current SDL text")@@ -1635,13 +1644,30 @@ func applyMinimalChangesToChunks(previousChunks *schema.SDLChunks, currentSchema}}+ // Build sequences map: schema.table -> sequences for that table+ tableSequencesMap := make(map[string][]*storepb.SequenceMetadata)+ for _, schema := range currentMetadata.Schemas {+ schemaName := schema.Name+ if schemaName == "" {+ schemaName = "public"+ }+ for _, sequence := range schema.Sequences {+ if sequence.OwnerTable != "" {+ tableKey := schemaName + "." + sequence.OwnerTable+ tableSequencesMap[tableKey] = append(tableSequencesMap[tableKey], sequence)+ }+ }+ }+// Process table additions: add new tables to chunksfor tableKey, currentTable := range currentTables {if _, exists := previousTables[tableKey]; !exists {// Table was added - generate SDL for the new table and parse it to ASTvar buf strings.BuilderschemaName, _ := parseIdentifier(tableKey)- err := writeCreateTableSDL(&buf, schemaName, currentTable)+ // Get sequences for this table+ tableSequences := tableSequencesMap[tableKey]+ err := writeCreateTableSDL(&buf, schemaName, currentTable, tableSequences)if err != nil {return errors.Wrapf(err, "failed to generate SDL for new table %s", tableKey)}@@ -1680,8 +1706,10 @@ func applyMinimalChangesToChunks(previousChunks *schema.SDLChunks, currentSchema// Table exists in both schemas - check for column differencesif existingKey, chunkExists := existingChunkKeys[tableKey]; chunkExists {if chunk := previousChunks.Tables[existingKey]; chunk != nil {+ // Get sequences for this table+ tableSequences := tableSequencesMap[tableKey]// Apply both column and constraint changes to the existing chunk using a single rewriter- err := applyTableChangesToChunk(chunk, currentTable, previousTable)+ err := applyTableChangesToChunk(chunk, currentTable, previousTable, tableSequences)if err != nil {return errors.Wrapf(err, "failed to apply table changes to table %s", tableKey)}@@ -1725,7 +1753,7 @@ func applyMinimalChangesToChunks(previousChunks *schema.SDLChunks, currentSchema// applyTableChangesToChunk applies minimal column and constraint changes to an existing CREATE TABLE chunk// by working with the individual chunk's SQL text instead of the full script's tokenStream-func applyTableChangesToChunk(chunk *schema.SDLChunk, currentTable, previousTable *storepb.TableMetadata) error {+func applyTableChangesToChunk(chunk *schema.SDLChunk, currentTable, previousTable *storepb.TableMetadata, sequences []*storepb.SequenceMetadata) error {if chunk == nil || chunk.ASTNode == nil || currentTable == nil || previousTable == nil {return nil}@@ -1767,7 +1795,7 @@ func applyTableChangesToChunk(chunk *schema.SDLChunk, currentTable, previousTablrewriter := antlr.NewTokenStreamRewriter(tokenStream)// Apply column changes using the rewriter- err = applyColumnChanges(rewriter, createStmt, currentTable, previousTable)+ err = applyColumnChanges(rewriter, createStmt, currentTable, previousTable, sequences)if err != nil {return errors.Wrapf(err, "failed to apply column changes")}@@ -1804,7 +1832,7 @@ func applyTableChangesToChunk(chunk *schema.SDLChunk, currentTable, previousTabl}// applyColumnChanges applies column changes using the provided rewriter without parsing SQL-func applyColumnChanges(rewriter *antlr.TokenStreamRewriter, createStmt *parser.CreatestmtContext, currentTable, previousTable *storepb.TableMetadata) error {+func applyColumnChanges(rewriter *antlr.TokenStreamRewriter, createStmt *parser.CreatestmtContext, currentTable, previousTable *storepb.TableMetadata, sequences []*storepb.SequenceMetadata) error {// Create column maps for efficient lookupscurrentColumns := make(map[string]*storepb.ColumnMetadata)previousColumns := make(map[string]*storepb.ColumnMetadata)@@ -1839,7 +1867,7 @@ func applyColumnChanges(rewriter *antlr.TokenStreamRewriter, createStmt *parser.// Column exists in both - check if modifiedif !columnsEqual(currentCol, previousCol) {columnDef := existingColumnDefs.Map[columnName]- err := modifyColumnInAST(rewriter, columnDef.ASTNode, currentCol)+ err := modifyColumnInAST(rewriter, columnDef.ASTNode, currentCol, currentTable.Name, sequences)if err != nil {return errors.Wrapf(err, "failed to modify column %s", columnName)}@@ -1852,7 +1880,7 @@ func applyColumnChanges(rewriter *antlr.TokenStreamRewriter, createStmt *parser.for _, currentCol := range currentTable.Columns {if _, exists := previousColumns[currentCol.Name]; !exists {// Column was added- err := addColumnToAST(rewriter, createStmt, currentCol)+ err := addColumnToAST(rewriter, createStmt, currentCol, currentTable.Name, sequences)if err != nil {return errors.Wrapf(err, "failed to add column %s", currentCol.Name)}@@ -2178,7 +2206,7 @@ func deleteColumnFromAST(rewriter *antlr.TokenStreamRewriter, columnDef parser.I}// modifyColumnInAST modifies an existing column definition using token rewriter-func modifyColumnInAST(rewriter *antlr.TokenStreamRewriter, columnDef parser.IColumnDefContext, newColumn *storepb.ColumnMetadata) error {+func modifyColumnInAST(rewriter *antlr.TokenStreamRewriter, columnDef parser.IColumnDefContext, newColumn *storepb.ColumnMetadata, tableName string, sequences []*storepb.SequenceMetadata) error {if columnDef == nil || newColumn == nil {return errors.New("column definition or new column metadata is nil")}@@ -2190,7 +2218,7 @@ func modifyColumnInAST(rewriter *antlr.TokenStreamRewriter, columnDef parser.ICo}// Generate new column definition SDL- newColumnSDL := generateColumnSDL(newColumn)+ newColumnSDL := generateColumnSDL(newColumn, tableName, sequences)// Replace the entire column definitionrewriter.ReplaceDefault(startToken.GetTokenIndex(), stopToken.GetTokenIndex(), newColumnSDL)@@ -2199,7 +2227,7 @@ func modifyColumnInAST(rewriter *antlr.TokenStreamRewriter, columnDef parser.ICo}// addColumnToAST adds a new column definition to the CREATE TABLE statement-func addColumnToAST(rewriter *antlr.TokenStreamRewriter, createStmt *parser.CreatestmtContext, newColumn *storepb.ColumnMetadata) error {+func addColumnToAST(rewriter *antlr.TokenStreamRewriter, createStmt *parser.CreatestmtContext, newColumn *storepb.ColumnMetadata, tableName string, sequences []*storepb.SequenceMetadata) error {if createStmt == nil || newColumn == nil {return errors.New("create statement or new column metadata is nil")}@@ -2216,7 +2244,7 @@ func addColumnToAST(rewriter *antlr.TokenStreamRewriter, createStmt *parser.Creatoken := rewriter.GetTokenStream().Get(i)if token.GetTokenType() == parser.PostgreSQLParserOPEN_PAREN {// Found opening parenthesis, insert new column after it- newColumnSDL := "\n " + generateColumnSDL(newColumn) + "\n"+ newColumnSDL := "\n " + generateColumnSDL(newColumn, tableName, sequences) + "\n"rewriter.InsertAfterDefault(i, newColumnSDL)return nil}@@ -2235,7 +2263,7 @@ func addColumnToAST(rewriter *antlr.TokenStreamRewriter, createStmt *parser.Crea}// Generate new column definition SDL with leading comma and proper indentation- newColumnSDL := ",\n " + generateColumnSDL(newColumn)+ newColumnSDL := ",\n " + generateColumnSDL(newColumn, tableName, sequences)// Insert after the last columnrewriter.InsertAfterDefault(stopToken.GetTokenIndex(), newColumnSDL)@@ -2244,13 +2272,13 @@ func addColumnToAST(rewriter *antlr.TokenStreamRewriter, createStmt *parser.Crea}// generateColumnSDL generates SDL text for a single column definition using the extracted writeColumnSDL function-func generateColumnSDL(column *storepb.ColumnMetadata) string {+func generateColumnSDL(column *storepb.ColumnMetadata, tableName string, sequences []*storepb.SequenceMetadata) string {if column == nil {return ""}var buf strings.Builder- err := writeColumnSDL(&buf, column)+ err := writeColumnSDL(&buf, column, tableName, sequences)if err != nil {// If there's an error writing to the buffer, return empty string// This should rarely happen since we're writing to a strings.Builderdiff --git a/backend/plugin/schema/pg/get_sdl_diff_test.go b/backend/plugin/schema/pg/get_sdl_diff_test.goindex 5f42351ee5..dc0b9f08dd 100644--- a/backend/plugin/schema/pg/get_sdl_diff_test.go+++ b/backend/plugin/schema/pg/get_sdl_diff_test.go@@ -328,11 +328,13 @@ func TestGetSDLDiff_MinimalChangesScenario(t *testing.T) {func TestApplyMinimalChangesToChunks(t *testing.T) {tests := []struct {- name string- previousUserSDLText string- currentSchema *model.DatabaseSchema- previousSchema *model.DatabaseSchema- expectedTables []string+ name string+ previousUserSDLText string+ currentSchema *model.DatabaseSchema+ previousSchema *model.DatabaseSchema+ expectedTables []string+ shouldContainSerial bool+ shouldContainIdentity bool}{{name: "add_new_table_to_existing_chunks",@@ -393,7 +395,168 @@ func TestApplyMinimalChangesToChunks(t *testing.T) {storepb.Engine_POSTGRES,false,),- expectedTables: []string{"public.users", "public.posts"},+ expectedTables: []string{"public.users", "public.posts"},+ shouldContainSerial: false,+ },+ {+ name: "add_table_with_serial_column",+ previousUserSDLText: `CREATE TABLE users (+ id SERIAL PRIMARY KEY,+ name TEXT NOT NULL+ );`,+ 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", Nullable: false},+ {Name: "name", Type: "text", Nullable: false},+ },+ },+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "bigint",+ Nullable: false,+ Default: "nextval('products_id_seq'::regclass)",+ },+ {Name: "name", Type: "text", Nullable: false},+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "products_id_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ OwnerTable: "products",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ 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", Nullable: false},+ {Name: "name", Type: "text", Nullable: false},+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTables: []string{"public.users", "public.products"},+ shouldContainSerial: true,+ },+ {+ name: "add_table_with_identity_column",+ previousUserSDLText: `CREATE TABLE users (+ id SERIAL PRIMARY KEY,+ name TEXT NOT NULL+ );`,+ 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", Nullable: false},+ {Name: "name", Type: "text", Nullable: false},+ },+ },+ {+ Name: "orders",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ IdentityGeneration: storepb.ColumnMetadata_BY_DEFAULT,+ },+ {Name: "total", Type: "numeric(10,2)", Nullable: false},+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "orders_id_seq",+ DataType: "integer",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "2147483647",+ Cycle: false,+ OwnerTable: "orders",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ 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", Nullable: false},+ {Name: "name", Type: "text", Nullable: false},+ },+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTables: []string{"public.users", "public.orders"},+ shouldContainIdentity: true,},{name: "multi_schema_with_same_table_names",@@ -483,6 +646,19 @@ func TestApplyMinimalChangesToChunks(t *testing.T) {text := chunk.GetText()require.NotEmpty(t, text)require.Contains(t, strings.ToUpper(text), "CREATE TABLE")++ // Check for serial if expected (only for products table)+ if tt.shouldContainSerial && expectedTable == "public.products" {+ upperText := strings.ToUpper(text)+ require.Contains(t, upperText, "SERIAL", "Expected serial type in products table")+ }++ // Check for identity if expected (only for orders table)+ if tt.shouldContainIdentity && expectedTable == "public.orders" {+ upperText := strings.ToUpper(text)+ require.Contains(t, upperText, "GENERATED", "Expected GENERATED keyword in orders table")+ require.Contains(t, upperText, "AS IDENTITY", "Expected AS IDENTITY in orders table")+ }}})}@@ -1724,3 +1900,547 @@ func createMockDatabaseSchemaForColumnDeletion() *model.DatabaseSchema {return model.NewDatabaseSchema(metadata, nil, nil, storepb.Engine_POSTGRES, false)}++// TestGetSDLDiff_SerialAndIdentityColumns tests serial and identity column handling in SDL diff+func TestGetSDLDiff_SerialAndIdentityColumns(t *testing.T) {+ tests := []struct {+ name string+ currentSDLText string+ previousUserSDLText string+ currentSchema *model.DatabaseSchema+ expectedTableChanges int+ description string+ }{+ {+ name: "serial_column_format_difference_should_skip",+ currentSDLText: `CREATE TABLE "public"."users" (+ "id" serial,+ "name" text NOT NULL+);`,+ previousUserSDLText: `CREATE TABLE public.users (+ id integer NOT NULL DEFAULT nextval('users_id_seq'::regclass),+ name TEXT NOT NULL+);`,+ 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",+ Nullable: false,+ Default: "nextval('users_id_seq'::regclass)",+ },+ {+ Name: "name",+ Type: "text",+ Nullable: false,+ },+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "users_id_seq",+ DataType: "integer",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "2147483647",+ Cycle: false,+ OwnerTable: "users",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTableChanges: 0,+ description: "Serial vs expanded nextval format should not create diff when representing same structure",+ },+ {+ name: "bigserial_column_format_difference_should_skip",+ currentSDLText: `CREATE TABLE "public"."orders" (+ "order_id" bigserial,+ "customer_name" text NOT NULL+);`,+ previousUserSDLText: `CREATE TABLE public.orders (+ order_id bigint NOT NULL DEFAULT nextval('orders_order_id_seq'::regclass),+ customer_name TEXT NOT NULL+);`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "orders",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "order_id",+ Type: "bigint",+ Nullable: false,+ Default: "nextval('orders_order_id_seq'::regclass)",+ },+ {+ Name: "customer_name",+ Type: "text",+ Nullable: false,+ },+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "orders_order_id_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ OwnerTable: "orders",+ OwnerColumn: "order_id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTableChanges: 0,+ description: "Bigserial vs expanded bigint+nextval format should not create diff",+ },+ {+ name: "smallserial_column_format_difference_should_skip",+ currentSDLText: `CREATE TABLE "public"."items" (+ "item_id" smallserial,+ "item_name" text NOT NULL+);`,+ previousUserSDLText: `CREATE TABLE public.items (+ item_id smallint NOT NULL DEFAULT nextval('items_item_id_seq'::regclass),+ item_name TEXT NOT NULL+);`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "items",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "item_id",+ Type: "smallint",+ Nullable: false,+ Default: "nextval('items_item_id_seq'::regclass)",+ },+ {+ Name: "item_name",+ Type: "text",+ Nullable: false,+ },+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "items_item_id_seq",+ DataType: "smallint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "32767",+ Cycle: false,+ OwnerTable: "items",+ OwnerColumn: "item_id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTableChanges: 0,+ description: "Smallserial vs expanded smallint+nextval format should not create diff",+ },+ {+ name: "identity_always_format_difference_should_skip",+ currentSDLText: `CREATE TABLE "public"."products" (+ "id" bigint GENERATED ALWAYS AS IDENTITY,+ "name" text NOT NULL+);`,+ previousUserSDLText: `CREATE TABLE public.products (+ id bigint NOT NULL,+ name TEXT NOT NULL+);`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "bigint",+ Nullable: false,+ IdentityGeneration: storepb.ColumnMetadata_ALWAYS,+ },+ {+ Name: "name",+ Type: "text",+ Nullable: false,+ },+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "products_id_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ OwnerTable: "products",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTableChanges: 0,+ description: "GENERATED ALWAYS AS IDENTITY vs plain bigint should not create diff when metadata matches",+ },+ {+ name: "identity_by_default_with_options_format_difference_should_skip",+ currentSDLText: `CREATE TABLE "public"."invoices" (+ "id" integer GENERATED BY DEFAULT AS IDENTITY (START WITH 100 INCREMENT BY 5),+ "amount" numeric(10,2) NOT NULL+);`,+ previousUserSDLText: `CREATE TABLE public.invoices (+ id integer NOT NULL,+ amount DECIMAL(10,2) NOT NULL+);`,+ currentSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "invoices",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ IdentityGeneration: storepb.ColumnMetadata_BY_DEFAULT,+ },+ {+ Name: "amount",+ Type: "numeric(10,2)",+ Nullable: false,+ },+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "invoices_id_seq",+ DataType: "integer",+ Start: "100",+ Increment: "5",+ MinValue: "1",+ MaxValue: "2147483647",+ Cycle: false,+ OwnerTable: "invoices",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ expectedTableChanges: 0,+ description: "GENERATED BY DEFAULT AS IDENTITY with options should not create diff when metadata matches",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ t.Logf("Test description: %s", tt.description)++ // Call GetSDLDiff+ diff, err := GetSDLDiff(tt.currentSDLText, tt.previousUserSDLText, tt.currentSchema, nil)+ require.NoError(t, err)+ require.NotNil(t, diff)++ // Check the number of table changes+ require.Equal(t, tt.expectedTableChanges, len(diff.TableChanges),+ "Expected %d table changes but got %d", tt.expectedTableChanges, len(diff.TableChanges))++ // Log the detected changes for debugging+ if len(diff.TableChanges) > 0 {+ t.Log("Detected table changes:")+ for i, change := range diff.TableChanges {+ t.Logf(" %d. Table: %s.%s, Action: %v", i+1, change.SchemaName, change.TableName, change.Action)+ }+ }+ })+ }+}++// TestConvertDatabaseSchemaToSDL_SerialAndIdentitySequences tests that serial and identity sequences+// are properly merged into column definitions and not generated separately+func TestConvertDatabaseSchemaToSDL_SerialAndIdentitySequences(t *testing.T) {+ tests := []struct {+ name string+ dbSchema *model.DatabaseSchema+ shouldContainSerial bool+ shouldContainIdentity bool+ shouldNotContainSequence []string+ shouldNotContainNextval bool+ description string+ }{+ {+ name: "serial_column_should_not_generate_separate_sequence",+ dbSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "users",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ Default: "nextval('users_id_seq'::regclass)",+ },+ {+ Name: "name",+ Type: "text",+ Nullable: false,+ },+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "users_id_seq",+ DataType: "integer",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "2147483647",+ Cycle: false,+ OwnerTable: "users",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ shouldContainSerial: true,+ shouldNotContainSequence: []string{"users_id_seq"},+ shouldNotContainNextval: true,+ description: "Serial column should use serial type and not generate separate sequence",+ },+ {+ name: "identity_column_should_not_generate_separate_sequence",+ dbSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "products",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "bigint",+ Nullable: false,+ IdentityGeneration: storepb.ColumnMetadata_ALWAYS,+ },+ {+ Name: "name",+ Type: "text",+ Nullable: false,+ },+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "products_id_seq",+ DataType: "bigint",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "9223372036854775807",+ Cycle: false,+ OwnerTable: "products",+ OwnerColumn: "id",+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ shouldContainIdentity: true,+ shouldNotContainSequence: []string{"products_id_seq"},+ description: "Identity column should use GENERATED AS IDENTITY and not generate separate sequence",+ },+ {+ name: "independent_sequence_should_still_be_generated",+ dbSchema: model.NewDatabaseSchema(+ &storepb.DatabaseSchemaMetadata{+ Name: "test_db",+ Schemas: []*storepb.SchemaMetadata{+ {+ Name: "public",+ Tables: []*storepb.TableMetadata{+ {+ Name: "orders",+ Columns: []*storepb.ColumnMetadata{+ {+ Name: "id",+ Type: "integer",+ Nullable: false,+ Default: "nextval('orders_id_seq'::regclass)",+ },+ },+ },+ },+ Sequences: []*storepb.SequenceMetadata{+ {+ Name: "orders_id_seq",+ DataType: "integer",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "2147483647",+ Cycle: false,+ OwnerTable: "orders",+ OwnerColumn: "id",+ },+ {+ Name: "independent_seq",+ DataType: "integer",+ Start: "1",+ Increment: "1",+ MinValue: "1",+ MaxValue: "2147483647",+ Cycle: false,+ OwnerTable: "", // No owner - independent sequence+ },+ },+ },+ },+ },+ nil,+ nil,+ storepb.Engine_POSTGRES,+ false,+ ),+ shouldContainSerial: true,+ shouldNotContainSequence: []string{"orders_id_seq"},+ description: "Independent sequence (not owned by column) should still be generated separately",+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ t.Logf("Test description: %s", tt.description)++ // Convert database schema to SDL+ result, err := convertDatabaseSchemaToSDL(tt.dbSchema)+ require.NoError(t, err)+ require.NotEmpty(t, result)++ t.Logf("Generated SDL:\n%s", result)++ // Check for serial type if expected+ if tt.shouldContainSerial {+ require.Contains(t, result, "serial", "SDL should contain serial type")+ }++ // Check for identity syntax if expected+ if tt.shouldContainIdentity {+ require.Contains(t, result, "GENERATED", "SDL should contain GENERATED keyword")+ require.Contains(t, result, "AS IDENTITY", "SDL should contain AS IDENTITY")+ }++ // Check that specified sequences are NOT generated separately+ for _, seqName := range tt.shouldNotContainSequence {+ // The sequence name should not appear in a CREATE SEQUENCE statement+ require.NotContains(t, result, fmt.Sprintf(`CREATE SEQUENCE "%s"`, seqName),+ "SDL should not contain separate CREATE SEQUENCE for %s", seqName)+ require.NotContains(t, result, fmt.Sprintf(`CREATE SEQUENCE "public"."%s"`, seqName),+ "SDL should not contain separate CREATE SEQUENCE for public.%s", seqName)+ }++ // Check that nextval is not used if shouldNotContainNextval is true+ if tt.shouldNotContainNextval {+ require.NotContains(t, result, "nextval", "SDL should not contain nextval() when using serial")+ }++ // Check that independent sequences ARE generated (if present)+ if strings.Contains(tt.description, "independent") {+ require.Contains(t, result, "independent_seq", "SDL should contain independent sequence")+ require.Contains(t, result, "CREATE SEQUENCE", "SDL should have CREATE SEQUENCE for independent sequence")+ }+ })+ }+}diff --git a/backend/plugin/schema/pg/sequence_name_extraction_test.go b/backend/plugin/schema/pg/sequence_name_extraction_test.gonew file mode 100644index 0000000000..f1c978e93e--- /dev/null+++ b/backend/plugin/schema/pg/sequence_name_extraction_test.go@@ -0,0 +1,185 @@+package pg++import (+ "testing"++ "github.com/stretchr/testify/assert"+)++func TestExtractSequenceNameFromNextval(t *testing.T) {+ testCases := []struct {+ name string+ defaultValue string+ expectedName string+ description string+ }{+ {+ name: "Simple sequence name with single quotes",+ defaultValue: "nextval('users_id_seq')",+ expectedName: "users_id_seq",+ description: "Basic case with simple sequence name",+ },+ {+ name: "Sequence name with schema qualification",+ defaultValue: "nextval('public.users_id_seq')",+ expectedName: "users_id_seq",+ description: "Schema-qualified sequence name",+ },+ {+ name: "Sequence with regclass cast",+ defaultValue: "nextval('users_id_seq'::regclass)",+ expectedName: "users_id_seq",+ description: "Sequence with ::regclass type cast",+ },+ {+ name: "Schema and regclass",+ defaultValue: "nextval('public.users_id_seq'::regclass)",+ expectedName: "users_id_seq",+ description: "Schema-qualified sequence with regclass cast",+ },+ {+ name: "Quoted sequence name",+ defaultValue: `nextval('"Users_Id_Seq"')`,+ expectedName: "Users_Id_Seq",+ description: "Sequence name with double quotes for case sensitivity",+ },+ {+ name: "Quoted schema and sequence",+ defaultValue: `nextval('"Public"."Users_Id_Seq"')`,+ expectedName: "Users_Id_Seq",+ description: "Both schema and sequence quoted",+ },+ {+ name: "Mixed case with public schema",+ defaultValue: `nextval('public."MixedCaseSeq"')`,+ expectedName: "MixedCaseSeq",+ description: "Unquoted schema with quoted sequence",+ },+ {+ name: "Quoted schema unquoted sequence",+ defaultValue: `nextval('"Public".users_id_seq')`,+ expectedName: "users_id_seq",+ description: "Quoted schema with unquoted sequence",+ },+ {+ name: "Complex case with all features",+ defaultValue: `nextval('"MySchema"."MySeq"'::regclass)`,+ expectedName: "MySeq",+ description: "Quoted schema, quoted sequence, and regclass",+ },+ {+ name: "Uppercase NEXTVAL",+ defaultValue: `NEXTVAL('users_id_seq')`,+ expectedName: "users_id_seq",+ description: "Case-insensitive NEXTVAL function name",+ },+ {+ name: "Mixed case nextval",+ defaultValue: `NextVal('users_id_seq')`,+ expectedName: "users_id_seq",+ description: "Mixed case nextval function name",+ },+ {+ name: "Sequence with spaces",+ defaultValue: `nextval( 'users_id_seq' )`,+ expectedName: "users_id_seq",+ description: "Extra spaces around sequence name",+ },+ {+ name: "Double quoted outer quotes",+ defaultValue: `nextval("users_id_seq")`,+ expectedName: "users_id_seq",+ description: "Double quotes as outer quotes",+ },+ {+ name: "Empty default",+ defaultValue: "",+ expectedName: "",+ description: "Empty string should return empty",+ },+ {+ name: "No nextval",+ defaultValue: "42",+ expectedName: "",+ description: "Non-nextval default should return empty",+ },+ {+ name: "Malformed nextval",+ defaultValue: "nextval(",+ expectedName: "",+ description: "Malformed nextval should return empty",+ },+ {+ name: "Special characters in schema",+ defaultValue: `nextval('"schema-name"."seq_name"')`,+ expectedName: "seq_name",+ description: "Schema with special characters",+ },+ {+ name: "Multiple dots",+ defaultValue: `nextval('"my.schema"."my.seq"')`,+ expectedName: "my.seq",+ description: "Dots within quoted identifiers",+ },+ }++ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ result := extractSequenceNameFromNextval(tc.defaultValue)+ assert.Equal(t, tc.expectedName, result, tc.description)+ })+ }+}++func TestExtractIdentifierFromQualifiedName(t *testing.T) {+ testCases := []struct {+ name string+ qualifiedName string+ expected string+ description string+ }{+ {+ name: "Simple identifier",+ qualifiedName: "users_id_seq",+ expected: "users_id_seq",+ description: "No schema qualification",+ },+ {+ name: "Schema qualified",+ qualifiedName: "public.users_id_seq",+ expected: "users_id_seq",+ description: "Simple schema.sequence",+ },+ {+ name: "Both quoted",+ qualifiedName: `"Public"."Users_Id_Seq"`,+ expected: `"Users_Id_Seq"`,+ description: "Both parts quoted",+ },+ {+ name: "Only sequence quoted",+ qualifiedName: `public."MixedCase"`,+ expected: `"MixedCase"`,+ description: "Only sequence name quoted",+ },+ {+ name: "Dot in quoted identifier",+ qualifiedName: `"my.schema"."my.seq"`,+ expected: `"my.seq"`,+ description: "Dots inside quoted parts should be ignored",+ },+ {+ name: "Empty string",+ qualifiedName: "",+ expected: "",+ description: "Empty input should return empty",+ },+ }++ for _, tc := range testCases {+ t.Run(tc.name, func(t *testing.T) {+ result := extractIdentifierFromQualifiedName(tc.qualifiedName)+ assert.Equal(t, tc.expected, result, tc.description)+ })+ }+}