Skip to content

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

test(tidb): replace dml_dry_run container test with a fake driver (#21432)

Danny Xu84b8353d251f
diff --git a/backend/plugin/advisor/tidb/advisor_statement_dml_dry_run_test.go b/backend/plugin/advisor/tidb/advisor_statement_dml_dry_run_test.go
new file mode 100644
index 0000000000..1578263bf5
--- /dev/null
+++ b/backend/plugin/advisor/tidb/advisor_statement_dml_dry_run_test.go
@@ -0,0 +1,147 @@
+package tidb
+
+import (
+ "context"
+ "database/sql"
+ "database/sql/driver"
+ "errors"
+ "io"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+
+ "github.com/bytebase/bytebase/backend/component/sheet"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+ "github.com/bytebase/bytebase/backend/plugin/advisor"
+ "github.com/bytebase/bytebase/backend/plugin/advisor/code"
+)
+
+// dryRunFake stands in for TiDB. It records every statement the advisor sends,
+// prefixed with "tx: " or "autocommit: ", and rejects any statement containing
+// failOn.
+var dryRunFake struct {
+ queries []string
+ failOn string
+}
+
+func init() {
+ sql.Register("test_tidb_dml_dry_run", dryRunFakeDriver{})
+}
+
+type dryRunFakeDriver struct{}
+
+func (dryRunFakeDriver) Open(string) (driver.Conn, error) { return &dryRunFakeConn{}, nil }
+
+type dryRunFakeConn struct{ inTx bool }
+
+func (*dryRunFakeConn) Prepare(string) (driver.Stmt, error) { return nil, driver.ErrSkip }
+func (*dryRunFakeConn) Close() error { return nil }
+func (c *dryRunFakeConn) Begin() (driver.Tx, error) { c.inTx = true; return c, nil }
+func (c *dryRunFakeConn) Commit() error { c.inTx = false; return nil }
+func (c *dryRunFakeConn) Rollback() error { c.inTx = false; return nil }
+
+func (c *dryRunFakeConn) QueryContext(_ context.Context, query string, _ []driver.NamedValue) (driver.Rows, error) {
+ prefix := "autocommit: "
+ if c.inTx {
+ prefix = "tx: "
+ }
+ dryRunFake.queries = append(dryRunFake.queries, prefix+query)
+ if dryRunFake.failOn != "" && strings.Contains(query, dryRunFake.failOn) {
+ return nil, errors.New("rejected: " + query)
+ }
+ return dryRunFakeRows{}, nil
+}
+
+type dryRunFakeRows struct{}
+
+func (dryRunFakeRows) Columns() []string { return []string{"id"} }
+func (dryRunFakeRows) Close() error { return nil }
+func (dryRunFakeRows) Next([]driver.Value) error { return io.EOF }
+
+func TestStatementDMLDryRun(t *testing.T) {
+ db, err := sql.Open("test_tidb_dml_dry_run", "")
+ require.NoError(t, err)
+ defer db.Close()
+
+ sm := sheet.NewManager()
+ rules := []*storepb.SQLReviewRule{{Type: storepb.SQLReviewRule_STATEMENT_DML_DRY_RUN, Level: storepb.SQLReviewRule_WARNING, Engine: storepb.Engine_TIDB}}
+
+ tests := []struct {
+ name string
+ statement string
+ failOn string
+ wantQueries []string
+ wantAdvice int
+ }{
+ {
+ name: "plain dml is explained in a transaction",
+ statement: "DELETE FROM t WHERE id = 1;",
+ wantQueries: []string{"tx: EXPLAIN DELETE FROM t WHERE id = 1"},
+ },
+ {
+ name: "explain failure is reported",
+ statement: "DELETE FROM t WHERE missing_col = 1",
+ failOn: "missing_col",
+ wantQueries: []string{"tx: EXPLAIN DELETE FROM t WHERE missing_col = 1"},
+ wantAdvice: 1,
+ },
+ {
+ name: "batch dry runs in autocommit then explains the inner dml",
+ statement: "BATCH ON id LIMIT 2 DELETE FROM t WHERE id > 0",
+ wantQueries: []string{
+ "autocommit: BATCH ON id LIMIT 2 DRY RUN DELETE FROM t WHERE id > 0",
+ "tx: EXPLAIN DELETE FROM t WHERE id > 0",
+ },
+ },
+ {
+ name: "batch update",
+ statement: "BATCH ON id LIMIT 2 UPDATE t SET name = 'y' WHERE id > 0",
+ wantQueries: []string{
+ "autocommit: BATCH ON id LIMIT 2 DRY RUN UPDATE t SET name = 'y' WHERE id > 0",
+ "tx: EXPLAIN UPDATE t SET name = 'y' WHERE id > 0",
+ },
+ },
+ {
+ name: "batch insert select",
+ statement: "BATCH ON id LIMIT 2 INSERT INTO t2 SELECT id, name FROM t",
+ wantQueries: []string{
+ "autocommit: BATCH ON id LIMIT 2 DRY RUN INSERT INTO t2 SELECT id, name FROM t",
+ "tx: EXPLAIN INSERT INTO t2 SELECT id, name FROM t",
+ },
+ },
+ {
+ name: "batch without the optional ON clause",
+ statement: "BATCH LIMIT 2 DELETE FROM t WHERE id > 0",
+ wantQueries: []string{
+ "autocommit: BATCH LIMIT 2 DRY RUN DELETE FROM t WHERE id > 0",
+ "tx: EXPLAIN DELETE FROM t WHERE id > 0",
+ },
+ },
+ {
+ name: "batch dry run failure skips the explain",
+ statement: "BATCH ON missing_col LIMIT 2 DELETE FROM t WHERE id > 0",
+ failOn: "missing_col",
+ wantQueries: []string{"autocommit: BATCH ON missing_col LIMIT 2 DRY RUN DELETE FROM t WHERE id > 0"},
+ wantAdvice: 1,
+ },
+ }
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ dryRunFake.queries = nil
+ dryRunFake.failOn = tc.failOn
+
+ advices, err := advisor.SQLReviewCheck(context.Background(), sm, tc.statement, rules, advisor.Context{
+ DBType: storepb.Engine_TIDB,
+ Driver: db,
+ NoAppendBuiltin: true,
+ })
+ require.NoError(t, err)
+ require.Equal(t, tc.wantQueries, dryRunFake.queries)
+ require.Len(t, advices, tc.wantAdvice)
+ for _, a := range advices {
+ require.Equal(t, code.StatementDMLDryRunFailed.Int32(), a.Code)
+ }
+ })
+ }
+}
diff --git a/backend/plugin/advisor/tidb/advisor_statement_dml_dry_run_testcontainer_test.go b/backend/plugin/advisor/tidb/advisor_statement_dml_dry_run_testcontainer_test.go
deleted file mode 100644
index 25b81e736f..0000000000
--- a/backend/plugin/advisor/tidb/advisor_statement_dml_dry_run_testcontainer_test.go
+++ /dev/null
@@ -1,98 +0,0 @@
-package tidb
-
-import (
- "context"
- "database/sql"
- "fmt"
- "testing"
-
- _ "github.com/go-sql-driver/mysql"
- "github.com/stretchr/testify/require"
-
- "github.com/bytebase/bytebase/backend/common/testcontainer"
- "github.com/bytebase/bytebase/backend/component/sheet"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
-)
-
-// TestStatementDMLDryRunTiDBContainer exercises the dml_dry_run advisor against
-// a real TiDB instance. The fixture-based TestTiDBRules path runs with a nil
-// Driver, so it never reaches the EXPLAIN / BATCH DRY RUN logic — this is the
-// only test that validates the live behavior after the omni migration (B1.2),
-// in particular that the BATCH DRY RUN and inner-DML SQL derived by text-slicing
-// (omni has no statement-level deparse) execute correctly against TiDB.
-func TestStatementDMLDryRunTiDBContainer(t *testing.T) {
- ctx := context.Background()
- container := testcontainer.GetTestTiDBContainer(ctx, t)
- defer container.Close(ctx)
-
- const database = "dml_dry_run_test"
- if _, err := container.GetDB().ExecContext(ctx, "CREATE DATABASE IF NOT EXISTS "+database); err != nil {
- t.Fatalf("create database: %v", err)
- }
-
- dsn := fmt.Sprintf("root@tcp(%s:%s)/%s?multiStatements=true&tls=false", container.GetHost(), container.GetPort(), database)
- db, err := sql.Open("mysql", dsn)
- require.NoError(t, err)
- defer db.Close()
- for _, ddl := range []string{
- "CREATE TABLE t (id INT PRIMARY KEY, name VARCHAR(50))",
- "CREATE TABLE t2 (id INT PRIMARY KEY, name VARCHAR(50))",
- "INSERT INTO t VALUES (1, 'a'), (2, 'b'), (3, 'c')",
- } {
- _, err := db.ExecContext(ctx, ddl)
- require.NoError(t, err, ddl)
- }
-
- sm := sheet.NewManager()
- rule := &storepb.SQLReviewRule{Type: storepb.SQLReviewRule_STATEMENT_DML_DRY_RUN, Level: storepb.SQLReviewRule_WARNING, Engine: storepb.Engine_TIDB}
-
- testCases := []struct {
- name string
- statement string
- wantAdvice bool
- }{
- {"plain delete valid", "DELETE FROM t WHERE id = 1", false},
- {"plain update valid", "UPDATE t SET name = 'x' WHERE id = 1", false},
- {"plain insert valid", "INSERT INTO t VALUES (4, 'd')", false},
- {"plain unknown column", "DELETE FROM t WHERE missing_col = 1", true},
- {"plain unknown table", "DELETE FROM missing_table WHERE id = 1", true},
- {"batch delete valid", "BATCH ON id LIMIT 2 DELETE FROM t WHERE id > 0", false},
- {"batch update valid", "BATCH ON id LIMIT 2 UPDATE t SET name = 'y' WHERE id > 0", false},
- {"batch insert-select valid", "BATCH ON id LIMIT 2 INSERT INTO t2 SELECT id, name FROM t", false},
- {"batch no-on valid", "BATCH LIMIT 2 DELETE FROM t WHERE id > 0", false},
- {"batch inner unknown column", "BATCH ON id LIMIT 2 DELETE FROM t WHERE missing_col > 0", true},
- {"batch unknown shard column", "BATCH ON missing_col LIMIT 2 DELETE FROM t WHERE id > 0", true},
- // TiDB rejects non-transactional REPLACE ... VALUES (only INSERT ... SELECT
- // is supported). The advisor must surface that as a dry-run failure; this
- // also exercises the REPLACE (*InsertStmt{IsReplace}) text-slice path.
- {"batch replace-values rejected by tidb", "BATCH ON id LIMIT 2 REPLACE INTO t VALUES (1, 'z')", true},
- }
- for _, tc := range testCases {
- t.Run(tc.name, func(t *testing.T) {
- checkCtx := advisor.Context{
- DBType: storepb.Engine_TIDB,
- Driver: db,
- CurrentDatabase: database,
- NoAppendBuiltin: true,
- }
- advices, err := advisor.SQLReviewCheck(ctx, sm, tc.statement, []*storepb.SQLReviewRule{rule}, checkCtx)
- require.NoError(t, err)
- if tc.wantAdvice {
- require.NotEmpty(t, advices, "expected dry-run advice for %q", tc.statement)
- for _, a := range advices {
- require.Equal(t, code.StatementDMLDryRunFailed.Int32(), a.Code)
- }
- } else {
- require.Empty(t, advices, "expected no advice for %q", tc.statement)
- }
- })
- }
-
- // A dry run must not mutate data: all 3 seed rows remain (no test case INSERTs
- // persist, since EXPLAIN / BATCH DRY RUN never execute the DML).
- var count int
- require.NoError(t, db.QueryRowContext(ctx, "SELECT COUNT(*) FROM t").Scan(&count))
- require.Equal(t, 3, count, "dry run must not mutate data")
-}