Add a db:reset command only for dev mode

This commit is contained in:
Vikram Rangnekar 2019-11-22 00:07:06 -05:00
parent a4c09dedd5
commit 6bd27d7c79
5 changed files with 49 additions and 42 deletions

View File

@ -1358,7 +1358,7 @@ roles:
- updated_at: "now" - updated_at: "now"
delete: delete:
deny: true block: true
- name: admin - name: admin
match: id = 1000 match: id = 1000

View File

@ -110,6 +110,13 @@ e.g. db:migrate -+1
Run: cmdDBSetup, 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{ rootCmd.AddCommand(&cobra.Command{
Use: "new APP-NAME", Use: "new APP-NAME",
Short: "Create a new application", Short: "Create a new application",
@ -276,3 +283,13 @@ func initCompiler() {
logger.Fatal().Err(err).Msg("failed to initialized resolvers") 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")
}
}
}

View File

@ -36,6 +36,7 @@ var newMigrationText = `-- Write your migrate up statements here
` `
func cmdDBSetup(cmd *cobra.Command, args []string) { func cmdDBSetup(cmd *cobra.Command, args []string) {
initConfOnce()
cmdDBCreate(cmd, []string{}) cmdDBCreate(cmd, []string{})
cmdDBMigrate(cmd, []string{"up"}) 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) logger.Warn().Msgf("failed to read seed file '%s'", sfile)
} }
func cmdDBCreate(cmd *cobra.Command, args []string) { func cmdDBReset(cmd *cobra.Command, args []string) {
var err error initConfOnce()
if conf, err = initConf(); err != nil { if conf.Production {
logger.Fatal().Err(err).Msg("failed to read config") 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() ctx := context.Background()
conn, err := initDB(conf, false) conn, err := initDB(conf, false)
@ -80,12 +87,7 @@ func cmdDBCreate(cmd *cobra.Command, args []string) {
} }
func cmdDBDrop(cmd *cobra.Command, args []string) { func cmdDBDrop(cmd *cobra.Command, args []string) {
var err error initConfOnce()
if conf, err = initConf(); err != nil {
logger.Fatal().Err(err).Msg("failed to read config")
}
ctx := context.Background() ctx := context.Background()
conn, err := initDB(conf, false) conn, err := initDB(conf, false)
@ -110,12 +112,7 @@ func cmdDBNew(cmd *cobra.Command, args []string) {
os.Exit(1) os.Exit(1)
} }
var err error initConfOnce()
if conf, err = initConf(); err != nil {
logger.Fatal().Err(err).Msg("failed to read config")
}
name := args[0] name := args[0]
m, err := migrate.FindMigrations(conf.MigrationsPath) m, err := migrate.FindMigrations(conf.MigrationsPath)
@ -144,19 +141,14 @@ func cmdDBNew(cmd *cobra.Command, args []string) {
} }
func cmdDBMigrate(cmd *cobra.Command, args []string) { func cmdDBMigrate(cmd *cobra.Command, args []string) {
var err error
if len(args) == 0 { if len(args) == 0 {
cmd.Help() cmd.Help()
os.Exit(1) os.Exit(1)
} }
initConfOnce()
dest := args[0] dest := args[0]
if conf, err = initConf(); err != nil {
logger.Fatal().Err(err).Msg("failed to read config")
}
conn, err := initDB(conf, true) conn, err := initDB(conf, true)
if err != nil { if err != nil {
logger.Fatal().Err(err).Msg("failed to connect to database") 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) { func cmdDBStatus(cmd *cobra.Command, args []string) {
var err error initConfOnce()
if conf, err = initConf(); err != nil {
logger.Fatal().Err(err).Msg("failed to read config")
}
conn, err := initDB(conf, true) conn, err := initDB(conf, true)
if err != nil { if err != nil {

View File

@ -62,7 +62,7 @@ func cmdDBSeed(cmd *cobra.Command, args []string) {
func graphQLFunc(query string, data interface{}, opt map[string]string) map[string]interface{} { func graphQLFunc(query string, data interface{}, opt map[string]string) map[string]interface{} {
b, err := json.Marshal(data) b, err := json.Marshal(data)
if err != nil { if err != nil {
logger.Fatal().Err(err).Msg("failed to json serialize") panic(err)
} }
ctx := context.Background() ctx := context.Background()
@ -85,7 +85,7 @@ func graphQLFunc(query string, data interface{}, opt map[string]string) map[stri
st, err := c.buildStmtByRole(role) st, err := c.buildStmtByRole(role)
if err != nil { if err != nil {
logger.Fatal().Err(err).Msg("graphql query failed") panic(fmt.Errorf("graphql query failed: %s", err))
} }
buf := &bytes.Buffer{} 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)) _, err = t.ExecuteFunc(buf, argMap(c))
if err == errNoUserID { if err == errNoUserID {
logger.Fatal().Msg("query requires a user_id") panic(fmt.Errorf("query requires a user_id"))
} }
if err != nil { if err != nil {
logger.Fatal().Err(err).Send() panic(err)
} }
finalSQL := buf.String() finalSQL := buf.String()
tx, err := db.Begin(c) tx, err := db.Begin(c)
if err != nil { if err != nil {
logger.Fatal().Err(err).Send() panic(err)
} }
defer tx.Rollback(c) defer tx.Rollback(c)
// if err := c.setLocalUserID(tx); err != nil { if conf.DB.SetUserID {
// return nil, 0, err if err := c.setLocalUserID(tx); err != nil {
// } panic(err)
}
}
var root []byte var root []byte
if err = tx.QueryRow(c, finalSQL).Scan(&root); err != nil { 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 { if err := tx.Commit(c); err != nil {
logger.Fatal().Err(err).Send() panic(err)
} }
res, err := c.execRemoteJoin(st.qc, st.skipped, root) res, err := c.execRemoteJoin(st.qc, st.skipped, root)
if err != nil { if err != nil {
logger.Fatal().Err(err).Msg("remote join failed") panic(err)
} }
val := make(map[string]interface{}) val := make(map[string]interface{})
err = json.Unmarshal(res, &val) err = json.Unmarshal(res, &val)
if err != nil { if err != nil {
logger.Fatal().Err(err).Msg("failed to deserialize json") panic(fmt.Errorf("failed to deserialize json: %s", err))
} }
return val return val

View File

@ -3,7 +3,7 @@ host_port: 0.0.0.0:8080
web_ui: true web_ui: true
# debug, info, warn, error, fatal, panic # debug, info, warn, error, fatal, panic
log_level: "debug" log_level: "info"
# When production mode is 'true' only queries # When production mode is 'true' only queries
# from the allow list are permitted. # from the allow list are permitted.
@ -184,7 +184,7 @@ roles:
- updated_at: "now" - updated_at: "now"
delete: delete:
deny: true block: true
- name: admin - name: admin
match: id = 1000 match: id = 1000