2021-11-21 14:05:07 +01:00
|
|
|
package entity
|
|
|
|
|
2022-01-05 18:15:39 +01:00
|
|
|
import (
|
2022-03-29 00:21:50 +02:00
|
|
|
"os"
|
2022-01-05 18:15:39 +01:00
|
|
|
"time"
|
2022-03-29 00:21:50 +02:00
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/migrate"
|
2022-04-15 09:42:07 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/clean"
|
2022-01-05 18:15:39 +01:00
|
|
|
)
|
|
|
|
|
2022-04-04 14:21:43 +02:00
|
|
|
// onReady contains init functions to be called when the
|
|
|
|
// initialization of the database is complete.
|
|
|
|
var onReady []func()
|
|
|
|
|
|
|
|
// ready executes init callbacks when the initialization of the
|
|
|
|
// database is complete.
|
|
|
|
func ready() {
|
|
|
|
for _, init := range onReady {
|
|
|
|
init()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InitDb creates database tables and inserts default fixtures as needed.
|
2022-10-15 21:54:11 +02:00
|
|
|
func InitDb(opt migrate.Options) {
|
2022-04-04 14:21:43 +02:00
|
|
|
if !HasDbProvider() {
|
|
|
|
log.Error("migrate: no database provider")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-05 18:15:39 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
if opt.DropDeprecated {
|
2021-11-28 13:52:27 +01:00
|
|
|
DeprecatedTables.Drop(Db())
|
2021-11-21 14:05:07 +01:00
|
|
|
}
|
|
|
|
|
2022-10-15 21:54:11 +02:00
|
|
|
Entities.Migrate(Db(), opt)
|
2021-11-28 13:52:27 +01:00
|
|
|
Entities.WaitForMigration(Db())
|
2021-11-21 14:05:07 +01:00
|
|
|
|
|
|
|
CreateDefaultFixtures()
|
2022-01-05 18:15:39 +01:00
|
|
|
|
2022-04-04 14:21:43 +02:00
|
|
|
ready()
|
|
|
|
|
|
|
|
log.Debugf("migrate: completed in %s", time.Since(start))
|
2021-11-21 14:05:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitTestDb connects to and completely initializes the test database incl fixtures.
|
2022-10-01 15:17:04 +02:00
|
|
|
func InitTestDb(driver, dsn string) *DbConn {
|
2021-11-21 14:05:07 +01:00
|
|
|
if HasDbProvider() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-04 14:21:43 +02:00
|
|
|
start := time.Now()
|
|
|
|
|
2022-03-29 00:21:50 +02:00
|
|
|
// Set default test database driver.
|
2021-11-21 14:05:07 +01:00
|
|
|
if driver == "test" || driver == "sqlite" || driver == "" || dsn == "" {
|
2022-03-29 00:21:50 +02:00
|
|
|
driver = SQLite3
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set default database DSN.
|
|
|
|
if driver == SQLite3 {
|
|
|
|
if dsn == "" {
|
|
|
|
dsn = SQLiteMemoryDSN
|
|
|
|
} else if dsn != SQLiteTestDB {
|
|
|
|
// Continue.
|
|
|
|
} else if err := os.Remove(dsn); err == nil {
|
2022-04-15 09:42:07 +02:00
|
|
|
log.Debugf("sqlite: test file %s removed", clean.Log(dsn))
|
2022-03-29 00:21:50 +02:00
|
|
|
}
|
2021-11-21 14:05:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("initializing %s test db in %s", driver, dsn)
|
|
|
|
|
2022-10-01 15:17:04 +02:00
|
|
|
// Create gorm.DB connection provider.
|
|
|
|
db := &DbConn{
|
2021-11-21 14:05:07 +01:00
|
|
|
Driver: driver,
|
|
|
|
Dsn: dsn,
|
|
|
|
}
|
|
|
|
|
2022-10-01 15:17:04 +02:00
|
|
|
// Insert test fixtures into the database.
|
2021-11-21 14:05:07 +01:00
|
|
|
SetDbProvider(db)
|
|
|
|
ResetTestFixtures()
|
2022-03-30 20:36:25 +02:00
|
|
|
File{}.RegenerateIndex()
|
2021-11-21 14:05:07 +01:00
|
|
|
|
2022-04-04 14:21:43 +02:00
|
|
|
ready()
|
|
|
|
|
|
|
|
log.Debugf("migrate: completed in %s", time.Since(start))
|
|
|
|
|
2021-11-21 14:05:07 +01:00
|
|
|
return db
|
|
|
|
}
|