/
config_db.go
445 lines (364 loc) Β· 10.6 KB
/
config_db.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package config
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/migrate"
"github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/pkg/clean"
"github.com/photoprism/photoprism/pkg/txt"
)
// SQL Databases.
// TODO: PostgreSQL support requires upgrading GORM, so generic column data types can be used.
const (
MySQL = "mysql"
MariaDB = "mariadb"
Postgres = "postgres"
SQLite3 = "sqlite3"
)
// SQLite default DSNs.
const (
SQLiteTestDB = ".test.db"
SQLiteMemoryDSN = ":memory:"
)
// DatabaseDriver returns the database driver name.
func (c *Config) DatabaseDriver() string {
switch strings.ToLower(c.options.DatabaseDriver) {
case MySQL, MariaDB:
c.options.DatabaseDriver = MySQL
case SQLite3, "sqlite", "sqllite", "test", "file", "":
c.options.DatabaseDriver = SQLite3
case "tidb":
log.Warnf("config: database driver 'tidb' is deprecated, using sqlite")
c.options.DatabaseDriver = SQLite3
c.options.DatabaseDsn = ""
default:
log.Warnf("config: unsupported database driver %s, using sqlite", c.options.DatabaseDriver)
c.options.DatabaseDriver = SQLite3
c.options.DatabaseDsn = ""
}
return c.options.DatabaseDriver
}
// DatabaseDsn returns the database data source name (DSN).
func (c *Config) DatabaseDsn() string {
if c.options.DatabaseDsn == "" {
switch c.DatabaseDriver() {
case MySQL, MariaDB:
address := c.DatabaseServer()
// Connect via TCP or Unix Domain Socket?
if strings.HasPrefix(address, "/") {
log.Debugf("mariadb: connecting via Unix domain socket")
address = fmt.Sprintf("unix(%s)", address)
} else {
address = fmt.Sprintf("tcp(%s)", address)
}
return fmt.Sprintf(
"%s:%s@%s/%s?charset=utf8mb4,utf8&collation=utf8mb4_unicode_ci&parseTime=true&timeout=%ds",
c.DatabaseUser(),
c.DatabasePassword(),
address,
c.DatabaseName(),
c.DatabaseTimeout(),
)
case Postgres:
return fmt.Sprintf(
"user=%s password=%s dbname=%s host=%s port=%d connect_timeout=%d sslmode=disable TimeZone=UTC",
c.DatabaseUser(),
c.DatabasePassword(),
c.DatabaseName(),
c.DatabaseHost(),
c.DatabasePort(),
c.DatabaseTimeout(),
)
case SQLite3:
return filepath.Join(c.StoragePath(), "index.db?_busy_timeout=5000")
default:
log.Errorf("config: empty database dsn")
return ""
}
}
return c.options.DatabaseDsn
}
// DatabaseFile returns the filename part of a sqlite database DSN.
func (c *Config) DatabaseFile() string {
fileName, _, _ := strings.Cut(c.DatabaseDsn(), "?")
return fileName
}
// ParseDatabaseDsn parses the database dsn and extracts user, password, database server, and name.
func (c *Config) ParseDatabaseDsn() {
if c.options.DatabaseDsn == "" || c.options.DatabaseServer != "" {
return
}
d := NewDSN(c.options.DatabaseDsn)
c.options.DatabaseName = d.Name
c.options.DatabaseServer = d.Server
c.options.DatabaseUser = d.User
c.options.DatabasePassword = d.Password
}
// DatabaseServer the database server.
func (c *Config) DatabaseServer() string {
c.ParseDatabaseDsn()
if c.DatabaseDriver() == SQLite3 {
return ""
} else if c.options.DatabaseServer == "" {
return "localhost"
}
return c.options.DatabaseServer
}
// DatabaseHost the database server host.
func (c *Config) DatabaseHost() string {
if c.DatabaseDriver() == SQLite3 {
return ""
}
if s := strings.Split(c.DatabaseServer(), ":"); len(s) > 0 {
return s[0]
}
return c.options.DatabaseServer
}
// DatabasePort the database server port.
func (c *Config) DatabasePort() int {
const defaultPort = 3306
if server := c.DatabaseServer(); server == "" {
return 0
} else if s := strings.Split(server, ":"); len(s) != 2 {
return defaultPort
} else if port, err := strconv.Atoi(s[1]); err != nil {
return defaultPort
} else if port < 1 || port > 65535 {
return defaultPort
} else {
return port
}
}
// DatabasePortString the database server port as string.
func (c *Config) DatabasePortString() string {
if c.DatabaseDriver() == SQLite3 {
return ""
}
return strconv.Itoa(c.DatabasePort())
}
// DatabaseName the database schema name.
func (c *Config) DatabaseName() string {
c.ParseDatabaseDsn()
if c.DatabaseDriver() == SQLite3 {
return c.DatabaseDsn()
} else if c.options.DatabaseName == "" {
return "photoprism"
}
return c.options.DatabaseName
}
// DatabaseUser returns the database user name.
func (c *Config) DatabaseUser() string {
if c.DatabaseDriver() == SQLite3 {
return ""
}
c.ParseDatabaseDsn()
if c.options.DatabaseUser == "" {
return "photoprism"
}
return c.options.DatabaseUser
}
// DatabasePassword returns the database user password.
func (c *Config) DatabasePassword() string {
if c.DatabaseDriver() == SQLite3 {
return ""
}
c.ParseDatabaseDsn()
return c.options.DatabasePassword
}
// DatabaseTimeout returns the TCP timeout in seconds for establishing a database connection:
// - https://github.com/photoprism/photoprism/issues/4059#issuecomment-1989119004
// - https://github.com/go-sql-driver/mysql/blob/master/README.md#timeout
func (c *Config) DatabaseTimeout() int {
// Ensure that the timeout is between 1 and a maximum
// of 60 seconds, with a default of 15 seconds.
if c.options.DatabaseTimeout <= 0 {
return 15
} else if c.options.DatabaseTimeout > 60 {
return 60
}
return c.options.DatabaseTimeout
}
// DatabaseConns returns the maximum number of open connections to the database.
func (c *Config) DatabaseConns() int {
limit := c.options.DatabaseConns
if limit <= 0 {
limit = (runtime.NumCPU() * 2) + 16
}
if limit > 1024 {
limit = 1024
}
return limit
}
// DatabaseConnsIdle returns the maximum number of idle connections to the database (equal or less than open).
func (c *Config) DatabaseConnsIdle() int {
limit := c.options.DatabaseConnsIdle
if limit <= 0 {
limit = runtime.NumCPU() + 8
}
if limit > c.DatabaseConns() {
limit = c.DatabaseConns()
}
return limit
}
// Db returns the db connection.
func (c *Config) Db() *gorm.DB {
if c.db == nil {
log.Fatal("config: database not connected")
}
return c.db
}
// CloseDb closes the db connection (if any).
func (c *Config) CloseDb() error {
if c.db != nil {
if err := c.db.Close(); err == nil {
c.db = nil
} else {
return err
}
}
return nil
}
// SetDbOptions sets the database collation to unicode if supported.
func (c *Config) SetDbOptions() {
switch c.DatabaseDriver() {
case MySQL, MariaDB:
c.Db().Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci")
case Postgres:
// Ignore for now.
case SQLite3:
// Not required as unicode is default.
}
}
// RegisterDb sets the database options and connection provider.
func (c *Config) RegisterDb() {
c.SetDbOptions()
entity.SetDbProvider(c)
}
// InitDb initializes the database without running previously failed migrations.
func (c *Config) InitDb() {
c.RegisterDb()
c.MigrateDb(false, nil)
}
// MigrateDb initializes the database and migrates the schema if needed.
func (c *Config) MigrateDb(runFailed bool, ids []string) {
entity.Admin.UserName = c.AdminUser()
// Only migrate once automatically per version.
version := migrate.FirstOrCreateVersion(c.Db(), migrate.NewVersion(c.Version(), c.Edition()))
entity.InitDb(migrate.Opt(version.NeedsMigration(), runFailed, ids))
if err := version.Migrated(c.Db()); err != nil {
log.Warnf("config: %s (migrate)", err)
}
// Init admin account?
if c.AdminPassword() == "" {
log.Warnf("config: password required to initialize %s account", clean.LogQuote(c.AdminUser()))
} else {
entity.Admin.InitAccount(c.AdminUser(), c.AdminPassword())
}
go entity.Error{}.LogEvents()
}
// InitTestDb drops all tables in the currently configured database and re-creates them.
func (c *Config) InitTestDb() {
entity.ResetTestFixtures()
if c.AdminPassword() == "" {
// Do nothing.
} else {
entity.Admin.InitAccount(c.AdminUser(), c.AdminPassword())
}
go entity.Error{}.LogEvents()
}
// checkDb checks the database server version.
func (c *Config) checkDb(db *gorm.DB) error {
switch c.DatabaseDriver() {
case MySQL:
type Res struct {
Value string `gorm:"column:Value;"`
}
var res Res
if err := db.Raw("SHOW VARIABLES LIKE 'innodb_version'").Scan(&res).Error; err != nil {
return nil
} else if v := strings.Split(res.Value, "."); len(v) < 3 {
log.Warnf("config: unknown database server version")
} else if major := txt.UInt(v[0]); major < 10 {
return fmt.Errorf("config: MySQL %s is not supported, see https://docs.photoprism.app/getting-started/#databases", res.Value)
} else if sub := txt.UInt(v[1]); sub < 5 || sub == 5 && txt.UInt(v[2]) < 12 {
return fmt.Errorf("config: MariaDB %s is not supported, see https://docs.photoprism.app/getting-started/#databases", res.Value)
}
}
return nil
}
// connectDb establishes a database connection.
func (c *Config) connectDb() error {
// Make sure this is not running twice.
mutex.Db.Lock()
defer mutex.Db.Unlock()
// Get database driver and data source name.
dbDriver := c.DatabaseDriver()
dbDsn := c.DatabaseDsn()
if dbDriver == "" {
return errors.New("config: database driver not specified")
}
if dbDsn == "" {
return errors.New("config: database DSN not specified")
}
// Open database connection.
db, err := gorm.Open(dbDriver, dbDsn)
if err != nil || db == nil {
log.Infof("config: waiting for the database to become available")
for i := 1; i <= 12; i++ {
db, err = gorm.Open(dbDriver, dbDsn)
if db != nil && err == nil {
break
}
time.Sleep(5 * time.Second)
}
if err != nil || db == nil {
return err
}
}
// Configure database logging.
db.LogMode(false)
db.SetLogger(log)
// Set database connection parameters.
db.DB().SetMaxOpenConns(c.DatabaseConns())
db.DB().SetMaxIdleConns(c.DatabaseConnsIdle())
db.DB().SetConnMaxLifetime(time.Hour)
// Check database server version.
if err = c.checkDb(db); err != nil {
if c.Unsafe() {
log.Error(err)
} else {
return err
}
}
// Ok.
c.db = db
return nil
}
// ImportSQL imports a file to the currently configured database.
func (c *Config) ImportSQL(filename string) {
contents, err := os.ReadFile(filename)
if err != nil {
log.Error(err)
return
}
statements := strings.Split(string(contents), ";\n")
q := c.Db().Unscoped()
for _, stmt := range statements {
// Skip empty lines and comments
if len(stmt) < 3 || stmt[0] == '#' || stmt[0] == ';' {
continue
}
var result struct{}
q.Raw(stmt).Scan(&result)
}
}