2021-11-21 14:05:07 +01:00
|
|
|
package migrate
|
|
|
|
|
2021-11-24 12:42:18 +01:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2022-03-30 20:36:25 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/list"
|
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
"github.com/dustin/go-humanize/english"
|
2021-11-24 12:42:18 +01:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
)
|
2021-11-21 14:05:07 +01:00
|
|
|
|
|
|
|
// Migrations represents a sorted list of migrations.
|
|
|
|
type Migrations []Migration
|
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
// MigrationMap represents a map of migrations.
|
|
|
|
type MigrationMap map[string]Migration
|
|
|
|
|
|
|
|
// Existing finds and returns previously executed database schema migrations.
|
2022-10-15 21:54:11 +02:00
|
|
|
func Existing(db *gorm.DB, stage string) MigrationMap {
|
|
|
|
var err error
|
2021-11-28 13:52:27 +01:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
if db == nil {
|
|
|
|
return make(MigrationMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get SQL dialect name.
|
|
|
|
name := db.Dialect().GetName()
|
|
|
|
|
|
|
|
if name == "" {
|
|
|
|
return make(MigrationMap)
|
|
|
|
}
|
2021-11-28 13:52:27 +01:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
// Make sure a "migrations" table exists.
|
|
|
|
once[name].Do(func() {
|
|
|
|
err = db.AutoMigrate(&Migration{}).Error
|
|
|
|
})
|
2021-11-28 13:52:27 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2022-10-15 21:54:11 +02:00
|
|
|
return make(MigrationMap)
|
2021-11-28 13:52:27 +01:00
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
found := Migrations{}
|
2021-11-28 13:52:27 +01:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
stmt := db
|
2021-11-28 13:52:27 +01:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
if stage == StageMain {
|
|
|
|
stmt = stmt.Where("stage = ? OR stage = '' OR stage IS NULL", stage)
|
|
|
|
} else if stage != "" {
|
|
|
|
stmt = stmt.Where("stage = ?", stage)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = stmt.Find(&found).Error; err != nil {
|
|
|
|
log.Warnf("migrate: %s (find existing)", err)
|
|
|
|
return make(MigrationMap)
|
|
|
|
}
|
2021-11-28 13:52:27 +01:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
result := make(MigrationMap, len(found))
|
|
|
|
|
|
|
|
for _, m := range found {
|
2021-11-28 13:52:27 +01:00
|
|
|
result[m.ID] = m
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-11-21 14:05:07 +01:00
|
|
|
// Start runs all migrations that haven't been executed yet.
|
2022-10-15 21:54:11 +02:00
|
|
|
func (m *Migrations) Start(db *gorm.DB, opt Options) {
|
|
|
|
if db == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
// Find previously executed migrations.
|
2022-10-15 21:54:11 +02:00
|
|
|
executed := Existing(db, opt.StageName())
|
2021-11-28 13:52:27 +01:00
|
|
|
|
2022-01-05 18:15:39 +01:00
|
|
|
if prev := len(executed); prev == 0 {
|
2023-02-09 13:14:56 +01:00
|
|
|
log.Debugf("migrate: no previously executed migrations [%s]", opt.StageName())
|
2022-01-05 18:15:39 +01:00
|
|
|
} else {
|
2023-02-09 13:14:56 +01:00
|
|
|
log.Debugf("migrate: executing %s migrations", opt.StageName())
|
2022-01-05 18:15:39 +01:00
|
|
|
log.Debugf("migrate: found %s", english.Plural(len(executed), "previous migration", "previous migrations"))
|
|
|
|
}
|
2021-11-28 13:52:27 +01:00
|
|
|
|
2021-11-21 14:05:07 +01:00
|
|
|
for _, migration := range *m {
|
2022-10-15 21:54:11 +02:00
|
|
|
if migration.Skip(opt) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-24 12:42:18 +01:00
|
|
|
start := time.Now()
|
2022-04-13 22:17:59 +02:00
|
|
|
migration.StartedAt = start.UTC().Truncate(time.Second)
|
2021-11-24 12:42:18 +01:00
|
|
|
|
2022-03-30 20:36:25 +02:00
|
|
|
// Excluded?
|
2022-10-15 21:54:11 +02:00
|
|
|
if list.Excludes(opt.Migrations, migration.ID) {
|
|
|
|
log.Tracef("migrate: %s skipped", migration.ID)
|
2022-03-30 20:36:25 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
// Already executed?
|
|
|
|
if done, ok := executed[migration.ID]; ok {
|
2022-04-01 16:02:58 +02:00
|
|
|
// Repeat?
|
2022-10-15 21:54:11 +02:00
|
|
|
if !done.Repeat(opt.RunFailed) && !list.Contains(opt.Migrations, migration.ID) {
|
2021-11-28 13:52:27 +01:00
|
|
|
log.Debugf("migrate: %s skipped", migration.ID)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else if err := db.Create(migration).Error; err != nil {
|
|
|
|
// Should not happen.
|
|
|
|
log.Warnf("migrate: creating %s failed with %s [%s]", migration.ID, err, time.Since(start))
|
2021-11-24 12:42:18 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
// Run migration.
|
2021-11-24 12:42:18 +01:00
|
|
|
if err := migration.Execute(db); err != nil {
|
|
|
|
migration.Fail(err, db)
|
2021-11-28 13:52:27 +01:00
|
|
|
log.Errorf("migrate: executing %s failed with %s [%s]", migration.ID, err, time.Since(start))
|
2021-11-24 12:42:18 +01:00
|
|
|
} else if err = migration.Finish(db); err != nil {
|
2021-11-28 13:52:27 +01:00
|
|
|
log.Warnf("migrate: updating %s failed with %s [%s]", migration.ID, err, time.Since(start))
|
2021-11-24 12:42:18 +01:00
|
|
|
} else {
|
2021-11-28 13:52:27 +01:00
|
|
|
log.Infof("migrate: %s successful [%s]", migration.ID, time.Since(start))
|
2021-11-24 12:42:18 +01:00
|
|
|
}
|
2021-11-21 14:05:07 +01:00
|
|
|
}
|
|
|
|
}
|