Skip to content

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

refactor: standardize DatabaseState.deleted field handling across engines (#18103)

Danny Xucd060b4b2d99
diff --git a/backend/plugin/advisor/catalog/walk_through_for_mysql.go b/backend/plugin/advisor/catalog/walk_through_for_mysql.go
index 164750caa8..9e13d629df 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_mysql.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql.go
@@ -58,7 +58,7 @@ func (d *DatabaseState) mysqlChangeState(in *mysqlparser.ParseResult) (err *Walk
}
}()
- if d.deleted {
+ if d.IsDeleted() {
return &WalkThroughError{
Type: ErrorTypeDatabaseIsDeleted,
Content: fmt.Sprintf("Database `%s` is deleted", d.name),
diff --git a/backend/plugin/advisor/catalog/walk_through_for_pg.go b/backend/plugin/advisor/catalog/walk_through_for_pg.go
index 75c2e4c6ea..1a4f3fad4d 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_pg.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_pg.go
@@ -65,18 +65,6 @@ func (l *pgCatalogListener) setError(err *WalkThroughError) {
l.err = err
}
-// Helper method to check if database is deleted
-func (l *pgCatalogListener) checkDatabaseNotDeleted() bool {
- if l.databaseState.IsDeleted() {
- l.setError(&WalkThroughError{
- Type: ErrorTypeDatabaseIsDeleted,
- Content: fmt.Sprintf(`Database %q is deleted`, l.databaseState.name),
- })
- return false
- }
- return true
-}
-
// ========================================
// CREATE TABLE handling
// ========================================
@@ -87,10 +75,6 @@ func (l *pgCatalogListener) EnterCreatestmt(ctx *parser.CreatestmtContext) {
return
}
- if !l.checkDatabaseNotDeleted() {
- return
- }
-
l.currentLine = ctx.GetStart().GetLine()
// Extract table name and schema
@@ -406,10 +390,6 @@ func (l *pgCatalogListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {
return
}
- if !l.checkDatabaseNotDeleted() {
- return
- }
-
l.currentLine = ctx.GetStart().GetLine()
// Extract relation (table) name
@@ -546,10 +526,6 @@ func (l *pgCatalogListener) EnterAltertablestmt(ctx *parser.AltertablestmtContex
return
}
- if !l.checkDatabaseNotDeleted() {
- return
- }
-
l.currentLine = ctx.GetStart().GetLine()
// Extract table name
@@ -1027,10 +1003,6 @@ func (l *pgCatalogListener) EnterDropstmt(ctx *parser.DropstmtContext) {
return
}
- if !l.checkDatabaseNotDeleted() {
- return
- }
-
l.currentLine = ctx.GetStart().GetLine()
// Check IF EXISTS
@@ -1084,6 +1056,41 @@ func (l *pgCatalogListener) EnterDropstmt(ctx *parser.DropstmtContext) {
}
}
+// ========================================
+// DROP DATABASE handling
+// ========================================
+
+// EnterDropdbstmt handles DROP DATABASE statements.
+// PostgreSQL does not allow dropping the currently connected database.
+// Walk-through also disallows DROP DATABASE for other databases as it's out of scope.
+func (l *pgCatalogListener) EnterDropdbstmt(ctx *parser.DropdbstmtContext) {
+ if !isTopLevel(ctx.GetParent()) || l.err != nil {
+ return
+ }
+
+ l.currentLine = ctx.GetStart().GetLine()
+
+ // Extract database name
+ if ctx.Name() == nil {
+ return
+ }
+
+ databaseName := pgparser.NormalizePostgreSQLName(ctx.Name())
+
+ // PostgreSQL does not allow dropping the currently open database.
+ // This matches the real PostgreSQL behavior: "ERROR: cannot drop the currently open database"
+ if l.databaseState.isCurrentDatabase(databaseName) {
+ l.setError(&WalkThroughError{
+ Type: ErrorTypeAccessOtherDatabase,
+ Content: fmt.Sprintf("Cannot drop the currently open database %q", databaseName),
+ })
+ return
+ }
+
+ // DROP DATABASE for other databases is out of scope for single-database walk-through
+ l.setError(NewAccessOtherDatabaseError(l.databaseState.name, databaseName))
+}
+
func (l *pgCatalogListener) dropTable(anyName parser.IAny_nameContext, ifExists bool) *WalkThroughError {
parts := pgparser.NormalizePostgreSQLAnyName(anyName)
if len(parts) == 0 {
@@ -1271,10 +1278,6 @@ func (l *pgCatalogListener) EnterRenamestmt(ctx *parser.RenamestmtContext) {
return
}
- if !l.checkDatabaseNotDeleted() {
- return
- }
-
l.currentLine = ctx.GetStart().GetLine()
// Check if this is INDEX rename (ALTER INDEX ... RENAME TO ...)
@@ -1397,10 +1400,6 @@ func (l *pgCatalogListener) EnterViewstmt(ctx *parser.ViewstmtContext) {
return
}
- if !l.checkDatabaseNotDeleted() {
- return
- }
-
l.currentLine = ctx.GetStart().GetLine()
// Extract view name
diff --git a/backend/plugin/advisor/catalog/walk_through_for_tidb.go b/backend/plugin/advisor/catalog/walk_through_for_tidb.go
index 785adbc177..54dfe714aa 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_tidb.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_tidb.go
@@ -41,7 +41,7 @@ func (d *DatabaseState) changeState(in tidbast.StmtNode) (err *WalkThroughError)
err.Line = in.OriginTextPosition()
}
}()
- if d.deleted {
+ if d.IsDeleted() {
return &WalkThroughError{
Type: ErrorTypeDatabaseIsDeleted,
Content: fmt.Sprintf("Database `%s` is deleted", d.name),
@@ -135,7 +135,7 @@ func (d *DatabaseState) dropDatabase(node *tidbast.DropDatabaseStmt) *WalkThroug
return NewAccessOtherDatabaseError(d.name, node.Name.O)
}
- d.deleted = true
+ d.MarkDeleted()
return nil
}