Skip to content

Commit

Permalink
Move database object discovery into objects file
Browse files Browse the repository at this point in the history
  • Loading branch information
lewissteele committed Nov 29, 2024
1 parent 09d5873 commit d332bce
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions internal/db/objects.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package db

import (
"fmt"
"strings"

"gorm.io/gorm"
"gorm.io/gorm/logger"
)

var Databases []string

Check failure on line 11 in internal/db/objects.go

View workflow job for this annotation

GitHub Actions / build

other declaration of Databases
var Tables []string

Check failure on line 12 in internal/db/objects.go

View workflow job for this annotation

GitHub Actions / build

other declaration of Tables

func cacheObjects() {
conn, _ := gorm.Open(
dialector(UserDB),

Check failure on line 16 in internal/db/objects.go

View workflow job for this annotation

GitHub Actions / build

undefined: dialector
&gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
SkipDefaultTransaction: true,
})

db, _ := conn.DB()
db.SetMaxOpenConns(1)

cacheDatabases(conn)

for _, d := range Databases {
cacheTables(d, conn)
}

db.Close()
}

func cacheDatabases(conn *gorm.DB) {
var databases []string
Conn.Raw("show databases").Scan(&databases)

for _, database := range databases {
Databases = append(
Databases,
database,
)
}
}

func cacheTables(database string, conn *gorm.DB) {
conn.Exec(fmt.Sprintf("use `%s`", database))

var tables []string
conn.Raw("show tables").Scan(&tables)

for _, table := range tables {
if Selected() == database {

Check failure on line 53 in internal/db/objects.go

View workflow job for this annotation

GitHub Actions / build

undefined: Selected
Tables = append(Tables, table)
continue
}

Tables = append(Tables, strings.Join([]string{database, table}, "."))
}
}

0 comments on commit d332bce

Please sign in to comment.