arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
Add cli command to wipe the SQL DB

- Refactor the migrations code so that packr is only called (via an init
  function in `db/utils.go`).
- When loading the migrations, make sure there is at least one migration,
  otherwise panic (this would happen if the node is built incorrectly)
Eduard S eduard@iden3.io
Wed, 30 Dec 2020 11:53:14 +0100
commit

e5fc403451558809792b71407d5a50a64c4ba460

parent

2e51860c37d7a51493e7903db79720fa8692d41d

3 files changed, 100 insertions(+), 17 deletions(-)

jump to
M cli/node/main.go → cli/node/main.go

@@ -9,6 +9,7 @@

ethKeystore "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/crypto" "github.com/hermeznetwork/hermez-node/config" + dbUtils "github.com/hermeznetwork/hermez-node/db" "github.com/hermeznetwork/hermez-node/log" "github.com/hermeznetwork/hermez-node/node" "github.com/hermeznetwork/tracerr"

@@ -19,6 +20,7 @@ const (

flagCfg = "cfg" flagMode = "mode" flagSK = "privatekey" + flagYes = "yes" modeSync = "sync" modeCoord = "coord" )

@@ -52,6 +54,41 @@ if err != nil {

return tracerr.Wrap(err) } log.Infow("Imported private key", "addr", acc.Address.Hex()) + return nil +} + +func cmdWipeSQL(c *cli.Context) error { + _cfg, err := parseCli(c) + if err != nil { + return tracerr.Wrap(fmt.Errorf("error parsing flags and config: %w", err)) + } + cfg := _cfg.node + yes := c.Bool(flagYes) + if !yes { + fmt.Print("*WARNING* Are you sure you want to delete the SQL DB? [y/N]: ") + var input string + if _, err := fmt.Scanln(&input); err != nil { + return tracerr.Wrap(err) + } + input = strings.ToLower(input) + if !(input == "y" || input == "yes") { + return nil + } + } + db, err := dbUtils.ConnectSQLDB( + cfg.PostgreSQL.Port, + cfg.PostgreSQL.Host, + cfg.PostgreSQL.User, + cfg.PostgreSQL.Password, + cfg.PostgreSQL.Name, + ) + if err != nil { + return tracerr.Wrap(err) + } + log.Info("Wiping SQL DB...") + if err := dbUtils.MigrationsDown(db.DB); err != nil { + return tracerr.Wrap(err) + } return nil }

@@ -157,6 +194,19 @@ &cli.StringFlag{

Name: flagSK, Usage: "ethereum `PRIVATE_KEY` in hex", Required: true, + }}, + }, + { + Name: "wipesql", + Aliases: []string{}, + Usage: "Wipe the SQL DB (HistoryDB and L2DB), " + + "leaving the DB in a clean state", + Action: cmdWipeSQL, + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: flagYes, + Usage: "automatic yes to the prompt", + Required: false, }}, }, {
M db/utils.go → db/utils.go

@@ -15,8 +15,43 @@ migrate "github.com/rubenv/sql-migrate"

"github.com/russross/meddler" ) -// InitSQLDB runs migrations and registers meddlers -func InitSQLDB(port int, host, user, password, name string) (*sqlx.DB, error) { +var migrations *migrate.PackrMigrationSource + +func init() { + migrations = &migrate.PackrMigrationSource{ + Box: packr.New("hermez-db-migrations", "./migrations"), + } + ms, err := migrations.FindMigrations() + if err != nil { + panic(err) + } + if len(ms) == 0 { + panic(fmt.Errorf("no SQL migrations found")) + } +} + +// MigrationsUp runs the SQL migrations Up +func MigrationsUp(db *sql.DB) error { + nMigrations, err := migrate.Exec(db, "postgres", migrations, migrate.Up) + if err != nil { + return tracerr.Wrap(err) + } + log.Info("successfully ran ", nMigrations, " migrations Up") + return nil +} + +// MigrationsDown runs the SQL migrations Down +func MigrationsDown(db *sql.DB) error { + nMigrations, err := migrate.Exec(db, "postgres", migrations, migrate.Down) + if err != nil { + return tracerr.Wrap(err) + } + log.Info("successfully ran ", nMigrations, " migrations Down") + return nil +} + +// ConnectSQLDB connects to the SQL DB +func ConnectSQLDB(port int, host, user, password, name string) (*sqlx.DB, error) { // Init meddler initMeddler() meddler.Default = meddler.PostgreSQL

@@ -33,15 +68,19 @@ db, err := sqlx.Connect("postgres", psqlconn)

if err != nil { return nil, tracerr.Wrap(err) } + return db, nil +} + +// InitSQLDB runs migrations and registers meddlers +func InitSQLDB(port int, host, user, password, name string) (*sqlx.DB, error) { + db, err := ConnectSQLDB(port, host, user, password, name) + if err != nil { + return nil, tracerr.Wrap(err) + } // Run DB migrations - migrations := &migrate.PackrMigrationSource{ - Box: packr.New("hermez-db-migrations", "./migrations"), - } - nMigrations, err := migrate.Exec(db.DB, "postgres", migrations, migrate.Up) - if err != nil { + if err := MigrationsUp(db.DB); err != nil { return nil, tracerr.Wrap(err) } - log.Info("successfully ran ", nMigrations, " migrations") return db, nil }
M test/dbUtils.go → test/dbUtils.go

@@ -3,9 +3,8 @@

import ( "testing" - "github.com/gobuffalo/packr/v2" + dbUtils "github.com/hermeznetwork/hermez-node/db" "github.com/jmoiron/sqlx" - migrate "github.com/rubenv/sql-migrate" "github.com/stretchr/testify/assert" )

@@ -28,15 +27,10 @@

// WipeDB redo all the migrations of the SQL DB (HistoryDB and L2DB), // efectively recreating the original state func WipeDB(db *sqlx.DB) { - migrations := &migrate.PackrMigrationSource{ - Box: packr.New("hermez-db-migrations", "../db/migrations"), - } - _, err := migrate.Exec(db.DB, "postgres", migrations, migrate.Down) - if err != nil { + if err := dbUtils.MigrationsDown(db.DB); err != nil { panic(err) } - _, err = migrate.Exec(db.DB, "postgres", migrations, migrate.Up) - if err != nil { + if err := dbUtils.MigrationsUp(db.DB); err != nil { panic(err) } }