2021-11-21 14:05:07 +01:00
|
|
|
package entity
|
|
|
|
|
2022-01-05 18:15:39 +01:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-11-21 14:05:07 +01:00
|
|
|
// MigrateDb creates database tables and inserts default fixtures as needed.
|
2021-11-28 13:52:27 +01:00
|
|
|
func MigrateDb(dropDeprecated, runFailed bool) {
|
2022-01-05 18:15:39 +01:00
|
|
|
start := time.Now()
|
|
|
|
|
2021-11-21 14:05:07 +01:00
|
|
|
if dropDeprecated {
|
2021-11-28 13:52:27 +01:00
|
|
|
DeprecatedTables.Drop(Db())
|
2021-11-21 14:05:07 +01:00
|
|
|
}
|
|
|
|
|
2021-11-28 13:52:27 +01:00
|
|
|
Entities.Migrate(Db(), runFailed)
|
|
|
|
Entities.WaitForMigration(Db())
|
2021-11-21 14:05:07 +01:00
|
|
|
|
|
|
|
CreateDefaultFixtures()
|
2022-01-05 18:15:39 +01:00
|
|
|
|
|
|
|
log.Debugf("entity: successfully initialized [%s]", time.Since(start))
|
2021-11-21 14:05:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitTestDb connects to and completely initializes the test database incl fixtures.
|
|
|
|
func InitTestDb(driver, dsn string) *Gorm {
|
|
|
|
if HasDbProvider() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if driver == "test" || driver == "sqlite" || driver == "" || dsn == "" {
|
|
|
|
driver = "sqlite3"
|
|
|
|
dsn = ".test.db"
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("initializing %s test db in %s", driver, dsn)
|
|
|
|
|
|
|
|
db := &Gorm{
|
|
|
|
Driver: driver,
|
|
|
|
Dsn: dsn,
|
|
|
|
}
|
|
|
|
|
|
|
|
SetDbProvider(db)
|
|
|
|
ResetTestFixtures()
|
|
|
|
|
|
|
|
return db
|
|
|
|
}
|