2021-11-21 14:05:07 +01:00
|
|
|
package migrate
|
|
|
|
|
|
|
|
import (
|
2022-10-15 21:54:11 +02:00
|
|
|
"fmt"
|
2021-11-21 14:05:07 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Migration represents a database schema migration.
|
|
|
|
type Migration struct {
|
|
|
|
ID string `gorm:"size:16;primary_key;auto_increment:false;" json:"ID" yaml:"ID"`
|
|
|
|
Dialect string `gorm:"size:16;" json:"Dialect" yaml:"Dialect,omitempty"`
|
2022-10-15 21:54:11 +02:00
|
|
|
Stage string `gorm:"size:16;" json:"Stage" yaml:"Stage,omitempty"`
|
2021-11-21 14:05:07 +01:00
|
|
|
Error string `gorm:"size:255;" json:"Error" yaml:"Error,omitempty"`
|
|
|
|
Source string `gorm:"size:16;" json:"Source" yaml:"Source,omitempty"`
|
2021-11-24 12:42:18 +01:00
|
|
|
Statements []string `gorm:"-" json:"Statements" yaml:"Statements,omitempty"`
|
2021-11-21 14:05:07 +01:00
|
|
|
StartedAt time.Time `json:"StartedAt" yaml:"StartedAt,omitempty"`
|
|
|
|
FinishedAt *time.Time `json:"FinishedAt" yaml:"FinishedAt,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// TableName returns the entity database table name.
|
|
|
|
func (Migration) TableName() string {
|
|
|
|
return "migrations"
|
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
// StageName returns the stage name.
|
|
|
|
func (m *Migration) StageName() string {
|
|
|
|
if m.Stage == "" {
|
|
|
|
return StageMain
|
|
|
|
} else {
|
|
|
|
return m.Stage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunStage checks if the run stage matches.
|
|
|
|
func (m *Migration) RunStage(name string) bool {
|
|
|
|
return m.StageName() == name
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip checks if the migration should be skipped based on the options.
|
|
|
|
func (m *Migration) Skip(opt Options) bool {
|
|
|
|
return !m.RunStage(opt.StageName())
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:02:58 +02:00
|
|
|
// Finished tests if the migration has been finished yet.
|
|
|
|
func (m *Migration) Finished() bool {
|
|
|
|
if m.FinishedAt == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return !m.FinishedAt.IsZero()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunDuration returns the run duration of started migrations.
|
|
|
|
func (m *Migration) RunDuration() time.Duration {
|
|
|
|
if m.Error != "" || m.StartedAt.IsZero() {
|
|
|
|
return time.Duration(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.Finished() {
|
|
|
|
return m.FinishedAt.UTC().Sub(m.StartedAt.UTC())
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Now().UTC().Sub(m.StartedAt.UTC())
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repeat tests if the migration should be repeated.
|
|
|
|
func (m *Migration) Repeat(runFailed bool) bool {
|
|
|
|
if runFailed && m.Error != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't repeat if finished.
|
|
|
|
if m.Finished() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repeat not started yet.
|
|
|
|
if m.StartedAt.IsZero() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repeat if "running" for more than 60 minutes.
|
|
|
|
return m.RunDuration().Minutes() >= 60
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fail marks the migration as failed by adding an error message and removing the FinishedAt timestamp.
|
2021-11-21 14:05:07 +01:00
|
|
|
func (m *Migration) Fail(err error, db *gorm.DB) {
|
2022-10-15 21:54:11 +02:00
|
|
|
if err == nil || db == nil {
|
2021-11-21 14:05:07 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:02:58 +02:00
|
|
|
m.FinishedAt = nil
|
|
|
|
|
|
|
|
if err.Error() == "" {
|
|
|
|
m.Error = "unknown error"
|
|
|
|
} else {
|
|
|
|
m.Error = err.Error()
|
|
|
|
}
|
2021-11-28 13:52:27 +01:00
|
|
|
|
2024-01-20 15:01:40 +01:00
|
|
|
db.Model(m).Updates(Map{"FinishedAt": m.FinishedAt, "Error": m.Error})
|
2021-11-21 14:05:07 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 16:02:58 +02:00
|
|
|
// Finish updates the FinishedAt timestamp and removes the error message when the migration was successful.
|
2021-11-24 12:42:18 +01:00
|
|
|
func (m *Migration) Finish(db *gorm.DB) error {
|
2022-10-15 21:54:11 +02:00
|
|
|
if db == nil {
|
|
|
|
return fmt.Errorf("db is nil")
|
|
|
|
}
|
|
|
|
|
2022-04-13 22:17:59 +02:00
|
|
|
finished := time.Now().UTC().Truncate(time.Second)
|
2022-04-01 16:02:58 +02:00
|
|
|
m.FinishedAt = &finished
|
|
|
|
m.Error = ""
|
2024-01-20 15:01:40 +01:00
|
|
|
return db.Model(m).Updates(Map{"FinishedAt": m.FinishedAt, "Error": m.Error}).Error
|
2021-11-21 14:05:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute runs the migration.
|
2021-11-24 12:42:18 +01:00
|
|
|
func (m *Migration) Execute(db *gorm.DB) error {
|
2022-10-15 21:54:11 +02:00
|
|
|
if db == nil {
|
|
|
|
return fmt.Errorf("db is nil")
|
|
|
|
}
|
|
|
|
|
2022-03-30 20:36:25 +02:00
|
|
|
for _, s := range m.Statements { // ADD
|
2021-11-24 12:42:18 +01:00
|
|
|
if err := db.Exec(s).Error; err != nil {
|
2022-03-30 20:36:25 +02:00
|
|
|
// Log the errors triggered by ALTER and DROP statements
|
|
|
|
// and otherwise ignore them, since some databases do not
|
|
|
|
// support "IF EXISTS".
|
2022-10-15 21:54:11 +02:00
|
|
|
if IgnoreErr.Matches(s, err.Error()) {
|
|
|
|
log.Tracef("migrate: ignored %s", err)
|
2021-11-28 13:52:27 +01:00
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2021-11-24 12:42:18 +01:00
|
|
|
}
|
2021-11-21 14:05:07 +01:00
|
|
|
}
|
|
|
|
|
2021-11-24 12:42:18 +01:00
|
|
|
return nil
|
2021-11-21 14:05:07 +01:00
|
|
|
}
|