Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: skip set address in migration if PieceTracker table has no rows #1614

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions extern/boostd-data/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,8 @@ var yugabyteMigrateCmd = &cli.Command{
Required: true,
},
&cli.StringFlag{
Name: "miner-address",
Usage: "default miner address eg f1234",
Required: true,
Name: "miner-address",
Usage: "default miner address eg f1234",
},
},
Action: func(cctx *cli.Context) error {
Expand All @@ -197,11 +196,21 @@ var yugabyteMigrateCmd = &cli.Command{
ConnectString: cctx.String("connect-string"),
}

maddr, err := address.NewFromString(cctx.String("miner-address"))
if err != nil {
return fmt.Errorf("parsing miner address '%s': %w", maddr, err)
maddr := migrations.DisabledMinerAddr
if cctx.IsSet("miner-address") {
var err error
maddr, err = address.NewFromString(cctx.String("miner-address"))
if err != nil {
return fmt.Errorf("parsing miner address '%s': %w", maddr, err)
}
}
migrator := yugabyte.NewMigrator(settings.ConnectString, maddr)
return migrator.Migrate()
err := migrator.Migrate()
if err != nil && errors.Is(err, migrations.ErrMissingMinerAddr) {
msg := "You must set the miner-address flag to do the migration. " +
"Set it to the address of the storage miner eg f1234"
return fmt.Errorf(msg)
}
return err
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,40 @@ var ErrMissingMinerAddr = errors.New("cannot perform migration: miner address ha
// however due to a bug in the migration library we have to do this in a
// separate migration (20230810151349_piece_tracker_add_maddr_pk)
func upAddMinerAddr(ctx context.Context, tx *sql.Tx) error {
if migrationParams.MinerAddress == DisabledMinerAddr {
// Check if there are any rows in the PieceTracker table.
// If so, then after we add the MinerAddr column we need to set it to a
// default value.
// Note that if we perform a SELECT with the Transaction's connection it
// causes problems with subsequent operations on the Transaction. So instead
// we need to open a new connection just for the SELECT.
var count int
sqldb, err := sql.Open("postgres", migrationParams.ConnectString)
if err != nil {
return fmt.Errorf("opening postgres connection to %s: %w", migrationParams.ConnectString, err)
}
err = sqldb.QueryRowContext(ctx, "SELECT COUNT(*) as cnt FROM PieceTracker").Scan(&count)
if err != nil {
return err
}

if count > 0 && migrationParams.MinerAddress == DisabledMinerAddr {
return ErrMissingMinerAddr
}
minerAddr := migrationParams.MinerAddress.String()

_, err := tx.ExecContext(ctx, "ALTER TABLE PieceTracker DROP CONSTRAINT piecetracker_pkey")
_, err = tx.ExecContext(ctx, "ALTER TABLE PieceTracker DROP CONSTRAINT piecetracker_pkey")
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, "ALTER TABLE PieceTracker ADD COLUMN MinerAddr TEXT NOT NULL DEFAULT ''")
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, fmt.Sprintf("UPDATE PieceTracker SET MinerAddr='%s'", minerAddr))
if err != nil {
return err
if count > 0 {
minerAddr := migrationParams.MinerAddress.String()
_, err = tx.ExecContext(ctx, fmt.Sprintf("UPDATE PieceTracker SET MinerAddr='%s'", minerAddr))
if err != nil {
return err
}
}

_, err = tx.ExecContext(ctx, "ALTER TABLE PieceFlagged DROP CONSTRAINT pieceflagged_pkey")
Expand All @@ -62,11 +80,13 @@ func upAddMinerAddr(ctx context.Context, tx *sql.Tx) error {
if err != nil {
return err
}
_, err = tx.ExecContext(ctx, fmt.Sprintf("UPDATE PieceFlagged SET MinerAddr='%s'", minerAddr))
if err != nil {
return err
if count > 0 {
minerAddr := migrationParams.MinerAddress.String()
_, err = tx.ExecContext(ctx, fmt.Sprintf("UPDATE PieceFlagged SET MinerAddr='%s'", minerAddr))
if err != nil {
return err
}
}

// Note that due to a bug in the migration library, we add the primary
// key back in the next migration: 20230810151349_piece_tracker_add_maddr_pk

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package migrations
import (
"context"
"database/sql"
"fmt"
"github.com/pressly/goose/v3"
)

Expand All @@ -13,18 +12,12 @@ func init() {

// Add a primary key constraint to the PieceTracker & PieceFlagged tables
func upPieceTrackerAddPk(ctx context.Context, tx *sql.Tx) error {
exec := func(cmd string, args ...interface{}) error {
fmt.Println(cmd, args)
_, err := tx.ExecContext(ctx, cmd, args...)
return err
}

err := exec("ALTER TABLE PieceTracker ADD CONSTRAINT piecetracker_pkey PRIMARY KEY (MinerAddr, PieceCid)")
_, err := tx.ExecContext(ctx, "ALTER TABLE PieceTracker ADD CONSTRAINT piecetracker_pkey PRIMARY KEY (MinerAddr, PieceCid)")
if err != nil {
return err
}

err = exec("ALTER TABLE PieceFlagged ADD CONSTRAINT pieceflagged_pkey PRIMARY KEY (MinerAddr, PieceCid)")
_, err = tx.ExecContext(ctx, "ALTER TABLE PieceFlagged ADD CONSTRAINT pieceflagged_pkey PRIMARY KEY (MinerAddr, PieceCid)")
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion extern/boostd-data/yugabyte/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var EmbedMigrations embed.FS

// Used to pass global parameters to the migration functions
type MigrateParams struct {
MinerAddress address.Address
ConnectString string
MinerAddress address.Address
}

var migrationParams *MigrateParams
Expand Down
2 changes: 1 addition & 1 deletion extern/boostd-data/yugabyte/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (m *Migrator) Migrate() error {
return fmt.Errorf("opening postgres connection to %s: %w", m.connectString, err)
}

err = migrations.Migrate(sqldb, migrations.MigrateParams{MinerAddress: m.minerAddr})
err = migrations.Migrate(sqldb, migrations.MigrateParams{ConnectString: m.connectString, MinerAddress: m.minerAddr})
if err != nil {
return fmt.Errorf("running postgres migrations: %w", err)
}
Expand Down