Sybren A. Stüvel 9f5e4cc0cc License: license all code under "GPL-3.0-or-later"
The add-on code was copy-pasted from other addons and used the GPL v2
license, whereas by accident the LICENSE text file had the GNU "Affero" GPL
license v3 (instead of regular GPL v3).

This is now all streamlined, and all code is licensed as "GPL v3 or later".

Furthermore, the code comments just show a SPDX License Identifier
instead of an entire license block.
2022-03-07 15:26:46 +01:00

60 lines
1.1 KiB
Go

// Package persistence provides the database interface for Flamenco Manager.
package persistence
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"context"
"github.com/rs/zerolog/log"
"gorm.io/gorm"
// sqlite "git.blender.org/flamenco/pkg/gorm-modernc-sqlite"
"github.com/glebarez/sqlite"
)
// DB provides the database interface.
type DB struct {
gormDB *gorm.DB
}
func OpenDB(ctx context.Context, dsn string) (*DB, error) {
log.Info().Str("dsn", dsn).Msg("opening database")
db, err := openDB(ctx, dsn)
if err != nil {
return nil, err
}
if err := db.migrate(); err != nil {
return nil, err
}
log.Debug().Msg("database automigration succesful")
return db, nil
}
func openDB(ctx context.Context, uri string) (*DB, error) {
globalLogLevel := log.Logger.GetLevel()
dblogger := NewDBLogger(log.Level(globalLogLevel))
config := gorm.Config{
Logger: dblogger,
}
return openDBWithConfig(uri, &config)
}
func openDBWithConfig(uri string, config *gorm.Config) (*DB, error) {
dialector := sqlite.Open(uri)
gormDB, err := gorm.Open(dialector, config)
if err != nil {
return nil, err
}
db := DB{
gormDB: gormDB,
}
return &db, nil
}