Skip to content

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

refactor: remove IsCaseSensitive from database store lookups (#18559)

Danny Xu185d0e75b750
diff --git a/backend/api/v1/common.go b/backend/api/v1/common.go
index fa1ba53ee0..2fb99298f3 100644
--- a/backend/api/v1/common.go
+++ b/backend/api/v1/common.go
@@ -461,10 +461,9 @@ func getDatabaseMessage(ctx context.Context, s *store.Store, databaseResourceNam
}
find := &store.FindDatabaseMessage{
- InstanceID: &instanceID,
- DatabaseName: &databaseName,
- IsCaseSensitive: store.IsObjectCaseSensitive(instance),
- ShowDeleted: true,
+ InstanceID: &instanceID,
+ DatabaseName: &databaseName,
+ ShowDeleted: true,
}
database, err := s.GetDatabase(ctx, find)
if err != nil {
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index d10a63e529..fb20962487 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -553,9 +553,8 @@ func (s *DatabaseService) GetDatabaseSchema(ctx context.Context, req *connect.Re
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("instance %q not found", instanceID))
}
database, err := s.store.GetDatabase(ctx, &store.FindDatabaseMessage{
- InstanceID: &instanceID,
- DatabaseName: &databaseName,
- IsCaseSensitive: store.IsObjectCaseSensitive(instance),
+ InstanceID: &instanceID,
+ DatabaseName: &databaseName,
})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Errorf("%v", err.Error()))
@@ -606,9 +605,8 @@ func (s *DatabaseService) GetDatabaseSDLSchema(ctx context.Context, req *connect
}
database, err := s.store.GetDatabase(ctx, &store.FindDatabaseMessage{
- InstanceID: &instanceID,
- DatabaseName: &databaseName,
- IsCaseSensitive: store.IsObjectCaseSensitive(instance),
+ InstanceID: &instanceID,
+ DatabaseName: &databaseName,
})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Errorf("%v", err.Error()))
diff --git a/backend/api/v1/database_service_changelog.go b/backend/api/v1/database_service_changelog.go
index 56262703b4..a52383b714 100644
--- a/backend/api/v1/database_service_changelog.go
+++ b/backend/api/v1/database_service_changelog.go
@@ -96,9 +96,8 @@ func (s *DatabaseService) GetChangelog(ctx context.Context, req *connect.Request
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("instance %q not found", instanceID))
}
database, err := s.store.GetDatabase(ctx, &store.FindDatabaseMessage{
- InstanceID: &instanceID,
- DatabaseName: &databaseName,
- IsCaseSensitive: store.IsObjectCaseSensitive(instance),
+ InstanceID: &instanceID,
+ DatabaseName: &databaseName,
})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
diff --git a/backend/component/export/resources.go b/backend/component/export/resources.go
index a4726d34ae..29098db337 100644
--- a/backend/component/export/resources.go
+++ b/backend/component/export/resources.go
@@ -81,9 +81,8 @@ func getResourcesForMySQL(
}
database, err := storeInstance.GetDatabase(ctx, &store.FindDatabaseMessage{
- InstanceID: &instance.ResourceID,
- DatabaseName: &databaseName,
- IsCaseSensitive: store.IsObjectCaseSensitive(instance),
+ InstanceID: &instance.ResourceID,
+ DatabaseName: &databaseName,
})
if err != nil {
if httpErr, ok := err.(*echo.HTTPError); ok && httpErr.Code == echo.ErrNotFound.Code {
@@ -114,9 +113,8 @@ func getResourcesForMySQL(
}
if sourceColumn.Database != dbMetadata.GetProto().Name {
resourceDB, err := storeInstance.GetDatabase(ctx, &store.FindDatabaseMessage{
- InstanceID: &instance.ResourceID,
- DatabaseName: &sourceColumn.Database,
- IsCaseSensitive: store.IsObjectCaseSensitive(instance),
+ InstanceID: &instance.ResourceID,
+ DatabaseName: &sourceColumn.Database,
})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get database %v in instance %v", sourceColumn.Database, instance.ResourceID))
@@ -169,9 +167,8 @@ func getResourcesForPostgres(
}
database, err := storeInstance.GetDatabase(ctx, &store.FindDatabaseMessage{
- InstanceID: &instance.ResourceID,
- DatabaseName: &databaseName,
- IsCaseSensitive: store.IsObjectCaseSensitive(instance),
+ InstanceID: &instance.ResourceID,
+ DatabaseName: &databaseName,
})
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrap(err, "failed to fetch database"))
diff --git a/backend/component/sampleinstance/manager.go b/backend/component/sampleinstance/manager.go
index c4368c3f9d..9ea3f685b1 100644
--- a/backend/component/sampleinstance/manager.go
+++ b/backend/component/sampleinstance/manager.go
@@ -280,9 +280,8 @@ func (m *Manager) generateInstance(
}
testDatabase, err := m.store.GetDatabase(ctx, &store.FindDatabaseMessage{
- InstanceID: &instance.ResourceID,
- DatabaseName: &dbName,
- IsCaseSensitive: store.IsObjectCaseSensitive(instance),
+ InstanceID: &instance.ResourceID,
+ DatabaseName: &dbName,
})
if err != nil {
return errors.Wrapf(err, "failed to find onboarding database %v", dbName)
diff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.go
index 3eb62c93c7..407d65af1e 100644
--- a/backend/runner/approval/runner.go
+++ b/backend/runner/approval/runner.go
@@ -727,9 +727,8 @@ func (r *Runner) getDatabaseMap(ctx context.Context, databases []string) (map[st
continue
}
db, err := r.store.GetDatabase(ctx, &store.FindDatabaseMessage{
- InstanceID: &instanceID,
- DatabaseName: &databaseName,
- IsCaseSensitive: store.IsObjectCaseSensitive(instance),
+ InstanceID: &instanceID,
+ DatabaseName: &databaseName,
})
if err != nil {
return nil, err
diff --git a/backend/store/database.go b/backend/store/database.go
index ffeead3ce4..bbb091869f 100644
--- a/backend/store/database.go
+++ b/backend/store/database.go
@@ -67,9 +67,6 @@ type FindDatabaseMessage struct {
// This is used for existing tasks with archived databases.
ShowDeleted bool
- // IsCaseSensitive is used to ignore case sensitive when finding database.
- IsCaseSensitive bool
-
FilterQ *qb.Query
Limit *int
Offset *int
@@ -144,11 +141,7 @@ func (s *Store) ListDatabases(ctx context.Context, find *FindDatabaseMessage) ([
where.And("db.instance = ?", *v)
}
if v := find.DatabaseName; v != nil {
- if find.IsCaseSensitive {
- where.And("db.name = ?", *v)
- } else {
- where.And("LOWER(db.name) = LOWER(?)", *v)
- }
+ where.And("db.name = ?", *v)
}
if v := find.Engine; v != nil {
where.And("instance.metadata->>'engine' = ?", *v)
diff --git a/docs/plans/2025-12-20-remove-database-iscasesensitive-design.md b/docs/plans/2025-12-20-remove-database-iscasesensitive-design.md
new file mode 100644
index 0000000000..dde93736d3
--- /dev/null
+++ b/docs/plans/2025-12-20-remove-database-iscasesensitive-design.md
@@ -0,0 +1,136 @@
+# Remove IsCaseSensitive from Database Store Lookups
+
+**Date:** 2025-12-20
+**Status:** Proposed
+
+## Problem Statement
+
+The `IsCaseSensitive` field in `FindDatabaseMessage` adds unnecessary complexity to database store lookups:
+
+**Current behavior:**
+- When `IsCaseSensitive=true`: Uses `db.name = ?` (exact match)
+- When `IsCaseSensitive=false`: Uses `LOWER(db.name) = LOWER(?)` (case-insensitive)
+
+**Why it exists:**
+- Originally intended to handle database engines where object names are case-insensitive (MySQL with `lower_case_table_names`, TiDB, MSSQL)
+- Assumption: User might provide "mydb" when database is stored as "MyDB"
+
+**Why it's unnecessary:**
+
+1. **Database names are synced exactly** - The sync pipeline stores database names exactly as returned by the engine with no normalization (`mysql/sync.go:102`, `tidb/sync.go:94`, `pg/pg.go:305`)
+
+2. **Clients use exact names** - API clients get resource names from our responses (e.g., `GET /v1/instances/prod/databases` returns `{name: "instances/prod/databases/MyDB"}`) and reuse them verbatim in subsequent requests
+
+3. **Internal references are exact** - Tasks, rollouts, and other internal components store the exact database name from database records
+
+4. **Performance cost** - `LOWER(db.name) = LOWER(?)` prevents efficient index usage on the `UNIQUE(instance, name)` constraint
+
+## Proposed Solution
+
+Remove the `IsCaseSensitive` field entirely and always use exact matching in store layer queries.
+
+**Code change:**
+
+```go
+// Before (database.go:147-151)
+if find.IsCaseSensitive {
+ where.And("db.name = ?", *v)
+} else {
+ where.And("LOWER(db.name) = LOWER(?)", *v)
+}
+
+// After
+where.And("db.name = ?", *v)
+```
+
+**Scope of changes:**
+
+1. **Store layer (remove case-insensitive logic):**
+ - `backend/store/database.go` - Remove `IsCaseSensitive` field from `FindDatabaseMessage` (line 71)
+ - Simplify query logic (lines 147-151) to always use exact match
+ - Remove ~15 call sites that set `IsCaseSensitive: store.IsObjectCaseSensitive(instance)`:
+ - `backend/api/v1/database_service.go`
+ - `backend/api/v1/database_service_changelog.go`
+ - `backend/api/v1/common.go`
+ - `backend/api/v1/rollout_service.go`
+ - `backend/component/export/resources.go`
+ - `backend/component/sampleinstance/manager.go`
+ - `backend/runner/approval/runner.go`
+ - `backend/runner/taskrun/database_migrate_executor.go`
+
+2. **Query/LSP layer (keep existing logic):**
+ - **No changes** to `IsObjectCaseSensitive()` function in `backend/store/instance.go:348`
+ - This function is still needed for Query/LSP identifier matching
+ - When users write SQL like `SELECT * FROM mydb.users`, the Query/LSP layer needs to match "mydb" against stored metadata using case-sensitivity rules
+ - Continues using `IsObjectCaseSensitive(instance)` for auto-completion and span analysis
+
+3. **Parser contexts (keep existing):**
+ - `backend/plugin/parser/base/` - Keep `IsCaseSensitive` field in parser contexts
+ - These are unrelated to store lookups, used for SQL parsing rules
+
+## Migration Strategy
+
+- **No data migration needed** - Only changing query behavior
+- **No API changes** - This is internal to the store layer
+- **Backward compatible** - Exact matching is stricter but represents correct behavior
+
+## Edge Case Handling
+
+If a database name mismatch occurs (e.g., from manual DB manipulation or API bug):
+- **Before**: Case-insensitive match might succeed incorrectly
+- **After**: Returns "not found" - correct fail-fast behavior
+
+## Validation
+
+Before implementing the change:
+
+1. **Verify no case duplicates exist:**
+ ```sql
+ SELECT instance, name FROM db
+ GROUP BY instance, LOWER(name)
+ HAVING COUNT(*) > 1;
+ ```
+ Expected: Zero rows (enforced by `UNIQUE(instance, name)` constraint)
+
+2. **Grep for manual database name construction:**
+ ```bash
+ grep -r "DatabaseName.*=" backend/ | grep -v "database.DatabaseName"
+ ```
+ Verify callers get names from database records, not constructing them
+
+## Testing Strategy
+
+1. **Existing tests should pass** - If tests break, they were relying on incorrect fuzzy matching
+
+2. **Add explicit test for exact matching:**
+ ```go
+ func TestGetDatabase_ExactMatchRequired(t *testing.T) {
+ // Create database "MyDatabase"
+ // Query with "mydatabase" should return nil (not found)
+ // Query with "MyDatabase" should succeed
+ }
+ ```
+
+3. **Integration test**: Sync a real MySQL/TiDB instance, verify databases are found with their exact synced names
+
+## Benefits
+
+1. **Performance** - Exact match enables index usage on `UNIQUE(instance, name)` constraint
+2. **Simplicity** - Removes conditional query logic and boolean flag
+3. **Correctness** - Fail fast on mismatches rather than fuzzy matching
+4. **Clarity** - Separates concerns: store lookups use exact match, Query/LSP uses case rules
+
+## Implementation Order
+
+1. Run validation queries to verify no case-mismatch scenarios exist
+2. Remove `IsCaseSensitive` field from `FindDatabaseMessage` struct
+3. Simplify query logic to always use exact match
+4. Remove `IsCaseSensitive` assignments from all ~15 call sites
+5. Run full test suite
+6. Manual testing with MySQL (case-insensitive) and PostgreSQL (case-sensitive) instances
+
+## Rollout Safety
+
+- **Low risk** - Makes queries stricter, not looser
+- **Observable** - Issues manifest as "database not found" errors (easy to spot in logs)
+- **Reversible** - Can restore `IsCaseSensitive` logic if needed (though it shouldn't be)