From c8cc15eb35277c1629cac86f45e0d813cc08e8a0 Mon Sep 17 00:00:00 2001 From: Shane C Date: Wed, 6 Nov 2024 15:18:14 -0500 Subject: [PATCH] add db commands --- cmd/handler.go | 3 -- cmd/lock.go | 62 ++++++++++++++++++++++++++++++++ cmd/migrate.go | 66 ++++++++++++++++++++++++++++++++++ cmd/rollback.go | 67 +++++++++++++++++++++++++++++++++++ cmd/root_dev.go | 16 +++++++++ cmd/templates/handler.go.tmpl | 7 ++++ cmd/unlock.go | 52 +++++++++++++++++++++++++++ config.example.toml | 4 +++ 8 files changed, 274 insertions(+), 3 deletions(-) create mode 100644 cmd/lock.go create mode 100644 cmd/migrate.go create mode 100644 cmd/rollback.go create mode 100644 cmd/root_dev.go create mode 100644 cmd/unlock.go diff --git a/cmd/handler.go b/cmd/handler.go index 0e139d6..0dd4896 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -197,7 +197,6 @@ var handlerCmd = &cobra.Command{ if err != nil { log.Fatalln(err) } - defer viewFileOut.Close() var buffer bytes.Buffer if err := viewTempl.Execute(&buffer, tmplData); err != nil { @@ -253,7 +252,6 @@ var handlerCmd = &cobra.Command{ if err != nil { log.Fatalln(err) } - defer handlerFileOut.Close() var buffer bytes.Buffer if err := handlerTempl.Execute(&buffer, tmplData); err != nil { @@ -268,7 +266,6 @@ var handlerCmd = &cobra.Command{ if err != nil { log.Fatalln(err) } - defer handlerFileOut.Close() var buffer bytes.Buffer if err := handlerTempl.Execute(&buffer, tmplData); err != nil { diff --git a/cmd/lock.go b/cmd/lock.go new file mode 100644 index 0000000..d97296d --- /dev/null +++ b/cmd/lock.go @@ -0,0 +1,62 @@ +/* +Package cmd +Copyright © 2024 Shane C. +*/ +package cmd + +import ( + "context" + "github.com/jackc/pgx/v5/pgxpool" + "github.com/jackc/pgx/v5/stdlib" + "github.com/uptrace/bun" + "github.com/uptrace/bun/dialect/pgdialect" + "github.com/uptrace/bun/migrate" + "log" + "omnibill.net/omnibill/migrations" + "omnibill.net/omnibill/shared" + + "github.com/spf13/cobra" +) + +// lockCmd represents the lock command +var lockCmd = &cobra.Command{ + Use: "lock", + Short: "Locks DB migrations", + Run: func(cmd *cobra.Command, args []string) { + dsn := shared.GetPostgresURI() + + config, err := pgxpool.ParseConfig(dsn) + if err != nil { + log.Fatalln(err) + } + pool, err := pgxpool.NewWithConfig(context.Background(), config) + if err != nil { + log.Fatalln(err) + } + + sqldb := stdlib.OpenDBFromPool(pool) + db := bun.NewDB(sqldb, pgdialect.New()) + + migrator := migrate.NewMigrator(db, migrations.Migrations) + ctx := context.Background() + + err = migrator.Lock(ctx) + if err != nil { + log.Fatalln(err) + } + }, +} + +func init() { + migrateCmd.AddCommand(lockCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // lockCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // lockCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/migrate.go b/cmd/migrate.go new file mode 100644 index 0000000..f7dab63 --- /dev/null +++ b/cmd/migrate.go @@ -0,0 +1,66 @@ +/* +Package cmd +Copyright © 2024 Shane C. +*/ +package cmd + +import ( + "context" + "fmt" + "github.com/jackc/pgx/v5/pgxpool" + "github.com/jackc/pgx/v5/stdlib" + "github.com/spf13/cobra" + "github.com/uptrace/bun" + "github.com/uptrace/bun/dialect/pgdialect" + "github.com/uptrace/bun/migrate" + "log" + "omnibill.net/omnibill/migrations" + "omnibill.net/omnibill/shared" +) + +// migrateCmd represents the migrate command +var migrateCmd = &cobra.Command{ + Use: "migrate", + Short: "Migrate the db to the most recent version available", + Run: func(cmd *cobra.Command, args []string) { + dsn := shared.GetPostgresURI() + + config, err := pgxpool.ParseConfig(dsn) + if err != nil { + log.Fatalln(err) + } + pool, err := pgxpool.NewWithConfig(context.Background(), config) + if err != nil { + log.Fatalln(err) + } + + sqldb := stdlib.OpenDBFromPool(pool) + db := bun.NewDB(sqldb, pgdialect.New()) + + migrator := migrate.NewMigrator(db, migrations.Migrations) + ctx := context.Background() + if err := migrator.Init(ctx); err != nil { + log.Fatalln(err) + } + + if err := migrator.Lock(ctx); err != nil { + log.Fatalln(err) + + } + defer migrator.Unlock(ctx) + + group, err := migrator.Migrate(ctx) + if err != nil { + log.Fatalln(err) + } + if group.IsZero() { + fmt.Printf("there are no new migrations to run (database is up to date)\n") + return + } + fmt.Printf("migrated to %s\n", group) + }, +} + +func init() { + rootCmd.AddCommand(migrateCmd) +} diff --git a/cmd/rollback.go b/cmd/rollback.go new file mode 100644 index 0000000..dbf7704 --- /dev/null +++ b/cmd/rollback.go @@ -0,0 +1,67 @@ +/* +Package cmd +Copyright © 2024 Shane C. +*/ +package cmd + +import ( + "context" + "fmt" + "github.com/jackc/pgx/v5/pgxpool" + "github.com/jackc/pgx/v5/stdlib" + "github.com/uptrace/bun" + "github.com/uptrace/bun/dialect/pgdialect" + "github.com/uptrace/bun/migrate" + "log" + "omnibill.net/omnibill/migrations" + "omnibill.net/omnibill/shared" + + "github.com/spf13/cobra" +) + +// rollbackCmd represents the rollback command +var rollbackCmd = &cobra.Command{ + Use: "rollback", + Short: "Rollback db migrations", + Run: func(cmd *cobra.Command, args []string) { + dsn := shared.GetPostgresURI() + + config, err := pgxpool.ParseConfig(dsn) + if err != nil { + log.Fatalln(err) + } + pool, err := pgxpool.NewWithConfig(context.Background(), config) + if err != nil { + log.Fatalln(err) + } + + sqldb := stdlib.OpenDBFromPool(pool) + db := bun.NewDB(sqldb, pgdialect.New()) + + migrator := migrate.NewMigrator(db, migrations.Migrations) + ctx := context.Background() + if err := migrator.Init(ctx); err != nil { + log.Fatalln(err) + } + + if err := migrator.Lock(ctx); err != nil { + log.Fatalln(err) + + } + defer migrator.Unlock(ctx) + + group, err := migrator.Rollback(ctx) + if err != nil { + log.Fatalln(err) + } + if group.IsZero() { + fmt.Printf("there is nothing to rollback\n") + return + } + fmt.Printf("rolled back to %s\n", group) + }, +} + +func init() { + migrateCmd.AddCommand(rollbackCmd) +} diff --git a/cmd/root_dev.go b/cmd/root_dev.go new file mode 100644 index 0000000..3aee91b --- /dev/null +++ b/cmd/root_dev.go @@ -0,0 +1,16 @@ +//go:build dev + +package cmd + +import ( + "github.com/spf13/viper" + "os" +) + +func init() { + path, err := os.Getwd() + if err != nil { + panic(err) + } + viper.AddConfigPath(path) +} diff --git a/cmd/templates/handler.go.tmpl b/cmd/templates/handler.go.tmpl index fdc7f81..dfa89ce 100644 --- a/cmd/templates/handler.go.tmpl +++ b/cmd/templates/handler.go.tmpl @@ -1,3 +1,10 @@ +/* + Handler Information: + Route: /{{.Path}} + Has View:{{ if .GetView }} yes {{ else }} no {{ end }} + Needs Auth:{{ if .RequireAuth }} yes {{ else }} no {{ end }} +*/ + package {{.PackagePath}} import ( diff --git a/cmd/unlock.go b/cmd/unlock.go new file mode 100644 index 0000000..598fabf --- /dev/null +++ b/cmd/unlock.go @@ -0,0 +1,52 @@ +/* +Package cmd +Copyright © 2024 Shane C. +*/ +package cmd + +import ( + "context" + "github.com/jackc/pgx/v5/pgxpool" + "github.com/jackc/pgx/v5/stdlib" + "github.com/uptrace/bun" + "github.com/uptrace/bun/dialect/pgdialect" + "github.com/uptrace/bun/migrate" + "log" + "omnibill.net/omnibill/migrations" + "omnibill.net/omnibill/shared" + + "github.com/spf13/cobra" +) + +// unlockCmd represents the unlock command +var unlockCmd = &cobra.Command{ + Use: "unlock", + Short: "Unlocks DB migrations", + Run: func(cmd *cobra.Command, args []string) { + dsn := shared.GetPostgresURI() + + config, err := pgxpool.ParseConfig(dsn) + if err != nil { + log.Fatalln(err) + } + pool, err := pgxpool.NewWithConfig(context.Background(), config) + if err != nil { + log.Fatalln(err) + } + + sqldb := stdlib.OpenDBFromPool(pool) + db := bun.NewDB(sqldb, pgdialect.New()) + + migrator := migrate.NewMigrator(db, migrations.Migrations) + ctx := context.Background() + + err = migrator.Unlock(ctx) + if err != nil { + log.Fatalln(err) + } + }, +} + +func init() { + migrateCmd.AddCommand(unlockCmd) +} diff --git a/config.example.toml b/config.example.toml index 004ac20..ac20046 100644 --- a/config.example.toml +++ b/config.example.toml @@ -9,6 +9,10 @@ use_https = false proxy = "" port = 9000 +# Queue Settings +[omnibill.queues] +enable_ui = false + # Database Settings [omnibill.database] host = "127.0.0.1"