Skip to content

Commit

Permalink
implement migrations down & up cli action
Browse files Browse the repository at this point in the history
  • Loading branch information
Intizar-T committed Dec 17, 2024
1 parent 160c752 commit ab26ef5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 13 deletions.
5 changes: 5 additions & 0 deletions sequencer/Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ tasks:
cmds:
- go run main.go run --cfg cfg.toml

run-migrate:
dotenv: [".env"]
cmds:
- go run main.go migrate

test-historydb:
dotenv: [".env"]
cmds:
Expand Down
4 changes: 2 additions & 2 deletions sequencer/database/historydb/historydb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestMain(m *testing.M) {
if err != nil {
panic(err)
}
historyDB = NewHistoryDB(db, db /*, nil*/)
historyDB = NewHistoryDB(db, db)
// apiConnCon := database.NewAPIConnectionController(1, time.Second)
// historyDBWithACC = NewHistoryDB(db, db, apiConnCon)

Expand Down Expand Up @@ -1004,4 +1004,4 @@ func setTestBlocks(from, to int64) []common.Block {
panic(err)
}
return blocks
}
}
2 changes: 0 additions & 2 deletions sequencer/database/migrations/0001.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ CREATE TABLE batch (
forger_addr BYTEA NOT NULL, -- fake foreign key for coordinator
-- fees_collected BYTEA NOT NULL,
-- fee_idxs_coordinator BYTEA NOT NULL,

account_root DECIMAL(78,0) NOT NULL,
vouch_root DECIMAL(78,0) NOT NULL,
score_root DECIMAL(78,0) NOT NULL,
exit_root DECIMAL(78,0) NOT NULL,

num_accounts BIGINT NOT NULL,
last_idx BIGINT NOT NULL,
forge_l1_txs_num BIGINT,
Expand Down
2 changes: 1 addition & 1 deletion sequencer/database/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func InitTestSQLDB() (*sqlx.DB, error) {
}
dbname := os.Getenv("PGDATABASE")
if dbname == "" {
dbname = "hermez"
dbname = "tokamak"
}
return InitSQLDB(port, host, user, pass, dbname)
}
Expand Down
59 changes: 52 additions & 7 deletions sequencer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"fmt"
"os"
"os/signal"
"strconv"
"tokamak-sybil-resistance/common"
"tokamak-sybil-resistance/config"
"tokamak-sybil-resistance/database"
"tokamak-sybil-resistance/log"
"tokamak-sybil-resistance/node"

Expand Down Expand Up @@ -98,7 +100,7 @@ func cmdRun(c *cli.Context) error {
}
// TODO: Initialize lof library
// log.Init(cfg.node.Log.Level, cfg.node.Log.Out)
innerNode, err := node.NewNode( /*cfg.mode, */ cfg.node, c.App.Version)
innerNode, err := node.NewNode(cfg.node, c.App.Version)
if err != nil {
return common.Wrap(fmt.Errorf("error starting node: %w", err))
}
Expand All @@ -109,17 +111,54 @@ func cmdRun(c *cli.Context) error {
return nil
}

func runMigrations(c *cli.Context) error {
fmt.Println("Running migrations")
host := os.Getenv("PGHOST")
if host == "" {
host = "localhost"
}
port, _ := strconv.Atoi(os.Getenv("PGPORT"))
if port == 0 {
port = 5432
}
user := os.Getenv("PGUSER")
if user == "" {
user = "hermez"
}
pass := os.Getenv("PGPASSWORD")
if pass == "" {
return common.Wrap(fmt.Errorf("PGPASSWORD is not set"))
}
dbname := os.Getenv("PGDATABASE")
if dbname == "" {
dbname = "tokamak"
}

db, err := database.ConnectSQLDB(port, host, user, pass, dbname)
if err != nil {
return common.Wrap(fmt.Errorf("error running migrations: %w", err))
}
defer db.Close()

if err := database.MigrationsDown(db.DB, 0); err != nil {
return common.Wrap(fmt.Errorf("error running migrations: %w", err))
}

if err := database.MigrationsUp(db.DB); err != nil {
return common.Wrap(fmt.Errorf("error running migrations: %w", err))
}

os.Exit(0)

return nil
}

func main() {
app := cli.NewApp()
app.Name = "tokamak-node"
app.Version = "v1"

flags := []cli.Flag{
// &cli.StringFlag{
// Name: flagMode,
// Usage: fmt.Sprintf("Set node `MODE` (can be \"%v\" or \"%v\")", modeSync, modeCoord),
// Required: true,
// },
&cli.StringFlag{
Name: flagCfg,
Usage: "Node configuration `FILE`",
Expand All @@ -131,10 +170,16 @@ func main() {
{
Name: "run",
Aliases: []string{},
Usage: "Run the tokamak-node", // in the indicated mode",
Usage: "Run the tokamak-node",
Action: cmdRun,
Flags: flags,
},
{
Name: "migrate",
Aliases: []string{},
Usage: "Run the migrations down & up",
Action: runMigrations,
},
}

err := app.Run(os.Args)
Expand Down
2 changes: 1 addition & 1 deletion sequencer/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func isDirectoryEmpty(path string) (bool, error) {
}

// NewNode creates a Node
func NewNode( /*mode Mode, */ cfg *config.Node, version string) (*Node, error) {
func NewNode(cfg *config.Node, version string) (*Node, error) {
meddler.Debug = cfg.Debug.MeddlerLogs
// Stablish DB connection
dbWrite, err := dbUtils.InitSQLDB(
Expand Down

0 comments on commit ab26ef5

Please sign in to comment.