Skip to content

Commit

Permalink
Setup now works
Browse files Browse the repository at this point in the history
So we initialize the specific database type in PostInit, which also makes it unavailable to the manager.
  • Loading branch information
NodudeWasTaken committed Nov 8, 2024
1 parent 4d729b0 commit 23ba774
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 33 deletions.
4 changes: 2 additions & 2 deletions internal/autotag/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 21 additions & 13 deletions internal/manager/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"errors"
"fmt"
"math"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/sqlite/anonymise.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 13 additions & 1 deletion pkg/sqlite/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type DBInterface interface {
Version() uint
WithDatabase(ctx context.Context) (context.Context, error)
TestMode()
Pointer() *Database
}

type Database struct {
Expand All @@ -148,7 +149,7 @@ type Database struct {
lockChan chan struct{}
}

func newDatabase() *storeRepository {
func newStoreRepo() *storeRepository {
fileStore := NewFileStore()
folderStore := NewFolderStore()
galleryStore := NewGalleryStore(fileStore, folderStore)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 5 additions & 6 deletions pkg/sqlite/database_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type PostgresDB struct {
Database
*Database
}

const (
Expand All @@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions pkg/sqlite/database_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions pkg/sqlite/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 23ba774

Please sign in to comment.