diff --git a/cmd/migrate/cmd.go b/cmd/migrate/cmd.go index e5c2b67f5..bd6903696 100644 --- a/cmd/migrate/cmd.go +++ b/cmd/migrate/cmd.go @@ -9,6 +9,7 @@ import ( v3 "github.com/forbole/callisto/v4/cmd/migrate/v3" v5 "github.com/forbole/callisto/v4/cmd/migrate/v5" + v6 "github.com/forbole/callisto/v4/cmd/migrate/v6" ) type Migrator func(parseCfg *parsecmdtypes.Config) error @@ -17,6 +18,7 @@ var ( migrations = map[string]Migrator{ "v3": v3.RunMigration, "v5": v5.RunMigration, + "v6": v6.RunMigration, } ) diff --git a/cmd/migrate/v6/migrate.go b/cmd/migrate/v6/migrate.go new file mode 100644 index 000000000..e417f3ff8 --- /dev/null +++ b/cmd/migrate/v6/migrate.go @@ -0,0 +1,43 @@ +package v6 + +import ( + "fmt" + + v6db "github.com/forbole/callisto/v4/database/migrate/v6" + parse "github.com/forbole/juno/v5/cmd/parse/types" + "github.com/forbole/juno/v5/database" + "github.com/forbole/juno/v5/database/postgresql" + "github.com/forbole/juno/v5/types/config" +) + +// RunMigration runs the migrations to v5 +func RunMigration(parseConfig *parse.Config) error { + cfg, err := GetConfig() + if err != nil { + return fmt.Errorf("error while reading config: %s", err) + } + + // Migrate the database + err = migrateDb(cfg, parseConfig) + if err != nil { + return fmt.Errorf("error while migrating database: %s", err) + } + + return nil +} + +func migrateDb(cfg config.Config, parseConfig *parse.Config) error { + // Build the codec + encodingConfig := parseConfig.GetEncodingConfigBuilder()() + + // Get the db + databaseCtx := database.NewContext(cfg.Database, encodingConfig, parseConfig.GetLogger()) + db, err := postgresql.Builder(databaseCtx) + if err != nil { + return fmt.Errorf("error while building the db: %s", err) + } + + // Build the migrator and perform the migrations + migrator := v6db.NewMigrator(db.(*postgresql.Database)) + return migrator.Migrate() +} diff --git a/cmd/migrate/v6/utils.go b/cmd/migrate/v6/utils.go new file mode 100644 index 000000000..8a08bc526 --- /dev/null +++ b/cmd/migrate/v6/utils.go @@ -0,0 +1,29 @@ +package v6 + +import ( + "fmt" + "os" + "path" + + "github.com/forbole/juno/v5/types/config" + "gopkg.in/yaml.v3" +) + +// GetConfig returns the configuration reading it from the config.yaml file present inside the home directory +func GetConfig() (config.Config, error) { + file := path.Join(config.HomePath, "config.yaml") + + // Make sure the path exists + if _, err := os.Stat(file); os.IsNotExist(err) { + return config.Config{}, fmt.Errorf("config file does not exist") + } + + bz, err := os.ReadFile(file) + if err != nil { + return config.Config{}, fmt.Errorf("error while reading config file: %s", err) + } + + var cfg config.Config + err = yaml.Unmarshal(bz, &cfg) + return cfg, err +} diff --git a/database/migrate/v6/migrate.go b/database/migrate/v6/migrate.go new file mode 100644 index 000000000..d3de525bb --- /dev/null +++ b/database/migrate/v6/migrate.go @@ -0,0 +1,39 @@ +package v6 + +// Migrate implements database.Migrator +func (db *Migrator) Migrate() error { + stmt := ` +BEGIN; + +DROP TABLE gov_params; +CREATE TABLE gov_params +( + one_row_id BOOLEAN NOT NULL DEFAULT TRUE PRIMARY KEY, + params JSONB NOT NULL, + height BIGINT NOT NULL, + CHECK (one_row_id) +); + +ALTER TABLE proposal ADD COLUMN metadata TEXT NOT NULL DEFAULT ''; +ALTER TABLE proposal DROP COLUMN proposal_route; +ALTER TABLE proposal DROP COLUMN proposal_type; + +ALTER TABLE proposal_deposit ADD COLUMN transaction_hash TEXT NOT NULL DEFAULT ''; +ALTER TABLE proposal_deposit DROP CONSTRAINT unique_deposit; +ALTER TABLE proposal_deposit ADD CONSTRAINT unique_deposit UNIQUE (proposal_id, depositor_address, transaction_hash); + +ALTER TABLE proposal_vote ADD COLUMN weight TEXT NOT NULL DEFAULT '1.0'; + +ALTER TABLE proposal_vote DROP CONSTRAINT unique_vote; +ALTER TABLE proposal_vote ADD CONSTRAINT unique_vote UNIQUE (proposal_id, voter_address, option); + +ALTER TABLE validator_voting_power ALTER COLUMN voting_power TYPE BIGINT USING voting_power::BIGINT; +ALTER TABLE proposal_validator_status_snapshot ALTER COLUMN voting_power TYPE BIGINT USING voting_power::BIGINT; + +COMMIT; +` + + _, err := db.SQL.Exec(stmt) + + return err +} diff --git a/database/migrate/v6/migrator.go b/database/migrate/v6/migrator.go new file mode 100644 index 000000000..41451f969 --- /dev/null +++ b/database/migrate/v6/migrator.go @@ -0,0 +1,21 @@ +package v6 + +import ( + "github.com/jmoiron/sqlx" + + "github.com/forbole/juno/v5/database" + "github.com/forbole/juno/v5/database/postgresql" +) + +var _ database.Migrator = &Migrator{} + +// Migrator represents the database migrator that should be used to migrate from v4 of the database to v5 +type Migrator struct { + SQL *sqlx.DB +} + +func NewMigrator(db *postgresql.Database) *Migrator { + return &Migrator{ + SQL: db.SQL, + } +}