Skip to content

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

feat: support pg sequence for pg SDL diff (#17431)

rebelice39cb55b0db89
diff --git a/backend/plugin/schema/differ.go b/backend/plugin/schema/differ.go
index c0c95fb8f9..ebfe63ebd9 100644
--- a/backend/plugin/schema/differ.go
+++ b/backend/plugin/schema/differ.go
@@ -225,6 +225,8 @@ type SequenceDiff struct {
SequenceName string
OldSequence *storepb.SequenceMetadata
NewSequence *storepb.SequenceMetadata
+ OldASTNode any // AST node for old sequence
+ NewASTNode any // AST node for new sequence
}
// EnumTypeDiff represents changes to an enum type.
diff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.go
index 8df3f15116..c6e724593d 100644
--- a/backend/plugin/schema/pg/get_sdl_diff.go
+++ b/backend/plugin/schema/pg/get_sdl_diff.go
@@ -58,6 +58,9 @@ func GetSDLDiff(currentSDLText, previousUserSDLText string, currentSchema, previ
// Process function changes
processFunctionChanges(currentChunks, previousChunks, diff)
+ // Process sequence changes
+ processSequenceChanges(currentChunks, previousChunks, diff)
+
return diff, nil
}
@@ -1322,3 +1325,68 @@ func processFunctionChanges(currentChunks, previousChunks *schema.SDLChunks, dif
}
}
}
+
+// processSequenceChanges analyzes sequence changes between current and previous chunks
+// Following the text-first comparison pattern for performance optimization
+func processSequenceChanges(currentChunks, previousChunks *schema.SDLChunks, diff *schema.MetadataDiff) {
+ // Process current sequences to find created and modified sequences
+ for identifier, currentChunk := range currentChunks.Sequences {
+ if previousChunk, exists := previousChunks.Sequences[identifier]; exists {
+ // Sequence exists in both - check if modified by comparing text first
+ currentText := currentChunk.GetText(currentChunks.Tokens)
+ previousText := previousChunk.GetText(previousChunks.Tokens)
+ if currentText != previousText {
+ // Sequence was modified - use drop and recreate pattern (PostgreSQL standard)
+ schemaName, sequenceName := parseIdentifier(identifier)
+ diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{
+ Action: schema.MetadataDiffActionDrop,
+ SchemaName: schemaName,
+ SequenceName: sequenceName,
+ OldSequence: nil, // Will be populated when SDL drift detection is implemented
+ NewSequence: nil, // Will be populated when SDL drift detection is implemented
+ OldASTNode: previousChunk.ASTNode,
+ NewASTNode: nil,
+ })
+ diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: schemaName,
+ SequenceName: sequenceName,
+ OldSequence: nil, // Will be populated when SDL drift detection is implemented
+ NewSequence: nil, // Will be populated when SDL drift detection is implemented
+ OldASTNode: nil,
+ NewASTNode: currentChunk.ASTNode,
+ })
+ }
+ // If text is identical, skip - no changes detected
+ } else {
+ // New sequence
+ schemaName, sequenceName := parseIdentifier(identifier)
+ diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{
+ Action: schema.MetadataDiffActionCreate,
+ SchemaName: schemaName,
+ SequenceName: sequenceName,
+ OldSequence: nil,
+ NewSequence: nil,
+ OldASTNode: nil,
+ NewASTNode: currentChunk.ASTNode,
+ })
+ }
+ }
+
+ // Process previous sequences to find dropped ones
+ for identifier, previousChunk := range previousChunks.Sequences {
+ if _, exists := currentChunks.Sequences[identifier]; !exists {
+ // Sequence was dropped
+ schemaName, sequenceName := parseIdentifier(identifier)
+ diff.SequenceChanges = append(diff.SequenceChanges, &schema.SequenceDiff{
+ Action: schema.MetadataDiffActionDrop,
+ SchemaName: schemaName,
+ SequenceName: sequenceName,
+ OldSequence: nil, // Will be populated when SDL drift detection is implemented
+ NewSequence: nil,
+ OldASTNode: previousChunk.ASTNode,
+ NewASTNode: nil,
+ })
+ }
+ }
+}
diff --git a/backend/plugin/schema/pg/sequence_sdl_diff_test.go b/backend/plugin/schema/pg/sequence_sdl_diff_test.go
new file mode 100644
index 0000000000..1d8a5dea73
--- /dev/null
+++ b/backend/plugin/schema/pg/sequence_sdl_diff_test.go
@@ -0,0 +1,306 @@
+package pg
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/plugin/schema"
+)
+
+func TestSequenceSDLDiff(t *testing.T) {
+ tests := []struct {
+ name string
+ previousSDL string
+ currentSDL string
+ expectedSequenceChanges int
+ expectedActions []schema.MetadataDiffAction
+ }{
+ {
+ name: "Create new sequence",
+ previousSDL: ``,
+ currentSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL
+ );
+
+ CREATE SEQUENCE user_seq
+ START WITH 1
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+ `,
+ expectedSequenceChanges: 1,
+ expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},
+ },
+ {
+ name: "Drop sequence",
+ previousSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL
+ );
+
+ CREATE SEQUENCE user_seq
+ START WITH 1
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+ `,
+ currentSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL
+ );
+ `,
+ expectedSequenceChanges: 1,
+ expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop},
+ },
+ {
+ name: "Modify sequence (drop and recreate)",
+ previousSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL
+ );
+
+ CREATE SEQUENCE user_seq
+ START WITH 1
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+ `,
+ currentSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL
+ );
+
+ CREATE SEQUENCE user_seq
+ START WITH 10
+ INCREMENT BY 2
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 5;
+ `,
+ expectedSequenceChanges: 2, // Drop + Create
+ expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionDrop, schema.MetadataDiffActionCreate},
+ },
+ {
+ name: "No changes to sequence",
+ previousSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL
+ );
+
+ CREATE SEQUENCE user_seq
+ START WITH 1
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+ `,
+ currentSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL
+ );
+
+ CREATE SEQUENCE user_seq
+ START WITH 1
+ INCREMENT BY 1
+ NO MINVALUE
+ NO MAXVALUE
+ CACHE 1;
+ `,
+ expectedSequenceChanges: 0,
+ expectedActions: []schema.MetadataDiffAction{},
+ },
+ {
+ name: "Multiple sequences with different changes",
+ previousSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL
+ );
+
+ CREATE SEQUENCE user_seq
+ START WITH 1
+ INCREMENT BY 1;
+
+ CREATE SEQUENCE order_seq
+ START WITH 100
+ INCREMENT BY 1;
+ `,
+ currentSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL
+ );
+
+ CREATE SEQUENCE user_seq
+ START WITH 1
+ INCREMENT BY 1;
+
+ CREATE SEQUENCE product_seq
+ START WITH 1000
+ INCREMENT BY 10;
+ `,
+ expectedSequenceChanges: 2, // Drop order_seq + Create product_seq
+ expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate, schema.MetadataDiffActionDrop},
+ },
+ {
+ name: "Schema-qualified sequence 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 SEQUENCE test_schema.product_id_seq
+ START WITH 1
+ INCREMENT BY 1
+ OWNED BY test_schema.products.id;
+ `,
+ expectedSequenceChanges: 1,
+ expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},
+ },
+ {
+ name: "Sequence with complex options",
+ previousSDL: `
+ CREATE TABLE orders (
+ id BIGINT PRIMARY KEY,
+ amount DECIMAL(10,2)
+ );
+ `,
+ currentSDL: `
+ CREATE TABLE orders (
+ id BIGINT PRIMARY KEY,
+ amount DECIMAL(10,2)
+ );
+
+ CREATE SEQUENCE order_id_seq
+ AS BIGINT
+ START WITH 1000000
+ INCREMENT BY 1
+ MINVALUE 1
+ MAXVALUE 9223372036854775807
+ CACHE 50
+ NO CYCLE
+ OWNED BY orders.id;
+ `,
+ expectedSequenceChanges: 1,
+ expectedActions: []schema.MetadataDiffAction{schema.MetadataDiffActionCreate},
+ },
+ {
+ name: "SERIAL column changes (implicit sequence handling)",
+ previousSDL: `
+ CREATE TABLE users (
+ name VARCHAR(255) NOT NULL
+ );
+ `,
+ currentSDL: `
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(255) NOT NULL
+ );
+ `,
+ expectedSequenceChanges: 0, // SERIAL sequences are handled at table level, not explicit sequence level
+ expectedActions: []schema.MetadataDiffAction{},
+ },
+ }
+
+ 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.expectedSequenceChanges, len(diff.SequenceChanges),
+ "Expected %d sequence changes, got %d", tt.expectedSequenceChanges, len(diff.SequenceChanges))
+
+ // Check that the actions match expectations
+ var actualActions []schema.MetadataDiffAction
+ for _, seqDiff := range diff.SequenceChanges {
+ actualActions = append(actualActions, seqDiff.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, seqDiff := range diff.SequenceChanges {
+ switch seqDiff.Action {
+ case schema.MetadataDiffActionCreate:
+ assert.NotNil(t, seqDiff.NewASTNode,
+ "Sequence diff %d should have NewASTNode for CREATE action", i)
+ assert.Nil(t, seqDiff.OldASTNode,
+ "Sequence diff %d should not have OldASTNode for CREATE action", i)
+ case schema.MetadataDiffActionDrop:
+ assert.NotNil(t, seqDiff.OldASTNode,
+ "Sequence diff %d should have OldASTNode for DROP action", i)
+ assert.Nil(t, seqDiff.NewASTNode,
+ "Sequence diff %d should not have NewASTNode for DROP action", i)
+ default:
+ t.Errorf("Unexpected action %v for sequence diff %d", seqDiff.Action, i)
+ }
+ }
+ })
+ }
+}
+
+func TestSequenceIdentifierParsing(t *testing.T) {
+ tests := []struct {
+ name string
+ identifier string
+ expectedSchema string
+ expectedSequence string
+ }{
+ {
+ name: "Simple sequence name",
+ identifier: "my_seq",
+ expectedSchema: "public",
+ expectedSequence: "my_seq",
+ },
+ {
+ name: "Schema-qualified sequence name",
+ identifier: "test_schema.my_seq",
+ expectedSchema: "test_schema",
+ expectedSequence: "my_seq",
+ },
+ {
+ name: "Public schema explicit",
+ identifier: "public.user_id_seq",
+ expectedSchema: "public",
+ expectedSequence: "user_id_seq",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ schema, sequence := parseIdentifier(tt.identifier)
+ assert.Equal(t, tt.expectedSchema, schema)
+ assert.Equal(t, tt.expectedSequence, sequence)
+ })
+ }
+}