mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 11:26:44 +01:00
Merge pull request #439 from hermeznetwork/feature/cliwipesql
Add cli command to wipe the SQL DB
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
|||||||
ethKeystore "github.com/ethereum/go-ethereum/accounts/keystore"
|
ethKeystore "github.com/ethereum/go-ethereum/accounts/keystore"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
"github.com/hermeznetwork/hermez-node/config"
|
"github.com/hermeznetwork/hermez-node/config"
|
||||||
|
dbUtils "github.com/hermeznetwork/hermez-node/db"
|
||||||
"github.com/hermeznetwork/hermez-node/log"
|
"github.com/hermeznetwork/hermez-node/log"
|
||||||
"github.com/hermeznetwork/hermez-node/node"
|
"github.com/hermeznetwork/hermez-node/node"
|
||||||
"github.com/hermeznetwork/tracerr"
|
"github.com/hermeznetwork/tracerr"
|
||||||
@@ -19,6 +20,7 @@ const (
|
|||||||
flagCfg = "cfg"
|
flagCfg = "cfg"
|
||||||
flagMode = "mode"
|
flagMode = "mode"
|
||||||
flagSK = "privatekey"
|
flagSK = "privatekey"
|
||||||
|
flagYes = "yes"
|
||||||
modeSync = "sync"
|
modeSync = "sync"
|
||||||
modeCoord = "coord"
|
modeCoord = "coord"
|
||||||
)
|
)
|
||||||
@@ -55,6 +57,41 @@ func cmdImportKey(c *cli.Context) error {
|
|||||||
return nil
|
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
|
||||||
|
}
|
||||||
|
|
||||||
func cmdRun(c *cli.Context) error {
|
func cmdRun(c *cli.Context) error {
|
||||||
cfg, err := parseCli(c)
|
cfg, err := parseCli(c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -159,6 +196,19 @@ func main() {
|
|||||||
Required: true,
|
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,
|
||||||
|
}},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
Name: "run",
|
Name: "run",
|
||||||
Aliases: []string{},
|
Aliases: []string{},
|
||||||
|
|||||||
55
db/utils.go
55
db/utils.go
@@ -15,8 +15,43 @@ import (
|
|||||||
"github.com/russross/meddler"
|
"github.com/russross/meddler"
|
||||||
)
|
)
|
||||||
|
|
||||||
// InitSQLDB runs migrations and registers meddlers
|
var migrations *migrate.PackrMigrationSource
|
||||||
func InitSQLDB(port int, host, user, password, name string) (*sqlx.DB, error) {
|
|
||||||
|
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
|
// Init meddler
|
||||||
initMeddler()
|
initMeddler()
|
||||||
meddler.Default = meddler.PostgreSQL
|
meddler.Default = meddler.PostgreSQL
|
||||||
@@ -33,15 +68,19 @@ func InitSQLDB(port int, host, user, password, name string) (*sqlx.DB, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, tracerr.Wrap(err)
|
return nil, tracerr.Wrap(err)
|
||||||
}
|
}
|
||||||
// Run DB migrations
|
return db, nil
|
||||||
migrations := &migrate.PackrMigrationSource{
|
}
|
||||||
Box: packr.New("hermez-db-migrations", "./migrations"),
|
|
||||||
}
|
// InitSQLDB runs migrations and registers meddlers
|
||||||
nMigrations, err := migrate.Exec(db.DB, "postgres", migrations, migrate.Up)
|
func InitSQLDB(port int, host, user, password, name string) (*sqlx.DB, error) {
|
||||||
|
db, err := ConnectSQLDB(port, host, user, password, name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, tracerr.Wrap(err)
|
return nil, tracerr.Wrap(err)
|
||||||
}
|
}
|
||||||
log.Info("successfully ran ", nMigrations, " migrations")
|
// Run DB migrations
|
||||||
|
if err := MigrationsUp(db.DB); err != nil {
|
||||||
|
return nil, tracerr.Wrap(err)
|
||||||
|
}
|
||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,8 @@ package test
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/gobuffalo/packr/v2"
|
dbUtils "github.com/hermeznetwork/hermez-node/db"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
migrate "github.com/rubenv/sql-migrate"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -28,15 +27,10 @@ func AssertUSD(t *testing.T, expected, actual *float64) {
|
|||||||
// WipeDB redo all the migrations of the SQL DB (HistoryDB and L2DB),
|
// WipeDB redo all the migrations of the SQL DB (HistoryDB and L2DB),
|
||||||
// efectively recreating the original state
|
// efectively recreating the original state
|
||||||
func WipeDB(db *sqlx.DB) {
|
func WipeDB(db *sqlx.DB) {
|
||||||
migrations := &migrate.PackrMigrationSource{
|
if err := dbUtils.MigrationsDown(db.DB); err != nil {
|
||||||
Box: packr.New("hermez-db-migrations", "../db/migrations"),
|
|
||||||
}
|
|
||||||
_, err := migrate.Exec(db.DB, "postgres", migrations, migrate.Down)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
_, err = migrate.Exec(db.DB, "postgres", migrations, migrate.Up)
|
if err := dbUtils.MigrationsUp(db.DB); err != nil {
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user