2022-11-13 19:17:07 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
scn "blackforestbytes.com/simplecloudnotifier"
|
2022-11-13 22:31:28 +01:00
|
|
|
"context"
|
2022-11-13 19:17:07 +01:00
|
|
|
"database/sql"
|
2022-11-13 22:31:28 +01:00
|
|
|
_ "embed"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-11-13 19:17:07 +01:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2022-11-18 21:25:40 +01:00
|
|
|
"time"
|
2022-11-13 19:17:07 +01:00
|
|
|
)
|
|
|
|
|
2022-11-18 23:12:37 +01:00
|
|
|
//go:embed schema_1.ddl
|
|
|
|
var schema1 string
|
2022-11-13 22:31:28 +01:00
|
|
|
|
2022-11-18 23:12:37 +01:00
|
|
|
//go:embed schema_2.ddl
|
|
|
|
var schema2 string
|
2022-11-13 22:31:28 +01:00
|
|
|
|
2022-11-18 23:12:37 +01:00
|
|
|
//go:embed schema_3.ddl
|
|
|
|
var schema3 string
|
2022-11-18 21:25:40 +01:00
|
|
|
|
|
|
|
type Database struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDatabase(conf scn.Config) (*Database, error) {
|
2022-11-13 22:31:28 +01:00
|
|
|
db, err := sql.Open("sqlite3", conf.DBFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:25:40 +01:00
|
|
|
return &Database{db}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) Migrate(ctx context.Context) error {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 24*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
schema, err := db.ReadSchema(ctx)
|
2022-11-13 22:31:28 +01:00
|
|
|
if schema == 0 {
|
|
|
|
|
2022-11-18 23:12:37 +01:00
|
|
|
_, err = db.db.ExecContext(ctx, schema3)
|
2022-11-13 22:31:28 +01:00
|
|
|
if err != nil {
|
2022-11-18 21:25:40 +01:00
|
|
|
return err
|
2022-11-13 22:31:28 +01:00
|
|
|
}
|
|
|
|
|
2022-11-18 21:25:40 +01:00
|
|
|
return nil
|
2022-11-13 22:31:28 +01:00
|
|
|
|
|
|
|
} else if schema == 1 {
|
2022-11-18 21:25:40 +01:00
|
|
|
return errors.New("cannot autom. upgrade schema 1")
|
2022-11-13 22:31:28 +01:00
|
|
|
} else if schema == 2 {
|
2022-11-18 21:25:40 +01:00
|
|
|
return errors.New("cannot autom. upgrade schema 2") //TODO
|
|
|
|
} else if schema == 3 {
|
|
|
|
return nil // current
|
2022-11-13 22:31:28 +01:00
|
|
|
} else {
|
2022-11-18 21:25:40 +01:00
|
|
|
return errors.New(fmt.Sprintf("Unknown DB schema: %d", schema))
|
2022-11-13 22:31:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:25:40 +01:00
|
|
|
func (db *Database) ReadSchema(ctx context.Context) (int, error) {
|
2022-11-13 22:31:28 +01:00
|
|
|
|
2022-11-18 21:25:40 +01:00
|
|
|
r1, err := db.db.QueryContext(ctx, "SELECT name FROM sqlite_master WHERE type='table' AND name='meta'")
|
2022-11-13 22:31:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !r1.Next() {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
|
2022-11-18 21:25:40 +01:00
|
|
|
r2, err := db.db.QueryContext(ctx, "SELECT value_int FROM meta WHERE meta_key='schema'")
|
2022-11-13 22:31:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if !r2.Next() {
|
|
|
|
return 0, errors.New("no schema entry in meta table")
|
|
|
|
}
|
|
|
|
|
|
|
|
var schema int
|
|
|
|
err = r2.Scan(&schema)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return schema, nil
|
2022-11-13 19:17:07 +01:00
|
|
|
}
|
2022-11-18 21:25:40 +01:00
|
|
|
|
|
|
|
func (db *Database) Ping() error {
|
|
|
|
return db.db.Ping()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Database) BeginTx(ctx context.Context) (*sql.Tx, error) {
|
|
|
|
return db.db.BeginTx(ctx, nil)
|
|
|
|
}
|