Config: Always initialize fixtures, even when skipping migrations #3215
Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
parent
9054d8cf0f
commit
74772aea97
|
@ -177,7 +177,7 @@ func resetIndexDb(c *config.Config) {
|
|||
tables.Drop(c.Db())
|
||||
|
||||
log.Infoln("restoring default schema")
|
||||
entity.InitDb(migrate.Opt(false, nil))
|
||||
entity.InitDb(migrate.Opt(true, false, nil))
|
||||
|
||||
// Reset admin account?
|
||||
if c.AdminPassword() == "" {
|
||||
|
|
|
@ -285,13 +285,9 @@ func (c *Config) MigrateDb(runFailed bool, ids []string) {
|
|||
|
||||
// Only migrate once automatically per version.
|
||||
version := migrate.FirstOrCreateVersion(c.Db(), migrate.NewVersion(c.Version(), c.Edition()))
|
||||
if version.NeedsMigration() || runFailed || len(ids) > 0 {
|
||||
entity.InitDb(migrate.Opt(runFailed, ids))
|
||||
if err := version.Migrated(c.Db()); err != nil {
|
||||
log.Warnf("config: %s (migrate)", err)
|
||||
}
|
||||
} else {
|
||||
log.Debugf("config: skipped migrations, already done")
|
||||
entity.InitDb(migrate.Opt(version.NeedsMigration(), runFailed, ids))
|
||||
if err := version.Migrated(c.Db()); err != nil {
|
||||
log.Warnf("config: %s (migrate)", err)
|
||||
}
|
||||
|
||||
// Init admin account?
|
||||
|
|
|
@ -346,7 +346,6 @@ func (m *User) CanLogIn() bool {
|
|||
} else {
|
||||
return acl.Resources.Allow(acl.ResourceConfig, role, acl.AccessOwn)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// CanUseWebDAV checks whether the user is allowed to use WebDAV to synchronize files.
|
||||
|
|
|
@ -20,7 +20,7 @@ func CreateDefaultFixtures() {
|
|||
func ResetTestFixtures() {
|
||||
start := time.Now()
|
||||
|
||||
Entities.Migrate(Db(), migrate.Opt(false, nil))
|
||||
Entities.Migrate(Db(), migrate.Opt(true, false, nil))
|
||||
Entities.WaitForMigration(Db())
|
||||
Entities.Truncate(Db())
|
||||
|
||||
|
|
|
@ -44,14 +44,14 @@ func TestMySQL8(t *testing.T) {
|
|||
Entities.Drop(db)
|
||||
|
||||
// First migration.
|
||||
Entities.Migrate(db, migrate.Opt(false, nil))
|
||||
Entities.Migrate(db, migrate.Opt(true, false, nil))
|
||||
Entities.WaitForMigration(db)
|
||||
|
||||
// Second migration.
|
||||
Entities.Migrate(db, migrate.Opt(false, nil))
|
||||
Entities.Migrate(db, migrate.Opt(true, false, nil))
|
||||
Entities.WaitForMigration(db)
|
||||
|
||||
// Third migration with force flag.
|
||||
Entities.Migrate(db, migrate.Opt(false, []string{"20211121-094727"}))
|
||||
Entities.Migrate(db, migrate.Opt(true, false, []string{"20211121-094727"}))
|
||||
Entities.WaitForMigration(db)
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ func TestDialectMysql(t *testing.T) {
|
|||
db.LogMode(false)
|
||||
db.SetLogger(log)
|
||||
|
||||
opt := Opt(true, nil)
|
||||
opt := Opt(true, true, nil)
|
||||
|
||||
// Run pre-migrations.
|
||||
if err = Run(db, opt.Pre()); err != nil {
|
||||
|
|
|
@ -52,7 +52,7 @@ func TestDialectSQLite3(t *testing.T) {
|
|||
db.LogMode(false)
|
||||
db.SetLogger(log)
|
||||
|
||||
opt := Opt(true, nil)
|
||||
opt := Opt(true, true, nil)
|
||||
|
||||
// Run pre-migrations.
|
||||
if err = Run(db, opt.Pre()); err != nil {
|
||||
|
|
|
@ -10,8 +10,8 @@ type Options struct {
|
|||
}
|
||||
|
||||
// Opt returns migration options based on the specified parameters.
|
||||
func Opt(runFailed bool, ids []string) Options {
|
||||
runAll := len(ids) == 0
|
||||
func Opt(runAll, runFailed bool, ids []string) Options {
|
||||
runAll = len(ids) == 0 && (runAll || runFailed)
|
||||
return Options{
|
||||
AutoMigrate: runAll,
|
||||
RunStage: StageMain,
|
||||
|
|
|
@ -37,7 +37,9 @@ var UnknownVersion = Version{
|
|||
|
||||
// NeedsMigration tests if the Version is not yet installed.
|
||||
func (m *Version) NeedsMigration() bool {
|
||||
if m.MigratedAt == nil {
|
||||
if m == nil {
|
||||
return true
|
||||
} else if m.MigratedAt == nil || m.CreatedAt.IsZero() {
|
||||
return true
|
||||
} else if m.Unknown() {
|
||||
return true
|
||||
|
@ -141,6 +143,12 @@ func (m *Version) String() string {
|
|||
|
||||
// Unknown checks if the version is unknown.
|
||||
func (m *Version) Unknown() bool {
|
||||
if m == nil {
|
||||
return true
|
||||
} else if m.Version == "" {
|
||||
return true
|
||||
}
|
||||
|
||||
return m.Version == UnknownVersion.Version
|
||||
}
|
||||
|
||||
|
@ -148,6 +156,8 @@ func (m *Version) Unknown() bool {
|
|||
func (m *Version) CreateTable(db *gorm.DB) (err error) {
|
||||
if db == nil {
|
||||
return fmt.Errorf("db is nil")
|
||||
} else if m == nil {
|
||||
return fmt.Errorf("version is nil")
|
||||
}
|
||||
|
||||
versionOnce.Do(func() {
|
||||
|
|
Loading…
Reference in a new issue