MCPcopy Index your code
hub / github.com/bytebase/bytebase / ListDatabases

Method ListDatabases

backend/store/database.go:124–257  ·  view source on GitHub ↗

ListDatabases lists all databases.

(ctx context.Context, find *FindDatabaseMessage)

Source from the content-addressed store, hash-verified

122
123// ListDatabases lists all databases.
124func (s *Store) ListDatabases(ctx context.Context, find *FindDatabaseMessage) ([]*DatabaseMessage, error) {
125 from := qb.Q().Space("db")
126 where := qb.Q().Space("TRUE")
127
128 if filterQ := find.FilterQ; filterQ != nil {
129 where.And("?", filterQ)
130 // Check if the filter requires the db_schema table for table filtering
131 sql, _, err := filterQ.ToSQL()
132 if err == nil && strings.Contains(sql, "ds.metadata") {
133 from.Space("INNER JOIN db_schema ds ON db.instance = ds.instance AND db.name = ds.db_name")
134 }
135 }
136
137 from.Space("LEFT JOIN instance ON db.instance = instance.resource_id")
138
139 if find.Workspace != "" {
140 where.And("instance.workspace = ?", find.Workspace)
141 }
142 if v := find.ProjectID; v != nil {
143 where.And("db.project = ?", *v)
144 }
145 if v := find.EffectiveEnvironmentID; v != nil {
146 where.And(`COALESCE(
147 db.environment,
148 instance.environment
149 ) = ?`, *v)
150 }
151 if v := find.InstanceID; v != nil {
152 where.And("db.instance = ?", *v)
153 }
154 if v := find.DatabaseName; v != nil {
155 where.And("db.name = ?", *v)
156 }
157 if len(find.DatabaseNames) > 0 {
158 where.And("db.name = ANY(?)", find.DatabaseNames)
159 }
160 if v := find.Engine; v != nil {
161 where.And("instance.metadata->>'engine' = ?", v.String())
162 }
163 if !find.ShowDeleted {
164 where.And("instance.deleted = ?", false)
165 where.And("db.deleted = ?", false)
166 }
167
168 q := qb.Q().Space(`
169 SELECT
170 db.project,
171 COALESCE(
172 db.environment,
173 instance.environment
174 ),
175 db.environment,
176 db.instance,
177 db.name,
178 db.deleted,
179 db.metadata,
180 instance.metadata->>'engine'
181 FROM ?

Callers 1

GetDatabaseMethod · 0.95

Calls 13

GetDBMethod · 0.95
QFunction · 0.92
getDatabaseCacheKeyFunction · 0.85
SpaceMethod · 0.80
AndMethod · 0.80
ToSQLMethod · 0.80
JoinMethod · 0.80
QueryContextMethod · 0.80
ScanMethod · 0.80
UnmarshalMethod · 0.80
StringMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected