Browse Source

finished re-integrating DB cli and global CLI

main
Artemis 1 month ago
parent
commit
76cb2a396e
  1. 2
      .gitignore
  2. 20
      db/core.go
  3. 67
      db/migrations/core.go
  4. 10
      gallery.go
  5. 33
      internal/app/cli.go
  6. 1
      internal/config/config.go

2
.gitignore

@ -1,2 +1,4 @@
.idea/
*.toml
gallery
*.db

20
db/core.go

@ -5,6 +5,8 @@ import (
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/sqlitedialect"
"github.com/uptrace/bun/driver/sqliteshim"
"github.com/uptrace/bun/migrate"
"runtime"
)
type Database struct {
@ -12,15 +14,23 @@ type Database struct {
db *bun.DB
}
func (d Database) Open(dsn string) error {
func Open(dsn string) (*Database, error) {
sqlDb, err := sql.Open(sqliteshim.ShimName, dsn)
if err != nil {
return err
return nil, err
}
d.sqlDb = sqlDb
maxOpenConnections := 4 * runtime.GOMAXPROCS(0)
sqlDb.SetMaxOpenConns(maxOpenConnections)
sqlDb.SetMaxIdleConns(maxOpenConnections)
return &Database{
sqlDb: sqlDb,
db: bun.NewDB(sqlDb, sqlitedialect.New()),
}, nil
}
d.db = bun.NewDB(sqlDb, sqlitedialect.New())
return nil
func (d Database) GetMigrator(migrations *migrate.Migrations) *migrate.Migrator {
return migrate.NewMigrator(d.db, migrations)
}
func (d Database) Close() error {

67
db/migrations/core.go

@ -2,6 +2,7 @@ package migrations
import (
"fmt"
"git.artemix.space/go/gallery/internal/app"
"github.com/uptrace/bun/migrate"
"github.com/urfave/cli/v2"
"strings"
@ -9,7 +10,8 @@ import (
var Migrations = migrate.NewMigrations()
func newDBCommand(migrations *migrate.Migrations) *cli.Command {
// DbManagementCommands Migration system taken from bun-starter package
func DbManagementCommands(migrations *migrate.Migrations) *cli.Command {
return &cli.Command{
Name: "db",
Usage: "manage database migrations",
@ -18,29 +20,27 @@ func newDBCommand(migrations *migrate.Migrations) *cli.Command {
Name: "init",
Usage: "create migration tables",
Action: func(c *cli.Context) error {
ctx, app, err := bunapp.StartCLI(c)
ctx, err := app.Init(c)
if err != nil {
return err
}
defer app.Stop()
migrator := migrate.NewMigrator(app.DB(), migrations)
return migrator.Init(ctx)
migrator := ctx.Db.GetMigrator(migrations)
return migrator.Init(c.Context)
},
},
{
Name: "migrate",
Usage: "migrate database",
Action: func(c *cli.Context) error {
ctx, app, err := bunapp.StartCLI(c)
ctx, err := app.Init(c)
if err != nil {
return err
}
defer app.Stop()
migrator := migrate.NewMigrator(app.DB(), migrations)
migrator := ctx.Db.GetMigrator(migrations)
group, err := migrator.Migrate(ctx)
group, err := migrator.Migrate(c.Context)
if err != nil {
return err
}
@ -58,15 +58,14 @@ func newDBCommand(migrations *migrate.Migrations) *cli.Command {
Name: "rollback",
Usage: "rollback the last migration group",
Action: func(c *cli.Context) error {
ctx, app, err := bunapp.StartCLI(c)
ctx, err := app.Init(c)
if err != nil {
return err
}
defer app.Stop()
migrator := migrate.NewMigrator(app.DB(), migrations)
migrator := ctx.Db.GetMigrator(migrations)
group, err := migrator.Rollback(ctx)
group, err := migrator.Rollback(c.Context)
if err != nil {
return err
}
@ -84,44 +83,41 @@ func newDBCommand(migrations *migrate.Migrations) *cli.Command {
Name: "lock",
Usage: "lock migrations",
Action: func(c *cli.Context) error {
ctx, app, err := bunapp.StartCLI(c)
ctx, err := app.Init(c)
if err != nil {
return err
}
defer app.Stop()
migrator := migrate.NewMigrator(app.DB(), migrations)
return migrator.Lock(ctx)
migrator := ctx.Db.GetMigrator(migrations)
return migrator.Lock(c.Context)
},
},
{
Name: "unlock",
Usage: "unlock migrations",
Action: func(c *cli.Context) error {
ctx, app, err := bunapp.StartCLI(c)
ctx, err := app.Init(c)
if err != nil {
return err
}
defer app.Stop()
migrator := migrate.NewMigrator(app.DB(), migrations)
return migrator.Unlock(ctx)
migrator := ctx.Db.GetMigrator(migrations)
return migrator.Unlock(c.Context)
},
},
{
Name: "create_go",
Usage: "create Go migration",
Action: func(c *cli.Context) error {
ctx, app, err := bunapp.StartCLI(c)
ctx, err := app.Init(c)
if err != nil {
return err
}
defer app.Stop()
migrator := migrate.NewMigrator(app.DB(), migrations)
migrator := ctx.Db.GetMigrator(migrations)
name := strings.Join(c.Args().Slice(), "_")
mf, err := migrator.CreateGoMigration(ctx, name)
mf, err := migrator.CreateGoMigration(c.Context, name)
if err != nil {
return err
}
@ -134,16 +130,15 @@ func newDBCommand(migrations *migrate.Migrations) *cli.Command {
Name: "create_sql",
Usage: "create up and down SQL migrations",
Action: func(c *cli.Context) error {
ctx, app, err := bunapp.StartCLI(c)
ctx, err := app.Init(c)
if err != nil {
return err
}
defer app.Stop()
migrator := migrate.NewMigrator(app.DB(), migrations)
migrator := ctx.Db.GetMigrator(migrations)
name := strings.Join(c.Args().Slice(), "_")
files, err := migrator.CreateSQLMigrations(ctx, name)
files, err := migrator.CreateSQLMigrations(c.Context, name)
if err != nil {
return err
}
@ -159,15 +154,14 @@ func newDBCommand(migrations *migrate.Migrations) *cli.Command {
Name: "status",
Usage: "print migrations status",
Action: func(c *cli.Context) error {
ctx, app, err := bunapp.StartCLI(c)
ctx, err := app.Init(c)
if err != nil {
return err
}
defer app.Stop()
migrator := migrate.NewMigrator(app.DB(), migrations)
migrator := ctx.Db.GetMigrator(migrations)
ms, err := migrator.MigrationsWithStatus(ctx)
ms, err := migrator.MigrationsWithStatus(c.Context)
if err != nil {
return err
}
@ -182,15 +176,14 @@ func newDBCommand(migrations *migrate.Migrations) *cli.Command {
Name: "mark_applied",
Usage: "mark migrations as applied without actually running them",
Action: func(c *cli.Context) error {
ctx, app, err := bunapp.StartCLI(c)
ctx, err := app.Init(c)
if err != nil {
return err
}
defer app.Stop()
migrator := migrate.NewMigrator(app.DB(), migrations)
migrator := ctx.Db.GetMigrator(migrations)
group, err := migrator.Migrate(ctx, migrate.WithNopMigration())
group, err := migrator.Migrate(c.Context, migrate.WithNopMigration())
if err != nil {
return err
}

10
gallery.go

@ -1,6 +1,8 @@
package main
import (
"git.artemix.space/go/gallery/db/migrations"
"git.artemix.space/go/gallery/internal/app"
"git.artemix.space/go/gallery/internal/config"
"git.artemix.space/go/gallery/web"
"github.com/urfave/cli/v2"
@ -25,22 +27,24 @@ func main() {
Name: "start",
Usage: "Starts the HTTP server",
Action: func(c *cli.Context) error {
conf, err := config.GetConfig(c.String("config"))
ctx, err := app.Init(c)
if err != nil {
return err
}
listen, err := config.GetListener(conf.ListenAddress)
listen, err := config.GetListener(ctx.Config.ListenAddress)
if err != nil {
return err
}
handlers := web.Handlers{
Config: *conf,
Config: *ctx.Config,
Db: *ctx.Db,
}
return run(listen, handlers)
},
},
migrations.DbManagementCommands(migrations.Migrations),
},
}

33
internal/app/cli.go

@ -0,0 +1,33 @@
package app
import (
"fmt"
"git.artemix.space/go/gallery/db"
"git.artemix.space/go/gallery/internal/config"
"github.com/urfave/cli/v2"
"os"
)
type App struct {
Config *config.Config
Db *db.Database
}
func Init(c *cli.Context) (*App, error) {
conf, err := config.GetConfig(c.String("config"))
if err != nil {
return nil, err
}
dbHolder, err := db.Open(conf.DbPath)
return &App{
Config: conf,
Db: dbHolder,
}, nil
}
func (a App) Close() {
if err := a.Db.Close(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, fmt.Sprintln(err))
}
}

1
internal/config/config.go

@ -21,6 +21,7 @@ const (
type Config struct {
ListenAddress string `fig:"listen"`
DbPath string `fig:"db" default:"gallery.db"`
VerboseLogging bool `fig:"verbose"`
}

Loading…
Cancel
Save