Add a db:reset command only for dev mode
This commit is contained in:
parent
a4c09dedd5
commit
6bd27d7c79
|
@ -1358,7 +1358,7 @@ roles:
|
|||
- updated_at: "now"
|
||||
|
||||
delete:
|
||||
deny: true
|
||||
block: true
|
||||
|
||||
- name: admin
|
||||
match: id = 1000
|
||||
|
|
17
serv/cmd.go
17
serv/cmd.go
|
@ -110,6 +110,13 @@ e.g. db:migrate -+1
|
|||
Run: cmdDBSetup,
|
||||
})
|
||||
|
||||
rootCmd.AddCommand(&cobra.Command{
|
||||
Use: "db:reset",
|
||||
Short: "Reset database",
|
||||
Long: "This command will drop, create, migrate and seed the database (won't run in production)",
|
||||
Run: cmdDBReset,
|
||||
})
|
||||
|
||||
rootCmd.AddCommand(&cobra.Command{
|
||||
Use: "new APP-NAME",
|
||||
Short: "Create a new application",
|
||||
|
@ -276,3 +283,13 @@ func initCompiler() {
|
|||
logger.Fatal().Err(err).Msg("failed to initialized resolvers")
|
||||
}
|
||||
}
|
||||
|
||||
func initConfOnce() {
|
||||
var err error
|
||||
|
||||
if conf == nil {
|
||||
if conf, err = initConf(); err != nil {
|
||||
logger.Fatal().Err(err).Msg("failed to read config")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,6 +36,7 @@ var newMigrationText = `-- Write your migrate up statements here
|
|||
`
|
||||
|
||||
func cmdDBSetup(cmd *cobra.Command, args []string) {
|
||||
initConfOnce()
|
||||
cmdDBCreate(cmd, []string{})
|
||||
cmdDBMigrate(cmd, []string{"up"})
|
||||
|
||||
|
@ -54,13 +55,19 @@ func cmdDBSetup(cmd *cobra.Command, args []string) {
|
|||
logger.Warn().Msgf("failed to read seed file '%s'", sfile)
|
||||
}
|
||||
|
||||
func cmdDBCreate(cmd *cobra.Command, args []string) {
|
||||
var err error
|
||||
func cmdDBReset(cmd *cobra.Command, args []string) {
|
||||
initConfOnce()
|
||||
|
||||
if conf, err = initConf(); err != nil {
|
||||
logger.Fatal().Err(err).Msg("failed to read config")
|
||||
if conf.Production {
|
||||
logger.Fatal().Msg("db:reset does not work in production")
|
||||
return
|
||||
}
|
||||
cmdDBDrop(cmd, []string{})
|
||||
cmdDBSetup(cmd, []string{})
|
||||
}
|
||||
|
||||
func cmdDBCreate(cmd *cobra.Command, args []string) {
|
||||
initConfOnce()
|
||||
ctx := context.Background()
|
||||
|
||||
conn, err := initDB(conf, false)
|
||||
|
@ -80,12 +87,7 @@ func cmdDBCreate(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
|
||||
func cmdDBDrop(cmd *cobra.Command, args []string) {
|
||||
var err error
|
||||
|
||||
if conf, err = initConf(); err != nil {
|
||||
logger.Fatal().Err(err).Msg("failed to read config")
|
||||
}
|
||||
|
||||
initConfOnce()
|
||||
ctx := context.Background()
|
||||
|
||||
conn, err := initDB(conf, false)
|
||||
|
@ -110,12 +112,7 @@ func cmdDBNew(cmd *cobra.Command, args []string) {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
var err error
|
||||
|
||||
if conf, err = initConf(); err != nil {
|
||||
logger.Fatal().Err(err).Msg("failed to read config")
|
||||
}
|
||||
|
||||
initConfOnce()
|
||||
name := args[0]
|
||||
|
||||
m, err := migrate.FindMigrations(conf.MigrationsPath)
|
||||
|
@ -144,19 +141,14 @@ func cmdDBNew(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
|
||||
func cmdDBMigrate(cmd *cobra.Command, args []string) {
|
||||
var err error
|
||||
|
||||
if len(args) == 0 {
|
||||
cmd.Help()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
initConfOnce()
|
||||
dest := args[0]
|
||||
|
||||
if conf, err = initConf(); err != nil {
|
||||
logger.Fatal().Err(err).Msg("failed to read config")
|
||||
}
|
||||
|
||||
conn, err := initDB(conf, true)
|
||||
if err != nil {
|
||||
logger.Fatal().Err(err).Msg("failed to connect to database")
|
||||
|
@ -251,11 +243,7 @@ func cmdDBMigrate(cmd *cobra.Command, args []string) {
|
|||
}
|
||||
|
||||
func cmdDBStatus(cmd *cobra.Command, args []string) {
|
||||
var err error
|
||||
|
||||
if conf, err = initConf(); err != nil {
|
||||
logger.Fatal().Err(err).Msg("failed to read config")
|
||||
}
|
||||
initConfOnce()
|
||||
|
||||
conn, err := initDB(conf, true)
|
||||
if err != nil {
|
||||
|
|
|
@ -62,7 +62,7 @@ func cmdDBSeed(cmd *cobra.Command, args []string) {
|
|||
func graphQLFunc(query string, data interface{}, opt map[string]string) map[string]interface{} {
|
||||
b, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
logger.Fatal().Err(err).Msg("failed to json serialize")
|
||||
panic(err)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
@ -85,7 +85,7 @@ func graphQLFunc(query string, data interface{}, opt map[string]string) map[stri
|
|||
|
||||
st, err := c.buildStmtByRole(role)
|
||||
if err != nil {
|
||||
logger.Fatal().Err(err).Msg("graphql query failed")
|
||||
panic(fmt.Errorf("graphql query failed: %s", err))
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
|
@ -94,45 +94,47 @@ func graphQLFunc(query string, data interface{}, opt map[string]string) map[stri
|
|||
_, err = t.ExecuteFunc(buf, argMap(c))
|
||||
|
||||
if err == errNoUserID {
|
||||
logger.Fatal().Msg("query requires a user_id")
|
||||
panic(fmt.Errorf("query requires a user_id"))
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
logger.Fatal().Err(err).Send()
|
||||
panic(err)
|
||||
}
|
||||
|
||||
finalSQL := buf.String()
|
||||
|
||||
tx, err := db.Begin(c)
|
||||
if err != nil {
|
||||
logger.Fatal().Err(err).Send()
|
||||
panic(err)
|
||||
}
|
||||
defer tx.Rollback(c)
|
||||
|
||||
// if err := c.setLocalUserID(tx); err != nil {
|
||||
// return nil, 0, err
|
||||
// }
|
||||
if conf.DB.SetUserID {
|
||||
if err := c.setLocalUserID(tx); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
var root []byte
|
||||
|
||||
if err = tx.QueryRow(c, finalSQL).Scan(&root); err != nil {
|
||||
logger.Fatal().Err(err).Msg("sql query failed")
|
||||
panic(fmt.Errorf("sql query failed: %s", err))
|
||||
}
|
||||
|
||||
if err := tx.Commit(c); err != nil {
|
||||
logger.Fatal().Err(err).Send()
|
||||
panic(err)
|
||||
}
|
||||
|
||||
res, err := c.execRemoteJoin(st.qc, st.skipped, root)
|
||||
if err != nil {
|
||||
logger.Fatal().Err(err).Msg("remote join failed")
|
||||
panic(err)
|
||||
}
|
||||
|
||||
val := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(res, &val)
|
||||
if err != nil {
|
||||
logger.Fatal().Err(err).Msg("failed to deserialize json")
|
||||
panic(fmt.Errorf("failed to deserialize json: %s", err))
|
||||
}
|
||||
|
||||
return val
|
||||
|
|
|
@ -3,7 +3,7 @@ host_port: 0.0.0.0:8080
|
|||
web_ui: true
|
||||
|
||||
# debug, info, warn, error, fatal, panic
|
||||
log_level: "debug"
|
||||
log_level: "info"
|
||||
|
||||
# When production mode is 'true' only queries
|
||||
# from the allow list are permitted.
|
||||
|
@ -184,7 +184,7 @@ roles:
|
|||
- updated_at: "now"
|
||||
|
||||
delete:
|
||||
deny: true
|
||||
block: true
|
||||
|
||||
- name: admin
|
||||
match: id = 1000
|
||||
|
|
Loading…
Reference in New Issue