Config: Add system warning log when DatabaseDsn is used #5279

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2025-10-22 08:16:49 +02:00
parent 5606366d86
commit 55da70e955
5 changed files with 49 additions and 34 deletions

View file

@ -17,6 +17,7 @@ import (
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/entity/migrate"
"github.com/photoprism/photoprism/internal/event"
"github.com/photoprism/photoprism/internal/mutex"
"github.com/photoprism/photoprism/pkg/clean"
)
@ -100,12 +101,19 @@ func (c *Config) DatabaseSsl() bool {
}
}
// DatabaseDSN returns the database data source name (DSN).
func (c *Config) DatabaseDSN() string {
// normalizeDatabaseDSN maps the deprecated DatabaseDsn database configuration
// value to its current counterpart, DatabaseDSN, before consumption.
func (c *Config) normalizeDatabaseDSN() {
if c.options.DatabaseDSN == "" && c.options.Deprecated.DatabaseDsn != "" {
c.options.DatabaseDSN = c.options.Deprecated.DatabaseDsn
c.options.Deprecated.DatabaseDsn = ""
event.SystemWarn([]string{"config", "options", "DatabaseDsn has been deprecated in favor of DatabaseDSN"})
}
}
// DatabaseDSN returns the database data source name (DSN).
func (c *Config) DatabaseDSN() string {
c.normalizeDatabaseDSN()
if c.options.DatabaseDSN == "" {
switch c.DatabaseDriver() {
@ -149,18 +157,9 @@ func (c *Config) DatabaseDSN() string {
return c.options.DatabaseDSN
}
// DatabaseFile returns the filename part of a sqlite database DSN.
func (c *Config) DatabaseFile() string {
fileName, _, _ := strings.Cut(strings.TrimPrefix(c.DatabaseDSN(), "file:"), "?")
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.Deprecated.DatabaseDsn != "" {
c.options.DatabaseDSN = c.options.Deprecated.DatabaseDsn
c.options.Deprecated.DatabaseDsn = ""
}
c.normalizeDatabaseDSN()
if c.options.DatabaseDSN == "" || c.options.DatabaseServer != "" {
return
@ -174,6 +173,12 @@ func (c *Config) ParseDatabaseDSN() {
c.options.DatabasePassword = d.Password
}
// DatabaseFile returns the filename part of a sqlite database DSN.
func (c *Config) DatabaseFile() string {
fileName, _, _ := strings.Cut(strings.TrimPrefix(c.DatabaseDSN(), "file:"), "?")
return fileName
}
// DatabaseServer the database server.
func (c *Config) DatabaseServer() string {
c.ParseDatabaseDSN()

View file

@ -49,6 +49,20 @@ func TestConfig_DatabaseSsl(t *testing.T) {
assert.False(t, c.DatabaseSsl())
}
func TestConfig_normalizeDatabaseDSN(t *testing.T) {
c := NewConfig(CliTestContext())
c.options.Deprecated.DatabaseDsn = "foo:b@r@tcp(honeypot:1234)/baz?charset=utf8mb4,utf8&parseTime=true"
c.options.DatabaseDriver = MySQL
assert.Equal(t, "honeypot:1234", c.DatabaseServer())
assert.Equal(t, "honeypot", c.DatabaseHost())
assert.Equal(t, 1234, c.DatabasePort())
assert.Equal(t, "baz", c.DatabaseName())
assert.Equal(t, "foo", c.DatabaseUser())
assert.Equal(t, "b@r", c.DatabasePassword())
}
func TestConfig_ParseDatabaseDSN(t *testing.T) {
c := NewConfig(CliTestContext())

View file

@ -40,14 +40,9 @@ func Audit(level logrus.Level, ev []string, args ...interface{}) {
}
}
// AuditErr records an audit entry at error level.
func AuditErr(ev []string, args ...interface{}) {
Audit(logrus.ErrorLevel, ev, args...)
}
// AuditWarn records an audit entry at warning level.
func AuditWarn(ev []string, args ...interface{}) {
Audit(logrus.WarnLevel, ev, args...)
// AuditDebug records an audit entry at debug level.
func AuditDebug(ev []string, args ...interface{}) {
Audit(logrus.DebugLevel, ev, args...)
}
// AuditInfo records an audit entry at info level.
@ -55,7 +50,12 @@ func AuditInfo(ev []string, args ...interface{}) {
Audit(logrus.InfoLevel, ev, args...)
}
// AuditDebug records an audit entry at debug level.
func AuditDebug(ev []string, args ...interface{}) {
Audit(logrus.DebugLevel, ev, args...)
// AuditWarn records an audit entry at warning level.
func AuditWarn(ev []string, args ...interface{}) {
Audit(logrus.WarnLevel, ev, args...)
}
// AuditErr records an audit entry at error level.
func AuditErr(ev []string, args ...interface{}) {
Audit(logrus.ErrorLevel, ev, args...)
}

View file

@ -34,9 +34,9 @@ func System(level logrus.Level, ev []string, args ...interface{}) {
)
}
// SystemWarn records a system warning.
func SystemWarn(ev []string, args ...interface{}) {
System(logrus.WarnLevel, ev, args...)
// SystemDebug records a system debug message.
func SystemDebug(ev []string, args ...interface{}) {
System(logrus.DebugLevel, ev, args...)
}
// SystemInfo records a system info message.
@ -44,9 +44,9 @@ func SystemInfo(ev []string, args ...interface{}) {
System(logrus.InfoLevel, ev, args...)
}
// SystemDebug records a system debug message.
func SystemDebug(ev []string, args ...interface{}) {
System(logrus.DebugLevel, ev, args...)
// SystemWarn records a system warning.
func SystemWarn(ev []string, args ...interface{}) {
System(logrus.WarnLevel, ev, args...)
}
// SystemError records a system error message.

View file

@ -60,11 +60,7 @@ func (w *Faces) OptimizeFor(subj_uid string) (result FacesOptimizeResult, err er
"faces",
"optimize",
"retained manual clusters after merge",
"subject %s",
"iteration %d",
"cluster %d",
"count %d",
"ids %s",
"subject %s, iteration %d, cluster %d, count %d, ids %s",
}, subject, i, j, len(merge), clusterIDs)
log.Debugf("faces: retained manual clusters after merge: kept %d candidate cluster(s) [%s] for subject %s (merge) itr %d cluster %d", len(merge), clusterIDs, subject, i, j)