/
migrate.go
60 lines (52 loc) · 1.86 KB
/
migrate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package migration1568280052
import (
"github.com/smartcontractkit/chainlink/core/store/migrations/migration1565877314"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
)
type ExternalInitiator struct {
*gorm.Model
Name string `gorm:"not null;unique"`
URL models.WebURL `gorm:"not null"`
AccessKey string `gorm:"not null"`
Salt string `gorm:"not null"`
HashedSecret string `gorm:"not null"`
OutgoingSecret string `gorm:"not null"`
OutgoingToken string `gorm:"not null"`
}
// newExternalInitiator returns a copy of the old struct with the fields untouched.
func newExternalInitiator(arg migration1565877314.ExternalInitiator) ExternalInitiator {
return ExternalInitiator{
Model: arg.Model,
Name: arg.AccessKey,
URL: arg.URL,
AccessKey: arg.AccessKey,
Salt: arg.Salt,
HashedSecret: arg.HashedSecret,
OutgoingSecret: arg.OutgoingSecret,
OutgoingToken: arg.OutgoingToken,
}
}
// Migrate adds External Initiator Name and URL fields.
func Migrate(tx *gorm.DB) error {
var exis []migration1565877314.ExternalInitiator
if err := tx.Find(&exis).Error; err != nil {
return errors.Wrap(err, "could not load all External Intitiators")
}
// Make new table
if err := tx.DropTable(migration1565877314.ExternalInitiator{}).Error; err != nil {
return errors.Wrap(err, "could not drop old External Intitiator table")
}
if err := tx.AutoMigrate(&ExternalInitiator{}).Error; err != nil {
return errors.Wrap(err, "could not create new External Intitiator table")
}
// Copy
for _, old := range exis {
exi := newExternalInitiator(old)
if err := tx.Save(exi).Error; err != nil {
return errors.Wrap(err, "could not save migrated version of External Initiator")
}
}
return nil
}