diff --git a/internal/autotag/integration_test.go b/internal/autotag/integration_test.go index 4a4099040f8..efb010463e3 100644 --- a/internal/autotag/integration_test.go +++ b/internal/autotag/integration_test.go @@ -48,10 +48,10 @@ func getNewDB(databaseFile string) sqlite.DBInterface { dbUrl, valid := os.LookupEnv("PGSQL_TEST") if valid { fmt.Printf("Postgres backend for tests detected\n") - db = sqlite.NewPostgresDatabase(dbUrl, true) + db = sqlite.NewPostgresDatabase(sqlite.NewDatabase(), dbUrl, true) } else { fmt.Printf("SQLite backend for tests detected\n") - db = sqlite.NewSQLiteDatabase(databaseFile, true) + db = sqlite.NewSQLiteDatabase(sqlite.NewDatabase(), databaseFile, true) } return db diff --git a/internal/manager/init.go b/internal/manager/init.go index f89178443e2..1834b2df4b6 100644 --- a/internal/manager/init.go +++ b/internal/manager/init.go @@ -35,19 +35,7 @@ import ( func Initialize(cfg *config.Config, l *log.Logger) (*Manager, error) { ctx := context.TODO() - var db sqlite.DBInterface - - dbUrl := cfg.GetDatabaseUrl() - upperUrl := strings.ToUpper(dbUrl) - switch { - case strings.HasPrefix(upperUrl, string(sqlite.PostgresBackend)+":"): - db = sqlite.NewPostgresDatabase(dbUrl, true) - case strings.HasPrefix(upperUrl, string(sqlite.SqliteBackend)+":"): - db = sqlite.NewSQLiteDatabase(dbUrl[len(sqlite.SqliteBackend)+1:], true) - default: - // Assume it's the path to a SQLite database - for backwards compat - db = sqlite.NewSQLiteDatabase(dbUrl, true) - } + var db *sqlite.Database = sqlite.NewDatabase() repo := db.Repository() @@ -198,11 +186,31 @@ func initJobManager(cfg *config.Config) *job.Manager { return ret } +// Initializes the specific DB type +func (s *Manager) RefreshDB() { + cfg := s.Config + + var odb *sqlite.Database = s.Database.Pointer() + + dbUrl := cfg.GetDatabaseUrl() + upperUrl := strings.ToUpper(dbUrl) + switch { + case strings.HasPrefix(upperUrl, string(sqlite.PostgresBackend)+":"): + s.Database = sqlite.NewPostgresDatabase(odb, dbUrl, true) + case strings.HasPrefix(upperUrl, string(sqlite.SqliteBackend)+":"): + s.Database = sqlite.NewSQLiteDatabase(odb, dbUrl[len(sqlite.SqliteBackend)+1:], true) + default: + // Assume it's the path to a SQLite database - for backwards compat + s.Database = sqlite.NewSQLiteDatabase(odb, dbUrl, true) + } +} + // postInit initialises the paths, caches and database after the initial // configuration has been set. Should only be called if the configuration // is valid. func (s *Manager) postInit(ctx context.Context) error { s.RefreshConfig() + s.RefreshDB() s.SessionStore = session.NewStore(s.Config) s.PluginCache.RegisterSessionStore(s.SessionStore) diff --git a/internal/manager/manager.go b/internal/manager/manager.go index 138e38570b2..9a55f3d0185 100644 --- a/internal/manager/manager.go +++ b/internal/manager/manager.go @@ -6,6 +6,7 @@ import ( "context" "errors" "fmt" + "math" "net/http" "os" "path/filepath" @@ -383,7 +384,12 @@ func (s *Manager) GetSystemStatus() *SystemStatus { database := s.Database dbSchema := int(database.Version()) dbPath := database.DatabasePath() - appSchema := int(database.AppSchemaVersion()) + appSchema := math.MaxInt32 + + // Database is not initialized and cannot be used yet + if !s.Config.IsNewSystem() { + appSchema = int(database.AppSchemaVersion()) + } status := SystemStatusEnumOk if s.Config.IsNewSystem() { diff --git a/pkg/sqlite/anonymise.go b/pkg/sqlite/anonymise.go index a36642b0d39..1a02444fa57 100644 --- a/pkg/sqlite/anonymise.go +++ b/pkg/sqlite/anonymise.go @@ -31,7 +31,7 @@ type Anonymiser struct { var anon_dialect = goqu.Dialect("sqlite3") func NewAnonymiser(db DBInterface, outPath string) (*Anonymiser, error) { - newDB := NewSQLiteDatabase(outPath, false) + newDB := NewSQLiteDatabase(NewDatabase(), outPath, false) if err := newDB.Open(); err != nil { return nil, fmt.Errorf("opening %s: %w", outPath, err) } diff --git a/pkg/sqlite/database.go b/pkg/sqlite/database.go index 4f27375ff54..1f001f44a90 100644 --- a/pkg/sqlite/database.go +++ b/pkg/sqlite/database.go @@ -133,6 +133,7 @@ type DBInterface interface { Version() uint WithDatabase(ctx context.Context) (context.Context, error) TestMode() + Pointer() *Database } type Database struct { @@ -148,7 +149,7 @@ type Database struct { lockChan chan struct{} } -func newDatabase() *storeRepository { +func newStoreRepo() *storeRepository { fileStore := NewFileStore() folderStore := NewFolderStore() galleryStore := NewGalleryStore(fileStore, folderStore) @@ -177,6 +178,13 @@ func newDatabase() *storeRepository { return r } +func NewDatabase() *Database { + return &Database{ + storeRepository: newStoreRepo(), + lockChan: make(chan struct{}, 1), + } +} + func getDBBoolean(val bool) string { switch dbWrapper.dbType { case SqliteBackend: @@ -242,6 +250,10 @@ func isConstraintError(err error) bool { return false } +func (db *Database) Pointer() *Database { + return db +} + func (db *Database) SetSchemaVersion(version uint) { db.schemaVersion = version } diff --git a/pkg/sqlite/database_postgres.go b/pkg/sqlite/database_postgres.go index 74914c2d1d1..83f79607bf5 100644 --- a/pkg/sqlite/database_postgres.go +++ b/pkg/sqlite/database_postgres.go @@ -11,7 +11,7 @@ import ( ) type PostgresDB struct { - Database + *Database } const ( @@ -20,13 +20,12 @@ const ( maxPGReadConnections = 15 ) -func NewPostgresDatabase(dbConnector string, init bool) *PostgresDB { +func NewPostgresDatabase(odb *Database, dbConnector string, init bool) *PostgresDB { db := &PostgresDB{ - Database: Database{ - storeRepository: newDatabase(), - dbConfig: dbConnector, - }, + Database: odb, } + + db.dbConfig = dbConnector db.DBInterface = db if init { diff --git a/pkg/sqlite/database_sqlite.go b/pkg/sqlite/database_sqlite.go index 3b7ee417b5f..943093117fe 100644 --- a/pkg/sqlite/database_sqlite.go +++ b/pkg/sqlite/database_sqlite.go @@ -13,17 +13,15 @@ import ( ) type SQLiteDB struct { - Database + *Database } -func NewSQLiteDatabase(dbPath string, init bool) *SQLiteDB { +func NewSQLiteDatabase(odb *Database, dbPath string, init bool) *SQLiteDB { db := &SQLiteDB{ - Database: Database{ - storeRepository: newDatabase(), - lockChan: make(chan struct{}, 1), - dbConfig: dbPath, - }, + Database: odb, } + + db.dbConfig = dbPath db.DBInterface = db if init { diff --git a/pkg/sqlite/setup_test.go b/pkg/sqlite/setup_test.go index 8f7a9ac8eea..56d51be1300 100644 --- a/pkg/sqlite/setup_test.go +++ b/pkg/sqlite/setup_test.go @@ -634,10 +634,10 @@ func getNewDB(databaseFile string) sqlite.DBInterface { dbUrl, valid := os.LookupEnv("PGSQL_TEST") if valid { fmt.Printf("Postgres backend for tests detected\n") - db = sqlite.NewPostgresDatabase(dbUrl, true) + db = sqlite.NewPostgresDatabase(sqlite.NewDatabase(), dbUrl, true) } else { fmt.Printf("SQLite backend for tests detected\n") - db = sqlite.NewSQLiteDatabase(databaseFile, true) + db = sqlite.NewSQLiteDatabase(sqlite.NewDatabase(), databaseFile, true) } return db