Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: simplify PostgreSQL search path handling (#18120)
Danny Xu1facd652d7e1
diff --git a/backend/plugin/parser/pg/query_span_extractor.go b/backend/plugin/parser/pg/query_span_extractor.goindex 6af1f3e35d..5181b3c84d 100644--- a/backend/plugin/parser/pg/query_span_extractor.go+++ b/backend/plugin/parser/pg/query_span_extractor.go@@ -1201,15 +1201,12 @@ func (q *querySpanExtractor) findTableSchema(schemaName string, tableName stringDatabase: &q.defaultDatabase,}}- searchPath := q.searchPath- if schemaName != "" {- searchPath = []string{schemaName}- }- tableSchemaName, table := dbSchema.SearchTable(searchPath, tableName)- viewSchemaName, view := dbSchema.SearchView(searchPath, tableName)- materializedViewSchemaName, materializedView := dbSchema.SearchMaterializedView(searchPath, tableName)- foreignTableSchemaName, foreignTable := dbSchema.SearchExternalTable(searchPath, tableName)- sequenceSchemaName, sequence := dbSchema.SearchSequence(searchPath, tableName)+ searcher := dbSchema.NewSearcher(schemaName)+ tableSchemaName, table := searcher.SearchTable(tableName)+ viewSchemaName, view := searcher.SearchView(tableName)+ materializedViewSchemaName, materializedView := searcher.SearchMaterializedView(tableName)+ foreignTableSchemaName, foreignTable := searcher.SearchExternalTable(tableName)+ sequenceSchemaName, sequence := searcher.SearchSequence(tableName)if table == nil && view == nil && foreignTable == nil && materializedView == nil && sequence == nil {return nil, &parsererror.ResourceNotFoundError{diff --git a/backend/store/model/database.go b/backend/store/model/database.goindex 57ec37de09..79a0fc8270 100644--- a/backend/store/model/database.go+++ b/backend/store/model/database.go@@ -249,7 +249,7 @@ func NewDatabaseMetadata(metadata *storepb.DatabaseSchemaMetadata, isObjectCaseSproto: metadata,name: metadata.Name,owner: metadata.Owner,- searchPath: NormalizeSearchPath(metadata.SearchPath),+ searchPath: normalizeSearchPath(metadata.SearchPath),isObjectCaseSensitive: isObjectCaseSensitive,isDetailCaseSensitive: isDetailCaseSensitive,internal: make(map[string]*SchemaMetadata),@@ -409,67 +409,6 @@ func (d *DatabaseMetadata) GetSchema(name string) *SchemaMetadata {return d.internal[schemaID]}-func (d *DatabaseMetadata) SearchTable(searchPath []string, name string) (string, *TableMetadata) {- // Search in the search path first.- for _, schemaName := range searchPath {- schema := d.GetSchema(schemaName)- if schema == nil {- continue- }- table := schema.GetTable(name)- if table != nil {- return schema.proto.Name, table- }- }-- return "", nil-}--func (d *DatabaseMetadata) SearchIndex(searchPath []string, name string) (string, *IndexMetadata) {- // Search in the search path first.- for _, schemaName := range searchPath {- schema := d.GetSchema(schemaName)- if schema == nil {- continue- }- index := schema.GetIndex(name)- if index != nil {- return schema.proto.Name, index- }- }- return "", nil-}--func (d *DatabaseMetadata) SearchView(searchPath []string, name string) (string, *ViewMetadata) {- // Search in the search path first.- for _, schemaName := range searchPath {- schema := d.GetSchema(schemaName)- if schema == nil {- continue- }- view := schema.GetView(name)- if view != nil {- return schema.proto.Name, view- }- }- return "", nil-}--func (d *DatabaseMetadata) SearchExternalTable(searchPath []string, name string) (string, *ExternalTableMetadata) {- // Search in the search path first.- for _, schemaName := range searchPath {- schema := d.GetSchema(schemaName)- if schema == nil {- continue- }- externalTable := schema.GetExternalTable(name)- if externalTable != nil {- return schema.proto.Name, externalTable- }- }- return "", nil-}-// DatabaseName returns the name of the database.func (d *DatabaseMetadata) DatabaseName() string {if d.proto == nil {@@ -488,76 +427,6 @@ func (d *DatabaseMetadata) HasNoTable() bool {return true}-func (d *DatabaseMetadata) SearchSequence(searchPath []string, name string) (string, *SequenceMetadata) {- // Search in the search path first.- for _, schemaName := range searchPath {- schema := d.GetSchema(schemaName)- if schema == nil {- continue- }- sequence := schema.GetSequence(name)- if sequence != nil {- return schema.proto.Name, sequence- }- }- return "", nil-}--func (d *DatabaseMetadata) SearchMaterializedView(searchPath []string, name string) (string, *MaterializedViewMetadata) {- // Search in the search path first.- for _, schemaName := range searchPath {- schema := d.GetSchema(schemaName)- if schema == nil {- continue- }- materializedView := schema.GetMaterializedView(name)- if materializedView != nil {- return schema.proto.Name, materializedView- }- }- return "", nil-}--func (d *DatabaseMetadata) SearchFunctions(searchPath []string, name string) ([]string, []*FunctionMetadata) {- var schemas []string- var funcs []*FunctionMetadata- // Search in the search path first.- for _, schemaName := range searchPath {- schema := d.GetSchema(schemaName)- if schema == nil {- continue- }- for _, function := range schema.ListFunctions() {- if d.isDetailCaseSensitive {- if function.proto.Name == name {- schemas = append(schemas, schema.proto.Name)- funcs = append(funcs, function)- }- } else {- if strings.EqualFold(function.proto.Name, name) {- schemas = append(schemas, schema.proto.Name)- funcs = append(funcs, function)- }- }- }- }- return schemas, funcs-}--func (d *DatabaseMetadata) SearchObject(searchPath []string, name string) (string, string) {- // Search in the search path first.- for _, schemaName := range searchPath {- schema := d.GetSchema(schemaName)- if schema == nil {- continue- }- if schema.GetTable(name) != nil || schema.GetView(name) != nil || schema.GetMaterializedView(name) != nil || schema.GetFunction(name) != nil || schema.GetProcedure(name) != nil || schema.GetPackage(name) != nil || schema.GetSequence(name) != nil || schema.GetExternalTable(name) != nil {- return schema.proto.Name, name- }- }- return "", ""-}-// ListSchemaNames lists the schema names.func (d *DatabaseMetadata) ListSchemaNames() []string {var result []string@@ -1289,56 +1158,3 @@ func getIsDetailCaseSensitive(engine storepb.Engine) bool {return true}}--func IsSystemPath(path string) bool {- // PostgreSQL system schemas.- systemSchemas := []string{"pg_catalog", "information_schema", "pg_toast", "pg_temp_1", "pg_temp_2", "pg_global", "$user"}- for _, schema := range systemSchemas {- if strings.EqualFold(path, schema) {- return true- }- }- return false-}--// NormalizeSearchPath normalizes the search path string into a slice of strings.-func NormalizeSearchPath(searchPath string) []string {- if searchPath == "" {- return []string{}- }-- // Split the search path by comma and trim spaces.- parts := strings.Split(searchPath, ",")- for i, part := range parts {- parts[i] = strings.TrimSpace(part)- }-- // Remove empty parts.- var result []string- for _, part := range parts {- schema := strings.TrimSpace(part)- if part == "\"$user\"" {- continue- }- if strings.HasPrefix(part, "\"") && strings.HasSuffix(part, "\"") {- // Remove the quotes from the schema name.- schema = strings.Trim(schema, "\"")- } else if strings.HasPrefix(part, "'") && strings.HasSuffix(part, "'") {- // Remove the single quotes from the schema name.- schema = strings.Trim(schema, "'")- } else {- // For non-quoted schema names, we just return the lower string for PostgreSQL.- schema = strings.ToLower(schema)- }- schema = strings.TrimSpace(schema)- if IsSystemPath(schema) {- // Skip system schemas.- continue- }- if schema != "" {- result = append(result, schema)- }- }-- return result-}diff --git a/backend/store/model/pg_searcher.go b/backend/store/model/pg_searcher.gonew file mode 100644index 0000000000..6ac5a0f48b--- /dev/null+++ b/backend/store/model/pg_searcher.go@@ -0,0 +1,361 @@+package model++import "strings"++// isSystemPath checks if a path is a PostgreSQL system schema.+// NOTE: This is primarily designed for PostgreSQL.+func isSystemPath(path string) bool {+ // PostgreSQL system schemas.+ systemSchemas := []string{"pg_catalog", "information_schema", "pg_toast", "pg_temp_1", "pg_temp_2", "pg_global", "$user"}+ for _, schema := range systemSchemas {+ if strings.EqualFold(path, schema) {+ return true+ }+ }+ return false+}++// normalizeSearchPath normalizes the search path string into a slice of strings.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func normalizeSearchPath(searchPath string) []string {+ if searchPath == "" {+ return []string{}+ }++ // Split the search path by comma and trim spaces.+ parts := strings.Split(searchPath, ",")+ for i, part := range parts {+ parts[i] = strings.TrimSpace(part)+ }++ // Remove empty parts.+ var result []string+ for _, part := range parts {+ schema := strings.TrimSpace(part)+ if part == "\"$user\"" {+ continue+ }+ if strings.HasPrefix(part, "\"") && strings.HasSuffix(part, "\"") {+ // Remove the quotes from the schema name.+ schema = strings.Trim(schema, "\"")+ } else if strings.HasPrefix(part, "'") && strings.HasSuffix(part, "'") {+ // Remove the single quotes from the schema name.+ schema = strings.Trim(schema, "'")+ } else {+ // For non-quoted schema names, we just return the lower string for PostgreSQL.+ schema = strings.ToLower(schema)+ }+ schema = strings.TrimSpace(schema)+ if isSystemPath(schema) {+ // Skip system schemas.+ continue+ }+ if schema != "" {+ result = append(result, schema)+ }+ }++ return result+}++// DatabaseSearcher provides a fluent interface for searching database objects with a specific search path.+// This helper avoids repetitive searchPath parameter passing when performing multiple searches.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+type DatabaseSearcher struct {+ db *DatabaseMetadata+ searchPath []string+}++// NewSearcher creates a searcher for database objects.+// If schemaName is provided (non-empty), it searches only in that schema; otherwise uses the database's search path.+// If the database's search path is empty, defaults to ["public"] for PostgreSQL.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (d *DatabaseMetadata) NewSearcher(schemaName string) *DatabaseSearcher {+ searchPath := d.searchPath+ if schemaName != "" {+ searchPath = []string{schemaName}+ } else if len(searchPath) == 0 {+ // Default to "public" schema if no search path is set (PostgreSQL default)+ searchPath = []string{"public"}+ }+ return &DatabaseSearcher{+ db: d,+ searchPath: searchPath,+ }+}++// SearchTable searches for a table in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (s *DatabaseSearcher) SearchTable(name string) (string, *TableMetadata) {+ for _, schemaName := range s.searchPath {+ schema := s.db.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ table := schema.GetTable(name)+ if table != nil {+ return schema.proto.Name, table+ }+ }+ return "", nil+}++// SearchIndex searches for an index in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (s *DatabaseSearcher) SearchIndex(name string) (string, *IndexMetadata) {+ for _, schemaName := range s.searchPath {+ schema := s.db.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ index := schema.GetIndex(name)+ if index != nil {+ return schema.proto.Name, index+ }+ }+ return "", nil+}++// SearchView searches for a view in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (s *DatabaseSearcher) SearchView(name string) (string, *ViewMetadata) {+ for _, schemaName := range s.searchPath {+ schema := s.db.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ view := schema.GetView(name)+ if view != nil {+ return schema.proto.Name, view+ }+ }+ return "", nil+}++// SearchExternalTable searches for an external table in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (s *DatabaseSearcher) SearchExternalTable(name string) (string, *ExternalTableMetadata) {+ for _, schemaName := range s.searchPath {+ schema := s.db.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ externalTable := schema.GetExternalTable(name)+ if externalTable != nil {+ return schema.proto.Name, externalTable+ }+ }+ return "", nil+}++// SearchSequence searches for a sequence in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (s *DatabaseSearcher) SearchSequence(name string) (string, *SequenceMetadata) {+ for _, schemaName := range s.searchPath {+ schema := s.db.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ sequence := schema.GetSequence(name)+ if sequence != nil {+ return schema.proto.Name, sequence+ }+ }+ return "", nil+}++// SearchMaterializedView searches for a materialized view in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (s *DatabaseSearcher) SearchMaterializedView(name string) (string, *MaterializedViewMetadata) {+ for _, schemaName := range s.searchPath {+ schema := s.db.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ materializedView := schema.GetMaterializedView(name)+ if materializedView != nil {+ return schema.proto.Name, materializedView+ }+ }+ return "", nil+}++// SearchFunctions searches for functions in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (s *DatabaseSearcher) SearchFunctions(name string) ([]string, []*FunctionMetadata) {+ var schemas []string+ var funcs []*FunctionMetadata+ for _, schemaName := range s.searchPath {+ schema := s.db.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ for _, function := range schema.ListFunctions() {+ if s.db.isDetailCaseSensitive {+ if function.proto.Name == name {+ schemas = append(schemas, schema.proto.Name)+ funcs = append(funcs, function)+ }+ } else {+ if strings.EqualFold(function.proto.Name, name) {+ schemas = append(schemas, schema.proto.Name)+ funcs = append(funcs, function)+ }+ }+ }+ }+ return schemas, funcs+}++// SearchObject searches for any database object in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (s *DatabaseSearcher) SearchObject(name string) (string, string) {+ for _, schemaName := range s.searchPath {+ schema := s.db.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ if schema.GetTable(name) != nil || schema.GetView(name) != nil || schema.GetMaterializedView(name) != nil || schema.GetFunction(name) != nil || schema.GetProcedure(name) != nil || schema.GetPackage(name) != nil || schema.GetSequence(name) != nil || schema.GetExternalTable(name) != nil {+ return schema.proto.Name, name+ }+ }+ return "", ""+}++// SearchTable searches for a table in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (d *DatabaseMetadata) SearchTable(searchPath []string, name string) (string, *TableMetadata) {+ for _, schemaName := range searchPath {+ schema := d.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ table := schema.GetTable(name)+ if table != nil {+ return schema.proto.Name, table+ }+ }+ return "", nil+}++// SearchIndex searches for an index in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (d *DatabaseMetadata) SearchIndex(searchPath []string, name string) (string, *IndexMetadata) {+ for _, schemaName := range searchPath {+ schema := d.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ index := schema.GetIndex(name)+ if index != nil {+ return schema.proto.Name, index+ }+ }+ return "", nil+}++// SearchView searches for a view in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (d *DatabaseMetadata) SearchView(searchPath []string, name string) (string, *ViewMetadata) {+ for _, schemaName := range searchPath {+ schema := d.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ view := schema.GetView(name)+ if view != nil {+ return schema.proto.Name, view+ }+ }+ return "", nil+}++// SearchExternalTable searches for an external table in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (d *DatabaseMetadata) SearchExternalTable(searchPath []string, name string) (string, *ExternalTableMetadata) {+ for _, schemaName := range searchPath {+ schema := d.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ externalTable := schema.GetExternalTable(name)+ if externalTable != nil {+ return schema.proto.Name, externalTable+ }+ }+ return "", nil+}++// SearchSequence searches for a sequence in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (d *DatabaseMetadata) SearchSequence(searchPath []string, name string) (string, *SequenceMetadata) {+ for _, schemaName := range searchPath {+ schema := d.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ sequence := schema.GetSequence(name)+ if sequence != nil {+ return schema.proto.Name, sequence+ }+ }+ return "", nil+}++// SearchMaterializedView searches for a materialized view in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (d *DatabaseMetadata) SearchMaterializedView(searchPath []string, name string) (string, *MaterializedViewMetadata) {+ for _, schemaName := range searchPath {+ schema := d.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ materializedView := schema.GetMaterializedView(name)+ if materializedView != nil {+ return schema.proto.Name, materializedView+ }+ }+ return "", nil+}++// SearchFunctions searches for functions in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (d *DatabaseMetadata) SearchFunctions(searchPath []string, name string) ([]string, []*FunctionMetadata) {+ var schemas []string+ var funcs []*FunctionMetadata+ for _, schemaName := range searchPath {+ schema := d.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ for _, function := range schema.ListFunctions() {+ if d.isDetailCaseSensitive {+ if function.proto.Name == name {+ schemas = append(schemas, schema.proto.Name)+ funcs = append(funcs, function)+ }+ } else {+ if strings.EqualFold(function.proto.Name, name) {+ schemas = append(schemas, schema.proto.Name)+ funcs = append(funcs, function)+ }+ }+ }+ }+ return schemas, funcs+}++// SearchObject searches for any database object in the search path.+// NOTE: This is primarily designed for PostgreSQL's search_path concept.+func (d *DatabaseMetadata) SearchObject(searchPath []string, name string) (string, string) {+ for _, schemaName := range searchPath {+ schema := d.GetSchema(schemaName)+ if schema == nil {+ continue+ }+ if schema.GetTable(name) != nil || schema.GetView(name) != nil || schema.GetMaterializedView(name) != nil || schema.GetFunction(name) != nil || schema.GetProcedure(name) != nil || schema.GetPackage(name) != nil || schema.GetSequence(name) != nil || schema.GetExternalTable(name) != nil {+ return schema.proto.Name, name+ }+ }+ return "", ""+}