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 17:45:32 +02:00
|
|
|
"fmt"
|
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
|
|
|
|
|
|
|
|
func logError(result *gorm.DB) {
|
|
|
|
if result.Error != nil {
|
|
|
|
log.Error(result.Error.Error())
|
|
|
|
}
|
|
|
|
}
|
2020-04-30 14:32:48 +02:00
|
|
|
|
2020-05-08 17:45:32 +02:00
|
|
|
type Types map[string]interface{}
|
|
|
|
|
|
|
|
// List of database entities and their table names.
|
|
|
|
var Entities = Types{
|
2020-05-19 13:48:05 +02:00
|
|
|
"errors": &Error{},
|
2020-05-08 17:45:32 +02:00
|
|
|
"accounts": &Account{},
|
2020-05-22 16:29:12 +02:00
|
|
|
"folders": &Folder{},
|
2020-05-08 17:45:32 +02:00
|
|
|
"files": &File{},
|
|
|
|
"files_share": &FileShare{},
|
|
|
|
"files_sync": &FileSync{},
|
|
|
|
"photos": &Photo{},
|
2020-05-18 22:18:58 +02:00
|
|
|
"details": &Details{},
|
2020-05-08 17:45:32 +02:00
|
|
|
"places": &Place{},
|
|
|
|
"locations": &Location{},
|
|
|
|
"cameras": &Camera{},
|
|
|
|
"lenses": &Lens{},
|
|
|
|
"countries": &Country{},
|
|
|
|
"albums": &Album{},
|
|
|
|
"photos_albums": &PhotoAlbum{},
|
|
|
|
"labels": &Label{},
|
|
|
|
"categories": &Category{},
|
|
|
|
"photos_labels": &PhotoLabel{},
|
|
|
|
"keywords": &Keyword{},
|
|
|
|
"photos_keywords": &PhotoKeyword{},
|
|
|
|
"links": &Link{},
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:52:47 +02:00
|
|
|
type RowCount struct {
|
|
|
|
Count int
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:45:32 +02:00
|
|
|
// WaitForMigration waits for the database migration to be successful.
|
|
|
|
func (list Types) WaitForMigration() {
|
|
|
|
attempts := 100
|
|
|
|
for name := range list {
|
|
|
|
for i := 0; i <= attempts; i++ {
|
2020-05-30 14:52:47 +02:00
|
|
|
count := RowCount{}
|
|
|
|
if err := Db().Raw(fmt.Sprintf("SELECT COUNT(*) AS count FROM %s", name)).Scan(&count).Error; err == nil {
|
2020-05-09 11:00:22 +02:00
|
|
|
// log.Debugf("entity: table %s migrated", name)
|
2020-05-08 17:45:32 +02:00
|
|
|
break
|
|
|
|
} else {
|
2020-05-09 11:00:22 +02:00
|
|
|
log.Debugf("entity: wait for migration %s (%s)", err.Error(), name)
|
2020-05-08 17:45:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if i == attempts {
|
|
|
|
panic("migration failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 19:34:29 +02:00
|
|
|
// Truncate removes all data from tables without dropping them.
|
|
|
|
func (list Types) Truncate() {
|
|
|
|
for name := range list {
|
2020-05-30 14:52:47 +02:00
|
|
|
row := RowCount{}
|
|
|
|
if err := Db().Raw(fmt.Sprintf("DELETE FROM %s WHERE 1", name)).Scan(&row).Error; err == nil {
|
2020-05-08 19:34:29 +02:00
|
|
|
log.Debugf("entity: removed all data from %s", name)
|
|
|
|
break
|
2020-05-09 11:00:22 +02:00
|
|
|
} else if err.Error() != "record not found" {
|
|
|
|
log.Debugf("entity: truncate %s (%s)", err.Error(), name)
|
2020-05-08 19:34:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 17:45:32 +02:00
|
|
|
// Drop migrates all database tables of registered entities.
|
|
|
|
func (list Types) Migrate() {
|
|
|
|
for _, entity := range list {
|
2020-05-08 18:18:19 +02:00
|
|
|
if err := UnscopedDb().AutoMigrate(entity).Error; err != nil {
|
2020-05-09 11:00:22 +02:00
|
|
|
log.Debugf("entity: migrate %s (waiting 1s)", err.Error())
|
2020-05-08 18:18:19 +02:00
|
|
|
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
if err := UnscopedDb().AutoMigrate(entity).Error; err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2020-05-08 17:45:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Drop drops all database tables of registered entities.
|
|
|
|
func (list Types) Drop() {
|
|
|
|
for _, entity := range list {
|
2020-05-08 18:18:19 +02:00
|
|
|
if err := UnscopedDb().DropTableIfExists(entity).Error; err != nil {
|
2020-05-08 17:45:32 +02:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-08 16:36:09 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 19:34:29 +02:00
|
|
|
// Creates default database entries for test and production.
|
|
|
|
func CreateDefaultFixtures() {
|
2020-04-30 20:07:03 +02:00
|
|
|
CreateUnknownPlace()
|
2020-05-25 19:10:44 +02:00
|
|
|
CreateUnknownLocation()
|
2020-04-30 20:07:03 +02:00
|
|
|
CreateUnknownCountry()
|
|
|
|
CreateUnknownCamera()
|
|
|
|
CreateUnknownLens()
|
2020-05-30 14:52:47 +02:00
|
|
|
// CreateViews()
|
2020-04-30 14:32:48 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 19:34:29 +02:00
|
|
|
// MigrateDb creates all tables and inserts default entities as needed.
|
|
|
|
func MigrateDb() {
|
|
|
|
Entities.Migrate()
|
|
|
|
Entities.WaitForMigration()
|
2020-05-08 18:18:19 +02:00
|
|
|
|
2020-05-08 19:34:29 +02:00
|
|
|
CreateDefaultFixtures()
|
2020-04-30 14:32:48 +02:00
|
|
|
}
|
2020-04-30 21:21:09 +02:00
|
|
|
|
2020-05-08 19:34:29 +02:00
|
|
|
// ResetTestFixtures drops database tables for all known entities and re-creates them with fixtures.
|
|
|
|
func ResetTestFixtures() {
|
|
|
|
Entities.Migrate()
|
|
|
|
Entities.WaitForMigration()
|
|
|
|
Entities.Truncate()
|
2020-04-30 21:21:09 +02:00
|
|
|
|
2020-05-08 19:34:29 +02:00
|
|
|
CreateDefaultFixtures()
|
2020-04-30 21:21:09 +02:00
|
|
|
|
2020-05-08 19:34:29 +02:00
|
|
|
CreateTestFixtures()
|
2020-05-08 15:41:01 +02:00
|
|
|
}
|
|
|
|
|
2020-04-30 21:21:09 +02:00
|
|
|
// InitTestDb connects to and completely initializes the test database incl fixtures.
|
2020-05-30 14:52:47 +02:00
|
|
|
func InitTestDb(driver, dsn string) *Gorm {
|
2020-05-08 15:41:01 +02:00
|
|
|
if HasDbProvider() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-30 14:52:47 +02:00
|
|
|
if driver == "test" || driver == "sqlite" || driver == "" || dsn == "" {
|
|
|
|
driver = "sqlite3"
|
|
|
|
dsn = ".test.db"
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("initializing %s test db in %s", driver, dsn)
|
|
|
|
|
2020-04-30 21:21:09 +02:00
|
|
|
db := &Gorm{
|
2020-05-30 14:52:47 +02:00
|
|
|
Driver: driver,
|
2020-04-30 21:21:09 +02:00
|
|
|
Dsn: dsn,
|
|
|
|
}
|
|
|
|
|
|
|
|
SetDbProvider(db)
|
2020-05-08 19:34:29 +02:00
|
|
|
ResetTestFixtures()
|
2020-04-30 21:21:09 +02:00
|
|
|
|
|
|
|
return db
|
|
|
|
}
|