-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
entity_tables.go
152 lines (131 loc) Β· 4.36 KB
/
entity_tables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package entity
import (
"errors"
"fmt"
"time"
"github.com/jinzhu/gorm"
"github.com/photoprism/photoprism/internal/migrate"
"github.com/photoprism/photoprism/pkg/clean"
)
type Tables map[string]interface{}
// Entities contains database entities and their table names.
var Entities = Tables{
migrate.Migration{}.TableName(): &migrate.Migration{},
migrate.Version{}.TableName(): &migrate.Version{},
Error{}.TableName(): &Error{},
Password{}.TableName(): &Password{},
Passcode{}.TableName(): &Passcode{},
User{}.TableName(): &User{},
UserDetails{}.TableName(): &UserDetails{},
UserSettings{}.TableName(): &UserSettings{},
Session{}.TableName(): &Session{},
Client{}.TableName(): &Client{},
Service{}.TableName(): &Service{},
Folder{}.TableName(): &Folder{},
Duplicate{}.TableName(): &Duplicate{},
File{}.TableName(): &File{},
FileShare{}.TableName(): &FileShare{},
FileSync{}.TableName(): &FileSync{},
Photo{}.TableName(): &Photo{},
PhotoUser{}.TableName(): &PhotoUser{},
Details{}.TableName(): &Details{},
Place{}.TableName(): &Place{},
Cell{}.TableName(): &Cell{},
Camera{}.TableName(): &Camera{},
Lens{}.TableName(): &Lens{},
Country{}.TableName(): &Country{},
Album{}.TableName(): &Album{},
AlbumUser{}.TableName(): &AlbumUser{},
PhotoAlbum{}.TableName(): &PhotoAlbum{},
Label{}.TableName(): &Label{},
Category{}.TableName(): &Category{},
PhotoLabel{}.TableName(): &PhotoLabel{},
Keyword{}.TableName(): &Keyword{},
PhotoKeyword{}.TableName(): &PhotoKeyword{},
Link{}.TableName(): &Link{},
Subject{}.TableName(): &Subject{},
Face{}.TableName(): &Face{},
Marker{}.TableName(): &Marker{},
Reaction{}.TableName(): &Reaction{},
UserShare{}.TableName(): &UserShare{},
}
// WaitForMigration waits for the database migration to be successful and returns an error otherwise.
func (list Tables) WaitForMigration(db *gorm.DB) error {
type RowCount struct {
Count int
}
attempts := 100
for name := range list {
for i := 0; i <= attempts; i++ {
count := RowCount{}
if err := db.Raw(fmt.Sprintf("SELECT COUNT(*) AS count FROM %s", name)).Scan(&count).Error; err == nil {
log.Tracef("migrate: %s migrated", clean.Log(name))
break
} else {
log.Tracef("migrate: waiting for %s migration (%s)", clean.Log(name), err.Error())
time.Sleep(100 * time.Millisecond)
}
if i == attempts {
return errors.New("some database tables are missing")
}
}
}
return nil
}
// Truncate removes all data from tables without dropping them.
func (list Tables) Truncate(db *gorm.DB) {
var name string
defer func() {
if r := recover(); r != nil {
log.Errorf("migrate: %s in %s (truncate)", r, name)
}
}()
for name = range list {
if err := db.Exec(fmt.Sprintf("DELETE FROM %s WHERE 1", name)).Error; err == nil {
// log.Debugf("entity: removed all data from %s", name)
break
} else if err.Error() != "record not found" {
log.Debugf("migrate: %s in %s", err, clean.Log(name))
}
}
}
// Migrate migrates all database tables of registered entities.
func (list Tables) Migrate(db *gorm.DB, opt migrate.Options) {
var name string
var entity interface{}
defer func() {
if r := recover(); r != nil {
log.Errorf("migrate: %s in %s (panic)", r, name)
}
}()
log.Debugf("migrate: running database migrations")
// Run pre migrations, if any.
if err := migrate.Run(db, opt.Pre()); err != nil {
log.Error(err)
}
// Run ORM auto migrations.
if opt.AutoMigrate {
for name, entity = range list {
if err := db.AutoMigrate(entity).Error; err != nil {
log.Debugf("migrate: %s (waiting 1s)", err.Error())
time.Sleep(time.Second)
if err = db.AutoMigrate(entity).Error; err != nil {
log.Errorf("migrate: failed migrating %s", clean.Log(name))
panic(err)
}
}
}
}
// Run main migrations, if any.
if err := migrate.Run(db, opt); err != nil {
log.Error(err)
}
}
// Drop drops all database tables of registered entities.
func (list Tables) Drop(db *gorm.DB) {
for _, entity := range list {
if err := db.DropTableIfExists(entity).Error; err != nil {
panic(err)
}
}
}