Skip to content

Commit

Permalink
refactor: database setup
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jul 13, 2024
1 parent 10304b3 commit d9ddfe9
Show file tree
Hide file tree
Showing 19 changed files with 1,159 additions and 1,198 deletions.
3 changes: 1 addition & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
"extensions": [
"GitHub.copilot",
"GitHub.vscode-github-actions",
"golang.go",
"ms-vscode.makefile-tools"
"golang.go"
]
}
}
Expand Down
1 change: 1 addition & 0 deletions api/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ paths:
tags:
- Teams
summary: Get a list of teams
operationId: getTeams
security:
- api_key: []
parameters:
Expand Down
41 changes: 41 additions & 0 deletions cmd/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
seed "github.com/zeiss/gorm-seed"
"github.com/zeiss/knox/internal/adapters/database"
"github.com/zeiss/knox/internal/models"

"github.com/spf13/cobra"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)

var Migrate = &cobra.Command{
Use: "migrate",
Short: "Migrate the database",
RunE: func(cmd *cobra.Command, args []string) error {
conn, err := gorm.Open(postgres.Open(config.Flags.DatabaseURI), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: config.Flags.DatabaseTablePrefix,
},
})
if err != nil {
return err
}

store, err := seed.NewDatabase(conn, database.NewReadTx(), database.NewWriteTx())
if err != nil {
return err
}

return store.Migrate(
cmd.Context(),
&models.Environment{},
&models.Lock{},
&models.Project{},
&models.Snapshot{},
&models.State{},
)
},
}
112 changes: 0 additions & 112 deletions cmd/migrate/main.go

This file was deleted.

42 changes: 22 additions & 20 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cmd

import (
"context"
"log"

authz "github.com/zeiss/fiber-authz"
"github.com/zeiss/knox/internal/adapters/database"
"github.com/zeiss/knox/internal/adapters/handlers"
"github.com/zeiss/knox/internal/controllers"
Expand All @@ -15,8 +15,11 @@ import (
logger "github.com/gofiber/fiber/v2/middleware/logger"
requestid "github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/katallaxie/pkg/server"
"github.com/kelseyhightower/envconfig"
middleware "github.com/oapi-codegen/fiber-middleware"
"github.com/spf13/cobra"
authz "github.com/zeiss/fiber-authz"
seed "github.com/zeiss/gorm-seed"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
Expand All @@ -27,20 +30,24 @@ var config *cfg.Config
func init() {
config = cfg.New()

Root.PersistentFlags().StringVar(&config.Flags.Addr, "addr", ":8080", "addr")
Root.PersistentFlags().StringVar(&config.Flags.DB.Addr, "db-addr", config.Flags.DB.Addr, "Database address")
Root.PersistentFlags().StringVar(&config.Flags.DB.Database, "db-database", config.Flags.DB.Database, "Database name")
Root.PersistentFlags().StringVar(&config.Flags.DB.Username, "db-username", config.Flags.DB.Username, "Database user")
Root.PersistentFlags().StringVar(&config.Flags.DB.Password, "db-password", config.Flags.DB.Password, "Database password")
Root.PersistentFlags().IntVar(&config.Flags.DB.Port, "db-port", config.Flags.DB.Port, "Database port")
err := envconfig.Process("", config.Flags)
if err != nil {
log.Fatal(err)
}

Root.AddCommand(Seed)
Root.AddCommand(Migrate)

Root.PersistentFlags().StringVar(&config.Flags.Addr, "addr", config.Flags.Addr, "addr")
Root.PersistentFlags().StringVar(&config.Flags.DatabaseURI, "db-uri", config.Flags.DatabaseURI, "Database URI")
Root.PersistentFlags().StringVar(&config.Flags.DatabaseTablePrefix, "db-table-prefix", config.Flags.DatabaseTablePrefix, "Database table prefix")

Root.SilenceUsage = true
}

var Root = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
cfg := cfg.New()
srv := NewWebSrv(cfg)
srv := NewWebSrv(config)

s, _ := server.WithContext(cmd.Context())
s.Listen(srv, false)
Expand All @@ -64,21 +71,16 @@ func NewWebSrv(cfg *cfg.Config) *WebSrv {
// Start starts the server.
func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.RunFunc) func() error {
return func() error {
conn, err := gorm.Open(postgres.Open(s.cfg.DSN()), &gorm.Config{
conn, err := gorm.Open(postgres.Open(s.cfg.Flags.DatabaseURI), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: "knox_",
TablePrefix: config.Flags.DatabaseTablePrefix,
},
})
if err != nil {
return err
}

db, err := database.NewDB(conn)
if err != nil {
return err
}

err = db.Migrate(ctx)
store, err := seed.NewDatabase(conn, database.NewReadTx(), database.NewWriteTx())
if err != nil {
return err
}
Expand All @@ -103,9 +105,9 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R

app.Use(middleware.OapiRequestValidatorWithOptions(swagger, validatorOptions))

lc := controllers.NewLocksController(db)
sc := controllers.NewStateController(db)
pc := controllers.NewSnapshotController(db)
lc := controllers.NewLocksController(store)
sc := controllers.NewStateController(store)
pc := controllers.NewSnapshotController(store)

handlers := handlers.NewAPIHandlers(lc, sc, pc)
handler := openapi.NewStrictHandler(handlers, nil)
Expand Down
64 changes: 64 additions & 0 deletions cmd/seed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cmd

import (
"github.com/zeiss/fiber-goth/adapters"
seed "github.com/zeiss/gorm-seed"
"github.com/zeiss/knox/internal/models"

"github.com/spf13/cobra"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)

var seeds = []seed.Seed{
{
Name: "create team",
Run: func(db *gorm.DB) error {
team := adapters.GothTeam{
Name: "Zeiss",
Slug: "zeiss",
}

err := db.Create(&team).Error
if err != nil {
return err
}

project := models.Project{
Name: "demo",
TeamID: team.ID,
}

err = db.Create(&project).Error
if err != nil {
return err
}

return db.Create([]models.Environment{
{
ProjectID: project.ID,
Name: "dev",
},
}).Error
},
},
}

var Seed = &cobra.Command{
Use: "seed",
Short: "Seed the database",
RunE: func(cmd *cobra.Command, args []string) error {
conn, err := gorm.Open(postgres.Open(config.Flags.DatabaseURI), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: config.Flags.DatabaseTablePrefix,
},
})
if err != nil {
return err
}

seeder := seed.NewSeeder(conn)
return seeder.Seed(cmd.Context(), seeds...)
},
}
Loading

0 comments on commit d9ddfe9

Please sign in to comment.