@@ -26,25 +26,19 @@ const (
2626 OP_CREATE_STATEMENT = "create_statement"
2727)
2828
29- const selectSchemaNameTemplate = `
30- SELECT
31- SCHEMA_NAME
32- FROM
33- information_schema.schemata
34- WHERE
35- SCHEMA_NAME NOT IN %s`
36-
3729const (
38- selectTableName = `
30+ selectTablesTemplate = `
3931 SELECT
32+ TABLE_SCHEMA,
4033 TABLE_NAME,
4134 TABLE_TYPE,
4235 CREATE_TIME,
43- ifnull (UPDATE_TIME, CREATE_TIME) AS UPDATE_TIME
36+ IFNULL (UPDATE_TIME, CREATE_TIME) AS UPDATE_TIME
4437 FROM
4538 information_schema.tables
4639 WHERE
47- TABLE_SCHEMA = ?`
40+ TABLE_SCHEMA NOT IN %s
41+ ORDER BY TABLE_SCHEMA, TABLE_NAME`
4842
4943 // Note that the fully qualified table name is appendend to the query,
5044 // we can't use placeholders with this statement.
@@ -231,79 +225,47 @@ func (c *SchemaDetails) Stop() {
231225}
232226
233227func (c * SchemaDetails ) extractSchema (ctx context.Context ) error {
234- query := fmt .Sprintf (selectSchemaNameTemplate , buildExcludedSchemasClause (c .excludeSchemas ))
228+ query := fmt .Sprintf (selectTablesTemplate , buildExcludedSchemasClause (c .excludeSchemas ))
235229 rs , err := c .dbConnection .QueryContext (ctx , query )
236230 if err != nil {
237- return fmt .Errorf ("failed to query schemata : %w" , err )
231+ return fmt .Errorf ("failed to query tables : %w" , err )
238232 }
239233 defer rs .Close ()
240234
241- var schemas []string
235+ tables := []* tableInfo {}
242236 for rs .Next () {
243- var schema string
244- if err := rs .Scan (& schema ); err != nil {
245- level .Error (c .logger ).Log ("msg" , "failed to scan schemata" , "err" , err )
237+ var schema , tableName , tableType string
238+ var createTime , updateTime time.Time
239+ if err := rs .Scan (& schema , & tableName , & tableType , & createTime , & updateTime ); err != nil {
240+ level .Error (c .logger ).Log ("msg" , "failed to scan tables" , "err" , err )
246241 break
247242 }
248- schemas = append (schemas , schema )
249- }
250-
251- if err := rs .Err (); err != nil {
252- return fmt .Errorf ("failed to iterate over schemas result set: %w" , err )
253- }
243+ tables = append (tables , & tableInfo {
244+ schema : schema ,
245+ tableName : tableName ,
246+ tableType : tableType ,
247+ createTime : createTime ,
248+ updateTime : updateTime ,
249+ b64CreateStmt : "" ,
250+ b64TableSpec : "" ,
251+ })
254252
255- if len (schemas ) == 0 {
256- level .Info (c .logger ).Log ("msg" , "no schema detected from information_schema.schemata" )
257- return nil
253+ c .entryHandler .Chan () <- database_observability .BuildLokiEntry (
254+ logging .LevelInfo ,
255+ OP_TABLE_DETECTION ,
256+ fmt .Sprintf (`schema="%s" table="%s"` , schema , tableName ),
257+ )
258258 }
259259
260- tables := []* tableInfo {}
261-
262- for _ , schema := range schemas {
263- rs , err := c .dbConnection .QueryContext (ctx , selectTableName , schema )
264- if err != nil {
265- level .Error (c .logger ).Log ("msg" , "failed to query tables" , "err" , err )
266- break
267- }
268-
269- for rs .Next () {
270- var tableName , tableType string
271- var createTime , updateTime time.Time
272- if err := rs .Scan (& tableName , & tableType , & createTime , & updateTime ); err != nil {
273- level .Error (c .logger ).Log ("msg" , "failed to scan tables" , "err" , err )
274- break
275- }
276- tables = append (tables , & tableInfo {
277- schema : schema ,
278- tableName : tableName ,
279- tableType : tableType ,
280- createTime : createTime ,
281- updateTime : updateTime ,
282- b64CreateStmt : "" ,
283- b64TableSpec : "" ,
284- })
285-
286- c .entryHandler .Chan () <- database_observability .BuildLokiEntry (
287- logging .LevelInfo ,
288- OP_TABLE_DETECTION ,
289- fmt .Sprintf (`schema="%s" table="%s"` , schema , tableName ),
290- )
291- }
292-
293- iterErr := rs .Err ()
294- rs .Close ()
295-
296- if iterErr != nil {
297- return fmt .Errorf ("failed to iterate over tables result set: %w" , iterErr )
298- }
260+ if err := rs .Err (); err != nil {
261+ return fmt .Errorf ("failed to iterate over tables result set: %w" , err )
299262 }
300263
301264 if len (tables ) == 0 {
302265 level .Info (c .logger ).Log ("msg" , "no tables detected from information_schema.tables" )
303266 return nil
304267 }
305268
306- // TODO(cristian): consider moving this into the loop above
307269 for _ , table := range tables {
308270 fullyQualifiedTable := fmt .Sprintf ("`%s`.`%s`" , table .schema , table .tableName )
309271 cacheKey := fmt .Sprintf ("%s@%d" , fullyQualifiedTable , table .updateTime .Unix ())
0 commit comments