From 6bd27d7c79d102614027ccb24f32905949f08e8c Mon Sep 17 00:00:00 2001 From: Vikram Rangnekar Date: Fri, 22 Nov 2019 00:07:06 -0500 Subject: [PATCH] Add a db:reset command only for dev mode --- docs/guide.md | 2 +- serv/cmd.go | 17 +++++++++++++++++ serv/cmd_migrate.go | 42 +++++++++++++++--------------------------- serv/cmd_seed.go | 26 ++++++++++++++------------ tmpl/dev.yml | 4 ++-- 5 files changed, 49 insertions(+), 42 deletions(-) diff --git a/docs/guide.md b/docs/guide.md index 72daf77..59039f8 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -1358,7 +1358,7 @@ roles: - updated_at: "now" delete: - deny: true + block: true - name: admin match: id = 1000 diff --git a/serv/cmd.go b/serv/cmd.go index daca612..f4dee00 100644 --- a/serv/cmd.go +++ b/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") + } + } +} diff --git a/serv/cmd_migrate.go b/serv/cmd_migrate.go index 749aef7..a14091d 100644 --- a/serv/cmd_migrate.go +++ b/serv/cmd_migrate.go @@ -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 { diff --git a/serv/cmd_seed.go b/serv/cmd_seed.go index 9c61122..db5a202 100644 --- a/serv/cmd_seed.go +++ b/serv/cmd_seed.go @@ -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 diff --git a/tmpl/dev.yml b/tmpl/dev.yml index 87b2ba8..0f20b0e 100644 --- a/tmpl/dev.yml +++ b/tmpl/dev.yml @@ -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