2018-11-06 19:02:03 +01:00
|
|
|
/*
|
2020-02-21 01:14:45 +01:00
|
|
|
Package entity contains models for data storage based on GORM.
|
2018-11-06 19:02:03 +01:00
|
|
|
|
|
|
|
See http://gorm.io/docs/ for more information about GORM.
|
|
|
|
|
|
|
|
Additional information concerning data storage can be found in our Developer Guide:
|
|
|
|
|
|
|
|
https://github.com/photoprism/photoprism/wiki/Storage
|
|
|
|
*/
|
2019-12-11 16:55:18 +01:00
|
|
|
package entity
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
import (
|
2020-05-08 15:41:01 +02:00
|
|
|
"sync"
|
2020-04-30 21:21:09 +02:00
|
|
|
"time"
|
|
|
|
|
2019-12-11 19:11:44 +01:00
|
|
|
"github.com/jinzhu/gorm"
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
)
|
|
|
|
|
|
|
|
var log = event.Log
|
2020-05-08 15:41:01 +02:00
|
|
|
var resetFixturesOnce sync.Once
|
2019-12-11 19:11:44 +01:00
|
|
|
|
|
|
|
func logError(result *gorm.DB) {
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Error(result.Error.Error())
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 14:32:48 +02:00
|
|
|
|
2020-04-30 21:21:09 +02:00
|
|
|
// MigrateDb creates all tables and inserts default entities as needed.
|
|
|
|
func MigrateDb() {
|
2020-04-30 20:07:03 +02:00
|
|
|
Db().AutoMigrate(
|
2020-04-30 14:32:48 +02:00
|
|
|
&Account{},
|
|
|
|
&File{},
|
|
|
|
&FileShare{},
|
|
|
|
&FileSync{},
|
|
|
|
&Photo{},
|
|
|
|
&Description{},
|
|
|
|
&Place{},
|
|
|
|
&Location{},
|
|
|
|
&Camera{},
|
|
|
|
&Lens{},
|
|
|
|
&Country{},
|
|
|
|
&Album{},
|
|
|
|
&PhotoAlbum{},
|
|
|
|
&Label{},
|
|
|
|
&Category{},
|
|
|
|
&PhotoLabel{},
|
|
|
|
&Keyword{},
|
|
|
|
&PhotoKeyword{},
|
|
|
|
&Link{},
|
|
|
|
)
|
|
|
|
|
2020-05-08 16:01:34 +02:00
|
|
|
// Make sure changes have been written to disk.
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
2020-04-30 20:07:03 +02:00
|
|
|
CreateUnknownPlace()
|
|
|
|
CreateUnknownCountry()
|
|
|
|
CreateUnknownCamera()
|
|
|
|
CreateUnknownLens()
|
2020-04-30 14:32:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DropTables drops database tables for all known entities.
|
2020-04-30 21:21:09 +02:00
|
|
|
func DropTables() {
|
|
|
|
Db().DropTableIfExists(
|
2020-04-30 14:32:48 +02:00
|
|
|
&Account{},
|
|
|
|
&File{},
|
|
|
|
&FileShare{},
|
|
|
|
&FileSync{},
|
|
|
|
&Photo{},
|
|
|
|
&Description{},
|
|
|
|
&Place{},
|
|
|
|
&Location{},
|
|
|
|
&Camera{},
|
|
|
|
&Lens{},
|
|
|
|
&Country{},
|
|
|
|
&Album{},
|
|
|
|
&PhotoAlbum{},
|
|
|
|
&Label{},
|
|
|
|
&Category{},
|
|
|
|
&PhotoLabel{},
|
|
|
|
&Keyword{},
|
|
|
|
&PhotoKeyword{},
|
|
|
|
&Link{},
|
|
|
|
)
|
|
|
|
}
|
2020-04-30 21:21:09 +02:00
|
|
|
|
|
|
|
// ResetDb drops database tables for all known entities and re-creates them with fixtures.
|
|
|
|
func ResetDb(testFixtures bool) {
|
|
|
|
DropTables()
|
|
|
|
|
|
|
|
// Make sure changes have been written to disk.
|
2020-05-08 15:41:01 +02:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2020-04-30 21:21:09 +02:00
|
|
|
|
|
|
|
MigrateDb()
|
|
|
|
|
|
|
|
if testFixtures {
|
|
|
|
CreateTestFixtures()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 15:41:01 +02:00
|
|
|
// InitTestFixtures resets the database and test fixtures once.
|
|
|
|
func InitTestFixtures() {
|
|
|
|
resetFixturesOnce.Do(func() {
|
|
|
|
ResetDb(true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-04-30 21:21:09 +02:00
|
|
|
// InitTestDb connects to and completely initializes the test database incl fixtures.
|
|
|
|
func InitTestDb(dsn string) *Gorm {
|
2020-05-08 15:41:01 +02:00
|
|
|
if HasDbProvider() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-30 21:21:09 +02:00
|
|
|
db := &Gorm{
|
|
|
|
Driver: "mysql",
|
|
|
|
Dsn: dsn,
|
|
|
|
}
|
|
|
|
|
|
|
|
SetDbProvider(db)
|
2020-05-08 15:41:01 +02:00
|
|
|
InitTestFixtures()
|
2020-04-30 21:21:09 +02:00
|
|
|
|
|
|
|
return db
|
|
|
|
}
|