Skip to content

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

test(schema): share one container per engine package (#21351)

Danny Xu9b96ca061668
diff --git a/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go b/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go
index 1cf5fe5703..b4ce4fb165 100644
--- a/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go
+++ b/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go
@@ -15,7 +15,6 @@ import (
"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/schema"
"github.com/bytebase/bytebase/backend/store/model"
@@ -33,8 +32,7 @@ func TestGenerateMigrationWithTestcontainer(t *testing.T) {
ctx := context.Background()
// Start shared MSSQL container for all subtests
- container := testcontainer.GetTestMSSQLContainer(ctx, t)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedMSSQLContainer(t)
// Test cases with various schema changes
testCases := []struct {
diff --git a/backend/plugin/schema/mssql/get_database_definition_testcontainer_test.go b/backend/plugin/schema/mssql/get_database_definition_testcontainer_test.go
index ae6bf6ed0e..4e96ffb547 100644
--- a/backend/plugin/schema/mssql/get_database_definition_testcontainer_test.go
+++ b/backend/plugin/schema/mssql/get_database_definition_testcontainer_test.go
@@ -6,10 +6,10 @@ import (
"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"
"github.com/bytebase/bytebase/backend/plugin/schema"
)
@@ -17,8 +17,7 @@ import (
//nolint:tparallel
func TestGetDatabaseDefinitionWithTestcontainer(t *testing.T) {
ctx := context.Background()
- container := testcontainer.GetTestMSSQLContainer(ctx, t)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedMSSQLContainer(t)
testCases := []struct {
name string
@@ -397,7 +396,7 @@ GO
t.Run(tc.name, func(t *testing.T) {
t.Parallel() // Safe to parallelize - shared container, unique databases per test
// Use test name for database name - each test case has a unique name
- databaseName := fmt.Sprintf("test_%s", strings.ReplaceAll(tc.name, " ", "_"))
+ 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))
@@ -421,7 +420,7 @@ GO
require.NotEmpty(t, definitionX)
// Step 3: Create a new database and run the definition X
- newDatabaseName := fmt.Sprintf("test_copy_%s", strings.ReplaceAll(tc.name, " ", "_"))
+ newDatabaseName := fmt.Sprintf("copy_%s", databaseName)
// Create new database using container's master connection
_, err = container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE [%s]", newDatabaseName))
diff --git a/backend/plugin/schema/mssql/get_database_metadata_testcontainer_test.go b/backend/plugin/schema/mssql/get_database_metadata_testcontainer_test.go
index 3b40347d0b..29b780d403 100644
--- a/backend/plugin/schema/mssql/get_database_metadata_testcontainer_test.go
+++ b/backend/plugin/schema/mssql/get_database_metadata_testcontainer_test.go
@@ -6,18 +6,17 @@ import (
"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.GetTestMSSQLContainer(ctx, t)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedMSSQLContainer(t)
testCases := []struct {
name string
@@ -646,7 +645,7 @@ GO
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", strings.ReplaceAll(tc.name, " ", "_"))
+ 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))
diff --git a/backend/plugin/schema/mssql/main_test.go b/backend/plugin/schema/mssql/main_test.go
new file mode 100644
index 0000000000..7b4de34681
--- /dev/null
+++ b/backend/plugin/schema/mssql/main_test.go
@@ -0,0 +1,50 @@
+package mssql
+
+import (
+ "context"
+ "sync"
+ "testing"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+)
+
+// The package shares one SQL Server container, started on first use.
+//
+// Sharing it is worth a container start, and this package took three of them.
+// Starting it lazily is what keeps that saving from turning into a tax on the
+// tests that need no engine at all, which would otherwise pay for a container
+// they never open.
+var (
+ mssqlOnce sync.Once
+ mssqlContainer *testcontainer.Container
+ mssqlErr error
+)
+
+func TestMain(m *testing.M) {
+ defer func() {
+ if mssqlContainer != nil {
+ mssqlContainer.Close(context.Background())
+ }
+ }()
+ m.Run()
+}
+
+// sharedMSSQLContainer returns the package's SQL Server container, starting it on
+// the first call. Tests keep isolating themselves with a per-test database,
+// which is what makes one container enough.
+func sharedMSSQLContainer(t *testing.T) *testcontainer.Container {
+ t.Helper()
+
+ mssqlOnce.Do(func() {
+ container, err := testcontainer.GetMSSQLContainer(context.Background())
+ if err != nil {
+ mssqlErr = err
+ return
+ }
+ mssqlContainer = container
+ })
+ if mssqlErr != nil {
+ t.Fatalf("failed to start the shared SQL Server container: %v", mssqlErr)
+ }
+ return mssqlContainer
+}
diff --git a/backend/plugin/schema/mysql/generate_migration_testcontainer_test.go b/backend/plugin/schema/mysql/generate_migration_testcontainer_test.go
index 6d92876995..df8e9dcceb 100644
--- a/backend/plugin/schema/mysql/generate_migration_testcontainer_test.go
+++ b/backend/plugin/schema/mysql/generate_migration_testcontainer_test.go
@@ -14,7 +14,6 @@ import (
"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"
"github.com/bytebase/bytebase/backend/plugin/schema"
@@ -33,9 +32,7 @@ func TestGenerateMigrationWithTestcontainer(t *testing.T) {
ctx := context.Background()
// Start shared MySQL container for all subtests
- container, err := testcontainer.GetTestMySQLContainer(ctx)
- require.NoError(t, err)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedMySQLContainer(t)
// Test cases with various schema changes
testCases := []struct {
@@ -1695,7 +1692,7 @@ CREATE TABLE some_table (
// Step 1: Initialize the database schema and get schema result A
// Create a unique test database for parallel execution
testDBName := fmt.Sprintf("test_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
- _, err = container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE `%s`", testDBName))
+ _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE `%s`", testDBName))
require.NoError(t, err)
// Create MySQL driver for this test's database
diff --git a/backend/plugin/schema/mysql/get_database_definition_testcontainer_test.go b/backend/plugin/schema/mysql/get_database_definition_testcontainer_test.go
index a068599341..8efb7e1c2a 100644
--- a/backend/plugin/schema/mysql/get_database_definition_testcontainer_test.go
+++ b/backend/plugin/schema/mysql/get_database_definition_testcontainer_test.go
@@ -13,7 +13,6 @@ import (
"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"
"github.com/bytebase/bytebase/backend/plugin/schema"
@@ -28,9 +27,7 @@ func TestGetDatabaseDefinition(t *testing.T) {
ctx := context.Background()
// Start shared MySQL container for all subtests
- container, err := testcontainer.GetTestMySQLContainer(ctx)
- require.NoError(t, err)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedMySQLContainer(t)
type testCase struct {
description string
@@ -428,9 +425,12 @@ func TestGetDatabaseDefinitionWithConnectedDeps(t *testing.T) {
t.Skip("Skipping MySQL testcontainer test in short mode")
}
+ // Unique per run: TestMain keeps one container for the whole package, so a
+ // fixed name collides with itself under go test -count=2.
+ databaseName := fmt.Sprintf("test_complex_deps_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
+
const (
- databaseName = "test_complex_deps"
- originalDDL = `
+ originalDDL = `
CREATE TABLE department (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
@@ -470,12 +470,10 @@ CREATE TABLE project_member (
ctx := context.Background()
// Start MySQL container
- container, err := testcontainer.GetTestMySQLContainer(ctx)
- require.NoError(t, err)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedMySQLContainer(t)
// Create test database
- _, err = container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", databaseName))
+ _, err := container.GetDB().Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", databaseName))
require.NoError(t, err)
// Step 1: Initialize the database schema
diff --git a/backend/plugin/schema/mysql/get_database_metadata_testcontainer_test.go b/backend/plugin/schema/mysql/get_database_metadata_testcontainer_test.go
index 49d375b27a..775bfd0382 100644
--- a/backend/plugin/schema/mysql/get_database_metadata_testcontainer_test.go
+++ b/backend/plugin/schema/mysql/get_database_metadata_testcontainer_test.go
@@ -11,7 +11,6 @@ import (
"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"
)
@@ -24,9 +23,7 @@ func TestGetDatabaseMetadataWithTestcontainer(t *testing.T) {
ctx := context.Background()
// Start shared MySQL container for all subtests
- container, err := testcontainer.GetTestMySQLContainer(ctx)
- require.NoError(t, err)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedMySQLContainer(t)
// Test cases with various MySQL features
testCases := []struct {
diff --git a/backend/plugin/schema/mysql/main_test.go b/backend/plugin/schema/mysql/main_test.go
new file mode 100644
index 0000000000..225e382bb7
--- /dev/null
+++ b/backend/plugin/schema/mysql/main_test.go
@@ -0,0 +1,50 @@
+package mysql
+
+import (
+ "context"
+ "sync"
+ "testing"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+)
+
+// The package shares one MySQL container, started on first use.
+//
+// Sharing it is worth a container start, and this package took three of them.
+// Starting it lazily is what keeps that saving from turning into a tax on the
+// tests that need no engine at all, which would otherwise pay for a container
+// they never open.
+var (
+ mysqlOnce sync.Once
+ mysqlContainer *testcontainer.Container
+ mysqlErr error
+)
+
+func TestMain(m *testing.M) {
+ defer func() {
+ if mysqlContainer != nil {
+ mysqlContainer.Close(context.Background())
+ }
+ }()
+ m.Run()
+}
+
+// sharedMySQLContainer returns the package's MySQL container, starting it on
+// the first call. Tests keep isolating themselves with a per-test database,
+// which is what makes one container enough.
+func sharedMySQLContainer(t *testing.T) *testcontainer.Container {
+ t.Helper()
+
+ mysqlOnce.Do(func() {
+ container, err := testcontainer.GetTestMySQLContainer(context.Background())
+ if err != nil {
+ mysqlErr = err
+ return
+ }
+ mysqlContainer = container
+ })
+ if mysqlErr != nil {
+ t.Fatalf("failed to start the shared MySQL container: %v", mysqlErr)
+ }
+ return mysqlContainer
+}
diff --git a/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go b/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go
index 3f20296c1c..c706663e13 100644
--- a/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go
+++ b/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go
@@ -5,11 +5,10 @@ import (
"fmt"
"strings"
"testing"
- "time"
+ "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/schema"
)
@@ -23,8 +22,7 @@ func TestGetDatabaseDefinitionWithTestcontainer(t *testing.T) {
ctx := context.Background()
// Get PostgreSQL container from testcontainer.go
- pgContainer := testcontainer.GetTestPgContainer(ctx, t)
- t.Cleanup(func() { pgContainer.Close(ctx) })
+ pgContainer := sharedPgContainer(t)
// Test cases with various PostgreSQL features
testCases := []struct {
@@ -433,7 +431,7 @@ CREATE TABLE cities (
t.Parallel()
// Create first database with original DDL using unique name
- dbNameA := fmt.Sprintf("test_a_%s_%d", strings.ReplaceAll(tc.name, "-", "_"), time.Now().UnixNano()%1000000)
+ dbNameA := fmt.Sprintf("test_a_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
_, err := pgContainer.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", dbNameA))
require.NoError(t, err)
@@ -456,7 +454,7 @@ CREATE TABLE cities (
require.NotEmpty(t, generatedDDL, "generated DDL should not be empty")
// Create second database and apply generated DDL using unique name
- dbNameB := fmt.Sprintf("test_b_%s_%d", strings.ReplaceAll(tc.name, "-", "_"), time.Now().UnixNano()%1000000)
+ dbNameB := fmt.Sprintf("test_b_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
_, err = pgContainer.GetDB().Exec(fmt.Sprintf("CREATE DATABASE %s", dbNameB))
require.NoError(t, err)
@@ -914,10 +912,9 @@ func compareEnumsDef(t *testing.T, enumsA, enumsB []*storepb.EnumTypeMetadata) {
func TestSyncCompositeTypesWithTestcontainer(t *testing.T) {
ctx := context.Background()
- pgContainer := testcontainer.GetTestPgContainer(ctx, t)
- t.Cleanup(func() { pgContainer.Close(ctx) })
+ pgContainer := sharedPgContainer(t)
- dbName := fmt.Sprintf("test_sync_composite_%d", time.Now().UnixNano()%1000000)
+ 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)
diff --git a/backend/plugin/schema/pg/main_test.go b/backend/plugin/schema/pg/main_test.go
new file mode 100644
index 0000000000..96f755297b
--- /dev/null
+++ b/backend/plugin/schema/pg/main_test.go
@@ -0,0 +1,50 @@
+package pg
+
+import (
+ "context"
+ "sync"
+ "testing"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+)
+
+// The package shares one PostgreSQL container, started on first use.
+//
+// Sharing it is worth a container start, and this package took two of them.
+// Starting it lazily is what keeps that saving from turning into a tax on the
+// tests that need no engine at all, which would otherwise pay for a container
+// they never open.
+var (
+ pgOnce sync.Once
+ pgContainer *testcontainer.Container
+ pgErr error
+)
+
+func TestMain(m *testing.M) {
+ defer func() {
+ if pgContainer != nil {
+ pgContainer.Close(context.Background())
+ }
+ }()
+ m.Run()
+}
+
+// sharedPgContainer returns the package's PostgreSQL container, starting it on
+// the first call. Tests keep isolating themselves with a per-test database,
+// which is what makes one container enough.
+func sharedPgContainer(t *testing.T) *testcontainer.Container {
+ t.Helper()
+
+ pgOnce.Do(func() {
+ container, err := testcontainer.GetPgContainer(context.Background())
+ if err != nil {
+ pgErr = err
+ return
+ }
+ pgContainer = container
+ })
+ if pgErr != nil {
+ t.Fatalf("failed to start the shared PostgreSQL container: %v", pgErr)
+ }
+ return pgContainer
+}
diff --git a/backend/plugin/schema/tidb/generate_migration_testcontainer_test.go b/backend/plugin/schema/tidb/generate_migration_testcontainer_test.go
index c432e7a404..20bcf697c3 100644
--- a/backend/plugin/schema/tidb/generate_migration_testcontainer_test.go
+++ b/backend/plugin/schema/tidb/generate_migration_testcontainer_test.go
@@ -14,7 +14,6 @@ import (
"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/schema"
"github.com/bytebase/bytebase/backend/store/model"
@@ -30,8 +29,7 @@ func TestGenerateMigrationWithTestcontainer(t *testing.T) {
}
ctx := context.Background()
- container := testcontainer.GetTestTiDBContainer(ctx, t)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedTiDBContainer(t)
// Test cases with various schema changes
testCases := []struct {
diff --git a/backend/plugin/schema/tidb/get_database_definition_testcontainer_test.go b/backend/plugin/schema/tidb/get_database_definition_testcontainer_test.go
index 77f93d1542..f38401c112 100644
--- a/backend/plugin/schema/tidb/get_database_definition_testcontainer_test.go
+++ b/backend/plugin/schema/tidb/get_database_definition_testcontainer_test.go
@@ -13,7 +13,6 @@ import (
"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/schema"
)
@@ -26,8 +25,7 @@ func TestGetDatabaseDefinition(t *testing.T) {
}
ctx := context.Background()
- container := testcontainer.GetTestTiDBContainer(ctx, t)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedTiDBContainer(t)
type testCase struct {
description string
@@ -244,8 +242,7 @@ func TestGetDatabaseDefinitionWithConnectedDeps(t *testing.T) {
}
ctx := context.Background()
- container := testcontainer.GetTestTiDBContainer(ctx, t)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedTiDBContainer(t)
// Create unique test database using UUID
testDB := fmt.Sprintf("test_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
diff --git a/backend/plugin/schema/tidb/get_database_metadata_testcontainer_test.go b/backend/plugin/schema/tidb/get_database_metadata_testcontainer_test.go
index 41f7f73ab4..34bffc033d 100644
--- a/backend/plugin/schema/tidb/get_database_metadata_testcontainer_test.go
+++ b/backend/plugin/schema/tidb/get_database_metadata_testcontainer_test.go
@@ -9,7 +9,6 @@ import (
"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"
)
@@ -23,8 +22,7 @@ func TestGetDatabaseMetadataWithTestcontainer(t *testing.T) {
}
ctx := context.Background()
- container := testcontainer.GetTestTiDBContainer(ctx, t)
- t.Cleanup(func() { container.Close(ctx) })
+ container := sharedTiDBContainer(t)
// Test cases with various TiDB features
testCases := []struct {
diff --git a/backend/plugin/schema/tidb/main_test.go b/backend/plugin/schema/tidb/main_test.go
new file mode 100644
index 0000000000..2609d8a0e9
--- /dev/null
+++ b/backend/plugin/schema/tidb/main_test.go
@@ -0,0 +1,50 @@
+package tidb
+
+import (
+ "context"
+ "sync"
+ "testing"
+
+ "github.com/bytebase/bytebase/backend/common/testcontainer"
+)
+
+// The package shares one TiDB container, started on first use.
+//
+// Sharing it is worth a container start, and this package took four of them.
+// Starting it lazily is what keeps that saving from turning into a tax on the
+// tests that need no engine at all, which would otherwise pay for a container
+// they never open.
+var (
+ tidbOnce sync.Once
+ tidbContainer *testcontainer.Container
+ tidbErr error
+)
+
+func TestMain(m *testing.M) {
+ defer func() {
+ if tidbContainer != nil {
+ tidbContainer.Close(context.Background())
+ }
+ }()
+ m.Run()
+}
+
+// sharedTiDBContainer returns the package's TiDB container, starting it on
+// the first call. Tests keep isolating themselves with a per-test database,
+// which is what makes one container enough.
+func sharedTiDBContainer(t *testing.T) *testcontainer.Container {
+ t.Helper()
+
+ tidbOnce.Do(func() {
+ container, err := testcontainer.GetTiDBContainer(context.Background())
+ if err != nil {
+ tidbErr = err
+ return
+ }
+ tidbContainer = container
+ })
+ if tidbErr != nil {
+ t.Fatalf("failed to start the shared TiDB container: %v", tidbErr)
+ }
+ return tidbContainer
+}