Skip to content

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

chore: add testcontainer-based test for get database metadata (#16509)

rebeliced6c2bd4b4b8c
diff --git a/backend/plugin/schema/pg/get_database_metadata.go b/backend/plugin/schema/pg/get_database_metadata.go
index 24bef3765a..f278c9b17b 100644
--- a/backend/plugin/schema/pg/get_database_metadata.go
+++ b/backend/plugin/schema/pg/get_database_metadata.go
@@ -26,12 +26,14 @@ func GetDatabaseMetadata(schemaText string) (*storepb.DatabaseSchemaMetadata, er
}
extractor := &metadataExtractor{
- currentDatabase: "",
- currentSchema: "public",
- schemas: make(map[string]*storepb.SchemaMetadata),
- tables: make(map[tableKey]*storepb.TableMetadata),
- sequences: make(map[string]*storepb.SequenceMetadata),
- extensions: make(map[string]*storepb.ExtensionMetadata),
+ currentDatabase: "",
+ currentSchema: "public",
+ schemas: make(map[string]*storepb.SchemaMetadata),
+ tables: make(map[tableKey]*storepb.TableMetadata),
+ partitionTables: make(map[tableKey]bool),
+ partitionExpressions: make(map[tableKey]string),
+ sequences: make(map[string]*storepb.SequenceMetadata),
+ extensions: make(map[string]*storepb.ExtensionMetadata),
}
// Always ensure public schema exists
@@ -85,13 +87,15 @@ type tableKey struct {
type metadataExtractor struct {
*parser.BasePostgreSQLParserListener
- currentDatabase string
- currentSchema string
- schemas map[string]*storepb.SchemaMetadata
- tables map[tableKey]*storepb.TableMetadata
- sequences map[string]*storepb.SequenceMetadata
- extensions map[string]*storepb.ExtensionMetadata
- err error
+ currentDatabase string
+ currentSchema string
+ schemas map[string]*storepb.SchemaMetadata
+ tables map[tableKey]*storepb.TableMetadata
+ partitionTables map[tableKey]bool
+ partitionExpressions map[tableKey]string
+ sequences map[string]*storepb.SequenceMetadata
+ extensions map[string]*storepb.ExtensionMetadata
+ err error
}
// Helper function to get or create schema
@@ -139,8 +143,11 @@ func (e *metadataExtractor) getOrCreateTable(schemaName, tableName string) *stor
Partitions: nil,
}
- schema := e.getOrCreateSchema(schemaName)
- schema.Tables = append(schema.Tables, table)
+ // Only add to schema's table list if it's not a partition table
+ if !e.partitionTables[key] {
+ schema := e.getOrCreateSchema(schemaName)
+ schema.Tables = append(schema.Tables, table)
+ }
e.tables[key] = table
return table
@@ -175,6 +182,43 @@ func (e *metadataExtractor) EnterCreatestmt(ctx *parser.CreatestmtContext) {
qualifiedName := ctx.Qualified_name(0)
schemaName, tableName := e.extractSchemaAndTableName(qualifiedName)
+ // Check if this is a partition table (CREATE TABLE ... PARTITION OF ...)
+ // We'll check if there's a second qualified_name which would be the parent table
+ if ctx.Qualified_name(1) != nil && ctx.PARTITION() != nil && ctx.OF() != nil {
+ // Mark this table as a partition
+ key := tableKey{schema: schemaName, table: tableName}
+ e.partitionTables[key] = true
+
+ // Get the parent table and add this partition to it
+ parentSchema, parentTable := e.extractSchemaAndTableName(ctx.Qualified_name(1))
+ parentTableMetadata := e.getOrCreateTable(parentSchema, parentTable)
+
+ // Create partition metadata
+ partition := &storepb.TablePartitionMetadata{
+ Name: tableName,
+ }
+
+ // Extract FOR VALUES clause if present
+ if ctx.Partitionboundspec() != nil {
+ partition.Value = e.extractPartitionBoundSpec(ctx.Partitionboundspec())
+ }
+
+ // Get the parent table's partition expression
+ parentKey := tableKey{schema: parentSchema, table: parentTable}
+ if expr, ok := e.partitionExpressions[parentKey]; ok {
+ partition.Expression = expr
+ }
+
+ if parentTableMetadata.Partitions == nil {
+ parentTableMetadata.Partitions = []*storepb.TablePartitionMetadata{}
+ }
+ parentTableMetadata.Partitions = append(parentTableMetadata.Partitions, partition)
+
+ // Don't continue processing this table as a regular table
+ return
+ }
+
+ // Create the table (it's not a partition table)
tableMetadata := e.getOrCreateTable(schemaName, tableName)
// Extract table elements (columns, constraints)
@@ -182,9 +226,11 @@ func (e *metadataExtractor) EnterCreatestmt(ctx *parser.CreatestmtContext) {
e.extractTableElements(tableElementList, tableMetadata, schemaName)
}
- // Extract partition info
+ // Extract partition info and store the expression
if partitionSpec := ctx.Optpartitionspec(); partitionSpec != nil {
- e.extractPartitionSpec(partitionSpec, tableMetadata)
+ // Store the partition expression for this table
+ key := tableKey{schema: schemaName, table: tableName}
+ e.partitionExpressions[key] = e.extractPartitionExpression(partitionSpec)
}
}
@@ -250,7 +296,17 @@ func (e *metadataExtractor) extractColumnDef(ctx parser.IColumnDefContext, table
// Extract data type
if ctx.Typename() != nil {
- column.Type = extractTypeName(ctx.Typename())
+ // Get the raw type name to check for SERIAL
+ rawTypeName := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx.Typename())
+ rawTypeName = strings.ToLower(rawTypeName)
+
+ // SERIAL columns are implicitly NOT NULL
+ if rawTypeName == "serial" || rawTypeName == "bigserial" || rawTypeName == "smallserial" ||
+ rawTypeName == "serial4" || rawTypeName == "serial8" || rawTypeName == "serial2" {
+ column.Nullable = false
+ }
+
+ column.Type = e.extractTypeNameWithSchema(ctx.Typename(), schemaName)
}
// Extract column constraints
@@ -276,7 +332,129 @@ func extractTypeName(ctx parser.ITypenameContext) string {
return ""
}
// Get the full text representation of the type
- return ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
+ typeName := ctx.GetParser().GetTokenStream().GetTextFromRuleContext(ctx)
+ // Convert to lowercase to match sync.go output
+ typeName = strings.ToLower(typeName)
+
+ // Convert common type variations to match sync.go output
+ // varchar(n) -> character varying(n)
+ if strings.HasPrefix(typeName, "varchar(") {
+ return "character varying" + typeName[7:]
+ }
+ if typeName == "varchar" {
+ return "character varying"
+ }
+
+ // SERIAL types are stored as integer/bigint in the catalog
+ if typeName == "serial" || typeName == "serial4" {
+ return "integer"
+ }
+ if typeName == "bigserial" || typeName == "serial8" {
+ return "bigint"
+ }
+ if typeName == "smallserial" || typeName == "serial2" {
+ return "smallint"
+ }
+
+ // Handle array types: text[] -> _text
+ if strings.HasSuffix(typeName, "[]") {
+ // PostgreSQL internal representation uses underscore prefix
+ baseType := typeName[:len(typeName)-2]
+ return "_" + baseType
+ }
+
+ // Handle timestamp without explicit timezone
+ if typeName == "timestamp" {
+ return "timestamp without time zone"
+ }
+
+ // Handle decimal -> numeric (sync.go reports decimal as numeric without precision)
+ if strings.HasPrefix(typeName, "decimal(") {
+ // sync.go returns just "numeric" without precision for decimal types
+ return "numeric"
+ }
+ if typeName == "decimal" {
+ return "numeric"
+ }
+
+ return typeName
+}
+
+// extractTypeNameWithSchema extracts the type name and adds schema prefix for custom types
+func (*metadataExtractor) extractTypeNameWithSchema(ctx parser.ITypenameContext, schemaName string) string {
+ if ctx == nil {
+ return ""
+ }
+
+ typeName := extractTypeName(ctx)
+
+ // Check if this is a built-in PostgreSQL type
+ if isBuiltInType(typeName) {
+ return typeName
+ }
+
+ // Check if the type already has a schema prefix
+ if strings.Contains(typeName, ".") {
+ return typeName
+ }
+
+ // For custom types (like enums), add the schema prefix
+ return fmt.Sprintf("%s.%s", schemaName, typeName)
+}
+
+// isBuiltInType checks if a type is a built-in PostgreSQL type
+func isBuiltInType(typeName string) bool {
+ // Remove any precision/scale information for checking
+ baseType := typeName
+ if idx := strings.Index(typeName, "("); idx != -1 {
+ baseType = typeName[:idx]
+ }
+
+ // Common built-in PostgreSQL types
+ builtInTypes := map[string]bool{
+ "integer": true, "int": true, "int4": true,
+ "bigint": true, "int8": true,
+ "smallint": true, "int2": true,
+ "numeric": true, "decimal": true,
+ "real": true, "float4": true,
+ "double precision": true, "float8": true,
+ "serial": true, "serial4": true,
+ "bigserial": true, "serial8": true,
+ "character": true, "char": true,
+ "character varying": true, "varchar": true,
+ "text": true,
+ "boolean": true, "bool": true,
+ "date": true,
+ "time": true, "time without time zone": true, "time with time zone": true, "timetz": true,
+ "timestamp": true, "timestamp without time zone": true, "timestamp with time zone": true, "timestamptz": true,
+ "interval": true,
+ "uuid": true,
+ "json": true, "jsonb": true,
+ "xml": true,
+ "bytea": true,
+ "bit": true, "bit varying": true,
+ "money": true,
+ "inet": true, "cidr": true, "macaddr": true, "macaddr8": true,
+ "point": true, "line": true, "lseg": true, "box": true,
+ "path": true, "polygon": true, "circle": true,
+ "tsquery": true, "tsvector": true,
+ "pg_lsn": true,
+ "oid": true, "regclass": true, "regproc": true,
+ "name": true,
+ }
+
+ // Also check for array types (ending with [] or starting with _)
+ if strings.HasSuffix(baseType, "[]") {
+ arrayBase := baseType[:len(baseType)-2]
+ return builtInTypes[arrayBase]
+ }
+ if strings.HasPrefix(baseType, "_") {
+ // PostgreSQL internal array notation _typename
+ arrayBase := baseType[1:]
+ return builtInTypes[arrayBase]
+ }
+
+ return builtInTypes[baseType]
}
// extractColumnConstraint extracts column-level constraints
@@ -305,6 +483,7 @@ func (e *metadataExtractor) extractColumnConstraint(ctx parser.IColconstraintele
IsConstraint: true,
Expressions: []string{column.Name},
Descending: []bool{false},
+ Type: "btree",
}
// Use provided constraint name or generate one
if constraintName != "" {
@@ -320,6 +499,7 @@ func (e *metadataExtractor) extractColumnConstraint(ctx parser.IColconstraintele
IsConstraint: true,
Expressions: []string{column.Name},
Descending: []bool{false},
+ Type: "btree",
}
// Use provided constraint name or generate one
if constraintName != "" {
@@ -404,6 +584,7 @@ func (e *metadataExtractor) extractTableConstraint(ctx parser.ITableconstraintCo
IsConstraint: true,
Expressions: []string{},
Descending: []bool{},
+ Type: "btree",
}
if index.Name == "" {
index.Name = fmt.Sprintf("%s_pkey", table.Name)
@@ -426,6 +607,7 @@ func (e *metadataExtractor) extractTableConstraint(ctx parser.ITableconstraintCo
IsConstraint: true,
Expressions: []string{},
Descending: []bool{},
+ Type: "btree",
}
// Extract columns
if optColumnList := constraintElem.Opt_column_list(); optColumnList != nil && optColumnList.Columnlist() != nil {
@@ -592,28 +774,20 @@ func extractColumnList(ctx parser.IColumnlistContext) []string {
return columns
}
-// extractPartitionSpec extracts partition specification
-func (*metadataExtractor) extractPartitionSpec(ctx parser.IOptpartitionspecContext, table *storepb.TableMetadata) {
+// extractPartitionExpression extracts the partition expression (e.g., "RANGE (sale_date)")
+func (*metadataExtractor) extractPartitionExpression(ctx parser.IOptpartitionspecContext) string {
if ctx == nil {
- return
+ return ""
}
partitionSpec := ctx.Partitionspec()
if partitionSpec == nil {
- return
+ return ""
}
// Extract the full partition expression including the method (e.g., "RANGE (sale_date)")
expression := ctx.GetParser().GetTokenStream().GetTextFromTokens(partitionSpec.Colid().GetStart(), partitionSpec.CLOSE_PAREN().GetSymbol())
-
- // Create a partition metadata entry for the partitioning specification
- partition := &storepb.TablePartitionMetadata{
- Expression: expression,
- }
- if table.Partitions == nil {
- table.Partitions = []*storepb.TablePartitionMetadata{}
- }
- table.Partitions = append(table.Partitions, partition)
+ return expression
}
// EnterAltertablestmt is called when entering an ALTER TABLE statement
@@ -962,6 +1136,15 @@ func (e *metadataExtractor) EnterIndexstmt(ctx *parser.IndexstmtContext) {
index.Unique = true
}
+ // Extract index method (BTREE, HASH, GIN, GIST, etc.)
+ // Default to btree if not specified
+ index.Type = "btree"
+ if accessMethod := ctx.Access_method_clause(); accessMethod != nil {
+ if accessMethod.USING() != nil && accessMethod.Name() != nil {
+ index.Type = strings.ToLower(pgparser.NormalizePostgreSQLName(accessMethod.Name()))
+ }
+ }
+
// Extract index parameters (columns/expressions)
if indexParams := ctx.Index_params(); indexParams != nil {
for _, indexElem := range indexParams.AllIndex_elem() {
diff --git a/backend/plugin/schema/pg/get_database_metadata_test.go b/backend/plugin/schema/pg/get_database_metadata_test.go
deleted file mode 100644
index 39850078eb..0000000000
--- a/backend/plugin/schema/pg/get_database_metadata_test.go
+++ /dev/null
@@ -1,59 +0,0 @@
-package pg
-
-import (
- "encoding/json"
- "io"
- "os"
- "testing"
-
- "github.com/stretchr/testify/require"
- "gopkg.in/yaml.v3"
-)
-
-type getDatabaseMetadataCase struct {
- Input string `yaml:"input"`
- MetadataResult string `yaml:"metadata_result"`
-}
-
-func TestGetDatabaseMetadata(t *testing.T) {
- tests := []getDatabaseMetadataCase{}
- const (
- record = false
- )
- var (
- filepath = "testdata/get_database_metadata.yaml"
- )
-
- a := require.New(t)
- yamlFile, err := os.Open(filepath)
- a.NoError(err)
- defer yamlFile.Close()
-
- byteValue, err := io.ReadAll(yamlFile)
- a.NoError(err)
- a.NoError(yaml.Unmarshal(byteValue, &tests))
-
- for i, tc := range tests {
- // Get metadata from SQL
- meta, err := GetDatabaseMetadata(tc.Input)
- a.NoError(err, "Test case %d: Failed to get metadata", i)
-
- // Convert metadata to JSON for comparison
- jsonBytes, err := json.MarshalIndent(meta, "", " ")
- a.NoError(err)
- metadataResult := string(jsonBytes)
-
- if record {
- tests[i].MetadataResult = metadataResult
- } else {
- a.Equal(tc.MetadataResult, metadataResult, "Test case %d metadata mismatch: %s", i, tc.Input)
- }
- }
-
- if record {
- byteValue, err := yaml.Marshal(tests)
- a.NoError(err)
- err = os.WriteFile(filepath, byteValue, 0644)
- a.NoError(err)
- }
-}
diff --git a/backend/plugin/schema/pg/get_database_metadata_testcontainer_test.go b/backend/plugin/schema/pg/get_database_metadata_testcontainer_test.go
new file mode 100644
index 0000000000..50edc0e351
--- /dev/null
+++ b/backend/plugin/schema/pg/get_database_metadata_testcontainer_test.go
@@ -0,0 +1,736 @@
+package pg
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/jackc/pgx/v5"
+ "github.com/jackc/pgx/v5/stdlib"
+ "github.com/stretchr/testify/require"
+ "github.com/testcontainers/testcontainers-go"
+ "github.com/testcontainers/testcontainers-go/modules/postgres"
+ "github.com/testcontainers/testcontainers-go/wait"
+
+ "github.com/bytebase/bytebase/backend/plugin/db"
+ pgdb "github.com/bytebase/bytebase/backend/plugin/db/pg"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
+)
+
+// TestGetDatabaseMetadataWithTestcontainer tests the get_database_metadata function
+// by comparing its output with the metadata retrieved from a real PostgreSQL instance.
+func TestGetDatabaseMetadataWithTestcontainer(t *testing.T) {
+ ctx := context.Background()
+
+ // Start PostgreSQL container
+ pgContainer, err := postgres.Run(ctx,
+ "postgres:16-alpine",
+ postgres.WithDatabase("test_db"),
+ postgres.WithUsername("postgres"),
+ postgres.WithPassword("test"),
+ testcontainers.WithWaitStrategy(
+ wait.ForLog("database system is ready to accept connections").
+ WithOccurrence(2).
+ WithStartupTimeout(5*time.Minute),
+ ),
+ )
+ require.NoError(t, err)
+ defer func() {
+ if err := pgContainer.Terminate(ctx); err != nil {
+ t.Logf("failed to terminate container: %s", err)
+ }
+ }()
+
+ // Get connection string
+ connectionString, err := pgContainer.ConnectionString(ctx, "sslmode=disable")
+ require.NoError(t, err)
+
+ // Connect to the database
+ connConfig, err := pgx.ParseConfig(connectionString)
+ require.NoError(t, err)
+ db := stdlib.OpenDB(*connConfig)
+ defer db.Close()
+
+ // Test cases with various PostgreSQL features
+ testCases := []struct {
+ name string
+ ddl string
+ }{
+ {
+ name: "basic_tables_with_constraints",
+ ddl: `
+CREATE TABLE users (
+ id SERIAL PRIMARY KEY,
+ username VARCHAR(50) NOT NULL UNIQUE,
+ email VARCHAR(100) NOT NULL,
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ updated_at TIMESTAMP
+);
+
+CREATE TABLE posts (
+ id SERIAL PRIMARY KEY,
+ user_id INTEGER NOT NULL,
+ title VARCHAR(200) NOT NULL,
+ content TEXT,
+ published BOOLEAN DEFAULT false,
+ CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
+);
+
+CREATE INDEX idx_posts_user_id ON posts(user_id);
+CREATE INDEX idx_posts_published ON posts(published) WHERE published = true;
+`,
+ },
+ {
+ name: "sequences_and_custom_types",
+ ddl: `
+CREATE TYPE status_enum AS ENUM ('pending', 'active', 'inactive', 'deleted');
+CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral');
+
+CREATE SEQUENCE custom_id_seq START WITH 1000 INCREMENT BY 10;
+
+CREATE TABLE items (
+ id INTEGER DEFAULT nextval('custom_id_seq') PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ status status_enum DEFAULT 'pending',
+ user_mood mood
+);
+`,
+ },
+ {
+ name: "views_and_functions",
+ ddl: `
+CREATE TABLE employees (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) NOT NULL,
+ department VARCHAR(50),
+ salary DECIMAL(10, 2)
+);
+
+CREATE VIEW active_employees AS
+SELECT id, name, department
+FROM employees
+WHERE department IS NOT NULL;
+
+CREATE FUNCTION get_employee_count(dept VARCHAR) RETURNS INTEGER AS $$
+BEGIN
+ RETURN (SELECT COUNT(*) FROM employees WHERE department = dept);
+END;
+$$ LANGUAGE plpgsql;
+
+CREATE FUNCTION calculate_bonus(emp_id INTEGER) RETURNS DECIMAL AS $$
+DECLARE
+ emp_salary DECIMAL;
+BEGIN
+ SELECT salary INTO emp_salary FROM employees WHERE id = emp_id;
+ RETURN emp_salary * 0.1;
+END;
+$$ LANGUAGE plpgsql;
+`,
+ },
+ {
+ name: "partitioned_tables",
+ ddl: `
+CREATE TABLE sales (
+ id SERIAL,
+ sale_date DATE NOT NULL,
+ amount DECIMAL(10, 2) NOT NULL,
+ region VARCHAR(50) NOT NULL
+) PARTITION BY RANGE (sale_date);
+
+CREATE TABLE sales_2023_q1 PARTITION OF sales
+FOR VALUES FROM ('2023-01-01') TO ('2023-04-01');
+
+CREATE TABLE sales_2023_q2 PARTITION OF sales
+FOR VALUES FROM ('2023-04-01') TO ('2023-07-01');
+
+CREATE TABLE sales_2023_q3 PARTITION OF sales
+FOR VALUES FROM ('2023-07-01') TO ('2023-10-01');
+
+CREATE TABLE sales_2023_q4 PARTITION OF sales
+FOR VALUES FROM ('2023-10-01') TO ('2024-01-01');
+`,
+ },
+ {
+ name: "extensions_and_advanced_features",
+ ddl: `
+CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
+
+CREATE TABLE documents (
+ id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
+ title VARCHAR(200) NOT NULL,
+ content TEXT,
+ tags TEXT[],
+ metadata JSONB,
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE INDEX idx_documents_metadata ON documents USING GIN (metadata);
+CREATE INDEX idx_documents_tags ON documents USING GIN (tags);
+`,
+ },
+ {
+ name: "indexes_with_asc_desc",
+ ddl: `
+CREATE TABLE orders (
+ id SERIAL PRIMARY KEY,
+ customer_id INTEGER NOT NULL,
+ order_date DATE NOT NULL,
+ total_amount DECIMAL(10, 2) NOT NULL,
+ status VARCHAR(20)
+);
+
+-- Index with explicit DESC
+CREATE INDEX idx_orders_date_desc ON orders(order_date DESC);
+
+-- Index with multiple columns, mixed ASC/DESC
+CREATE INDEX idx_orders_customer_date ON orders(customer_id ASC, order_date DESC);
+
+-- Index with expressions and DESC
+CREATE INDEX idx_orders_year_month ON orders(EXTRACT(YEAR FROM order_date) DESC, EXTRACT(MONTH FROM order_date) ASC);
+
+-- Unique index with DESC
+CREATE UNIQUE INDEX idx_orders_customer_status ON orders(customer_id, status DESC) WHERE status IS NOT NULL;
+`,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ // Create a new database for each test case
+ dbName := fmt.Sprintf("test_%s", tc.name)
+ _, err := db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
+ require.NoError(t, err)
+ defer func() {
+ _, _ = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", dbName))
+ }()
+
+ // Connect to the test database
+ testConnConfig := *connConfig
+ testConnConfig.Database = dbName
+ testDB := stdlib.OpenDB(testConnConfig)
+ defer testDB.Close()
+
+ // Execute the DDL
+ _, err = testDB.Exec(tc.ddl)
+ require.NoError(t, err)
+
+ // Get metadata using Driver.SyncDBSchema
+ syncMetadata, err := getSyncMetadata(ctx, &testConnConfig, dbName)
+ require.NoError(t, err)
+
+ // Get metadata using get_database_metadata
+ parseMetadata, err := GetDatabaseMetadata(tc.ddl)
+ require.NoError(t, err)
+
+ // Compare the two metadata structures
+ compareMetadata(t, syncMetadata, parseMetadata)
+ })
+ }
+}
+
+// getSyncMetadata retrieves metadata from the live database using Driver.SyncDBSchema
+func getSyncMetadata(ctx context.Context, connConfig *pgx.ConnConfig, dbName string) (*storepb.DatabaseSchemaMetadata, error) {
+ // Create a driver instance using the pg package
+ driver := &pgdb.Driver{}
+
+ // Create connection config
+ config := db.ConnectionConfig{
+ DataSource: &storepb.DataSource{
+ Type: storepb.DataSourceType_ADMIN,
+ Username: connConfig.User,
+ Host: connConfig.Host,
+ Port: fmt.Sprintf("%d", connConfig.Port),
+ Database: dbName,
+ },
+ Password: connConfig.Password,
+ ConnectionContext: db.ConnectionContext{
+ EngineVersion: "16.0", // PostgreSQL 16
+ DatabaseName: dbName,
+ },
+ }
+
+ // Open connection using the driver
+ openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)
+ if err != nil {
+ return nil, err
+ }
+ defer openedDriver.Close(ctx)
+
+ // Use SyncDBSchema to get the metadata
+ pgDriver, ok := openedDriver.(*pgdb.Driver)
+ if !ok {
+ return nil, errors.New("failed to cast to pg.Driver")
+ }
+
+ metadata, err := pgDriver.SyncDBSchema(ctx)
+ if err != nil {
+ return nil, err
+ }
+
+ return metadata, nil
+}
+
+// compareMetadata compares metadata from sync.go and get_database_metadata
+func compareMetadata(t *testing.T, syncMeta, parseMeta *storepb.DatabaseSchemaMetadata) {
+ // Compare schemas
+ require.Equal(t, len(syncMeta.Schemas), len(parseMeta.Schemas), "number of schemas should match")
+
+ // Find the public schema in both
+ var syncPublic, parsePublic *storepb.SchemaMetadata
+ for _, schema := range syncMeta.Schemas {
+ if schema.Name == "public" {
+ syncPublic = schema
+ break
+ }
+ }
+ for _, schema := range parseMeta.Schemas {
+ if schema.Name == "public" {
+ parsePublic = schema
+ break
+ }
+ }
+
+ require.NotNil(t, syncPublic, "sync metadata should have public schema")
+ require.NotNil(t, parsePublic, "parse metadata should have public schema")
+
+ // Compare tables
+ compareTables(t, syncPublic.Tables, parsePublic.Tables)
+
+ // Compare views
+ compareViews(t, syncPublic.Views, parsePublic.Views)
+
+ // Compare functions
+ compareFunctions(t, syncPublic.Functions, parsePublic.Functions)
+
+ // Compare sequences
+ compareSequences(t, syncPublic.Sequences, parsePublic.Sequences)
+
+ // Compare enums
+ compareEnums(t, syncPublic.EnumTypes, parsePublic.EnumTypes)
+
+ // Compare extensions
+ compareExtensions(t, syncMeta.Extensions, parseMeta.Extensions)
+}
+
+// normalizeExpression normalizes an expression for comparison by:
+// - Converting to lowercase
+// - Removing extra whitespace
+// - Normalizing quotes
+func normalizeExpression(expr string) string {
+ // Convert to lowercase
+ expr = strings.ToLower(expr)
+
+ // Replace multiple spaces with single space
+ expr = strings.Join(strings.Fields(expr), " ")
+
+ // Remove spaces around parentheses
+ expr = strings.ReplaceAll(expr, " (", "(")
+ expr = strings.ReplaceAll(expr, "( ", "(")
+ expr = strings.ReplaceAll(expr, " )", ")")
+ expr = strings.ReplaceAll(expr, ") ", ")")
+
+ // Normalize quotes around identifiers
+ expr = strings.ReplaceAll(expr, "'", "")
+
+ return expr
+}
+
+// normalizeSQL normalizes SQL for comparison by:
+// - Converting to lowercase
+// - Removing extra whitespace
+// - Removing trailing semicolons
+// - Removing schema qualifiers for common schemas
+// - Normalizing parentheses
+func normalizeSQL(sql string) string {
+ // Convert to lowercase
+ sql = strings.ToLower(sql)
+
+ // Replace multiple spaces/newlines with single space
+ sql = strings.Join(strings.Fields(sql), " ")
+
+ // Remove trailing semicolons
+ sql = strings.TrimSuffix(sql, ";")
+
+ // Remove schema qualifiers for public schema
+ // This handles cases like "public.table_name" -> "table_name"
+ sql = strings.ReplaceAll(sql, "public.", "")
+
+ // Handle PostgreSQL's tendency to wrap WHERE conditions in parentheses
+ // e.g., "WHERE (condition)" -> "WHERE condition"
+ // We need to be careful to only remove the outermost parentheses around the entire WHERE clause
+ whereIndex := strings.Index(sql, "where (")
+ if whereIndex >= 0 {
+ // Find the matching closing parenthesis for the WHERE clause
+ afterWhere := sql[whereIndex+7:] // Skip "where ("
+ openCount := 1
+ closeIndex := -1
+
+ // Find the matching closing parenthesis
+ for i, ch := range afterWhere {
+ if ch == '(' {
+ openCount++
+ } else if ch == ')' {
+ openCount--
+ if openCount == 0 {
+ closeIndex = i
+ break
+ }
+ }
+ }
+
+ // If we found the matching closing parenthesis and it's at the end or followed by end/order/group
+ if closeIndex >= 0 {
+ beforeWhere := sql[:whereIndex+6] // Include "where "
+ afterCloseParen := ""
+ if whereIndex+7+closeIndex+1 < len(sql) {
+ afterCloseParen = sql[whereIndex+7+closeIndex+1:]
+ }
+
+ // Check if the closing paren is at the end or followed by valid SQL keywords
+ if afterCloseParen == "" || strings.HasPrefix(strings.TrimSpace(afterCloseParen), "order by") ||
+ strings.HasPrefix(strings.TrimSpace(afterCloseParen), "group by") ||
+ strings.HasPrefix(strings.TrimSpace(afterCloseParen), "limit") {
+ // Remove the parentheses
+ sql = beforeWhere + afterWhere[:closeIndex] + afterCloseParen
+ }
+ }
+ }
+
+ // Final trim
+ sql = strings.TrimSpace(sql)
+
+ return sql
+}
+
+// normalizeSignature normalizes function signatures for comparison
+func normalizeSignature(sig string) string {
+ // Convert to lowercase
+ sig = strings.ToLower(sig)
+
+ // Remove extra spaces
+ sig = strings.Join(strings.Fields(sig), " ")
+
+ // Remove spaces around parentheses and commas
+ sig = strings.ReplaceAll(sig, " (", "(")
+ sig = strings.ReplaceAll(sig, "( ", "(")
+ sig = strings.ReplaceAll(sig, " )", ")")
+ sig = strings.ReplaceAll(sig, ") ", ")")
+ sig = strings.ReplaceAll(sig, " ,", ",")
+ sig = strings.ReplaceAll(sig, ", ", ",")
+
+ // Remove quotes around function names
+ sig = strings.ReplaceAll(sig, "\"", "")
+
+ return sig
+}
+
+func compareTables(t *testing.T, syncTables, parseTables []*storepb.TableMetadata) {
+ // Log the tables found for debugging
+ t.Logf("Sync tables: %d", len(syncTables))
+ for _, table := range syncTables {
+ t.Logf(" - %s", table.Name)
+ }
+ t.Logf("Parse tables: %d", len(parseTables))
+ for _, table := range parseTables {
+ t.Logf(" - %s", table.Name)
+ }
+
+ require.Equal(t, len(syncTables), len(parseTables), "number of tables should match")
+
+ // Create maps for easier comparison
+ syncMap := make(map[string]*storepb.TableMetadata)
+ for _, table := range syncTables {
+ syncMap[table.Name] = table
+ }
+
+ parseMap := make(map[string]*storepb.TableMetadata)
+ for _, table := range parseTables {
+ parseMap[table.Name] = table
+ }
+
+ // Compare each table
+ for name, syncTable := range syncMap {
+ parseTable, exists := parseMap[name]
+ require.True(t, exists, "table %s should exist in parsed metadata", name)
+
+ // Compare columns
+ compareColumns(t, name, syncTable.Columns, parseTable.Columns)
+
+ // Compare indexes
+ compareIndexes(t, name, syncTable.Indexes, parseTable.Indexes)
+
+ // Compare foreign keys
+ compareForeignKeys(t, name, syncTable.ForeignKeys, parseTable.ForeignKeys)
+
+ // Compare partitions
+ comparePartitions(t, name, syncTable.Partitions, parseTable.Partitions)
+ }
+}
+
+func compareColumns(t *testing.T, tableName string, syncCols, parseCols []*storepb.ColumnMetadata) {
+ require.Equal(t, len(syncCols), len(parseCols), "table %s: number of columns should match", tableName)
+
+ // Create maps for easier comparison
+ syncMap := make(map[string]*storepb.ColumnMetadata)
+ for _, col := range syncCols {
+ syncMap[col.Name] = col
+ }
+
+ for _, parseCol := range parseCols {
+ syncCol, exists := syncMap[parseCol.Name]
+ require.True(t, exists, "table %s: column %s should exist in sync metadata", tableName, parseCol.Name)
+
+ // Compare column properties
+ require.Equal(t, syncCol.Type, parseCol.Type, "table %s, column %s: type should match", tableName, parseCol.Name)
+ require.Equal(t, syncCol.Nullable, parseCol.Nullable, "table %s, column %s: nullable should match", tableName, parseCol.Name)
+
+ // Compare default values if both exist
+ if syncCol.DefaultValue != nil && parseCol.DefaultValue != nil {
+ // Default values might be represented differently, so we just check they exist
+ t.Logf("table %s, column %s: default values exist in both", tableName, parseCol.Name)
+ }
+ }
+}
+
+func compareIndexes(t *testing.T, tableName string, syncIndexes, parseIndexes []*storepb.IndexMetadata) {
+ // Create maps for easier comparison
+ syncMap := make(map[string]*storepb.IndexMetadata)
+ for _, idx := range syncIndexes {
+ syncMap[idx.Name] = idx
+ }
+
+ parseMap := make(map[string]*storepb.IndexMetadata)
+ for _, idx := range parseIndexes {
+ parseMap[idx.Name] = idx
+ }
+
+ // Compare common indexes
+ for name, parseIdx := range parseMap {
+ syncIdx, exists := syncMap[name]
+ if !exists {
+ // Some indexes might be system-generated and not in DDL
+ t.Logf("table %s: index %s exists in parse but not in sync (might be implicit)", tableName, name)
+ continue
+ }
+
+ require.Equal(t, syncIdx.Primary, parseIdx.Primary, "table %s, index %s: primary should match", tableName, name)
+ require.Equal(t, syncIdx.Unique, parseIdx.Unique, "table %s, index %s: unique should match", tableName, name)
+
+ // Compare expressions - sync.go gets normalized expressions from PostgreSQL catalog
+ // while parser gets the raw expressions. We need to handle this difference.
+ if len(syncIdx.Expressions) == len(parseIdx.Expressions) {
+ for i := range syncIdx.Expressions {
+ syncExpr := normalizeExpression(syncIdx.Expressions[i])
+ parseExpr := normalizeExpression(parseIdx.Expressions[i])
+ require.Equal(t, syncExpr, parseExpr, "table %s, index %s: expression[%d] should match", tableName, name, i)
+ }
+ } else {
+ require.Equal(t, len(syncIdx.Expressions), len(parseIdx.Expressions), "table %s, index %s: expressions count should match", tableName, name)
+ }
+
+ // Compare descending order for each expression
+ // Note: sync.go currently doesn't populate the Descending field, so we need to handle both cases
+ if len(syncIdx.Descending) > 0 && len(parseIdx.Descending) > 0 {
+ // Both have descending info, compare them
+ require.Equal(t, len(syncIdx.Descending), len(parseIdx.Descending), "table %s, index %s: descending array length should match", tableName, name)
+ for i := range syncIdx.Descending {
+ require.Equal(t, syncIdx.Descending[i], parseIdx.Descending[i], "table %s, index %s: descending[%d] should match", tableName, name, i)
+ }
+ } else if len(parseIdx.Descending) > 0 {
+ // Only parser has descending info, verify it matches the number of expressions
+ require.Equal(t, len(parseIdx.Expressions), len(parseIdx.Descending), "table %s, index %s: descending array should match expressions count", tableName, name)
+ }
+
+ // Also compare the index type if available
+ if syncIdx.Type != "" || parseIdx.Type != "" {
+ require.Equal(t, syncIdx.Type, parseIdx.Type, "table %s, index %s: type should match", tableName, name)
+ }
+
+ // Compare IsConstraint field
+ require.Equal(t, syncIdx.IsConstraint, parseIdx.IsConstraint, "table %s, index %s: IsConstraint should match", tableName, name)
+ }
+}
+
+func compareForeignKeys(t *testing.T, tableName string, syncFKs, parseFKs []*storepb.ForeignKeyMetadata) {
+ require.Equal(t, len(syncFKs), len(parseFKs), "table %s: number of foreign keys should match", tableName)
+
+ // Create maps for easier comparison
+ syncMap := make(map[string]*storepb.ForeignKeyMetadata)
+ for _, fk := range syncFKs {
+ syncMap[fk.Name] = fk
+ }
+
+ for _, parseFk := range parseFKs {
+ syncFk, exists := syncMap[parseFk.Name]
+ require.True(t, exists, "table %s: foreign key %s should exist in sync metadata", tableName, parseFk.Name)
+
+ require.ElementsMatch(t, syncFk.Columns, parseFk.Columns, "table %s, FK %s: columns should match", tableName, parseFk.Name)
+ require.Equal(t, syncFk.ReferencedTable, parseFk.ReferencedTable, "table %s, FK %s: referenced table should match", tableName, parseFk.Name)
+ require.ElementsMatch(t, syncFk.ReferencedColumns, parseFk.ReferencedColumns, "table %s, FK %s: referenced columns should match", tableName, parseFk.Name)
+ }
+}
+
+func comparePartitions(t *testing.T, tableName string, syncParts, parseParts []*storepb.TablePartitionMetadata) {
+ require.Equal(t, len(syncParts), len(parseParts), "table %s: number of partitions should match", tableName)
+
+ // Create maps for easier comparison
+ syncMap := make(map[string]*storepb.TablePartitionMetadata)
+ for _, part := range syncParts {
+ syncMap[part.Name] = part
+ }
+
+ for _, parsePart := range parseParts {
+ syncPart, exists := syncMap[parsePart.Name]
+ require.True(t, exists, "table %s: partition %s should exist in sync metadata", tableName, parsePart.Name)
+ require.Equal(t, syncPart.Expression, parsePart.Expression, "table %s, partition %s: expression should match", tableName, parsePart.Name)
+ require.Equal(t, syncPart.Value, parsePart.Value, "table %s, partition %s: value should match", tableName, parsePart.Name)
+ }
+}
+
+func compareViews(t *testing.T, syncViews, parseViews []*storepb.ViewMetadata) {
+ require.Equal(t, len(syncViews), len(parseViews), "number of views should match")
+
+ // Create maps for easier comparison
+ syncMap := make(map[string]*storepb.ViewMetadata)
+ for _, view := range syncViews {
+ syncMap[view.Name] = view
+ }
+
+ for _, parseView := range parseViews {
+ syncView, exists := syncMap[parseView.Name]
+ require.True(t, exists, "view %s should exist in sync metadata", parseView.Name)
+
+ // Compare view definitions - they might be formatted differently
+ // PostgreSQL normalizes view definitions when storing them
+ syncDef := normalizeSQL(syncView.Definition)
+ parseDef := normalizeSQL(parseView.Definition)
+ require.Equal(t, syncDef, parseDef, "view %s: definition should match", parseView.Name)
+
+ // Compare comment if present
+ if syncView.Comment != "" || parseView.Comment != "" {
+ require.Equal(t, syncView.Comment, parseView.Comment, "view %s: comment should match", parseView.Name)
+ }
+ }
+}
+
+func compareFunctions(t *testing.T, syncFuncs, parseFuncs []*storepb.FunctionMetadata) {
+ // Function comparison is tricky because signatures might be formatted differently
+ t.Logf("sync has %d functions, parse has %d functions", len(syncFuncs), len(parseFuncs))
+
+ // Currently the parser doesn't extract functions from DDL, so we expect 0 functions from parser
+ // If parser starts extracting functions, we should implement proper comparison here
+ if len(parseFuncs) == 0 && len(syncFuncs) > 0 {
+ // This is expected - parser doesn't extract functions yet
+ return
+ }
+
+ // If parser starts returning functions, implement full comparison
+ require.Equal(t, len(syncFuncs), len(parseFuncs), "number of functions should match")
+
+ // Create maps for easier comparison - use function signature for mapping
+ syncMap := make(map[string]*storepb.FunctionMetadata)
+ for _, fn := range syncFuncs {
+ syncMap[fn.Signature] = fn
+ }
+
+ for _, parseFn := range parseFuncs {
+ // Try to find matching function by signature
+ var syncFn *storepb.FunctionMetadata
+ for _, sf := range syncFuncs {
+ if normalizeSignature(sf.Signature) == normalizeSignature(parseFn.Signature) {
+ syncFn = sf
+ break
+ }
+ }
+
+ require.NotNil(t, syncFn, "function with signature %s should exist in sync metadata", parseFn.Signature)
+
+ // Compare function definitions
+ syncDef := normalizeSQL(syncFn.Definition)
+ parseDef := normalizeSQL(parseFn.Definition)
+ require.Equal(t, syncDef, parseDef, "function %s: definition should match", parseFn.Name)
+
+ // Compare comment if present
+ if syncFn.Comment != "" || parseFn.Comment != "" {
+ require.Equal(t, syncFn.Comment, parseFn.Comment, "function %s: comment should match", parseFn.Name)
+ }
+ }
+}
+
+func compareSequences(t *testing.T, syncSeqs, parseSeqs []*storepb.SequenceMetadata) {
+ // Create maps for easier comparison
+ syncMap := make(map[string]*storepb.SequenceMetadata)
+ for _, seq := range syncSeqs {
+ syncMap[seq.Name] = seq
+ }
+
+ parseMap := make(map[string]*storepb.SequenceMetadata)
+ for _, seq := range parseSeqs {
+ parseMap[seq.Name] = seq
+ }
+
+ // Check sequences in parseSeqs
+ for _, parseSeq := range parseSeqs {
+ syncSeq, exists := syncMap[parseSeq.Name]
+ if !exists {
+ // SERIAL columns create implicit sequences that might not be in DDL
+ t.Logf("sequence %s exists in parse but not in sync (might be implicit from SERIAL)", parseSeq.Name)
+ continue
+ }
+
+ // Compare basic properties
+ if parseSeq.Start != "" {
+ require.Equal(t, syncSeq.Start, parseSeq.Start, "sequence %s: start value should match", parseSeq.Name)
+ }
+ if parseSeq.Increment != "" {
+ require.Equal(t, syncSeq.Increment, parseSeq.Increment, "sequence %s: increment should match", parseSeq.Name)
+ }
+ }
+
+ // Check sequences in syncSeqs that are not in parseSeqs
+ for _, syncSeq := range syncSeqs {
+ _, exists := parseMap[syncSeq.Name]
+ if !exists {
+ // Skip implicit sequences created by SERIAL columns
+ if strings.Contains(syncSeq.Name, "_id_seq") || strings.Contains(syncSeq.Name, "_seq") {
+ t.Logf("sequence %s exists in sync but not in parse (implicit sequence from SERIAL column)", syncSeq.Name)
+ continue
+ }
+ // For explicitly created sequences, this is an error
+ require.True(t, exists, "sequence %s should exist in parsed metadata", syncSeq.Name)
+ }
+ }
+}
+
+func compareEnums(t *testing.T, syncEnums, parseEnums []*storepb.EnumTypeMetadata) {
+ require.Equal(t, len(syncEnums), len(parseEnums), "number of enums should match")
+
+ // Create maps for easier comparison
+ syncMap := make(map[string]*storepb.EnumTypeMetadata)
+ for _, enum := range syncEnums {
+ syncMap[enum.Name] = enum
+ }
+
+ for _, parseEnum := range parseEnums {
+ syncEnum, exists := syncMap[parseEnum.Name]
+ require.True(t, exists, "enum %s should exist in sync metadata", parseEnum.Name)
+ require.ElementsMatch(t, syncEnum.Values, parseEnum.Values, "enum %s: values should match", parseEnum.Name)
+ }
+}
+
+func compareExtensions(t *testing.T, syncExts, parseExts []*storepb.ExtensionMetadata) {
+ // Create maps for easier comparison
+ syncMap := make(map[string]*storepb.ExtensionMetadata)
+ for _, ext := range syncExts {
+ syncMap[ext.Name] = ext
+ }
+
+ for _, parseExt := range parseExts {
+ syncExt, exists := syncMap[parseExt.Name]
+ require.True(t, exists, "extension %s should exist in sync metadata", parseExt.Name)
+ require.Equal(t, syncExt.Schema, parseExt.Schema, "extension %s: schema should match", parseExt.Name)
+ }
+}
diff --git a/backend/plugin/schema/pg/testdata/get_database_metadata.yaml b/backend/plugin/schema/pg/testdata/get_database_metadata.yaml
deleted file mode 100644
index c903d30261..0000000000
--- a/backend/plugin/schema/pg/testdata/get_database_metadata.yaml
+++ /dev/null
@@ -1,311 +0,0 @@
-- input: |
- CREATE SCHEMA IF NOT EXISTS myapp;
-
- CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA myapp;
-
- CREATE TYPE myapp.user_status AS ENUM ('active', 'inactive', 'suspended', 'deleted');
-
- CREATE TABLE myapp.users (
- id UUID DEFAULT myapp.uuid_generate_v4() PRIMARY KEY,
- username VARCHAR(50) NOT NULL,
- email VARCHAR(255) NOT NULL,
- status myapp.user_status DEFAULT 'active',
- full_name TEXT,
- metadata JSONB,
- created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
- updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
- CONSTRAINT uk_username UNIQUE (username),
- CONSTRAINT uk_email UNIQUE (email),
- CONSTRAINT chk_email_format CHECK (email ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$')
- );
-
- CREATE INDEX idx_users_status ON myapp.users (status);
- CREATE INDEX idx_users_created_at ON myapp.users (created_at DESC);
- CREATE INDEX idx_users_full_name_gin ON myapp.users USING gin (to_tsvector('english', full_name));
- CREATE INDEX idx_users_metadata ON myapp.users USING gin (metadata);
-
- CREATE FUNCTION myapp.update_user_timestamp()
- RETURNS TRIGGER AS $$
- BEGIN
- NEW.updated_at = CURRENT_TIMESTAMP;
- RETURN NEW;
- END;
- $$ LANGUAGE plpgsql;
- metadata_result: |-
- {
- "schemas": [
- {
- "name": "myapp",
- "tables": [
- {
- "name": "users",
- "columns": [
- {
- "name": "id",
- "DefaultValue": {
- "DefaultExpression": "myapp.uuid_generate_v4()"
- },
- "type": "UUID"
- },
- {
- "name": "username",
- "DefaultValue": null,
- "type": "VARCHAR(50)"
- },
- {
- "name": "email",
- "DefaultValue": null,
- "type": "VARCHAR(255)"
- },
- {
- "name": "status",
- "DefaultValue": {
- "DefaultExpression": "'active'"
- },
- "nullable": true,
- "type": "myapp.user_status"
- },
- {
- "name": "full_name",
- "DefaultValue": null,
- "nullable": true,
- "type": "TEXT"
- },
- {
- "name": "metadata",
- "DefaultValue": null,
- "nullable": true,
- "type": "JSONB"
- },
- {
- "name": "created_at",
- "DefaultValue": {
- "DefaultExpression": "CURRENT_TIMESTAMP"
- },
- "nullable": true,
- "type": "TIMESTAMP WITH TIME ZONE"
- },
- {
- "name": "updated_at",
- "DefaultValue": {
- "DefaultExpression": "CURRENT_TIMESTAMP"
- },
- "nullable": true,
- "type": "TIMESTAMP WITH TIME ZONE"
- }
- ],
- "indexes": [
- {
- "name": "users_pkey",
- "expressions": [
- "id"
- ],
- "descending": [
- false
- ],
- "unique": true,
- "primary": true,
- "is_constraint": true
- },
- {
- "name": "uk_username",
- "unique": true,
- "is_constraint": true
- },
- {
- "name": "uk_email",
- "unique": true,
- "is_constraint": true
- },
- {
- "name": "idx_users_status",
- "expressions": [
- "status"
- ],
- "descending": [
- false
- ]
- },
- {
- "name": "idx_users_created_at",
- "expressions": [
- "created_at"
- ],
- "descending": [
- true
- ]
- },
- {
- "name": "idx_users_full_name_gin",
- "expressions": [
- "to_tsvector('english', full_name)"
- ],
- "descending": [
- false
- ]
- },
- {
- "name": "idx_users_metadata",
- "expressions": [
- "metadata"
- ],
- "descending": [
- false
- ]
- }
- ],
- "check_constraints": [
- {
- "name": "chk_email_format",
- "expression": "email ~* '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}$'"
- }
- ]
- }
- ],
- "functions": [
- {
- "name": "update_user_timestamp",
- "definition": "CREATE FUNCTION myapp.update_user_timestamp()\nRETURNS TRIGGER AS $$\nBEGIN\n NEW.updated_at = CURRENT_TIMESTAMP;\n RETURN NEW;\nEND;\n$$ LANGUAGE plpgsql",
- "signature": "\"update_user_timestamp\"()"
- }
- ],
- "enum_types": [
- {
- "name": "user_status",
- "values": [
- "active",
- "inactive",
- "suspended",
- "deleted"
- ]
- }
- ]
- },
- {
- "name": "public"
- }
- ],
- "extensions": [
- {
- "name": "uuid-ossp",
- "schema": "myapp"
- }
- ]
- }
-- input: |
- CREATE TABLE users (
- id SERIAL PRIMARY KEY,
- name TEXT NOT NULL
- );
- metadata_result: |-
- {
- "schemas": [
- {
- "name": "public",
- "tables": [
- {
- "name": "users",
- "columns": [
- {
- "name": "id",
- "DefaultValue": null,
- "type": "SERIAL"
- },
- {
- "name": "name",
- "DefaultValue": null,
- "type": "TEXT"
- }
- ],
- "indexes": [
- {
- "name": "users_pkey",
- "expressions": [
- "id"
- ],
- "descending": [
- false
- ],
- "unique": true,
- "primary": true,
- "is_constraint": true
- }
- ]
- }
- ]
- }
- ]
- }
-- input: |
- CREATE TABLE test_ordering (
- a INTEGER,
- b TEXT,
- c TIMESTAMP
- );
- CREATE INDEX idx_mixed_order ON test_ordering (a ASC, b DESC, c);
- metadata_result: |-
- {
- "schemas": [
- {
- "name": "public",
- "tables": [
- {
- "name": "test_ordering",
- "columns": [
- {
- "name": "a",
- "DefaultValue": null,
- "nullable": true,
- "type": "INTEGER"
- },
- {
- "name": "b",
- "DefaultValue": null,
- "nullable": true,
- "type": "TEXT"
- },
- {
- "name": "c",
- "DefaultValue": null,
- "nullable": true,
- "type": "TIMESTAMP"
- }
- ],
- "indexes": [
- {
- "name": "idx_mixed_order",
- "expressions": [
- "a",
- "b",
- "c"
- ],
- "descending": [
- false,
- true,
- false
- ]
- }
- ]
- }
- ]
- }
- ]
- }
-- input: ""
- metadata_result: |-
- {
- "schemas": [
- {
- "name": "public"
- }
- ]
- }
-- input: " \n\t "
- metadata_result: |-
- {
- "schemas": [
- {
- "name": "public"
- }
- ]
- }
diff --git a/go.mod b/go.mod
index c378782192..fe01952068 100644
--- a/go.mod
+++ b/go.mod
@@ -91,6 +91,7 @@ require (
github.com/spf13/cobra v1.9.1
github.com/stretchr/testify v1.10.0
github.com/testcontainers/testcontainers-go v0.37.0
+ github.com/testcontainers/testcontainers-go/modules/postgres v0.37.0
github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75
github.com/trinodb/trino-go-client v0.323.0
github.com/vjeantet/ldapserver v1.0.1
diff --git a/go.sum b/go.sum
index 11510780bc..84677bc7c0 100644
--- a/go.sum
+++ b/go.sum
@@ -1646,6 +1646,8 @@ github.com/mattn/go-sqlite3 v1.14.28 h1:ThEiQrnbtumT+QMknw63Befp/ce/nUPgBPMlRFEu
github.com/mattn/go-sqlite3 v1.14.28/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/mattn/goveralls v0.0.2/go.mod h1:8d1ZMHsd7fW6IRPKQh46F2WRpyib5/X4FOpevwGNQEw=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
+github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI=
+github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o=
github.com/mediocregopher/mediocre-go-lib v0.0.0-20181029021733-cb65787f37ed/go.mod h1:dSsfyI2zABAdhcbvkXqgxOxrCsbYeHCPgrZkku60dSg=
github.com/mediocregopher/radix/v3 v3.3.0/go.mod h1:EmfVyvspXz1uZEyPBMyGK+kjWiKQGvsUt6O3Pj+LDCQ=
github.com/mediocregopher/radix/v3 v3.4.2/go.mod h1:8FL3F6UQRXHXIBSPUs5h0RybMF8i4n7wVopoX3x7Bv8=
@@ -2000,6 +2002,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/testcontainers/testcontainers-go v0.37.0 h1:L2Qc0vkTw2EHWQ08djon0D2uw7Z/PtHS/QzZZ5Ra/hg=
github.com/testcontainers/testcontainers-go v0.37.0/go.mod h1:QPzbxZhQ6Bclip9igjLFj6z0hs01bU8lrl2dHQmgFGM=
+github.com/testcontainers/testcontainers-go/modules/postgres v0.37.0 h1:hsVwFkS6s+79MbKEO+W7A1wNIw1fmkMtF4fg83m6kbc=
+github.com/testcontainers/testcontainers-go/modules/postgres v0.37.0/go.mod h1:Qj/eGbRbO/rEYdcRLmN+bEojzatP/+NS1y8ojl2PQsc=
github.com/the42/cartconvert v0.0.0-20131203171324-aae784c392b8 h1:I4DY8wLxJXCrMYzDM6lKCGc3IQwJX0PlTLsd3nQqI3c=
github.com/the42/cartconvert v0.0.0-20131203171324-aae784c392b8/go.mod h1:fWO/msnJVhHqN1yX6OBoxSyfj7TEj1hHiL8bJSQsK30=
github.com/tiancaiamao/gp v0.0.0-20221230034425-4025bc8a4d4a h1:J/YdBZ46WKpXsxsW93SG+q0F8KI+yFrcIDT4c/RNoc4=