Skip to content

Commit 918753d

Browse files
Myst9duwenxin99
andauthored
chore(tests): Add database cleanup functions (googleapis#1528)
## Description --- This change adds helper functions to cleanup the test databases (PostgreSQL, MySQL, and MSSQL) by dropping any orphan tables from previous test runs. These functions can be called at the beginning of each test run. ## PR Checklist --- > Thank you for opening a Pull Request! Before submitting your PR, there are a > few things you can do to make sure it goes smoothly: - [x] Make sure you reviewed [CONTRIBUTING.md](https://github.com/googleapis/genai-toolbox/blob/main/CONTRIBUTING.md) - [x] Make sure to open an issue as a [bug/issue](https://github.com/googleapis/genai-toolbox/issues/new/choose) before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) - [x] Make sure to add `!` if this involve a breaking change 🛠️ Fixes googleapis#1304 --------- Co-authored-by: Wenxin Du <117315983+duwenxin99@users.noreply.github.com>
1 parent b43c945 commit 918753d

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

tests/common.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"database/sql"
2323
"fmt"
24+
"strings"
2425
"testing"
2526

2627
"github.com/goccy/go-yaml"
@@ -804,3 +805,98 @@ func TestCloudSQLMySQL_IPTypeParsingFromYAML(t *testing.T) {
804805
})
805806
}
806807
}
808+
809+
// Finds and drops all tables in a postgres database.
810+
func CleanupPostgresTables(t *testing.T, ctx context.Context, pool *pgxpool.Pool) {
811+
query := `
812+
SELECT table_name FROM information_schema.tables
813+
WHERE table_schema = 'public' AND table_type = 'BASE TABLE';`
814+
815+
rows, err := pool.Query(ctx, query)
816+
if err != nil {
817+
t.Fatalf("Failed to query for all tables in 'public' schema: %v", err)
818+
}
819+
defer rows.Close()
820+
821+
var tablesToDrop []string
822+
for rows.Next() {
823+
var tableName string
824+
if err := rows.Scan(&tableName); err != nil {
825+
t.Errorf("Failed to scan table name: %v", err)
826+
continue
827+
}
828+
tablesToDrop = append(tablesToDrop, fmt.Sprintf("public.%q", tableName))
829+
}
830+
831+
if len(tablesToDrop) == 0 {
832+
return
833+
}
834+
835+
dropQuery := fmt.Sprintf("DROP TABLE IF EXISTS %s CASCADE;", strings.Join(tablesToDrop, ", "))
836+
837+
if _, err := pool.Exec(ctx, dropQuery); err != nil {
838+
t.Fatalf("Failed to drop all tables in 'public' schema: %v", err)
839+
}
840+
}
841+
842+
// Finds and drops all tables in a mysql database.
843+
func CleanupMySQLTables(t *testing.T, ctx context.Context, pool *sql.DB) {
844+
query := `
845+
SELECT table_name FROM information_schema.tables
846+
WHERE table_schema = DATABASE() AND table_type = 'BASE TABLE';`
847+
848+
rows, err := pool.QueryContext(ctx, query)
849+
if err != nil {
850+
t.Fatalf("Failed to query for all MySQL tables: %v", err)
851+
}
852+
defer rows.Close()
853+
854+
var tablesToDrop []string
855+
for rows.Next() {
856+
var tableName string
857+
if err := rows.Scan(&tableName); err != nil {
858+
t.Errorf("Failed to scan MySQL table name: %v", err)
859+
continue
860+
}
861+
tablesToDrop = append(tablesToDrop, fmt.Sprintf("`%s`", tableName))
862+
}
863+
864+
if len(tablesToDrop) == 0 {
865+
return
866+
}
867+
868+
// Disable foreign key checks, drop all tables and re-enable
869+
if _, err := pool.ExecContext(ctx, "SET FOREIGN_KEY_CHECKS = 0;"); err != nil {
870+
t.Fatalf("Failed to disable MySQL foreign key checks: %v", err)
871+
}
872+
873+
dropQuery := fmt.Sprintf("DROP TABLE IF EXISTS %s;", strings.Join(tablesToDrop, ", "))
874+
875+
if _, err := pool.ExecContext(ctx, dropQuery); err != nil {
876+
// Try to re-enable checks even if drop fails
877+
if _, err := pool.ExecContext(ctx, "SET FOREIGN_KEY_CHECKS = 1;"); err != nil {
878+
t.Logf("Also failed to re-enable foreign key checks: %v", err)
879+
}
880+
t.Fatalf("Failed to drop all MySQL tables: %v", err)
881+
}
882+
883+
// Re-enable foreign key checks
884+
if _, err := pool.ExecContext(ctx, "SET FOREIGN_KEY_CHECKS = 1;"); err != nil {
885+
t.Fatalf("Failed to re-enable MySQL foreign key checks: %v", err)
886+
}
887+
}
888+
889+
// Finds and drops all tables in an mssql database.
890+
func CleanupMSSQLTables(t *testing.T, ctx context.Context, pool *sql.DB) {
891+
disableConstraintsCmd := "EXEC sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'"
892+
if _, err := pool.ExecContext(ctx, disableConstraintsCmd); err != nil {
893+
t.Fatalf("Failed to disable MSSQL constraints: %v", err)
894+
}
895+
896+
// drop 'U' (User Tables)
897+
dropTablesCmd := "EXEC sp_MSforeachtable 'DROP TABLE ?', @whereand = 'AND O.Type = ''U'''"
898+
if _, err := pool.ExecContext(ctx, dropTablesCmd); err != nil {
899+
t.Fatalf("Failed to drop all MSSQL tables: %v", err)
900+
}
901+
902+
}

0 commit comments

Comments
 (0)