Skip to content

Commit

Permalink
feat: migrate for alter table miners pk
Browse files Browse the repository at this point in the history
  • Loading branch information
ta0li committed Nov 9, 2022
1 parent 9d85cad commit e0c23cd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
9 changes: 9 additions & 0 deletions storage/badger_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,5 +267,14 @@ func (s *badgerStore) MigrateToV2() error {
}
return txn.Set(storeVersionKey, version)
})
}

func (s *badgerStore) MigrateToV3() error {
return s.db.Update(func(txn *badger.Txn) error {
version, err := (&StoreVersion{ID: 1, Version: 3}).Bytes()
if err != nil {
return err
}
return txn.Set(storeVersionKey, version)
})
}
31 changes: 30 additions & 1 deletion storage/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,23 @@ func newMySQLStore(cnf *config.DBConfig) (Store, error) {
}
}

if err = session.AutoMigrate(&KeyPair{}, &User{}, &Miner{}, &Signer{}, &UserRateLimit{}, &StoreVersion{}); err != nil {
if err = session.AutoMigrate(&KeyPair{}, &User{}, &Signer{}, &UserRateLimit{}, &StoreVersion{}); err != nil {
return nil, err
}

// `miners` table changes the primary key in V1.9.0. AutoMigrate will fail, so need to handle migration independently.
if bHas := session.Migrator().HasTable(&Miner{}); !bHas {
if err := session.Migrator().CreateTable(&Miner{}); err != nil {
return nil, err
}
} else {
if bHas := session.Migrator().HasColumn(&Miner{}, "ID"); bHas {
if err := session.AutoMigrate(&Miner{}); err != nil {
return nil, err
}
}
}

return &mysqlStore{db: db}, nil
}

Expand Down Expand Up @@ -486,3 +499,19 @@ func (s *mysqlStore) MigrateToV2() error {
Clauses(clause.OnConflict{UpdateAll: true}).
Create(&StoreVersion{ID: 1, Version: 2}).Error
}

func (s *mysqlStore) MigrateToV3() error {
return s.db.Transaction(func(tx *gorm.DB) error {
if err := tx.Exec("alter table `miners` drop primary key;").Error; err != nil {
return err
}

if err := tx.Exec("alter table `miners` add column `id` bigint(20) not null auto_increment primary key first;").Error; err != nil {
return err
}

return tx.Model(&StoreVersion{}).
Clauses(clause.OnConflict{UpdateAll: true}).
Create(&StoreVersion{ID: 1, Version: 3}).Error
})
}
5 changes: 3 additions & 2 deletions storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ type Store interface {
Version() (uint64, error)
MigrateToV1() error
MigrateToV2() error
MigrateToV3() error
}

type KeyPair struct {
Expand Down Expand Up @@ -257,7 +258,7 @@ func (sa storedAddress) Value() (driver.Value, error) {

type Miner struct {
ID uint64 `gorm:"column:id;primary_key;bigint(20) unsigned AUTO_INCREMENT"`
Miner storedAddress `gorm:"column:miner;type:varchar(128);uniqueIndex:user_miner_idx,priority:2"`
Miner storedAddress `gorm:"column:miner;type:varchar(128);uniqueIndex:user_miner_idx,priority:2;NOT NULL"`
User string `gorm:"column:user;type:varchar(50);uniqueIndex:user_miner_idx,priority:1;NOT NULL"`
OpenMining *bool `gorm:"column:open_mining;default:1;comment:0-false,1-true"`
OrmTimestamp
Expand Down Expand Up @@ -286,7 +287,7 @@ func (m *Miner) setDeleted() {

type Signer struct {
ID uint64 `gorm:"column:id;primary_key;bigint(20) unsigned AUTO_INCREMENT;"`
Signer storedAddress `gorm:"column:signer;type:varchar(128);uniqueIndex:user_signer_idx,priority:2"`
Signer storedAddress `gorm:"column:signer;type:varchar(128);uniqueIndex:user_signer_idx,priority:2;NOT NULL"`
User string `gorm:"column:user;type:varchar(50);uniqueIndex:user_signer_idx,priority:1;NOT NULL"`
OrmTimestamp
}
Expand Down
1 change: 1 addition & 0 deletions storage/store_migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var migrationSchedules = map[uint64]struct {
}{
0: {from: 0, to: 1, migrate: Store.MigrateToV1},
1: {from: 1, to: 2, migrate: Store.MigrateToV2},
2: {from: 2, to: 3, migrate: Store.MigrateToV3},
}

func StoreMigrate(store Store) error {
Expand Down

0 comments on commit e0c23cd

Please sign in to comment.