add db commands

This commit is contained in:
Shane C. 2024-11-06 15:18:14 -05:00
parent b1f6175226
commit c8cc15eb35
Signed by: Shane C.
GPG key ID: E46B5FEA35B22FF9
8 changed files with 274 additions and 3 deletions

View file

@ -197,7 +197,6 @@ var handlerCmd = &cobra.Command{
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
defer viewFileOut.Close()
var buffer bytes.Buffer var buffer bytes.Buffer
if err := viewTempl.Execute(&buffer, tmplData); err != nil { if err := viewTempl.Execute(&buffer, tmplData); err != nil {
@ -253,7 +252,6 @@ var handlerCmd = &cobra.Command{
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
defer handlerFileOut.Close()
var buffer bytes.Buffer var buffer bytes.Buffer
if err := handlerTempl.Execute(&buffer, tmplData); err != nil { if err := handlerTempl.Execute(&buffer, tmplData); err != nil {
@ -268,7 +266,6 @@ var handlerCmd = &cobra.Command{
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
defer handlerFileOut.Close()
var buffer bytes.Buffer var buffer bytes.Buffer
if err := handlerTempl.Execute(&buffer, tmplData); err != nil { if err := handlerTempl.Execute(&buffer, tmplData); err != nil {

62
cmd/lock.go Normal file
View file

@ -0,0 +1,62 @@
/*
Package cmd
Copyright © 2024 Shane C. <shane@scaffoe.com>
*/
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")
}

66
cmd/migrate.go Normal file
View file

@ -0,0 +1,66 @@
/*
Package cmd
Copyright © 2024 Shane C. <shane@scaffoe.com>
*/
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)
}

67
cmd/rollback.go Normal file
View file

@ -0,0 +1,67 @@
/*
Package cmd
Copyright © 2024 Shane C. <shane@scaffoe.com>
*/
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)
}

16
cmd/root_dev.go Normal file
View file

@ -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)
}

View file

@ -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}} package {{.PackagePath}}
import ( import (

52
cmd/unlock.go Normal file
View file

@ -0,0 +1,52 @@
/*
Package cmd
Copyright © 2024 Shane C. <shane@scaffoe.com>
*/
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)
}

View file

@ -9,6 +9,10 @@ use_https = false
proxy = "" proxy = ""
port = 9000 port = 9000
# Queue Settings
[omnibill.queues]
enable_ui = false
# Database Settings # Database Settings
[omnibill.database] [omnibill.database]
host = "127.0.0.1" host = "127.0.0.1"