2024-10-05 10:12:45 +02:00
|
|
|
package sqlite
|
2024-09-24 07:44:00 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
timestampFormat = "2006-01-02 15:04:05"
|
|
|
|
)
|
|
|
|
|
|
|
|
var migrations = []string{
|
2024-09-26 07:21:48 +02:00
|
|
|
`CREATE TABLE events ("id" TEXT UNIQUE, "title" TEXT, "start" TIMESTAMP, "duration" TEXT)`,
|
2024-09-24 07:44:00 +02:00
|
|
|
`PRAGMA journal_mode=WAL`,
|
|
|
|
`PRAGMA synchronous=NORMAL`,
|
|
|
|
`PRAGMA cache_size=2000`,
|
2024-10-05 10:12:45 +02:00
|
|
|
`CREATE TABLE localids ("id" TEXT UNIQUE, "local_id" INTEGER)`,
|
2024-09-24 07:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrInvalidConfiguration = errors.New("invalid configuration")
|
|
|
|
ErrIncompatibleSQLMigration = errors.New("incompatible migration")
|
|
|
|
ErrNotEnoughSQLMigrations = errors.New("already more migrations than wanted")
|
|
|
|
ErrSqliteFailure = errors.New("sqlite returned an error")
|
|
|
|
)
|
|
|
|
|
2024-10-04 07:31:44 +02:00
|
|
|
type SqliteLocal struct {
|
2024-09-24 07:44:00 +02:00
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
2024-10-04 07:31:44 +02:00
|
|
|
func NewSqlites(dbPath string) (*SqliteLocal, *SqliteEvent, error) {
|
2024-09-24 07:44:00 +02:00
|
|
|
db, err := sql.Open("sqlite", dbPath)
|
|
|
|
if err != nil {
|
2024-10-04 07:31:44 +02:00
|
|
|
return nil, nil, fmt.Errorf("%w: %v", ErrInvalidConfiguration, err)
|
2024-09-24 07:44:00 +02:00
|
|
|
}
|
|
|
|
|
2024-10-04 07:31:44 +02:00
|
|
|
sl := &SqliteLocal{
|
2024-09-24 07:44:00 +02:00
|
|
|
db: db,
|
|
|
|
}
|
2024-10-04 07:31:44 +02:00
|
|
|
se := &SqliteEvent{
|
|
|
|
db: db,
|
2024-09-24 07:44:00 +02:00
|
|
|
}
|
|
|
|
|
2024-10-04 07:31:44 +02:00
|
|
|
if err := sl.migrate(migrations); err != nil {
|
|
|
|
return nil, nil, err
|
2024-09-24 07:44:00 +02:00
|
|
|
}
|
|
|
|
|
2024-10-04 07:31:44 +02:00
|
|
|
return sl, se, nil
|
2024-09-24 07:44:00 +02:00
|
|
|
}
|
|
|
|
|
2024-10-04 07:31:44 +02:00
|
|
|
func (s *SqliteLocal) migrate(wanted []string) error {
|
2024-09-24 07:44:00 +02:00
|
|
|
// admin table
|
|
|
|
if _, err := s.db.Exec(`
|
|
|
|
CREATE TABLE IF NOT EXISTS migration
|
|
|
|
("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "query" TEXT)
|
|
|
|
`); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// find existing
|
|
|
|
rows, err := s.db.Query(`SELECT query FROM migration ORDER BY id`)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
existing := []string{}
|
|
|
|
for rows.Next() {
|
|
|
|
var query string
|
|
|
|
if err := rows.Scan(&query); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
existing = append(existing, string(query))
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
|
|
|
|
// compare
|
|
|
|
missing, err := compareMigrations(wanted, existing)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// execute missing
|
|
|
|
for _, query := range missing {
|
|
|
|
if _, err := s.db.Exec(string(query)); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// register
|
|
|
|
if _, err := s.db.Exec(`
|
|
|
|
INSERT INTO migration
|
|
|
|
(query) VALUES (?)
|
|
|
|
`, query); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareMigrations(wanted, existing []string) ([]string, error) {
|
|
|
|
needed := []string{}
|
|
|
|
if len(wanted) < len(existing) {
|
|
|
|
return []string{}, ErrNotEnoughSQLMigrations
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, want := range wanted {
|
|
|
|
switch {
|
|
|
|
case i >= len(existing):
|
|
|
|
needed = append(needed, want)
|
|
|
|
case want == existing[i]:
|
|
|
|
// do nothing
|
|
|
|
case want != existing[i]:
|
|
|
|
return []string{}, fmt.Errorf("%w: %v", ErrIncompatibleSQLMigration, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return needed, nil
|
|
|
|
}
|