-
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: provide CLI app in a standalone package
Users can start the tool from their own entrypoints by using buncli.New/Run etc. They are also responsible for configuring the DB connection and AutoMigrator. bundb/bunctl will eventually use FromPlugin() to read config from a pre-built plugin.
Showing
7 changed files
with
181 additions
and
354 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package buncli | ||
|
||
import ( | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func CmdAuto(c *Config) *cli.Command { | ||
return &cli.Command{ | ||
Name: "auto", | ||
Usage: "Manage database schema with AutoMigrator", | ||
Subcommands: cli.Commands{ | ||
&cli.Command{ | ||
Name: "create", | ||
Usage: "Generate SQL migration files", | ||
Flags: []cli.Flag{ | ||
flagTx, | ||
}, | ||
Action: func(ctx *cli.Context) error { | ||
return autoCreate(ctx, c) | ||
}, | ||
}, | ||
&cli.Command{ | ||
Name: "migrate", | ||
Usage: "Generate SQL migrations and apply them right away", | ||
Action: func(ctx *cli.Context) error { | ||
return autoMigrate(ctx, c) | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
var ( | ||
// flagTx adds --transactional flag. | ||
flagTx = &cli.BoolFlag{ | ||
Name: "tx", | ||
Aliases: []string{"transactional"}, | ||
Usage: "write migrations to .tx.(up|down).sql file, they will be marked as transactional", | ||
Value: false, | ||
} | ||
) | ||
|
||
func autoMigrate(ctx *cli.Context, c *Config) error { | ||
_, err := c.AutoMigrator.Migrate(ctx.Context) | ||
return err | ||
} | ||
|
||
func autoCreate(ctx *cli.Context, c *Config) error { | ||
var err error | ||
if flagTx.Get(ctx) { | ||
_, err = c.AutoMigrator.CreateTxSQLMigrations(ctx.Context) | ||
} else { | ||
_, err = c.AutoMigrator.CreateSQLMigrations(ctx.Context) | ||
} | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
TODO: | ||
- Commands: | ||
- init - Create migration+locks tables [--cmd to add cmd/ folder structure] | ||
- migrate - Apply database migrations | ||
- rollback - Rollback the last migration group | ||
- create - Create template SQL migration filex [--go | --sql | --transactional] | ||
- unlock - Unlock locks table | ||
- provide NewCommand() *cli.Command intead of the cli.App, so that buncli could be embeded in the existing CLIs | ||
- configure logging and verbosity | ||
- (experimental, low prio) add FromPlugin() to read config from plugin and use from cmd/bundb. | ||
*/ | ||
package buncli | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/uptrace/bun" | ||
"github.com/uptrace/bun/migrate" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// bunApp is the root-level bundb app that all other commands attach to. | ||
var bunApp = &cli.App{ | ||
Name: "bundb", | ||
Usage: "Database migration tool for uptrace/bun", | ||
} | ||
|
||
// New creates a new CLI application for managing bun migrations. | ||
func New(c *Config) *App { | ||
bunApp.Commands = cli.Commands{ | ||
CmdAuto(c), | ||
} | ||
return &App{ | ||
App: bunApp, | ||
} | ||
} | ||
|
||
type Config struct { | ||
DB *bun.DB | ||
AutoMigrator *migrate.AutoMigrator | ||
} | ||
|
||
// Run calls cli.App.Run and returns its error. | ||
func Run(args []string, c *Config) error { | ||
return New(c).Run(args) | ||
} | ||
|
||
// RunCtx calls cli.App.RunContexta and returns its error. | ||
func RunContext(ctx context.Context, args []string, c *Config) error { | ||
return New(c).RunContext(ctx, args) | ||
} | ||
|
||
// App is a wrapper around cli.App that extends it with bun-specific features. | ||
type App struct { | ||
*cli.App | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module github.com/uptrace/bun/extra/buncli | ||
|
||
go 1.22.0 | ||
|
||
require ( | ||
github.com/uptrace/bun v1.2.7-0.20241125022320-89e9d5169f8a | ||
github.com/urfave/cli/v2 v2.27.5 | ||
) | ||
|
||
require ( | ||
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect | ||
github.com/jinzhu/inflection v1.0.0 // indirect | ||
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect | ||
github.com/russross/blackfriday/v2 v2.1.0 // indirect | ||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect | ||
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect | ||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect | ||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect | ||
golang.org/x/sys v0.27.0 // indirect | ||
) | ||
|
||
replace github.com/uptrace/bun => ../.. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc= | ||
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= | ||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4= | ||
github.com/puzpuzpuz/xsync/v3 v3.4.0/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA= | ||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= | ||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | ||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= | ||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= | ||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo= | ||
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs= | ||
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w= | ||
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ= | ||
github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IUPn0Bjt8= | ||
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok= | ||
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= | ||
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= | ||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4= | ||
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM= | ||
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= | ||
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters