2022-11-13 19:17:07 +01:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
scn "blackforestbytes.com/simplecloudnotifier"
|
2022-11-20 00:19:41 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/db/schema"
|
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
|
|
|
"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 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()
|
|
|
|
|
2022-11-20 00:19:41 +01:00
|
|
|
currschema, err := db.ReadSchema(ctx)
|
|
|
|
if currschema == 0 {
|
2022-11-13 22:31:28 +01:00
|
|
|
|
2022-11-20 00:19:41 +01:00
|
|
|
_, err = db.db.ExecContext(ctx, schema.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
|
|
|
|
2022-11-20 00:19:41 +01:00
|
|
|
} else if currschema == 1 {
|
2022-11-18 21:25:40 +01:00
|
|
|
return errors.New("cannot autom. upgrade schema 1")
|
2022-11-20 00:19:41 +01:00
|
|
|
} else if currschema == 2 {
|
2022-11-18 21:25:40 +01:00
|
|
|
return errors.New("cannot autom. upgrade schema 2") //TODO
|
2022-11-20 00:19:41 +01:00
|
|
|
} else if currschema == 3 {
|
2022-11-18 21:25:40 +01:00
|
|
|
return nil // current
|
2022-11-13 22:31:28 +01:00
|
|
|
} else {
|
2022-11-20 00:19:41 +01:00
|
|
|
return errors.New(fmt.Sprintf("Unknown DB schema: %d", currschema))
|
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)
|
|
|
|
}
|