Skip to content

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

chore: remove unnecessary cleanup code from testcontainer tests (#18894)

Danny Xu0cf6eb84782a
diff --git a/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go b/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go
index 76cb9e2d87..0f99f8127f 100644
--- a/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go
+++ b/backend/plugin/schema/mssql/generate_migration_testcontainer_test.go
@@ -2377,7 +2377,6 @@ GO
}
for _, testCase := range testCases {
- testCase := testCase // Capture range variable
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
// Step 1: Execute 5-step workflow
@@ -2426,23 +2425,6 @@ func executeFiveStepWorkflow(ctx context.Context, host string, port int, testNam
if _, err := driver.Execute(ctx, fmt.Sprintf("CREATE DATABASE [%s]", testDB), db.ExecuteOptions{CreateDatabase: true}); err != nil {
return errors.Wrap(err, "failed to create test database")
}
- defer func() {
- // Clean up test database - reconnect to master first
- driver.Close(ctx)
- config.DataSource.Database = "master"
- config.ConnectionContext.DatabaseName = "master"
- cleanupDriver, err := driverInstance.Open(ctx, storepb.Engine_MSSQL, config)
- if err == nil {
- // Set the database to single user mode to close any open connections
- _, _ = cleanupDriver.Execute(ctx, fmt.Sprintf("ALTER DATABASE [%s] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", testDB), db.ExecuteOptions{CreateDatabase: true})
- // Drop the database using CreateDatabase option to avoid transaction issues
- if _, err := cleanupDriver.Execute(ctx, fmt.Sprintf("DROP DATABASE [%s]", testDB), db.ExecuteOptions{CreateDatabase: true}); err != nil {
- // Log but don't fail if cleanup fails
- fmt.Printf("Warning: failed to clean up test database %s: %v\n", testDB, err)
- }
- cleanupDriver.Close(ctx)
- }
- }()
// Reconnect to test database
driver.Close(ctx)
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 4f361a72c2..728e8c99b8 100644
--- a/backend/plugin/schema/mssql/get_database_definition_testcontainer_test.go
+++ b/backend/plugin/schema/mssql/get_database_definition_testcontainer_test.go
@@ -403,7 +403,6 @@ GO
}
for _, tc := range testCases {
- tc := tc // Capture range variable
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
@@ -432,18 +431,6 @@ GO
// Create test database
_, err = driver.Execute(ctx, fmt.Sprintf("CREATE DATABASE [%s]", databaseName), db.ExecuteOptions{CreateDatabase: true})
require.NoError(t, err)
- defer func() {
- // Clean up test database
- driver.Close(ctx)
- config.DataSource.Database = "master"
- config.ConnectionContext.DatabaseName = "master"
- cleanupDriver, err := driverInstance.Open(ctx, storepb.Engine_MSSQL, config)
- if err == nil {
- _, _ = cleanupDriver.Execute(ctx, fmt.Sprintf("ALTER DATABASE [%s] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", databaseName), db.ExecuteOptions{CreateDatabase: true})
- _, _ = cleanupDriver.Execute(ctx, fmt.Sprintf("DROP DATABASE [%s]", databaseName), db.ExecuteOptions{CreateDatabase: true})
- cleanupDriver.Close(ctx)
- }
- }()
// Reconnect to test database
driver.Close(ctx)
@@ -480,18 +467,6 @@ GO
_, err = masterDriver.Execute(ctx, fmt.Sprintf("CREATE DATABASE [%s]", newDatabaseName), db.ExecuteOptions{CreateDatabase: true})
require.NoError(t, err)
- defer func() {
- // Clean up new database
- masterDriver.Close(ctx)
- config.DataSource.Database = "master"
- config.ConnectionContext.DatabaseName = "master"
- cleanupDriver2, err := driverInstance.Open(ctx, storepb.Engine_MSSQL, config)
- if err == nil {
- _, _ = cleanupDriver2.Execute(ctx, fmt.Sprintf("ALTER DATABASE [%s] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", newDatabaseName), db.ExecuteOptions{CreateDatabase: true})
- _, _ = cleanupDriver2.Execute(ctx, fmt.Sprintf("DROP DATABASE [%s]", newDatabaseName), db.ExecuteOptions{CreateDatabase: true})
- cleanupDriver2.Close(ctx)
- }
- }()
// Connect to the new database
masterDriver.Close(ctx)
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 4458302b6f..4ca496a225 100644
--- a/backend/plugin/schema/mssql/get_database_metadata_testcontainer_test.go
+++ b/backend/plugin/schema/mssql/get_database_metadata_testcontainer_test.go
@@ -652,7 +652,6 @@ GO
}
for _, tc := range testCases {
- tc := tc // Capture range variable
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, " ", "_"))
@@ -682,20 +681,6 @@ GO
_, err = driver.Execute(ctx, fmt.Sprintf("CREATE DATABASE [%s]", databaseName), db.ExecuteOptions{CreateDatabase: true})
require.NoError(t, err)
- // Clean up database after test
- defer func() {
- // Reconnect to master to drop the test database
- driver.Close(ctx)
- config.DataSource.Database = "master"
- config.ConnectionContext.DatabaseName = "master"
- cleanupDriver, err := driverInstance.Open(ctx, storepb.Engine_MSSQL, config)
- if err == nil {
- _, _ = cleanupDriver.Execute(ctx, fmt.Sprintf("ALTER DATABASE [%s] SET SINGLE_USER WITH ROLLBACK IMMEDIATE", databaseName), db.ExecuteOptions{CreateDatabase: true})
- _, _ = cleanupDriver.Execute(ctx, fmt.Sprintf("DROP DATABASE [%s]", databaseName), db.ExecuteOptions{CreateDatabase: true})
- cleanupDriver.Close(ctx)
- }
- }()
-
// Connect to the test database
driver.Close(ctx)
config.DataSource.Database = databaseName
diff --git a/backend/plugin/schema/mysql/generate_migration_testcontainer_test.go b/backend/plugin/schema/mysql/generate_migration_testcontainer_test.go
index 983e006bb2..adebcf1a16 100644
--- a/backend/plugin/schema/mysql/generate_migration_testcontainer_test.go
+++ b/backend/plugin/schema/mysql/generate_migration_testcontainer_test.go
@@ -12,6 +12,7 @@ import (
// Import MySQL driver
_ "github.com/go-sql-driver/mysql"
"github.com/google/go-cmp/cmp"
+ "github.com/google/uuid"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/testing/protocmp"
@@ -1644,12 +1645,13 @@ CREATE TABLE some_table (
db := container.GetDB()
// Create a test database
- testDBName := fmt.Sprintf("test_%s", strings.ReplaceAll(tc.name, " ", "_"))
+ testDBName := fmt.Sprintf("test_%s", strings.ReplaceAll(uuid.New().String(), "-", "_"))
_, err = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", testDBName))
require.NoError(t, err)
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE `%s`", testDBName))
require.NoError(t, err)
defer func() {
+ _, _ = db.Exec("USE mysql")
_, _ = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", testDBName))
}()
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 bc9adcc11a..097fefebc7 100644
--- a/backend/plugin/schema/mysql/get_database_definition_testcontainer_test.go
+++ b/backend/plugin/schema/mysql/get_database_definition_testcontainer_test.go
@@ -324,9 +324,6 @@ CREATE TABLE translations (
newDBName := fmt.Sprintf("%s_recreated", tc.databaseName)
metadataB, err := createDatabaseAndSync(ctx, host, port, "root", "root-password", tc.databaseName, newDBName, definitionX)
require.NoError(t, err)
- defer func() {
- dropDatabase(ctx, host, port, "root", "root-password", tc.databaseName, newDBName)
- }()
// Step 4: Compare the database metadata A and B, should be the same
normalizeMetadata(metadataA)
@@ -467,9 +464,6 @@ CREATE TABLE project_member (
newDBName := fmt.Sprintf("%s_recreated", databaseName)
metadataB, err := createDatabaseAndSync(ctx, host, port, "root", "root-password", databaseName, newDBName, definitionX)
require.NoError(t, err)
- defer func() {
- dropDatabase(ctx, host, port, "root", "root-password", databaseName, newDBName)
- }()
// Compare
normalizeMetadata(metadataA)
@@ -564,27 +558,3 @@ func createDatabaseAndSync(ctx context.Context, host, port, username, password,
// Now connect to the new database and apply DDL
return initializeAndSyncDatabase(ctx, host, port, username, password, targetDB, ddl)
}
-
-// dropDatabase drops a database
-func dropDatabase(ctx context.Context, host, port, username, password, sourceDB, targetDB string) {
- driver := &mysqldb.Driver{}
- config := db.ConnectionConfig{
- DataSource: &storepb.DataSource{
- Type: storepb.DataSourceType_ADMIN,
- Username: username,
- Host: host,
- Port: port,
- Database: sourceDB,
- },
- Password: password,
- ConnectionContext: db.ConnectionContext{
- EngineVersion: "8.0",
- DatabaseName: sourceDB,
- },
- }
-
- if openedDriver, err := driver.Open(ctx, storepb.Engine_MYSQL, config); err == nil {
- _, _ = openedDriver.Execute(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", targetDB), db.ExecuteOptions{})
- openedDriver.Close(ctx)
- }
-}
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 7181546ed8..354f402d06 100644
--- a/backend/plugin/schema/mysql/get_database_metadata_testcontainer_test.go
+++ b/backend/plugin/schema/mysql/get_database_metadata_testcontainer_test.go
@@ -540,7 +540,7 @@ CREATE TABLE json_features (
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
- // Clean up any existing objects from previous tests
+ // Create test database (drop first to avoid conflicts between test cases)
_, err := container.GetDB().ExecContext(ctx, "DROP DATABASE IF EXISTS test_db")
require.NoError(t, err)
_, err = container.GetDB().ExecContext(ctx, "CREATE DATABASE test_db")
diff --git a/backend/plugin/schema/pg/generate_migration_testcontainer_test.go b/backend/plugin/schema/pg/generate_migration_testcontainer_test.go
index c35988df3b..9aae472e52 100644
--- a/backend/plugin/schema/pg/generate_migration_testcontainer_test.go
+++ b/backend/plugin/schema/pg/generate_migration_testcontainer_test.go
@@ -2150,9 +2150,6 @@ $$ LANGUAGE plpgsql;
dbName := fmt.Sprintf("test_%s", tc.name)
_, err := db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
require.NoError(t, err)
- defer func() {
- _, _ = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", dbName))
- }()
// Connect to the test database
testConnConfig := *connConfig
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 16314a1ddf..099ea2d2ff 100644
--- a/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go
+++ b/backend/plugin/schema/pg/get_database_definition_testcontainer_test.go
@@ -411,9 +411,6 @@ CREATE TABLE cities (
dbNameA := fmt.Sprintf("test_a_%s", tc.name)
_, err := db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbNameA))
require.NoError(t, err)
- defer func() {
- _, _ = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", dbNameA))
- }()
// Connect to database A
testDBA, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=postgres password=root-password database=%s sslmode=disable",
@@ -438,9 +435,6 @@ CREATE TABLE cities (
dbNameB := fmt.Sprintf("test_b_%s", tc.name)
_, err = db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbNameB))
require.NoError(t, err)
- defer func() {
- _, _ = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", dbNameB))
- }()
// Connect to database B
testDBB, err := sql.Open("pgx", fmt.Sprintf("host=%s port=%s user=postgres password=root-password database=%s sslmode=disable",
diff --git a/backend/plugin/schema/pg/get_database_metadata_testcontainer_test.go b/backend/plugin/schema/pg/get_database_metadata_testcontainer_test.go
index 66731e5572..6bdf60a5bb 100644
--- a/backend/plugin/schema/pg/get_database_metadata_testcontainer_test.go
+++ b/backend/plugin/schema/pg/get_database_metadata_testcontainer_test.go
@@ -874,9 +874,6 @@ INSERT INTO array_types_test (
dbName := fmt.Sprintf("test_%s", tc.name)
_, err := db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
require.NoError(t, err)
- defer func() {
- _, _ = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", dbName))
- }()
// Connect to the test database
testConnConfig := *connConfig
diff --git a/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go b/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go
index 50699a6a69..0dd64153f8 100644
--- a/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go
+++ b/backend/plugin/schema/pg/sdl_validation_testcontainer_test.go
@@ -918,9 +918,6 @@ CREATE TABLE employees (
dbName := fmt.Sprintf("sdl_test_%s", tc.name)
_, err := db.Exec(fmt.Sprintf("CREATE DATABASE %s", dbName))
require.NoError(t, err)
- defer func() {
- _, _ = db.Exec(fmt.Sprintf("DROP DATABASE IF EXISTS %s", dbName))
- }()
// Connect to the test database
testConnConfig := *connConfig
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 ce8b39a9d7..aa2014a7ec 100644
--- a/backend/plugin/schema/tidb/get_database_definition_testcontainer_test.go
+++ b/backend/plugin/schema/tidb/get_database_definition_testcontainer_test.go
@@ -195,9 +195,6 @@ CREATE TABLE nonclustered_pk (
// Create database
_, err := container.GetDB().ExecContext(ctx, fmt.Sprintf("CREATE DATABASE %s", tc.databaseName))
require.NoError(t, err)
- defer func() {
- _, _ = container.GetDB().ExecContext(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS %s", tc.databaseName))
- }()
// Execute original DDL
_, err = container.GetDB().ExecContext(ctx, fmt.Sprintf("USE %s", tc.databaseName))
@@ -218,9 +215,6 @@ CREATE TABLE nonclustered_pk (
newDatabaseName := fmt.Sprintf("%s_new", tc.databaseName)
_, err = container.GetDB().ExecContext(ctx, fmt.Sprintf("CREATE DATABASE %s", newDatabaseName))
require.NoError(t, err)
- defer func() {
- _, _ = container.GetDB().ExecContext(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS %s", newDatabaseName))
- }()
// Apply the generated definition to the new database
_, err = container.GetDB().ExecContext(ctx, fmt.Sprintf("USE %s", newDatabaseName))
@@ -300,9 +294,6 @@ CREATE TABLE project_members (
// Create database
_, err := container.GetDB().ExecContext(ctx, fmt.Sprintf("CREATE DATABASE %s", databaseName))
require.NoError(t, err)
- defer func() {
- _, _ = container.GetDB().ExecContext(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS %s", databaseName))
- }()
// Execute original DDL
_, err = container.GetDB().ExecContext(ctx, fmt.Sprintf("USE %s", databaseName))
@@ -323,9 +314,6 @@ CREATE TABLE project_members (
newDatabaseName := fmt.Sprintf("%s_new", databaseName)
_, err = container.GetDB().ExecContext(ctx, fmt.Sprintf("CREATE DATABASE %s", newDatabaseName))
require.NoError(t, err)
- defer func() {
- _, _ = container.GetDB().ExecContext(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS %s", newDatabaseName))
- }()
// Apply the generated definition to the new database
_, err = container.GetDB().ExecContext(ctx, fmt.Sprintf("USE %s", newDatabaseName))
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 441deb644a..75277da965 100644
--- a/backend/plugin/schema/tidb/get_database_metadata_testcontainer_test.go
+++ b/backend/plugin/schema/tidb/get_database_metadata_testcontainer_test.go
@@ -227,7 +227,7 @@ CREATE TABLE non_clustered_test (
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
- // Clean up any existing objects from previous tests
+ // Create test database (drop first to avoid conflicts between test cases)
_, err := container.GetDB().ExecContext(ctx, "DROP DATABASE IF EXISTS test_db")
require.NoError(t, err)
_, err = container.GetDB().ExecContext(ctx, "CREATE DATABASE test_db")