Skip to content

Commit

Permalink
Merge pull request #23 from Anondo/main
Browse files Browse the repository at this point in the history
feat: Replace sqlboiler with Gorm
  • Loading branch information
moshloop authored Apr 6, 2022
2 parents 21b1d43 + ac58745 commit 502a61e
Show file tree
Hide file tree
Showing 22 changed files with 214 additions and 5,457 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
.vscode
_DS_Store
.bin/
vendor
confighub
3 changes: 3 additions & 0 deletions cmd/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import (

var outputFile, outputFormat string

// Analyzers ...
var Analyzers = []v1.Analyzer{
analyzers.PatchAnalyzer,
aws.EC2InstanceAnalyzer,
}

// Analyze ...
var Analyze = &cobra.Command{
Use: "analyze <resources>",
Short: "Analyze configuration items and report discrepencies/issues.",
Expand Down
1 change: 1 addition & 0 deletions cmd/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/spf13/cobra"
)

// GoOffline ...
var GoOffline = &cobra.Command{
Use: "go-offline",
Long: "Download all dependencies so that confighub can work without an internet connection",
Expand Down
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/spf13/pflag"
)

// Root ...
var Root = &cobra.Command{
Use: "confighub",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
Expand All @@ -31,6 +32,7 @@ var (
date = "unknown"
)

// ServerFlags ...
func ServerFlags(flags *pflag.FlagSet) {
flags.IntVar(&httpPort, "httpPort", 8080, "Port to expose a health dashboard ")
flags.IntVar(&devGuiPort, "devGuiPort", 3004, "Port used by a local npm server in development mode")
Expand Down
2 changes: 2 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

var outputDir string
var filename string

// Run ...
var Run = &cobra.Command{
Use: "run <scraper.yaml>",
Short: "Run scrapers and return",
Expand Down
3 changes: 2 additions & 1 deletion cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/spf13/cobra"
)

// Serve ...
var Serve = &cobra.Command{
Use: "serve",
Run: func(cmd *cobra.Command, args []string) {
Expand All @@ -29,7 +30,7 @@ var Serve = &cobra.Command{
return c.String(http.StatusOK, "Hello, World!")
})
// PostgREST needs to know how it is exposed to create the correct links
db.HttpEndpoint = publicEndpoint + "/db"
db.HTTPEndpoint = publicEndpoint + "/db"
go db.StartPostgrest()

url, err := url.Parse("http://localhost:3000")
Expand Down
1 change: 1 addition & 0 deletions db/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/flanksource/confighub/db/models"
)

// GetJSON ...
func GetJSON(ci models.ConfigItem) []byte {
data, err := json.Marshal(ci.Config)
if err != nil {
Expand Down
51 changes: 44 additions & 7 deletions db/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"embed"
"os"
"time"

"github.com/flanksource/commons/logger"
"github.com/jackc/pgx/v4/log/logrusadapter"
Expand All @@ -13,14 +14,20 @@ import (
"github.com/pressly/goose/v3"
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/volatiletech/sqlboiler/v4/boil"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

var ConnectionString string
var Schema = "public"
var LogLevel = "info"
var HttpEndpoint = "http://localhost:8080/db"
// connection variables
var (
ConnectionString string
Schema = "public"
LogLevel = "info"
HTTPEndpoint = "http://localhost:8080/db"
defaultDB *gorm.DB
)

// Flags ...
func Flags(flags *pflag.FlagSet) {
flags.StringVar(&ConnectionString, "db", "DB_URL", "Connection string for the postgres database")
flags.StringVar(&Schema, "db-schema", "public", "")
Expand All @@ -29,6 +36,8 @@ func Flags(flags *pflag.FlagSet) {

//go:embed migrations/*.sql
var embedMigrations embed.FS

// Pool ...
var Pool *pgxpool.Pool
var pgxConnectionString string

Expand All @@ -40,6 +49,7 @@ func readFromEnv(v string) string {
return v
}

// Init ...
func Init(connection string) error {
ConnectionString = readFromEnv(connection)
Schema = readFromEnv(Schema)
Expand Down Expand Up @@ -83,12 +93,27 @@ func Init(connection string) error {
if err != nil {
return err
}
boil.SetDB(db)
logger.Infof("Initialized DB: %s", boil.GetDB())

gormDB, err := gorm.Open(postgres.New(postgres.Config{
Conn: db,
}), &gorm.Config{
NowFunc: func() time.Time {
return time.Now().UTC()
},
})

if err != nil {
return err
}

defaultDB = gormDB

logger.Infof("Initialized DB: %s", defaultDB)

return nil
}

// Migrate ...
func Migrate() error {
goose.SetBaseFS(embedMigrations)
db, err := GetDB()
Expand All @@ -103,6 +128,18 @@ func Migrate() error {
return nil
}

// GetDB ...
func GetDB() (*sql.DB, error) {
return sql.Open("pgx", pgxConnectionString)
}

// Ping pings the database for health check
func Ping() error {
d, _ := defaultDB.DB() // returns *sql.DB
return d.Ping()
}

// DefaultDB returns the default database connection instance
func DefaultDB() *gorm.DB {
return defaultDB
}
33 changes: 0 additions & 33 deletions db/models/boil_queries.go

This file was deleted.

16 changes: 0 additions & 16 deletions db/models/boil_table_names.go

This file was deleted.

52 changes: 0 additions & 52 deletions db/models/boil_types.go

This file was deleted.

Loading

0 comments on commit 502a61e

Please sign in to comment.