Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(mssql): report CREATE UNIQUE INDEX as unique when syncing, and move driver sync tests into their driver packages (#21406)
Danny Xuc7cb420aa9ca
diff --git a/backend/plugin/db/mssql/main_test.go b/backend/plugin/db/mssql/main_test.gonew file mode 100644index 0000000000..aae886b962--- /dev/null+++ b/backend/plugin/db/mssql/main_test.go@@ -0,0 +1,9 @@+package mssql++import (+ "testing"++ "github.com/bytebase/bytebase/backend/common/testcontainer"+)++func TestMain(m *testing.M) { testcontainer.Main(m) }diff --git a/backend/plugin/db/mssql/resource_change_integration_testcontainer_test.go b/backend/plugin/db/mssql/resource_change_integration_testcontainer_test.goindex 8c2659e531..150e7f5ac3 100644--- a/backend/plugin/db/mssql/resource_change_integration_testcontainer_test.go+++ b/backend/plugin/db/mssql/resource_change_integration_testcontainer_test.go@@ -3,7 +3,6 @@ package mssqlimport ("context""fmt"- "strconv""strings""testing""time"@@ -32,27 +31,25 @@ import (//nolint:tparallelfunc TestResourceChangeIntegrationMSSQL(t *testing.T) {ctx := context.Background()- container := testcontainer.GetTestMSSQLContainer(ctx, t)- defer container.Close(ctx)+ container := testcontainer.SharedMSSQLContainer(t)host := container.GetHost()- portInt, err := strconv.Atoi(container.GetPort())- require.NoError(t, err)+ port := container.GetPort()dbName := fmt.Sprintf("resource_change_%d", time.Now().UnixNano())- driver := openMSSQL(ctx, t, host, portInt, "master")- _, err = driver.Execute(ctx, fmt.Sprintf("CREATE DATABASE [%s]", dbName), db.ExecuteOptions{CreateDatabase: true})+ driver := openMSSQL(ctx, t, host, port, "master")+ _, err := driver.Execute(ctx, fmt.Sprintf("CREATE DATABASE [%s]", dbName), db.ExecuteOptions{CreateDatabase: true})require.NoError(t, err)driver.Close(ctx)defer func() {- cleanup := openMSSQL(ctx, t, host, portInt, "master")+ cleanup := openMSSQL(ctx, t, host, port, "master")defer cleanup.Close(ctx)_, _ = cleanup.Execute(ctx, fmt.Sprintf("ALTER DATABASE [%s] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", dbName), db.ExecuteOptions{CreateDatabase: true})_, _ = cleanup.Execute(ctx, fmt.Sprintf("DROP DATABASE [%s]", dbName), db.ExecuteOptions{CreateDatabase: true})}()- driver = openMSSQL(ctx, t, host, portInt, dbName)+ driver = openMSSQL(ctx, t, host, port, dbName)defer driver.Close(ctx)mssqlDriver, ok := driver.(*Driver)require.True(t, ok, "expected *Driver")@@ -158,7 +155,7 @@ func summarize(t *testing.T, sql string) *parserbase.ChangeSummary {return summary}-func openMSSQL(ctx context.Context, t *testing.T, host string, port int, database string) db.Driver {+func openMSSQL(ctx context.Context, t *testing.T, host, port, database string) db.Driver {t.Helper()driverInstance := &Driver{}cfg := db.ConnectionConfig{@@ -166,7 +163,7 @@ func openMSSQL(ctx context.Context, t *testing.T, host string, port int, databasType: storepb.DataSourceType_ADMIN,Username: "sa",Host: host,- Port: strconv.Itoa(port),+ Port: port,Database: database,},Password: "Test123!",diff --git a/backend/plugin/db/mssql/sync.go b/backend/plugin/db/mssql/sync.goindex ad757eede2..fad64adfeb 100644--- a/backend/plugin/db/mssql/sync.go+++ b/backend/plugin/db/mssql/sync.go@@ -837,6 +837,7 @@ func getIndexes(txn *sql.Tx, schemas []string) (map[db.TableKey][]*storepb.Indexi.index_id,i.name,i.type_desc,+ i.is_unique,col.name AS column_name,ic.is_descending_keyFROM sys.indexes i@@ -856,8 +857,8 @@ func getIndexes(txn *sql.Tx, schemas []string) (map[db.TableKey][]*storepb.Indexfor rows.Next() {var schemaName, tableName, indexName, typeDesc, colName sql.NullStringvar objectID, indexID sql.NullInt32- var isDescending sql.NullBool- if err := rows.Scan(&schemaName, &tableName, &objectID, &indexID, &indexName, &typeDesc, &colName, &isDescending); err != nil {+ var isUnique, isDescending sql.NullBool+ if err := rows.Scan(&schemaName, &tableName, &objectID, &indexID, &indexName, &typeDesc, &isUnique, &colName, &isDescending); err != nil {return nil, err}@@ -870,9 +871,11 @@ func getIndexes(txn *sql.Tx, schemas []string) (map[db.TableKey][]*storepb.IndexindexMap[key] = make(map[string]*storepb.IndexMetadata)}if _, ok := indexMap[key][indexName.String]; !ok {+ // This query excludes primary keys and unique constraints, which are+ // reported separately, so is_unique here means a CREATE UNIQUE INDEX.index := &storepb.IndexMetadata{Name: indexName.String,- Unique: false,+ Unique: isUnique.Valid && isUnique.Bool,Primary: false,IsConstraint: false,}diff --git a/backend/plugin/db/mssql/sync_spatial_testcontainer_test.go b/backend/plugin/db/mssql/sync_spatial_testcontainer_test.goindex 97c7d7f6a4..b55e31ccbb 100644--- a/backend/plugin/db/mssql/sync_spatial_testcontainer_test.go+++ b/backend/plugin/db/mssql/sync_spatial_testcontainer_test.go@@ -2,880 +2,493 @@ package mssqlimport ("context"- "fmt"- "strconv""strings""testing"- "time"+ "github.com/google/go-cmp/cmp"_ "github.com/microsoft/go-mssqldb"- "github.com/pkg/errors""github.com/stretchr/testify/require"+ "google.golang.org/protobuf/testing/protocmp""github.com/bytebase/bytebase/backend/common/testcontainer"storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/plugin/db")-// syncDBSchemaWithRetry attempts to sync the database schema with retry logic for transient errors-func syncDBSchemaWithRetry(ctx context.Context, driver *Driver) (*storepb.DatabaseSchemaMetadata, error) {- const maxRetries = 3- var lastErr error- for i := 0; i < maxRetries; i++ {- metadata, err := driver.SyncDBSchema(ctx)- if err == nil {- return metadata, nil- }-- // Check if it's a transient error that should be retried- errorStr := strings.ToLower(err.Error())- isTransientError := strings.Contains(errorStr, "eof") ||- strings.Contains(errorStr, "i/o timeout") ||- strings.Contains(errorStr, "connection") ||- strings.Contains(errorStr, "broken pipe") ||- strings.Contains(errorStr, "network") ||- strings.Contains(errorStr, "timeout")-- if isTransientError {- lastErr = err- if i < maxRetries-1 {- // Wait before retry with exponential backoff- waitTime := time.Duration(i+1) * 500 * time.Millisecond- time.Sleep(waitTime)- continue- }- }- // For non-transient errors, return immediately- return nil, err- }- return nil, errors.Wrapf(lastErr, "failed after %d retries", maxRetries)-}--func TestSyncSpatialIndexWithTestcontainer(t *testing.T) {+// TestSyncSpatialIndex pins what SyncDBSchema reports for SQL Server spatial+// indexes: tessellation scheme and grid levels, bounding box, storage options+// and the dimensional descriptor. sys.spatial_indexes exposes these through+// several catalog views, so a sync regression shows up here rather than in the+// parser tests under backend/plugin/schema/mssql.+//+//nolint:tparallel+func TestSyncSpatialIndex(t *testing.T) {ctx := context.Background()- container := testcontainer.GetTestMSSQLContainer(ctx, t)- defer container.Close(ctx)-- host := container.GetHost()- port := container.GetPort()- portInt, err := strconv.Atoi(port)- require.NoError(t, err)+ container := testcontainer.SharedMSSQLContainer(t)testCases := []struct {name stringsetupSQL string- validate func(*testing.T, *Driver, string)+ validate func(*testing.T, *storepb.DatabaseSchemaMetadata)}{{- name: "sync_spatial_indexes_with_full_configuration",+ name: "geometry_and_geography_indexes",setupSQL: `-CREATE SCHEMA [geo];+CREATE SCHEMA geo;GO--- Create table with spatial columns-CREATE TABLE [geo].[locations] (- [id] INT IDENTITY(1,1) PRIMARY KEY,- [name] NVARCHAR(200) NOT NULL,- [location_point] GEOGRAPHY NOT NULL,- [boundary_polygon] GEOGRAPHY NOT NULL,- [area_shape] GEOGRAPHY NOT NULL,- [description] NVARCHAR(MAX)+CREATE TABLE geo.locations (+ id INT IDENTITY(1,1) PRIMARY KEY,+ name NVARCHAR(200) NOT NULL,+ location_point GEOMETRY NOT NULL,+ boundary_polygon GEOMETRY,+ geo_location GEOGRAPHY NOT NULL,+ route_line GEOGRAPHY);GO--- Create GEOGRAPHY spatial indexes with comprehensive configuration-CREATE SPATIAL INDEX [idx_location_point] ON [geo].[locations] ([location_point])-USING GEOGRAPHY_GRID+CREATE SPATIAL INDEX idx_location_point ON geo.locations(location_point)+USING GEOMETRY_GRIDWITH (+ BOUNDING_BOX = (-180, -90, 180, 90),GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),CELLS_PER_OBJECT = 32,PAD_INDEX = ON,- FILLFACTOR = 85,- SORT_IN_TEMPDB = ON,+ FILLFACTOR = 90,ALLOW_ROW_LOCKS = ON,- ALLOW_PAGE_LOCKS = ON,- MAXDOP = 2,- DATA_COMPRESSION = PAGE+ ALLOW_PAGE_LOCKS = ON+);+GO++CREATE SPATIAL INDEX idx_boundary_polygon ON geo.locations(boundary_polygon)+USING GEOMETRY_GRID+WITH (+ BOUNDING_BOX = (0, 0, 1000, 1000),+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = HIGH, LEVEL_4 = HIGH),+ CELLS_PER_OBJECT = 64);GO-CREATE SPATIAL INDEX [idx_boundary_polygon] ON [geo].[locations] ([boundary_polygon])-USING GEOGRAPHY_GRID+CREATE SPATIAL INDEX idx_geo_location ON geo.locations(geo_location)+USING GEOGRAPHY_GRIDWITH (GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM),CELLS_PER_OBJECT = 16,- FILLFACTOR = 80+ FILLFACTOR = 85,+ PAD_INDEX = OFF,+ ALLOW_ROW_LOCKS = OFF,+ ALLOW_PAGE_LOCKS = OFF);GO-CREATE SPATIAL INDEX [idx_area_shape] ON [geo].[locations] ([area_shape])-USING GEOGRAPHY_GRID+CREATE SPATIAL INDEX idx_route_line ON geo.locations(route_line)+USING GEOGRAPHY_GRIDWITH (- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),- CELLS_PER_OBJECT = 64+ GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = HIGH, LEVEL_4 = HIGH),+ CELLS_PER_OBJECT = 128);GO`,- validate: func(t *testing.T, driver *Driver, _ string) {- // Add delay to allow metadata propagation- time.Sleep(1 * time.Second)-- // Sync database schema with retry logic- metadata, err := syncDBSchemaWithRetry(ctx, driver)- require.NoError(t, err)- require.NotNil(t, metadata)-- // Find the geo schema- var geoSchema *storepb.SchemaMetadata- for _, schema := range metadata.Schemas {- if schema.Name == "geo" {- geoSchema = schema- break- }- }- require.NotNil(t, geoSchema, "geo schema should exist")-- // Find the locations table- require.Len(t, geoSchema.Tables, 1)- table := geoSchema.Tables[0]- require.Equal(t, "locations", table.Name)-- // Verify spatial columns exist- spatialColumns := make(map[string]*storepb.ColumnMetadata)- for _, col := range table.Columns {- if strings.Contains(strings.ToLower(col.Type), "geography") {- spatialColumns[col.Name] = col- }- }- require.Contains(t, spatialColumns, "location_point")- require.Contains(t, spatialColumns, "boundary_polygon")- require.Contains(t, spatialColumns, "area_shape")-- // Verify spatial indexes- spatialIndexes := make(map[string]*storepb.IndexMetadata)- for _, idx := range table.Indexes {- if idx.Type == "SPATIAL" {- spatialIndexes[idx.Name] = idx- }- }+ validate: func(t *testing.T, metadata *storepb.DatabaseSchemaMetadata) {+ table := requireTable(t, metadata, "geo", "locations")- // Verify specific spatial indexes and their configurations- // Helper function to validate spatial index metadata- validateSpatialIndex := func(t *testing.T, idx *storepb.IndexMetadata, expectedName string, expectedColumn string, expectedDataType string) {- t.Helper()-- // Basic index properties- require.Equal(t, "SPATIAL", idx.Type, "Index type should be SPATIAL")- require.Equal(t, expectedName, idx.Name, "Index name should match")- require.False(t, idx.Unique, "Spatial indexes should not be unique")- require.False(t, idx.Primary, "Spatial indexes should not be primary")- require.Len(t, idx.Expressions, 1, "Should have exactly one expression")- require.Equal(t, expectedColumn, idx.Expressions[0], "Column name should match")- require.Len(t, idx.Descending, 1, "Should have descending flag")- require.False(t, idx.Descending[0], "Spatial indexes don't support descending")-- // Spatial configuration- require.NotNil(t, idx.SpatialConfig, "Spatial config should not be nil")- require.Equal(t, "SPATIAL", idx.SpatialConfig.Method, "Method should be SPATIAL")-- // Tessellation configuration- require.NotNil(t, idx.SpatialConfig.Tessellation, "Tessellation config should exist")- if idx.SpatialConfig.Tessellation.Scheme != "UNKNOWN" {- // Full SQL Server with spatial metadata- require.Equal(t, expectedDataType+"_GRID", idx.SpatialConfig.Tessellation.Scheme)-- // Grid levels (if available)- if len(idx.SpatialConfig.Tessellation.GridLevels) > 0 {- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- require.GreaterOrEqual(t, level.Level, int32(1), "Level should be >= 1")- require.LessOrEqual(t, level.Level, int32(4), "Level should be <= 4")- require.Contains(t, []string{"LOW", "MEDIUM", "HIGH"}, level.Density, "Density should be valid")- }- }-- // Cells per object- if idx.SpatialConfig.Tessellation.CellsPerObject > 0 {- require.GreaterOrEqual(t, idx.SpatialConfig.Tessellation.CellsPerObject, int32(1))- require.LessOrEqual(t, idx.SpatialConfig.Tessellation.CellsPerObject, int32(8192))- }-- // Bounding box (only for GEOMETRY indexes)- if expectedDataType == "GEOMETRY" && idx.SpatialConfig.Tessellation.BoundingBox != nil {- bbox := idx.SpatialConfig.Tessellation.BoundingBox- require.Less(t, bbox.Xmin, bbox.Xmax, "Xmin should be less than Xmax")- require.Less(t, bbox.Ymin, bbox.Ymax, "Ymin should be less than Ymax")- }- }-- // Storage configuration- require.NotNil(t, idx.SpatialConfig.Storage, "Storage config should exist")- if idx.SpatialConfig.Storage.Fillfactor > 0 {- require.GreaterOrEqual(t, idx.SpatialConfig.Storage.Fillfactor, int32(1))- require.LessOrEqual(t, idx.SpatialConfig.Storage.Fillfactor, int32(100))- }-- // Dimensional configuration- require.NotNil(t, idx.SpatialConfig.Dimensional, "Dimensional config should exist")- require.Equal(t, expectedDataType, idx.SpatialConfig.Dimensional.DataType)- require.Equal(t, int32(2), idx.SpatialConfig.Dimensional.Dimensions, "SQL Server spatial is always 2D")+ require.Len(t, table.Columns, 6)+ columns := make(map[string]*storepb.ColumnMetadata)+ for _, column := range table.Columns {+ columns[column.Name] = column}-- // Validate each spatial index with specific expected values- if idx, exists := spatialIndexes["idx_location_point"]; exists {- validateSpatialIndex(t, idx, "idx_location_point", "location_point", "GEOGRAPHY")-- // Verify specific configuration values from CREATE INDEX statement- require.Equal(t, int32(32), idx.SpatialConfig.Tessellation.CellsPerObject, "CellsPerObject should be 32")- require.Equal(t, int32(85), idx.SpatialConfig.Storage.Fillfactor, "Fillfactor should be 85")- require.True(t, idx.SpatialConfig.Storage.PadIndex, "PadIndex should be ON")- require.True(t, idx.SpatialConfig.Storage.AllowRowLocks, "AllowRowLocks should be ON")- require.True(t, idx.SpatialConfig.Storage.AllowPageLocks, "AllowPageLocks should be ON")- // Note: MAXDOP, SORT_IN_TEMPDB, and DATA_COMPRESSION might not be captured by sys.spatial_indexes-- // Verify grid levels- require.Len(t, idx.SpatialConfig.Tessellation.GridLevels, 4, "Should have 4 grid levels")- gridMap := make(map[int32]string)- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- gridMap[level.Level] = level.Density- }- require.Equal(t, "MEDIUM", gridMap[1], "Level 1 should be MEDIUM")- require.Equal(t, "HIGH", gridMap[2], "Level 2 should be HIGH")- require.Equal(t, "MEDIUM", gridMap[3], "Level 3 should be MEDIUM")- require.Equal(t, "LOW", gridMap[4], "Level 4 should be LOW")- } else {- t.Error("Expected spatial index idx_location_point not found")- }-- if idx, exists := spatialIndexes["idx_boundary_polygon"]; exists {- validateSpatialIndex(t, idx, "idx_boundary_polygon", "boundary_polygon", "GEOGRAPHY")-- // Verify specific configuration values- require.Equal(t, int32(16), idx.SpatialConfig.Tessellation.CellsPerObject, "CellsPerObject should be 16")- require.Equal(t, int32(80), idx.SpatialConfig.Storage.Fillfactor, "Fillfactor should be 80")-- // Verify grid levels- require.Len(t, idx.SpatialConfig.Tessellation.GridLevels, 4, "Should have 4 grid levels")- gridMap := make(map[int32]string)- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- gridMap[level.Level] = level.Density- }- require.Equal(t, "LOW", gridMap[1], "Level 1 should be LOW")- require.Equal(t, "MEDIUM", gridMap[2], "Level 2 should be MEDIUM")- require.Equal(t, "HIGH", gridMap[3], "Level 3 should be HIGH")- require.Equal(t, "MEDIUM", gridMap[4], "Level 4 should be MEDIUM")- } else {- t.Error("Expected spatial index idx_boundary_polygon not found")- }-- if idx, exists := spatialIndexes["idx_area_shape"]; exists {- validateSpatialIndex(t, idx, "idx_area_shape", "area_shape", "GEOGRAPHY")-- // Verify specific configuration values- require.Equal(t, int32(64), idx.SpatialConfig.Tessellation.CellsPerObject, "CellsPerObject should be 64")- // No fillfactor specified, so it should be 0 or default-- // Verify grid levels- require.Len(t, idx.SpatialConfig.Tessellation.GridLevels, 4, "Should have 4 grid levels")- gridMap := make(map[int32]string)- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- gridMap[level.Level] = level.Density- }- require.Equal(t, "HIGH", gridMap[1], "Level 1 should be HIGH")- require.Equal(t, "HIGH", gridMap[2], "Level 2 should be HIGH")- require.Equal(t, "MEDIUM", gridMap[3], "Level 3 should be MEDIUM")- require.Equal(t, "LOW", gridMap[4], "Level 4 should be LOW")- } else {- t.Error("Expected spatial index idx_area_shape not found")- }-- // Verify that we have at least the expected number of spatial indexes- // (Even if configuration details are missing in SQL Server Express)- require.GreaterOrEqual(t, len(spatialIndexes), 3, "Should have at least 3 spatial indexes")+ require.Equal(t, "geometry", strings.ToLower(columns["location_point"].Type))+ require.Equal(t, "geometry", strings.ToLower(columns["boundary_polygon"].Type))+ require.Equal(t, "geography", strings.ToLower(columns["geo_location"].Type))+ require.Equal(t, "geography", strings.ToLower(columns["route_line"].Type))++ require.Len(t, spatialIndexes(table), 4)++ locationPoint := requireSpatialIndex(t, table, "idx_location_point", "location_point", "GEOMETRY")+ require.Equal(t, int32(32), locationPoint.SpatialConfig.Tessellation.CellsPerObject)+ requireBoundingBox(t, locationPoint, -180, -90, 180, 90)+ requireGridLevels(t, locationPoint, "MEDIUM", "HIGH", "MEDIUM", "LOW")+ require.Equal(t, int32(90), locationPoint.SpatialConfig.Storage.Fillfactor)+ require.True(t, locationPoint.SpatialConfig.Storage.PadIndex)+ require.True(t, locationPoint.SpatialConfig.Storage.AllowRowLocks)+ require.True(t, locationPoint.SpatialConfig.Storage.AllowPageLocks)++ boundaryPolygon := requireSpatialIndex(t, table, "idx_boundary_polygon", "boundary_polygon", "GEOMETRY")+ require.Equal(t, int32(64), boundaryPolygon.SpatialConfig.Tessellation.CellsPerObject)+ requireBoundingBox(t, boundaryPolygon, 0, 0, 1000, 1000)+ requireGridLevels(t, boundaryPolygon, "LOW", "LOW", "HIGH", "HIGH")++ geoLocation := requireSpatialIndex(t, table, "idx_geo_location", "geo_location", "GEOGRAPHY")+ require.Equal(t, int32(16), geoLocation.SpatialConfig.Tessellation.CellsPerObject)+ requireGridLevels(t, geoLocation, "LOW", "MEDIUM", "HIGH", "MEDIUM")+ require.Equal(t, int32(85), geoLocation.SpatialConfig.Storage.Fillfactor)+ require.False(t, geoLocation.SpatialConfig.Storage.PadIndex)+ require.False(t, geoLocation.SpatialConfig.Storage.AllowRowLocks)+ require.False(t, geoLocation.SpatialConfig.Storage.AllowPageLocks)++ routeLine := requireSpatialIndex(t, table, "idx_route_line", "route_line", "GEOGRAPHY")+ require.Equal(t, int32(128), routeLine.SpatialConfig.Tessellation.CellsPerObject)+ requireGridLevels(t, routeLine, "HIGH", "HIGH", "HIGH", "HIGH")},},{- name: "sync_spatial_indexes_with_ddl_preservation",+ name: "configuration_bounds",setupSQL: `-CREATE SCHEMA [spatial_test];+CREATE TABLE dbo.spatial_edge_cases (+ id INT IDENTITY(1,1) PRIMARY KEY,+ min_config_geom GEOMETRY NOT NULL,+ max_config_geom GEOMETRY NOT NULL,+ min_config_geog GEOGRAPHY NOT NULL,+ max_config_geog GEOGRAPHY NOT NULL+);GO--- Create table with mixed spatial data types-CREATE TABLE [spatial_test].[mixed_spatial] (- [id] INT IDENTITY(1,1) PRIMARY KEY,- [name] NVARCHAR(100) NOT NULL,- [geo_point] GEOGRAPHY NOT NULL,- [geo_line] GEOGRAPHY NOT NULL,- [created_at] DATETIME2 DEFAULT GETDATE()+CREATE SPATIAL INDEX idx_min_geom ON dbo.spatial_edge_cases(min_config_geom)+USING GEOMETRY_GRID+WITH (+ BOUNDING_BOX = (0, 0, 1, 1),+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = LOW, LEVEL_4 = LOW),+ CELLS_PER_OBJECT = 1);GO--- Create spatial indexes that should be preserved-CREATE SPATIAL INDEX [idx_geo_point] ON [spatial_test].[mixed_spatial] ([geo_point])-USING GEOGRAPHY_GRID+CREATE SPATIAL INDEX idx_max_geom ON dbo.spatial_edge_cases(max_config_geom)+USING GEOMETRY_GRIDWITH (- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),- CELLS_PER_OBJECT = 16+ BOUNDING_BOX = (-1000000, -1000000, 1000000, 1000000),+ GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = HIGH, LEVEL_4 = HIGH),+ CELLS_PER_OBJECT = 8192,+ PAD_INDEX = ON,+ FILLFACTOR = 100,+ ALLOW_ROW_LOCKS = ON,+ ALLOW_PAGE_LOCKS = ON,+ MAXDOP = 4,+ DATA_COMPRESSION = PAGE);GO-CREATE SPATIAL INDEX [idx_geo_line] ON [spatial_test].[mixed_spatial] ([geo_line])-USING GEOGRAPHY_GRID+CREATE SPATIAL INDEX idx_min_geog ON dbo.spatial_edge_cases(min_config_geog)+USING GEOGRAPHY_GRIDWITH (- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = MEDIUM, LEVEL_4 = HIGH),- CELLS_PER_OBJECT = 8+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = LOW, LEVEL_4 = LOW),+ CELLS_PER_OBJECT = 1);GO-`,- validate: func(t *testing.T, driver *Driver, _ string) {- // Add delay to allow metadata propagation- time.Sleep(1 * time.Second)-- // First sync to capture initial state with retry logic- metadata1, err := syncDBSchemaWithRetry(ctx, driver)- require.NoError(t, err)- require.NotNil(t, metadata1)-- // Find the spatial_test schema- var spatialSchema *storepb.SchemaMetadata- for _, schema := range metadata1.Schemas {- if schema.Name == "spatial_test" {- spatialSchema = schema- break- }- }- require.NotNil(t, spatialSchema, "spatial_test schema should exist")-- // Find the mixed_spatial table- require.Len(t, spatialSchema.Tables, 1)- table := spatialSchema.Tables[0]- require.Equal(t, "mixed_spatial", table.Name)-- // Count spatial indexes- spatialIndexCount := 0- var spatialIndexes []*storepb.IndexMetadata- for _, idx := range table.Indexes {- if idx.Type == "SPATIAL" {- spatialIndexCount++- spatialIndexes = append(spatialIndexes, idx)- }- }-- require.GreaterOrEqual(t, spatialIndexCount, 2, "Should have at least 2 spatial indexes")-- // Verify specific indexes exist and have spatial config- indexNames := make(map[string]*storepb.IndexMetadata)- for _, idx := range spatialIndexes {- indexNames[idx.Name] = idx- // Spatial config should be available through DDL preservation- require.NotNil(t, idx.SpatialConfig, "Spatial config should be preserved for index %s", idx.Name)- }-- // Verify specific configuration values for idx_geo_point- if idx, exists := indexNames["idx_geo_point"]; exists {- require.Equal(t, int32(16), idx.SpatialConfig.Tessellation.CellsPerObject, "idx_geo_point CellsPerObject should be 16")- require.Equal(t, "GEOGRAPHY_GRID", idx.SpatialConfig.Tessellation.Scheme, "Should use GEOGRAPHY_GRID tessellation")-- // Verify grid levels for idx_geo_point- require.Len(t, idx.SpatialConfig.Tessellation.GridLevels, 4, "Should have 4 grid levels")- gridMap := make(map[int32]string)- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- gridMap[level.Level] = level.Density- }- require.Equal(t, "MEDIUM", gridMap[1], "Level 1 should be MEDIUM")- require.Equal(t, "HIGH", gridMap[2], "Level 2 should be HIGH")- require.Equal(t, "MEDIUM", gridMap[3], "Level 3 should be MEDIUM")- require.Equal(t, "LOW", gridMap[4], "Level 4 should be LOW")- }-- // Verify specific configuration values for idx_geo_line- if idx, exists := indexNames["idx_geo_line"]; exists {- require.Equal(t, int32(8), idx.SpatialConfig.Tessellation.CellsPerObject, "idx_geo_line CellsPerObject should be 8")- require.Equal(t, "GEOGRAPHY_GRID", idx.SpatialConfig.Tessellation.Scheme, "Should use GEOGRAPHY_GRID tessellation")-- // Verify grid levels for idx_geo_line- require.Len(t, idx.SpatialConfig.Tessellation.GridLevels, 4, "Should have 4 grid levels")- gridMap := make(map[int32]string)- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- gridMap[level.Level] = level.Density- }- require.Equal(t, "LOW", gridMap[1], "Level 1 should be LOW")- require.Equal(t, "LOW", gridMap[2], "Level 2 should be LOW")- require.Equal(t, "MEDIUM", gridMap[3], "Level 3 should be MEDIUM")- require.Equal(t, "HIGH", gridMap[4], "Level 4 should be HIGH")- }-- // Verify expected indexes are present- expectedIndexes := []string{"idx_geo_point", "idx_geo_line"}- for _, expectedName := range expectedIndexes {- if _, exists := indexNames[expectedName]; !exists {- // In case of naming variations, check for partial matches- found := false- for actualName := range indexNames {- if strings.Contains(actualName, "geo_point") || strings.Contains(actualName, "geo_line") {- found = true- break- }- }- if !found {- t.Errorf("No spatial index found matching pattern for %s", expectedName)- }- }- }-- // Perform second sync to test consistency with retry logic- metadata2, err := syncDBSchemaWithRetry(ctx, driver)- require.NoError(t, err)- require.NotNil(t, metadata2)-- // Find the spatial_test schema in second sync- var spatialSchema2 *storepb.SchemaMetadata- for _, schema := range metadata2.Schemas {- if schema.Name == "spatial_test" {- spatialSchema2 = schema- break- }- }- require.NotNil(t, spatialSchema2, "spatial_test schema should exist in second sync")-- // Verify consistency between syncs- table2 := spatialSchema2.Tables[0]- spatialIndexCount2 := 0- for _, idx := range table2.Indexes {- if idx.Type == "SPATIAL" {- spatialIndexCount2++- }- }-- require.Equal(t, spatialIndexCount, spatialIndexCount2, "Spatial index count should be consistent between syncs")+CREATE SPATIAL INDEX idx_max_geog ON dbo.spatial_edge_cases(max_config_geog)+USING GEOGRAPHY_GRID+WITH (+ GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = HIGH, LEVEL_4 = HIGH),+ CELLS_PER_OBJECT = 8192,+ FILLFACTOR = 50+);+GO+`,+ validate: func(t *testing.T, metadata *storepb.DatabaseSchemaMetadata) {+ table := requireTable(t, metadata, "dbo", "spatial_edge_cases")+ require.Len(t, spatialIndexes(table), 4)++ minGeom := requireSpatialIndex(t, table, "idx_min_geom", "min_config_geom", "GEOMETRY")+ require.Equal(t, int32(1), minGeom.SpatialConfig.Tessellation.CellsPerObject)+ requireBoundingBox(t, minGeom, 0, 0, 1, 1)+ requireGridLevels(t, minGeom, "LOW", "LOW", "LOW", "LOW")++ maxGeom := requireSpatialIndex(t, table, "idx_max_geom", "max_config_geom", "GEOMETRY")+ require.Equal(t, int32(8192), maxGeom.SpatialConfig.Tessellation.CellsPerObject)+ requireBoundingBox(t, maxGeom, -1000000, -1000000, 1000000, 1000000)+ requireGridLevels(t, maxGeom, "HIGH", "HIGH", "HIGH", "HIGH")+ require.Equal(t, int32(100), maxGeom.SpatialConfig.Storage.Fillfactor)+ require.True(t, maxGeom.SpatialConfig.Storage.PadIndex)+ require.True(t, maxGeom.SpatialConfig.Storage.AllowRowLocks)+ require.True(t, maxGeom.SpatialConfig.Storage.AllowPageLocks)++ minGeog := requireSpatialIndex(t, table, "idx_min_geog", "min_config_geog", "GEOGRAPHY")+ require.Equal(t, int32(1), minGeog.SpatialConfig.Tessellation.CellsPerObject)+ requireGridLevels(t, minGeog, "LOW", "LOW", "LOW", "LOW")++ maxGeog := requireSpatialIndex(t, table, "idx_max_geog", "max_config_geog", "GEOGRAPHY")+ require.Equal(t, int32(8192), maxGeog.SpatialConfig.Tessellation.CellsPerObject)+ requireGridLevels(t, maxGeog, "HIGH", "HIGH", "HIGH", "HIGH")+ require.Equal(t, int32(50), maxGeog.SpatialConfig.Storage.Fillfactor)},},{- name: "sync_geometry_spatial_indexes",+ name: "mixed_index_types",setupSQL: `-CREATE SCHEMA [geom_test];+CREATE TABLE dbo.mixed_indexes (+ id INT IDENTITY(1,1) PRIMARY KEY,+ name NVARCHAR(100) NOT NULL,+ code VARCHAR(50) UNIQUE,+ location GEOMETRY NOT NULL,+ area GEOGRAPHY,+ status INT,+ created_at DATETIME2 DEFAULT GETDATE()+);GO--- Create table with GEOMETRY spatial columns-CREATE TABLE [geom_test].[spatial_data] (- [id] INT IDENTITY(1,1) PRIMARY KEY,- [name] NVARCHAR(100) NOT NULL,- [location] GEOMETRY NOT NULL,- [boundary] GEOMETRY NOT NULL-);+CREATE INDEX idx_name ON dbo.mixed_indexes(name);+CREATE INDEX idx_status_created ON dbo.mixed_indexes(status, created_at);+CREATE UNIQUE INDEX idx_name_status ON dbo.mixed_indexes(name, status) WHERE status IS NOT NULL;GO--- Create GEOMETRY spatial indexes with bounding box-CREATE SPATIAL INDEX [idx_location_geom] ON [geom_test].[spatial_data] ([location])-USING GEOMETRY_GRID+CREATE SPATIAL INDEX idx_location ON dbo.mixed_indexes(location)+USING GEOMETRY_GRIDWITH (- BOUNDING_BOX = (0, 0, 100, 100),- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = HIGH, LEVEL_3 = LOW, LEVEL_4 = LOW),- CELLS_PER_OBJECT = 16,- PAD_INDEX = ON,- FILLFACTOR = 90+ BOUNDING_BOX = (-100, -100, 100, 100),+ GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM),+ CELLS_PER_OBJECT = 16);GO-CREATE SPATIAL INDEX [idx_boundary_geom] ON [geom_test].[spatial_data] ([boundary])-USING GEOMETRY_GRID+CREATE SPATIAL INDEX idx_area ON dbo.mixed_indexes(area)+USING GEOGRAPHY_GRIDWITH (- BOUNDING_BOX = (-180, -90, 180, 90),- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),- CELLS_PER_OBJECT = 64+ GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = LOW, LEVEL_3 = HIGH, LEVEL_4 = LOW),+ CELLS_PER_OBJECT = 32);GO-`,- validate: func(t *testing.T, driver *Driver, _ string) {- // Add delay to allow metadata propagation- time.Sleep(1 * time.Second)-- // Sync database schema with retry logic- metadata, err := syncDBSchemaWithRetry(ctx, driver)- require.NoError(t, err)- require.NotNil(t, metadata)-- // Find the geom_test schema- var geomSchema *storepb.SchemaMetadata- for _, schema := range metadata.Schemas {- if schema.Name == "geom_test" {- geomSchema = schema- break- }- }- require.NotNil(t, geomSchema, "geom_test schema should exist")-- // Find the spatial_data table- require.Len(t, geomSchema.Tables, 1)- table := geomSchema.Tables[0]- require.Equal(t, "spatial_data", table.Name)-- // Extract spatial indexes- spatialIndexes := make(map[string]*storepb.IndexMetadata)- for _, idx := range table.Indexes {- if idx.Type == "SPATIAL" {- spatialIndexes[idx.Name] = idx- }- }-- // Use the same validation function- validateSpatialIndex := func(t *testing.T, idx *storepb.IndexMetadata, expectedName string, expectedColumn string, expectedDataType string) {- t.Helper()-- // Basic index properties- require.Equal(t, "SPATIAL", idx.Type, "Index type should be SPATIAL")- require.Equal(t, expectedName, idx.Name, "Index name should match")- require.False(t, idx.Unique, "Spatial indexes should not be unique")- require.False(t, idx.Primary, "Spatial indexes should not be primary")- require.Len(t, idx.Expressions, 1, "Should have exactly one expression")- require.Equal(t, expectedColumn, idx.Expressions[0], "Column name should match")- require.Len(t, idx.Descending, 1, "Should have descending flag")- require.False(t, idx.Descending[0], "Spatial indexes don't support descending")-- // Spatial configuration- require.NotNil(t, idx.SpatialConfig, "Spatial config should not be nil")- require.Equal(t, "SPATIAL", idx.SpatialConfig.Method, "Method should be SPATIAL")-- // Tessellation configuration- require.NotNil(t, idx.SpatialConfig.Tessellation, "Tessellation config should exist")-- // Storage configuration- require.NotNil(t, idx.SpatialConfig.Storage, "Storage config should exist")-- // Dimensional configuration- require.NotNil(t, idx.SpatialConfig.Dimensional, "Dimensional config should exist")- require.Equal(t, expectedDataType, idx.SpatialConfig.Dimensional.DataType)- require.Equal(t, int32(2), idx.SpatialConfig.Dimensional.Dimensions, "SQL Server spatial is always 2D")- }- // Validate each GEOMETRY spatial index with specific values- if idx, exists := spatialIndexes["idx_location_geom"]; exists {- validateSpatialIndex(t, idx, "idx_location_geom", "location", "GEOMETRY")-- // Verify specific configuration values- require.Equal(t, int32(16), idx.SpatialConfig.Tessellation.CellsPerObject, "CellsPerObject should be 16")- require.Equal(t, int32(90), idx.SpatialConfig.Storage.Fillfactor, "Fillfactor should be 90")- require.True(t, idx.SpatialConfig.Storage.PadIndex, "PadIndex should be ON")-- // Verify bounding box for GEOMETRY indexes- require.NotNil(t, idx.SpatialConfig.Tessellation.BoundingBox, "GEOMETRY index should have bounding box")- bbox := idx.SpatialConfig.Tessellation.BoundingBox- require.Equal(t, float64(0), bbox.Xmin, "Xmin should be 0")- require.Equal(t, float64(0), bbox.Ymin, "Ymin should be 0")- require.Equal(t, float64(100), bbox.Xmax, "Xmax should be 100")- require.Equal(t, float64(100), bbox.Ymax, "Ymax should be 100")-- // Verify grid levels- require.Len(t, idx.SpatialConfig.Tessellation.GridLevels, 4, "Should have 4 grid levels")- gridMap := make(map[int32]string)- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- gridMap[level.Level] = level.Density- }- require.Equal(t, "MEDIUM", gridMap[1], "Level 1 should be MEDIUM")- require.Equal(t, "HIGH", gridMap[2], "Level 2 should be HIGH")- require.Equal(t, "LOW", gridMap[3], "Level 3 should be LOW")- require.Equal(t, "LOW", gridMap[4], "Level 4 should be LOW")- } else {- t.Error("Expected spatial index idx_location_geom not found")- }+CREATE NONCLUSTERED COLUMNSTORE INDEX idx_columnstore ON dbo.mixed_indexes(id, name, status);+GO+`,+ validate: func(t *testing.T, metadata *storepb.DatabaseSchemaMetadata) {+ table := requireTable(t, metadata, "dbo", "mixed_indexes")- if idx, exists := spatialIndexes["idx_boundary_geom"]; exists {- validateSpatialIndex(t, idx, "idx_boundary_geom", "boundary", "GEOMETRY")-- // Verify specific configuration values- require.Equal(t, int32(64), idx.SpatialConfig.Tessellation.CellsPerObject, "CellsPerObject should be 64")-- // Verify bounding box- require.NotNil(t, idx.SpatialConfig.Tessellation.BoundingBox, "GEOMETRY index should have bounding box")- bbox := idx.SpatialConfig.Tessellation.BoundingBox- require.Equal(t, float64(-180), bbox.Xmin, "Xmin should be -180")- require.Equal(t, float64(-90), bbox.Ymin, "Ymin should be -90")- require.Equal(t, float64(180), bbox.Xmax, "Xmax should be 180")- require.Equal(t, float64(90), bbox.Ymax, "Ymax should be 90")-- // Verify grid levels- require.Len(t, idx.SpatialConfig.Tessellation.GridLevels, 4, "Should have 4 grid levels")- gridMap := make(map[int32]string)- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- gridMap[level.Level] = level.Density+ // Spatial indexes must not disturb how sync classifies the+ // other index kinds on the same table.+ counts := make(map[string]int)+ for _, index := range table.Indexes {+ switch {+ case index.Primary:+ counts["PRIMARY"]+++ case index.Type == "SPATIAL":+ counts["SPATIAL"]+++ case index.Type == "NONCLUSTERED COLUMNSTORE":+ counts["COLUMNSTORE"]+++ case index.Unique:+ counts["UNIQUE"]+++ default:+ counts["REGULAR"]++}- require.Equal(t, "HIGH", gridMap[1], "Level 1 should be HIGH")- require.Equal(t, "HIGH", gridMap[2], "Level 2 should be HIGH")- require.Equal(t, "MEDIUM", gridMap[3], "Level 3 should be MEDIUM")- require.Equal(t, "LOW", gridMap[4], "Level 4 should be LOW")- } else {- t.Error("Expected spatial index idx_boundary_geom not found")}-- require.GreaterOrEqual(t, len(spatialIndexes), 2, "Should have at least 2 GEOMETRY spatial indexes")+ require.Equal(t, map[string]int{+ "PRIMARY": 1,+ "UNIQUE": 2,+ "REGULAR": 2,+ "SPATIAL": 2,+ "COLUMNSTORE": 1,+ }, counts)++ location := requireSpatialIndex(t, table, "idx_location", "location", "GEOMETRY")+ require.Equal(t, int32(16), location.SpatialConfig.Tessellation.CellsPerObject)+ requireBoundingBox(t, location, -100, -100, 100, 100)+ requireGridLevels(t, location, "MEDIUM", "MEDIUM", "MEDIUM", "MEDIUM")++ area := requireSpatialIndex(t, table, "idx_area", "area", "GEOGRAPHY")+ require.Equal(t, int32(32), area.SpatialConfig.Tessellation.CellsPerObject)+ requireGridLevels(t, area, "HIGH", "LOW", "HIGH", "LOW")},},{- name: "sync_spatial_indexes_with_regular_indexes",+ name: "computed_columns",setupSQL: `-CREATE SCHEMA [mixed_test];-GO---- Create table with both spatial and regular columns-CREATE TABLE [mixed_test].[data_table] (- [id] INT IDENTITY(1,1) PRIMARY KEY,- [name] NVARCHAR(100) NOT NULL,- [code] VARCHAR(50) UNIQUE,- [geo_location] GEOGRAPHY NOT NULL,- [status] NVARCHAR(20),- [created_at] DATETIME2+CREATE TABLE dbo.spatial_computed (+ id INT IDENTITY(1,1) PRIMARY KEY,+ shape GEOMETRY NOT NULL,+ area AS shape.STArea() PERSISTED,+ perimeter AS shape.STLength() PERSISTED,+ center_x AS shape.STCentroid().STX PERSISTED,+ center_y AS shape.STCentroid().STY PERSISTED,+ as_text AS shape.STAsText() PERSISTED);GO--- Create regular indexes-CREATE INDEX [idx_name] ON [mixed_test].[data_table] ([name]);-GO--CREATE INDEX [idx_status_created] ON [mixed_test].[data_table] ([status], [created_at]);-GO---- Create spatial index-CREATE SPATIAL INDEX [idx_geo_location] ON [mixed_test].[data_table] ([geo_location])-USING GEOGRAPHY_GRID+CREATE SPATIAL INDEX idx_shape ON dbo.spatial_computed(shape)+USING GEOMETRY_GRIDWITH (- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM),- CELLS_PER_OBJECT = 16+ BOUNDING_BOX = (-500, -500, 500, 500),+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM),+ CELLS_PER_OBJECT = 24,+ FILLFACTOR = 75);GO-`,- validate: func(t *testing.T, driver *Driver, _ string) {- // Sync database schema with retry logic for transient errors- metadata, err := syncDBSchemaWithRetry(ctx, driver)- require.NoError(t, err)- require.NotNil(t, metadata)-- // Find the mixed_test schema- var mixedSchema *storepb.SchemaMetadata- for _, schema := range metadata.Schemas {- if schema.Name == "mixed_test" {- mixedSchema = schema- break- }- }- require.NotNil(t, mixedSchema, "mixed_test schema should exist")-- // Find the data_table- require.Len(t, mixedSchema.Tables, 1)- table := mixedSchema.Tables[0]- require.Equal(t, "data_table", table.Name)- // Categorize indexes- var spatialIndexes []*storepb.IndexMetadata- var regularIndexes []*storepb.IndexMetadata- var uniqueIndexes []*storepb.IndexMetadata- var primaryKeys []*storepb.IndexMetadata+CREATE INDEX idx_area ON dbo.spatial_computed(area);+CREATE INDEX idx_center ON dbo.spatial_computed(center_x, center_y);+GO+`,+ validate: func(t *testing.T, metadata *storepb.DatabaseSchemaMetadata) {+ table := requireTable(t, metadata, "dbo", "spatial_computed")- for _, idx := range table.Indexes {- switch {- case idx.Type == "SPATIAL":- spatialIndexes = append(spatialIndexes, idx)- case idx.Primary:- primaryKeys = append(primaryKeys, idx)- case idx.Unique:- uniqueIndexes = append(uniqueIndexes, idx)- default:- regularIndexes = append(regularIndexes, idx)- }+ columns := make(map[string]*storepb.ColumnMetadata)+ for _, column := range table.Columns {+ columns[column.Name] = column}-- // Verify we have the expected types of indexes- require.Equal(t, 1, len(primaryKeys), "Should have 1 primary key")- require.Equal(t, 1, len(uniqueIndexes), "Should have 1 unique index (code)")- require.GreaterOrEqual(t, len(regularIndexes), 2, "Should have at least 2 regular indexes")- require.GreaterOrEqual(t, len(spatialIndexes), 1, "Should have at least 1 spatial index")-- // Verify spatial index details- if len(spatialIndexes) > 0 {- spatialIdx := spatialIndexes[0]- require.Equal(t, "SPATIAL", spatialIdx.Type)- require.False(t, spatialIdx.Unique)- require.False(t, spatialIdx.Primary)- require.Len(t, spatialIdx.Expressions, 1)- require.Equal(t, "geo_location", spatialIdx.Expressions[0])-- // Spatial config should be available through DDL preservation- require.NotNil(t, spatialIdx.SpatialConfig, "Spatial config should be preserved for geo_location index")- require.NotNil(t, spatialIdx.SpatialConfig.Tessellation, "Tessellation config should exist")-- // Verify specific configuration values from CREATE INDEX statement- require.Equal(t, int32(16), spatialIdx.SpatialConfig.Tessellation.CellsPerObject, "CellsPerObject should be 16")-- // Verify grid levels - all set to MEDIUM- require.Len(t, spatialIdx.SpatialConfig.Tessellation.GridLevels, 4, "Should have 4 grid levels")- for _, level := range spatialIdx.SpatialConfig.Tessellation.GridLevels {- require.Equal(t, "MEDIUM", level.Density, fmt.Sprintf("Level %d should be MEDIUM", level.Level))- }-- // Verify tessellation scheme- require.Equal(t, "GEOGRAPHY_GRID", spatialIdx.SpatialConfig.Tessellation.Scheme, "Should use GEOGRAPHY_GRID tessellation")-- // Verify dimensional configuration- require.NotNil(t, spatialIdx.SpatialConfig.Dimensional, "Dimensional config should exist")- require.Equal(t, "GEOGRAPHY", spatialIdx.SpatialConfig.Dimensional.DataType)- require.Equal(t, int32(2), spatialIdx.SpatialConfig.Dimensional.Dimensions, "SQL Server spatial is always 2D")+ for _, name := range []string{"area", "perimeter", "center_x", "center_y", "as_text"} {+ require.Contains(t, columns, name)}- // Verify regular index details- regularIndexNames := make(map[string]bool)- for _, idx := range regularIndexes {- regularIndexNames[idx.Name] = true- }+ shape := requireSpatialIndex(t, table, "idx_shape", "shape", "GEOMETRY")+ require.Equal(t, int32(24), shape.SpatialConfig.Tessellation.CellsPerObject)+ requireBoundingBox(t, shape, -500, -500, 500, 500)+ requireGridLevels(t, shape, "LOW", "MEDIUM", "HIGH", "MEDIUM")+ require.Equal(t, int32(75), shape.SpatialConfig.Storage.Fillfactor)- // Check for expected regular indexes (allowing for name variations)- hasNameIndex := false- hasStatusIndex := false- for name := range regularIndexNames {- if strings.Contains(name, "name") {- hasNameIndex = true- }- if strings.Contains(name, "status") {- hasStatusIndex = true+ regular := 0+ for _, index := range table.Indexes {+ if index.Type != "SPATIAL" && !index.Primary && !index.Unique {+ regular++}}- require.True(t, hasNameIndex, "Should have an index on name column")- require.True(t, hasStatusIndex, "Should have an index involving status column")+ require.Equal(t, 2, regular, "indexes over computed columns should survive the sync")},},}for _, tc := range testCases {t.Run(tc.name, func(t *testing.T) {- // Create unique database name for this test- databaseName := fmt.Sprintf("test_%s_%d", tc.name, time.Now().Unix())-- // Create driver instance- driverInstance := &Driver{}- config := db.ConnectionConfig{- DataSource: &storepb.DataSource{- Type: storepb.DataSourceType_ADMIN,- Username: "sa",- Host: host,- Port: strconv.Itoa(portInt),- Database: "master",- },- Password: "Test123!",- ConnectionContext: db.ConnectionContext{- DatabaseName: "master",- },- }-- // Open connection- driver, err := driverInstance.Open(ctx, storepb.Engine_MSSQL, config)- require.NoError(t, err)- defer driver.Close(ctx)+ t.Parallel()+ driver := newSyncTestDatabase(ctx, t, container)+ executeBatches(ctx, t, driver, tc.setupSQL)- // Create test database- _, err = driver.Execute(ctx, fmt.Sprintf("CREATE DATABASE [%s]", databaseName), db.ExecuteOptions{CreateDatabase: true})+ metadata, err := driver.SyncDBSchema(ctx)require.NoError(t, err)- // Clean up database after test- defer func() {- // Close current connection first- driver.Close(ctx)-- // Reconnect to master to drop the test database- config.DataSource.Database = "master"- config.ConnectionContext.DatabaseName = "master"- cleanupDriver, err := driverInstance.Open(ctx, storepb.Engine_MSSQL, config)- if err == nil {- // Add retry logic for database cleanup- for i := 0; i < 3; i++ {- // First, force close all connections to the test database- _, _ = cleanupDriver.Execute(ctx, fmt.Sprintf("ALTER DATABASE [%s] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", databaseName), db.ExecuteOptions{CreateDatabase: true})-- // Then drop the database- _, err := cleanupDriver.Execute(ctx, fmt.Sprintf("DROP DATABASE [%s]", databaseName), db.ExecuteOptions{CreateDatabase: true})- if err == nil {- break- }-- // Wait before retry- if i < 2 {- time.Sleep(500 * time.Millisecond)- }- }- cleanupDriver.Close(ctx)- }- }()+ tc.validate(t, metadata)+ })+ }+}- // Connect to the test database- driver.Close(ctx)- config.DataSource.Database = databaseName- config.ConnectionContext.DatabaseName = databaseName- driver, err = driverInstance.Open(ctx, storepb.Engine_MSSQL, config)- require.NoError(t, err)+// TestSyncSpatialIndexIsRepeatable guards the spatial branch of the sync against+// per-connection state: the second sync of an unchanged database must report the+// same indexes as the first.+func TestSyncSpatialIndexIsRepeatable(t *testing.T) {+ t.Parallel()+ ctx := context.Background()+ container := testcontainer.SharedMSSQLContainer(t)- // Execute setup SQL with retry logic for transient errors- statements := splitSQLStatements(tc.setupSQL)- for _, stmt := range statements {- stmt = strings.TrimSpace(stmt)- if stmt == "" {- continue- }+ driver := newSyncTestDatabase(ctx, t, container)+ executeBatches(ctx, t, driver, `+CREATE SCHEMA spatial_test;+GO- // Retry statement execution for transient errors- var lastErr error- for retry := 0; retry < 3; retry++ {- _, err = driver.Execute(ctx, stmt, db.ExecuteOptions{})- if err == nil {- break- }+CREATE TABLE spatial_test.mixed_spatial (+ id INT IDENTITY(1,1) PRIMARY KEY,+ name NVARCHAR(100) NOT NULL,+ geo_point GEOGRAPHY NOT NULL,+ geo_line GEOGRAPHY NOT NULL,+ created_at DATETIME2 DEFAULT GETDATE()+);+GO- // Check if it's a transient error- if strings.Contains(err.Error(), "EOF") || strings.Contains(err.Error(), "i/o timeout") || strings.Contains(err.Error(), "connection") {- lastErr = err- if retry < 2 {- time.Sleep(time.Duration(retry+1) * 500 * time.Millisecond)- continue- }- }- // For non-transient errors, fail immediately- require.NoError(t, err)- }- if lastErr != nil {- require.NoError(t, lastErr)- }- }+CREATE SPATIAL INDEX idx_geo_point ON spatial_test.mixed_spatial(geo_point)+USING GEOGRAPHY_GRID+WITH (+ GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),+ CELLS_PER_OBJECT = 16+);+GO++CREATE SPATIAL INDEX idx_geo_line ON spatial_test.mixed_spatial(geo_line)+USING GEOGRAPHY_GRID+WITH (+ GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = MEDIUM, LEVEL_4 = HIGH),+ CELLS_PER_OBJECT = 8+);+GO+`)- // Cast to *Driver to access SyncDBSchema- mssqlDriver, ok := driver.(*Driver)- require.True(t, ok)+ first, err := driver.SyncDBSchema(ctx)+ require.NoError(t, err)- // Add delay to allow metadata propagation for all test cases- time.Sleep(1 * time.Second)+ table := requireTable(t, first, "spatial_test", "mixed_spatial")+ require.Len(t, spatialIndexes(table), 2)- // Run validation- tc.validate(t, mssqlDriver, databaseName)- })+ geoPoint := requireSpatialIndex(t, table, "idx_geo_point", "geo_point", "GEOGRAPHY")+ require.Equal(t, int32(16), geoPoint.SpatialConfig.Tessellation.CellsPerObject)+ requireGridLevels(t, geoPoint, "MEDIUM", "HIGH", "MEDIUM", "LOW")++ geoLine := requireSpatialIndex(t, table, "idx_geo_line", "geo_line", "GEOGRAPHY")+ require.Equal(t, int32(8), geoLine.SpatialConfig.Tessellation.CellsPerObject)+ requireGridLevels(t, geoLine, "LOW", "LOW", "MEDIUM", "HIGH")++ second, err := driver.SyncDBSchema(ctx)+ require.NoError(t, err)++ // Keyed by name because sync assembles indexes out of a map, so their+ // order within a table is not itself part of the contract.+ byName := func(metadata *storepb.DatabaseSchemaMetadata) map[string]*storepb.IndexMetadata {+ indexes := make(map[string]*storepb.IndexMetadata)+ for _, index := range spatialIndexes(requireTable(t, metadata, "spatial_test", "mixed_spatial")) {+ indexes[index.Name] = index+ }+ return indexes}+ require.Empty(t, cmp.Diff(byName(first), byName(second), protocmp.Transform()))}-func splitSQLStatements(script string) []string {- var statements []string- lines := strings.Split(script, "\n")- var currentStatement strings.Builder-- for _, line := range lines {- trimmedLine := strings.TrimSpace(line)- if strings.EqualFold(trimmedLine, "GO") {- if currentStatement.Len() > 0 {- statements = append(statements, currentStatement.String())- currentStatement.Reset()- }- } else {- currentStatement.WriteString(line)- currentStatement.WriteString("\n")+func spatialIndexes(table *storepb.TableMetadata) []*storepb.IndexMetadata {+ var indexes []*storepb.IndexMetadata+ for _, index := range table.Indexes {+ if index.Type == "SPATIAL" {+ indexes = append(indexes, index)}}+ return indexes+}- if currentStatement.Len() > 0 {- statements = append(statements, currentStatement.String())+// requireSpatialIndex asserts the properties every synced spatial index carries+// and returns it for the case-specific configuration checks. dataType is+// GEOMETRY or GEOGRAPHY; only GEOMETRY indexes carry a bounding box.+func requireSpatialIndex(t *testing.T, table *storepb.TableMetadata, name, column, dataType string) *storepb.IndexMetadata {+ t.Helper()++ var index *storepb.IndexMetadata+ for _, candidate := range table.Indexes {+ if candidate.Name == name {+ index = candidate+ break+ }}+ require.NotNilf(t, index, "spatial index %s not synced", name)++ require.Equal(t, "SPATIAL", index.Type)+ require.False(t, index.Unique)+ require.False(t, index.Primary)+ require.Equal(t, []string{column}, index.Expressions)+ require.Equal(t, []bool{false}, index.Descending, "spatial indexes have no direction")++ require.NotNil(t, index.SpatialConfig)+ require.Equal(t, "SPATIAL", index.SpatialConfig.Method)+ require.NotNil(t, index.SpatialConfig.Tessellation)+ require.Equal(t, dataType+"_GRID", index.SpatialConfig.Tessellation.Scheme)+ require.NotNil(t, index.SpatialConfig.Storage)+ require.NotNil(t, index.SpatialConfig.Dimensional)+ require.Equal(t, dataType, index.SpatialConfig.Dimensional.DataType)+ require.Equal(t, int32(2), index.SpatialConfig.Dimensional.Dimensions, "SQL Server spatial is always 2D")++ if dataType == "GEOGRAPHY" {+ require.Nil(t, index.SpatialConfig.Tessellation.BoundingBox, "GEOGRAPHY indexes carry no bounding box")+ }+ return index+}- return statements+func requireBoundingBox(t *testing.T, index *storepb.IndexMetadata, xmin, ymin, xmax, ymax float64) {+ t.Helper()++ bbox := index.SpatialConfig.Tessellation.BoundingBox+ require.NotNil(t, bbox)+ require.Equal(t, xmin, bbox.Xmin)+ require.Equal(t, ymin, bbox.Ymin)+ require.Equal(t, xmax, bbox.Xmax)+ require.Equal(t, ymax, bbox.Ymax)+}++func requireGridLevels(t *testing.T, index *storepb.IndexMetadata, level1, level2, level3, level4 string) {+ t.Helper()++ densities := make(map[int32]string)+ for _, level := range index.SpatialConfig.Tessellation.GridLevels {+ densities[level.Level] = level.Density+ }+ require.Equal(t, map[int32]string{1: level1, 2: level2, 3: level3, 4: level4}, densities)}diff --git a/backend/plugin/db/mssql/sync_testutil_test.go b/backend/plugin/db/mssql/sync_testutil_test.gonew file mode 100644index 0000000000..84cf7b6426--- /dev/null+++ b/backend/plugin/db/mssql/sync_testutil_test.go@@ -0,0 +1,85 @@+package mssql++import (+ "context"+ "fmt"+ "strings"+ "testing"++ "github.com/google/uuid"+ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/common/testcontainer"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/db"+)++// newSyncTestDatabase gives the test a database of its own on the shared+// container so the cases can run in parallel, and returns a driver open on it.+func newSyncTestDatabase(ctx context.Context, t *testing.T, container *testcontainer.Container) *Driver {+ t.Helper()++ name := fmt.Sprintf("sync_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))+ _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE [%s]", name))+ require.NoError(t, err)++ opened := openMSSQL(ctx, t, container.GetHost(), container.GetPort(), name)+ t.Cleanup(func() { opened.Close(ctx) })++ driver, ok := opened.(*Driver)+ require.True(t, ok, "expected *Driver")+ return driver+}++// executeBatches runs a setup script one GO-separated batch at a time, which is+// what SQL Server requires for CREATE SCHEMA and CREATE INDEX.+func executeBatches(ctx context.Context, t *testing.T, driver *Driver, script string) {+ t.Helper()++ for _, batch := range splitSQLStatements(script) {+ if strings.TrimSpace(batch) == "" {+ continue+ }+ _, err := driver.Execute(ctx, batch, db.ExecuteOptions{})+ require.NoError(t, err, "setup batch: %s", batch)+ }+}++func requireTable(t *testing.T, metadata *storepb.DatabaseSchemaMetadata, schemaName, tableName string) *storepb.TableMetadata {+ t.Helper()++ for _, schema := range metadata.Schemas {+ if schema.Name != schemaName {+ continue+ }+ for _, table := range schema.Tables {+ if table.Name == tableName {+ return table+ }+ }+ }+ require.FailNowf(t, "table not synced", "%s.%s", schemaName, tableName)+ return nil+}++func splitSQLStatements(script string) []string {+ var statements []string+ var current strings.Builder++ for _, line := range strings.Split(script, "\n") {+ if strings.EqualFold(strings.TrimSpace(line), "GO") {+ if current.Len() > 0 {+ statements = append(statements, current.String())+ current.Reset()+ }+ continue+ }+ current.WriteString(line)+ current.WriteString("\n")+ }++ if current.Len() > 0 {+ statements = append(statements, current.String())+ }+ return statements+}diff --git a/backend/plugin/db/mssql/sync_unique_index_testcontainer_test.go b/backend/plugin/db/mssql/sync_unique_index_testcontainer_test.gonew file mode 100644index 0000000000..736962faea--- /dev/null+++ b/backend/plugin/db/mssql/sync_unique_index_testcontainer_test.go@@ -0,0 +1,91 @@+package mssql++import (+ "context"+ "testing"++ _ "github.com/microsoft/go-mssqldb"+ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/common/testcontainer"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+)++// TestSyncUniqueIndex covers which synced indexes carry Unique. SQL Server+// splits the answer across two catalog columns: is_unique_constraint for a+// UNIQUE constraint and is_unique for a standalone CREATE UNIQUE INDEX. Sync+// reads them in separate queries, and reading only the first reported every+// CREATE UNIQUE INDEX as non-unique, which made the omni parser and sync+// disagree and left every declared unique index looking like a pending change.+func TestSyncUniqueIndex(t *testing.T) {+ t.Parallel()+ ctx := context.Background()+ container := testcontainer.SharedMSSQLContainer(t)++ driver := newSyncTestDatabase(ctx, t, container)+ executeBatches(ctx, t, driver, `+CREATE TABLE dbo.index_kinds (+ id INT IDENTITY(1,1) PRIMARY KEY,+ code VARCHAR(50) UNIQUE,+ name NVARCHAR(100) NOT NULL,+ status INT,+ tag NVARCHAR(50)+);+GO++CREATE UNIQUE INDEX idx_unique_name ON dbo.index_kinds(name);+CREATE UNIQUE INDEX idx_unique_filtered ON dbo.index_kinds(status) WHERE status IS NOT NULL;+CREATE INDEX idx_plain_tag ON dbo.index_kinds(tag);+CREATE UNIQUE INDEX idx_unique_desc ON dbo.index_kinds(name DESC, status ASC);+GO+`)++ metadata, err := driver.SyncDBSchema(ctx)+ require.NoError(t, err)++ table := requireTable(t, metadata, "dbo", "index_kinds")+ byName := make(map[string]*storepb.IndexMetadata)+ for _, index := range table.Indexes {+ byName[index.Name] = index+ }++ // A standalone CREATE UNIQUE INDEX is unique but is not a constraint.+ for _, name := range []string{"idx_unique_name", "idx_unique_filtered", "idx_unique_desc"} {+ index := byName[name]+ require.NotNilf(t, index, "index %s not synced", name)+ require.Truef(t, index.Unique, "%s was created UNIQUE", name)+ require.Falsef(t, index.Primary, "%s is not a primary key", name)+ require.Falsef(t, index.IsConstraint, "%s is an index, not a constraint", name)+ }++ plain := byName["idx_plain_tag"]+ require.NotNil(t, plain)+ require.False(t, plain.Unique, "a plain CREATE INDEX must not be reported unique")++ // The UNIQUE column constraint keeps arriving through the constraint query,+ // which is the only path that sets IsConstraint.+ var columnConstraint *storepb.IndexMetadata+ for name, index := range byName {+ if index.IsConstraint && !index.Primary {+ require.Nilf(t, columnConstraint, "expected one unique constraint, also got %s", name)+ columnConstraint = index+ }+ }+ require.NotNil(t, columnConstraint, "the UNIQUE constraint on code should be synced")+ require.True(t, columnConstraint.Unique)+ require.Equal(t, []string{"code"}, columnConstraint.Expressions)++ var primary *storepb.IndexMetadata+ for _, index := range byName {+ if index.Primary {+ primary = index+ }+ }+ require.NotNil(t, primary)+ require.True(t, primary.Unique, "a primary key is unique")+ require.Equal(t, []string{"id"}, primary.Expressions)++ // Column order and direction must survive alongside the unique flag.+ require.Equal(t, []string{"name", "status"}, byName["idx_unique_desc"].Expressions)+ require.Equal(t, []bool{true, false}, byName["idx_unique_desc"].Descending)+}diff --git a/backend/plugin/db/pg/sync_composite_type_test.go b/backend/plugin/db/pg/sync_composite_type_test.gonew file mode 100644index 0000000000..6865e1d280--- /dev/null+++ b/backend/plugin/db/pg/sync_composite_type_test.go@@ -0,0 +1,117 @@+package pg++import (+ "context"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/common/testcontainer"+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/db"+)++// TestSyncCompositeTypes pins the exact metadata sync produces for composite+// types. The definition round-trip in backend/plugin/schema/pg compares two+// synced databases against each other, so a sync that is wrong the same way on+// both sides passes it vacuously; these assertions do not.+func TestSyncCompositeTypes(t *testing.T) {+ t.Parallel()+ ctx := context.Background()++ pgContainer := testcontainer.SharedPgContainer(t)+ dbName, pgDB := testcontainer.NewPgDatabase(t)++ _, err := pgDB.ExecContext(ctx, `+CREATE TYPE zz_base AS (street text COLLATE "C", city varchar(50));+COMMENT ON TYPE zz_base IS 'base address type';+COMMENT ON COLUMN zz_base.street IS 'street line';+CREATE TYPE aa_nested AS (home zz_base, homes zz_base[], tags text[]);+CREATE TYPE empty_type AS ();+CREATE SCHEMA geo;+CREATE TYPE geo.point2 AS (lat numeric(9,6), lng numeric(9,6));+CREATE TYPE wrapper AS (p geo.point2);+CREATE TABLE plain_table (id int);+CREATE SCHEMA locale;+CREATE COLLATION locale.mycoll (locale = 'C');+CREATE TYPE coll_qualified AS (v text COLLATE locale.mycoll);+`)+ require.NoError(t, err)++ driver := &Driver{}+ config := db.ConnectionConfig{+ DataSource: &storepb.DataSource{+ Type: storepb.DataSourceType_ADMIN,+ Username: "postgres",+ Host: pgContainer.GetHost(),+ Port: pgContainer.GetPort(),+ Database: dbName,+ },+ Password: "root-password",+ ConnectionContext: db.ConnectionContext{+ EngineVersion: "16.0",+ DatabaseName: dbName,+ },+ }++ openedDriver, err := driver.Open(ctx, storepb.Engine_POSTGRES, config)+ require.NoError(t, err)+ t.Cleanup(func() { require.NoError(t, openedDriver.Close(ctx)) })++ pgDriver, ok := openedDriver.(*Driver)+ require.True(t, ok)++ metadata, err := pgDriver.SyncDBSchema(ctx)+ require.NoError(t, err)++ compositesBySchema := make(map[string]map[string]*storepb.CompositeTypeMetadata)+ for _, schemaMeta := range metadata.Schemas {+ m := make(map[string]*storepb.CompositeTypeMetadata)+ for _, composite := range schemaMeta.CompositeTypes {+ m[composite.Name] = composite+ }+ compositesBySchema[schemaMeta.Name] = m+ }++ public := compositesBySchema["public"]+ require.Len(t, public, 5, "public schema should have exactly the created composite types")+ require.NotContains(t, public, "plain_table", "table row types must not be synced as composite types")++ collQualified := public["coll_qualified"]+ require.NotNil(t, collQualified)+ require.Len(t, collQualified.Attributes, 1)+ require.Equal(t, "locale.mycoll", collQualified.Attributes[0].Collation,+ "non-pg_catalog collations must be schema-qualified")++ base := public["zz_base"]+ require.NotNil(t, base)+ require.Equal(t, "base address type", base.Comment)+ require.Len(t, base.Attributes, 2)+ require.Equal(t, "street", base.Attributes[0].Name)+ require.Equal(t, "text", base.Attributes[0].Type)+ require.Equal(t, `"C"`, base.Attributes[0].Collation)+ require.Equal(t, "street line", base.Attributes[0].Comment)+ require.Equal(t, "city", base.Attributes[1].Name)+ require.Equal(t, "character varying(50)", base.Attributes[1].Type)+ require.Empty(t, base.Attributes[1].Collation)++ nested := public["aa_nested"]+ require.NotNil(t, nested)+ require.Len(t, nested.Attributes, 3)+ require.Equal(t, "public.zz_base", nested.Attributes[0].Type, "user-defined attribute types must be schema-qualified")+ require.Equal(t, "public.zz_base[]", nested.Attributes[1].Type)+ require.Equal(t, "text[]", nested.Attributes[2].Type)++ empty := public["empty_type"]+ require.NotNil(t, empty, "zero-attribute composite types must be synced")+ require.Empty(t, empty.Attributes)++ wrapper := public["wrapper"]+ require.NotNil(t, wrapper)+ require.Len(t, wrapper.Attributes, 1)+ require.Equal(t, "geo.point2", wrapper.Attributes[0].Type, "cross-schema attribute types must be schema-qualified")++ geo := compositesBySchema["geo"]+ require.Len(t, geo, 1)+ require.NotNil(t, geo["point2"])+}diff --git a/backend/plugin/schema/mssql/get_database_metadata_testcontainer_test.go b/backend/plugin/schema/mssql/get_database_metadata_testcontainer_test.godeleted file mode 100644index ce5d4309dd..0000000000--- a/backend/plugin/schema/mssql/get_database_metadata_testcontainer_test.go+++ /dev/null@@ -1,672 +0,0 @@-package mssql--import (- "context"- "fmt"- "strings"- "testing"-- "github.com/google/uuid"- _ "github.com/microsoft/go-mssqldb"- "github.com/stretchr/testify/require"-- "github.com/bytebase/bytebase/backend/common/testcontainer"- storepb "github.com/bytebase/bytebase/backend/generated-go/store"-)--//nolint:tparallel-func TestGetDatabaseMetadataWithTestcontainer(t *testing.T) {- ctx := context.Background()- container := testcontainer.SharedMSSQLContainer(t)-- testCases := []struct {- name string- setupSQL string- validate func(*testing.T, *storepb.DatabaseSchemaMetadata)- }{- {- name: "spatial_indexes_comprehensive",- setupSQL: `-CREATE SCHEMA geo;-GO--CREATE TABLE geo.locations (- id INT IDENTITY(1,1) PRIMARY KEY,- name NVARCHAR(200) NOT NULL,- location_point GEOMETRY NOT NULL,- boundary_polygon GEOMETRY,- geo_location GEOGRAPHY NOT NULL,- route_line GEOGRAPHY-);-GO---- Create GEOMETRY spatial indexes with full configuration-CREATE SPATIAL INDEX idx_location_point ON geo.locations(location_point)-USING GEOMETRY_GRID-WITH (- BOUNDING_BOX = (-180, -90, 180, 90),- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = HIGH, LEVEL_3 = MEDIUM, LEVEL_4 = LOW),- CELLS_PER_OBJECT = 32,- PAD_INDEX = ON,- FILLFACTOR = 90,- ALLOW_ROW_LOCKS = ON,- ALLOW_PAGE_LOCKS = ON-);-GO--CREATE SPATIAL INDEX idx_boundary_polygon ON geo.locations(boundary_polygon)-USING GEOMETRY_GRID-WITH (- BOUNDING_BOX = (0, 0, 1000, 1000),- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = HIGH, LEVEL_4 = HIGH),- CELLS_PER_OBJECT = 64-);-GO---- Create GEOGRAPHY spatial indexes with different configurations-CREATE SPATIAL INDEX idx_geo_location ON geo.locations(geo_location)-USING GEOGRAPHY_GRID-WITH (- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM),- CELLS_PER_OBJECT = 16,- FILLFACTOR = 85,- PAD_INDEX = OFF,- ALLOW_ROW_LOCKS = OFF,- ALLOW_PAGE_LOCKS = OFF-);-GO--CREATE SPATIAL INDEX idx_route_line ON geo.locations(route_line)-USING GEOGRAPHY_GRID-WITH (- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = HIGH, LEVEL_4 = HIGH),- CELLS_PER_OBJECT = 128-);-GO-`,- validate: func(t *testing.T, metadata *storepb.DatabaseSchemaMetadata) {- // Find the geo schema- var geoSchema *storepb.SchemaMetadata- for _, schema := range metadata.Schemas {- if schema.Name == "geo" {- geoSchema = schema- break- }- }- require.NotNil(t, geoSchema, "geo schema should exist")-- // Find the locations table- require.Len(t, geoSchema.Tables, 1)- table := geoSchema.Tables[0]- require.Equal(t, "locations", table.Name)-- // Verify columns- require.Len(t, table.Columns, 6)- columnNames := make(map[string]*storepb.ColumnMetadata)- for _, col := range table.Columns {- columnNames[col.Name] = col- }-- // Check spatial columns- require.Contains(t, columnNames, "location_point")- require.Equal(t, "geometry", strings.ToLower(columnNames["location_point"].Type))-- require.Contains(t, columnNames, "boundary_polygon")- require.Equal(t, "geometry", strings.ToLower(columnNames["boundary_polygon"].Type))-- require.Contains(t, columnNames, "geo_location")- require.Equal(t, "geography", strings.ToLower(columnNames["geo_location"].Type))-- require.Contains(t, columnNames, "route_line")- require.Equal(t, "geography", strings.ToLower(columnNames["route_line"].Type))-- // Verify spatial indexes with full configuration validation- spatialIndexes := make(map[string]*storepb.IndexMetadata)- for _, idx := range table.Indexes {- if idx.Type == "SPATIAL" {- spatialIndexes[idx.Name] = idx- }- }-- require.Len(t, spatialIndexes, 4, "Should have exactly 4 spatial indexes")-- // Validate idx_location_point- if idx, exists := spatialIndexes["idx_location_point"]; exists {- require.NotNil(t, idx.SpatialConfig, "Spatial config should exist for idx_location_point")- require.Equal(t, "SPATIAL", idx.SpatialConfig.Method)-- // Validate tessellation- require.NotNil(t, idx.SpatialConfig.Tessellation)- require.Equal(t, "GEOMETRY_GRID", idx.SpatialConfig.Tessellation.Scheme)- require.Equal(t, int32(32), idx.SpatialConfig.Tessellation.CellsPerObject)-- // Validate bounding box- require.NotNil(t, idx.SpatialConfig.Tessellation.BoundingBox)- bbox := idx.SpatialConfig.Tessellation.BoundingBox- require.Equal(t, float64(-180), bbox.Xmin)- require.Equal(t, float64(-90), bbox.Ymin)- require.Equal(t, float64(180), bbox.Xmax)- require.Equal(t, float64(90), bbox.Ymax)-- // Validate grid levels- require.Len(t, idx.SpatialConfig.Tessellation.GridLevels, 4)- gridMap := make(map[int32]string)- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- gridMap[level.Level] = level.Density- }- require.Equal(t, "MEDIUM", gridMap[1])- require.Equal(t, "HIGH", gridMap[2])- require.Equal(t, "MEDIUM", gridMap[3])- require.Equal(t, "LOW", gridMap[4])-- // Validate storage configuration- require.NotNil(t, idx.SpatialConfig.Storage)- require.Equal(t, int32(90), idx.SpatialConfig.Storage.Fillfactor)- require.True(t, idx.SpatialConfig.Storage.PadIndex)- require.True(t, idx.SpatialConfig.Storage.AllowRowLocks)- require.True(t, idx.SpatialConfig.Storage.AllowPageLocks)-- // Validate dimensional configuration- require.NotNil(t, idx.SpatialConfig.Dimensional)- require.Equal(t, "GEOMETRY", idx.SpatialConfig.Dimensional.DataType)- require.Equal(t, int32(2), idx.SpatialConfig.Dimensional.Dimensions)- } else {- t.Error("idx_location_point not found")- }-- // Validate idx_boundary_polygon- if idx, exists := spatialIndexes["idx_boundary_polygon"]; exists {- require.NotNil(t, idx.SpatialConfig, "Spatial config should exist for idx_boundary_polygon")- require.Equal(t, "GEOMETRY_GRID", idx.SpatialConfig.Tessellation.Scheme)- require.Equal(t, int32(64), idx.SpatialConfig.Tessellation.CellsPerObject)-- // Validate bounding box- bbox := idx.SpatialConfig.Tessellation.BoundingBox- require.Equal(t, float64(0), bbox.Xmin)- require.Equal(t, float64(0), bbox.Ymin)- require.Equal(t, float64(1000), bbox.Xmax)- require.Equal(t, float64(1000), bbox.Ymax)-- // Validate grid levels- gridMap := make(map[int32]string)- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- gridMap[level.Level] = level.Density- }- require.Equal(t, "LOW", gridMap[1])- require.Equal(t, "LOW", gridMap[2])- require.Equal(t, "HIGH", gridMap[3])- require.Equal(t, "HIGH", gridMap[4])- } else {- t.Error("idx_boundary_polygon not found")- }-- // Validate idx_geo_location- if idx, exists := spatialIndexes["idx_geo_location"]; exists {- require.NotNil(t, idx.SpatialConfig, "Spatial config should exist for idx_geo_location")- require.Equal(t, "GEOGRAPHY_GRID", idx.SpatialConfig.Tessellation.Scheme)- require.Equal(t, int32(16), idx.SpatialConfig.Tessellation.CellsPerObject)-- // GEOGRAPHY indexes don't have bounding box- require.Nil(t, idx.SpatialConfig.Tessellation.BoundingBox)-- // Validate grid levels- gridMap := make(map[int32]string)- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- gridMap[level.Level] = level.Density- }- require.Equal(t, "LOW", gridMap[1])- require.Equal(t, "MEDIUM", gridMap[2])- require.Equal(t, "HIGH", gridMap[3])- require.Equal(t, "MEDIUM", gridMap[4])-- // Validate storage configuration- require.NotNil(t, idx.SpatialConfig.Storage)- require.Equal(t, int32(85), idx.SpatialConfig.Storage.Fillfactor)- require.False(t, idx.SpatialConfig.Storage.PadIndex)- require.False(t, idx.SpatialConfig.Storage.AllowRowLocks)- require.False(t, idx.SpatialConfig.Storage.AllowPageLocks)-- // Validate dimensional configuration- require.Equal(t, "GEOGRAPHY", idx.SpatialConfig.Dimensional.DataType)- require.Equal(t, int32(2), idx.SpatialConfig.Dimensional.Dimensions)- } else {- t.Error("idx_geo_location not found")- }-- // Validate idx_route_line- if idx, exists := spatialIndexes["idx_route_line"]; exists {- require.NotNil(t, idx.SpatialConfig, "Spatial config should exist for idx_route_line")- require.Equal(t, "GEOGRAPHY_GRID", idx.SpatialConfig.Tessellation.Scheme)- require.Equal(t, int32(128), idx.SpatialConfig.Tessellation.CellsPerObject)-- // Validate grid levels- gridMap := make(map[int32]string)- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- gridMap[level.Level] = level.Density- }- require.Equal(t, "HIGH", gridMap[1])- require.Equal(t, "HIGH", gridMap[2])- require.Equal(t, "HIGH", gridMap[3])- require.Equal(t, "HIGH", gridMap[4])- } else {- t.Error("idx_route_line not found")- }- },- },- {- name: "spatial_indexes_edge_cases",- setupSQL: `-CREATE TABLE dbo.spatial_edge_cases (- id INT IDENTITY(1,1) PRIMARY KEY,- min_config_geom GEOMETRY NOT NULL,- max_config_geom GEOMETRY NOT NULL,- min_config_geog GEOGRAPHY NOT NULL,- max_config_geog GEOGRAPHY NOT NULL-);-GO---- Minimal configuration spatial index-CREATE SPATIAL INDEX idx_min_geom ON dbo.spatial_edge_cases(min_config_geom)-USING GEOMETRY_GRID-WITH (- BOUNDING_BOX = (0, 0, 1, 1),- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = LOW, LEVEL_4 = LOW),- CELLS_PER_OBJECT = 1-);-GO---- Maximum configuration spatial index-CREATE SPATIAL INDEX idx_max_geom ON dbo.spatial_edge_cases(max_config_geom)-USING GEOMETRY_GRID-WITH (- BOUNDING_BOX = (-1000000, -1000000, 1000000, 1000000),- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = HIGH, LEVEL_4 = HIGH),- CELLS_PER_OBJECT = 8192,- PAD_INDEX = ON,- FILLFACTOR = 100,- ALLOW_ROW_LOCKS = ON,- ALLOW_PAGE_LOCKS = ON,- MAXDOP = 4,- DATA_COMPRESSION = PAGE-);-GO---- Minimal GEOGRAPHY index-CREATE SPATIAL INDEX idx_min_geog ON dbo.spatial_edge_cases(min_config_geog)-USING GEOGRAPHY_GRID-WITH (- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = LOW, LEVEL_3 = LOW, LEVEL_4 = LOW),- CELLS_PER_OBJECT = 1-);-GO---- Maximum GEOGRAPHY index-CREATE SPATIAL INDEX idx_max_geog ON dbo.spatial_edge_cases(max_config_geog)-USING GEOGRAPHY_GRID-WITH (- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = HIGH, LEVEL_3 = HIGH, LEVEL_4 = HIGH),- CELLS_PER_OBJECT = 8192,- FILLFACTOR = 50-);-GO-`,- validate: func(t *testing.T, metadata *storepb.DatabaseSchemaMetadata) {- // Find dbo schema- var dboSchema *storepb.SchemaMetadata- for _, schema := range metadata.Schemas {- if schema.Name == "dbo" {- dboSchema = schema- break- }- }- require.NotNil(t, dboSchema, "dbo schema should exist")-- // Find spatial_edge_cases table- var spatialTable *storepb.TableMetadata- for _, table := range dboSchema.Tables {- if table.Name == "spatial_edge_cases" {- spatialTable = table- break- }- }- require.NotNil(t, spatialTable, "spatial_edge_cases table should exist")-- // Collect spatial indexes- spatialIndexes := make(map[string]*storepb.IndexMetadata)- for _, idx := range spatialTable.Indexes {- if idx.Type == "SPATIAL" {- spatialIndexes[idx.Name] = idx- }- }-- require.Len(t, spatialIndexes, 4, "Should have exactly 4 spatial indexes")-- // Validate minimal GEOMETRY index- if idx, exists := spatialIndexes["idx_min_geom"]; exists {- require.NotNil(t, idx.SpatialConfig)- require.Equal(t, int32(1), idx.SpatialConfig.Tessellation.CellsPerObject)-- bbox := idx.SpatialConfig.Tessellation.BoundingBox- require.Equal(t, float64(0), bbox.Xmin)- require.Equal(t, float64(0), bbox.Ymin)- require.Equal(t, float64(1), bbox.Xmax)- require.Equal(t, float64(1), bbox.Ymax)-- // All grid levels should be LOW- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- require.Equal(t, "LOW", level.Density)- }- } else {- t.Error("idx_min_geom not found")- }-- // Validate maximum GEOMETRY index- if idx, exists := spatialIndexes["idx_max_geom"]; exists {- require.NotNil(t, idx.SpatialConfig)- require.Equal(t, int32(8192), idx.SpatialConfig.Tessellation.CellsPerObject)-- bbox := idx.SpatialConfig.Tessellation.BoundingBox- require.Equal(t, float64(-1000000), bbox.Xmin)- require.Equal(t, float64(-1000000), bbox.Ymin)- require.Equal(t, float64(1000000), bbox.Xmax)- require.Equal(t, float64(1000000), bbox.Ymax)-- // All grid levels should be HIGH- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- require.Equal(t, "HIGH", level.Density)- }-- // Check storage options- require.Equal(t, int32(100), idx.SpatialConfig.Storage.Fillfactor)- require.True(t, idx.SpatialConfig.Storage.PadIndex)- require.True(t, idx.SpatialConfig.Storage.AllowRowLocks)- require.True(t, idx.SpatialConfig.Storage.AllowPageLocks)- } else {- t.Error("idx_max_geom not found")- }-- // Validate minimal GEOGRAPHY index- if idx, exists := spatialIndexes["idx_min_geog"]; exists {- require.NotNil(t, idx.SpatialConfig)- require.Equal(t, int32(1), idx.SpatialConfig.Tessellation.CellsPerObject)- require.Nil(t, idx.SpatialConfig.Tessellation.BoundingBox) // GEOGRAPHY doesn't have bounding box-- // All grid levels should be LOW- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- require.Equal(t, "LOW", level.Density)- }- } else {- t.Error("idx_min_geog not found")- }-- // Validate maximum GEOGRAPHY index- if idx, exists := spatialIndexes["idx_max_geog"]; exists {- require.NotNil(t, idx.SpatialConfig)- require.Equal(t, int32(8192), idx.SpatialConfig.Tessellation.CellsPerObject)-- // All grid levels should be HIGH- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- require.Equal(t, "HIGH", level.Density)- }-- require.Equal(t, int32(50), idx.SpatialConfig.Storage.Fillfactor)- } else {- t.Error("idx_max_geog not found")- }- },- },- {- name: "mixed_spatial_and_regular_indexes",- setupSQL: `-CREATE TABLE dbo.mixed_indexes (- id INT IDENTITY(1,1) PRIMARY KEY,- name NVARCHAR(100) NOT NULL,- code VARCHAR(50) UNIQUE,- location GEOMETRY NOT NULL,- area GEOGRAPHY,- status INT,- created_at DATETIME2 DEFAULT GETDATE()-);-GO---- Create regular indexes-CREATE INDEX idx_name ON dbo.mixed_indexes(name);-CREATE INDEX idx_status_created ON dbo.mixed_indexes(status, created_at);-CREATE UNIQUE INDEX idx_name_status ON dbo.mixed_indexes(name, status) WHERE status IS NOT NULL;-GO---- Create spatial indexes-CREATE SPATIAL INDEX idx_location ON dbo.mixed_indexes(location)-USING GEOMETRY_GRID-WITH (- BOUNDING_BOX = (-100, -100, 100, 100),- GRIDS = (LEVEL_1 = MEDIUM, LEVEL_2 = MEDIUM, LEVEL_3 = MEDIUM, LEVEL_4 = MEDIUM),- CELLS_PER_OBJECT = 16-);-GO--CREATE SPATIAL INDEX idx_area ON dbo.mixed_indexes(area)-USING GEOGRAPHY_GRID-WITH (- GRIDS = (LEVEL_1 = HIGH, LEVEL_2 = LOW, LEVEL_3 = HIGH, LEVEL_4 = LOW),- CELLS_PER_OBJECT = 32-);-GO---- Create columnstore index-CREATE NONCLUSTERED COLUMNSTORE INDEX idx_columnstore ON dbo.mixed_indexes(id, name, status);-GO-`,- validate: func(t *testing.T, metadata *storepb.DatabaseSchemaMetadata) {- // Find dbo schema- var dboSchema *storepb.SchemaMetadata- for _, schema := range metadata.Schemas {- if schema.Name == "dbo" {- dboSchema = schema- break- }- }- require.NotNil(t, dboSchema, "dbo schema should exist")-- // Find mixed_indexes table- var mixedTable *storepb.TableMetadata- for _, table := range dboSchema.Tables {- if table.Name == "mixed_indexes" {- mixedTable = table- break- }- }- require.NotNil(t, mixedTable, "mixed_indexes table should exist")-- // Count index types- indexTypes := make(map[string]int)- spatialIndexes := make(map[string]*storepb.IndexMetadata)-- for _, idx := range mixedTable.Indexes {- if idx.Primary {- indexTypes["PRIMARY"]++- } else if idx.Type == "SPATIAL" {- indexTypes["SPATIAL"]++- spatialIndexes[idx.Name] = idx- } else if idx.Type == "NONCLUSTERED COLUMNSTORE" {- indexTypes["COLUMNSTORE"]++- } else if idx.Unique {- indexTypes["UNIQUE"]++- } else {- indexTypes["REGULAR"]++- }- }-- require.Equal(t, 1, indexTypes["PRIMARY"], "Should have 1 primary key")- require.GreaterOrEqual(t, indexTypes["UNIQUE"], 1, "Should have at least 1 unique index")- require.GreaterOrEqual(t, indexTypes["REGULAR"], 2, "Should have at least 2 regular indexes")- require.Equal(t, 2, indexTypes["SPATIAL"], "Should have 2 spatial indexes")- require.Equal(t, 1, indexTypes["COLUMNSTORE"], "Should have 1 columnstore index")-- // Validate spatial indexes- if idx, exists := spatialIndexes["idx_location"]; exists {- require.NotNil(t, idx.SpatialConfig)- require.Equal(t, "GEOMETRY_GRID", idx.SpatialConfig.Tessellation.Scheme)- require.Equal(t, int32(16), idx.SpatialConfig.Tessellation.CellsPerObject)-- // All grid levels should be MEDIUM- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- require.Equal(t, "MEDIUM", level.Density)- }- }-- if idx, exists := spatialIndexes["idx_area"]; exists {- require.NotNil(t, idx.SpatialConfig)- require.Equal(t, "GEOGRAPHY_GRID", idx.SpatialConfig.Tessellation.Scheme)- require.Equal(t, int32(32), idx.SpatialConfig.Tessellation.CellsPerObject)-- // Check alternating grid levels- gridMap := make(map[int32]string)- for _, level := range idx.SpatialConfig.Tessellation.GridLevels {- gridMap[level.Level] = level.Density- }- require.Equal(t, "HIGH", gridMap[1])- require.Equal(t, "LOW", gridMap[2])- require.Equal(t, "HIGH", gridMap[3])- require.Equal(t, "LOW", gridMap[4])- }- },- },- {- name: "spatial_indexes_with_computed_columns",- setupSQL: `-CREATE TABLE dbo.spatial_computed (- id INT IDENTITY(1,1) PRIMARY KEY,- shape GEOMETRY NOT NULL,- -- Computed columns based on spatial data- area AS shape.STArea() PERSISTED,- perimeter AS shape.STLength() PERSISTED,- center_x AS shape.STCentroid().STX PERSISTED,- center_y AS shape.STCentroid().STY PERSISTED,- as_text AS shape.STAsText() PERSISTED-);-GO---- Create spatial index on the base column-CREATE SPATIAL INDEX idx_shape ON dbo.spatial_computed(shape)-USING GEOMETRY_GRID-WITH (- BOUNDING_BOX = (-500, -500, 500, 500),- GRIDS = (LEVEL_1 = LOW, LEVEL_2 = MEDIUM, LEVEL_3 = HIGH, LEVEL_4 = MEDIUM),- CELLS_PER_OBJECT = 24,- FILLFACTOR = 75-);-GO---- Create regular indexes on computed columns-CREATE INDEX idx_area ON dbo.spatial_computed(area);-CREATE INDEX idx_center ON dbo.spatial_computed(center_x, center_y);-GO-`,- validate: func(t *testing.T, metadata *storepb.DatabaseSchemaMetadata) {- // Find dbo schema- var dboSchema *storepb.SchemaMetadata- for _, schema := range metadata.Schemas {- if schema.Name == "dbo" {- dboSchema = schema- break- }- }- require.NotNil(t, dboSchema, "dbo schema should exist")-- // Find spatial_computed table- var computedTable *storepb.TableMetadata- for _, table := range dboSchema.Tables {- if table.Name == "spatial_computed" {- computedTable = table- break- }- }- require.NotNil(t, computedTable, "spatial_computed table should exist")-- // Verify computed columns exist- columnNames := make(map[string]*storepb.ColumnMetadata)- for _, col := range computedTable.Columns {- columnNames[col.Name] = col- }-- // Check that computed columns are present- require.Contains(t, columnNames, "area")- require.Contains(t, columnNames, "perimeter")- require.Contains(t, columnNames, "center_x")- require.Contains(t, columnNames, "center_y")- require.Contains(t, columnNames, "as_text")-- // Find spatial index- var spatialIndex *storepb.IndexMetadata- for _, idx := range computedTable.Indexes {- if idx.Type == "SPATIAL" && idx.Name == "idx_shape" {- spatialIndex = idx- break- }- }-- require.NotNil(t, spatialIndex, "idx_shape spatial index should exist")- require.NotNil(t, spatialIndex.SpatialConfig, "Spatial config should exist")-- // Validate spatial index configuration- require.Equal(t, "GEOMETRY_GRID", spatialIndex.SpatialConfig.Tessellation.Scheme)- require.Equal(t, int32(24), spatialIndex.SpatialConfig.Tessellation.CellsPerObject)- require.Equal(t, int32(75), spatialIndex.SpatialConfig.Storage.Fillfactor)-- // Validate bounding box- bbox := spatialIndex.SpatialConfig.Tessellation.BoundingBox- require.Equal(t, float64(-500), bbox.Xmin)- require.Equal(t, float64(-500), bbox.Ymin)- require.Equal(t, float64(500), bbox.Xmax)- require.Equal(t, float64(500), bbox.Ymax)-- // Validate grid levels- gridMap := make(map[int32]string)- for _, level := range spatialIndex.SpatialConfig.Tessellation.GridLevels {- gridMap[level.Level] = level.Density- }- require.Equal(t, "LOW", gridMap[1])- require.Equal(t, "MEDIUM", gridMap[2])- require.Equal(t, "HIGH", gridMap[3])- require.Equal(t, "MEDIUM", gridMap[4])-- // Verify regular indexes on computed columns exist- regularIndexCount := 0- for _, idx := range computedTable.Indexes {- if idx.Type != "SPATIAL" && !idx.Primary && !idx.Unique {- regularIndexCount++- }- }- require.GreaterOrEqual(t, regularIndexCount, 2, "Should have at least 2 regular indexes on computed columns")- },- },- }-- for _, tc := range testCases {- t.Run(tc.name, func(t *testing.T) {- t.Parallel() // Safe to parallelize - shared container, unique databases per test- databaseName := fmt.Sprintf("test_%s_%s", strings.ReplaceAll(tc.name, " ", "_"), strings.ReplaceAll(uuid.New().String(), "-", "_"))-- // Create test database using container's master connection- _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE [%s]", databaseName))- require.NoError(t, err)-- // Connect to the test database- driver, err := createMSSQLDriver(ctx, container.GetHost(), container.GetPort(), databaseName)- require.NoError(t, err)- defer driver.Close(ctx)-- // Execute setup SQL- err = executeSQL(ctx, driver, tc.setupSQL)- require.NoError(t, err)-- // Sync database schema to get full metadata- metadata, err := driver.SyncDBSchema(ctx)- require.NoError(t, err)-- // Validate the metadata- tc.validate(t, metadata)- })- }-}diff --git a/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go b/backend/plugin/schema/pg/get_database_definition_testcontainer_test.goindex bf1772d4b4..35ba3d1b0a 100644--- a/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go+++ b/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go@@ -907,93 +907,6 @@ func compareEnumsDef(t *testing.T, enumsA, enumsB []*storepb.EnumTypeMetadata) {}}-// TestSyncCompositeTypesWithTestcontainer pins the exact metadata sync-// produces for composite types, so a sync regression cannot pass vacuously-// through the A/B round-trip comparison above.-func TestSyncCompositeTypesWithTestcontainer(t *testing.T) {- ctx := context.Background()-- pgContainer := testcontainer.SharedPgContainer(t)-- dbName := fmt.Sprintf("test_sync_composite_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))- _, err := pgContainer.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))- require.NoError(t, err)-- driver, err := createPgDriver(ctx, pgContainer.GetHost(), pgContainer.GetPort(), dbName)- require.NoError(t, err)- defer driver.Close(ctx)-- _, err = driver.GetDB().ExecContext(ctx, `-CREATE TYPE zz_base AS (street text COLLATE "C", city varchar(50));-COMMENT ON TYPE zz_base IS 'base address type';-COMMENT ON COLUMN zz_base.street IS 'street line';-CREATE TYPE aa_nested AS (home zz_base, homes zz_base[], tags text[]);-CREATE TYPE empty_type AS ();-CREATE SCHEMA geo;-CREATE TYPE geo.point2 AS (lat numeric(9,6), lng numeric(9,6));-CREATE TYPE wrapper AS (p geo.point2);-CREATE TABLE plain_table (id int);-CREATE SCHEMA locale;-CREATE COLLATION locale.mycoll (locale = 'C');-CREATE TYPE coll_qualified AS (v text COLLATE locale.mycoll);-`)- require.NoError(t, err)-- metadata, err := driver.SyncDBSchema(ctx)- require.NoError(t, err)-- compositesBySchema := make(map[string]map[string]*storepb.CompositeTypeMetadata)- for _, schemaMeta := range metadata.Schemas {- m := make(map[string]*storepb.CompositeTypeMetadata)- for _, composite := range schemaMeta.CompositeTypes {- m[composite.Name] = composite- }- compositesBySchema[schemaMeta.Name] = m- }-- public := compositesBySchema["public"]- require.Len(t, public, 5, "public schema should have exactly the created composite types")- require.NotContains(t, public, "plain_table", "table row types must not be synced as composite types")-- collQualified := public["coll_qualified"]- require.NotNil(t, collQualified)- require.Len(t, collQualified.Attributes, 1)- require.Equal(t, "locale.mycoll", collQualified.Attributes[0].Collation,- "non-pg_catalog collations must be schema-qualified")-- base := public["zz_base"]- require.NotNil(t, base)- require.Equal(t, "base address type", base.Comment)- require.Len(t, base.Attributes, 2)- require.Equal(t, "street", base.Attributes[0].Name)- require.Equal(t, "text", base.Attributes[0].Type)- require.Equal(t, `"C"`, base.Attributes[0].Collation)- require.Equal(t, "street line", base.Attributes[0].Comment)- require.Equal(t, "city", base.Attributes[1].Name)- require.Equal(t, "character varying(50)", base.Attributes[1].Type)- require.Empty(t, base.Attributes[1].Collation)-- nested := public["aa_nested"]- require.NotNil(t, nested)- require.Len(t, nested.Attributes, 3)- require.Equal(t, "public.zz_base", nested.Attributes[0].Type, "user-defined attribute types must be schema-qualified")- require.Equal(t, "public.zz_base[]", nested.Attributes[1].Type)- require.Equal(t, "text[]", nested.Attributes[2].Type)-- empty := public["empty_type"]- require.NotNil(t, empty, "zero-attribute composite types must be synced")- require.Empty(t, empty.Attributes)-- wrapper := public["wrapper"]- require.NotNil(t, wrapper)- require.Len(t, wrapper.Attributes, 1)- require.Equal(t, "geo.point2", wrapper.Attributes[0].Type, "cross-schema attribute types must be schema-qualified")-- geo := compositesBySchema["geo"]- require.Len(t, geo, 1)- require.NotNil(t, geo["point2"])-}-func compareCompositeTypesDef(t *testing.T, compositesA, compositesB []*storepb.CompositeTypeMetadata) {require.Equal(t, len(compositesA), len(compositesB), "number of composite types should match")