2021-06-25 09:14:27 +02:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2021-09-19 11:59:26 +02:00
|
|
|
"ewintr.nl/gte/internal/task"
|
2021-09-04 12:20:35 +02:00
|
|
|
"github.com/google/uuid"
|
2021-06-25 09:14:27 +02:00
|
|
|
_ "modernc.org/sqlite"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sqliteMigration string
|
|
|
|
|
|
|
|
var sqliteMigrations = []sqliteMigration{
|
2021-07-24 08:43:05 +02:00
|
|
|
`CREATE TABLE task ("id" TEXT UNIQUE, "version" INTEGER, "folder" TEXT, "action" TEXT, "project" TEXT, "due" TEXT, "recur" TEXT)`,
|
2021-06-25 09:14:27 +02:00
|
|
|
`CREATE TABLE system ("latest_sync" INTEGER)`,
|
|
|
|
`INSERT INTO system (latest_sync) VALUES (0)`,
|
2021-07-24 08:43:05 +02:00
|
|
|
`CREATE TABLE local_id ("id" TEXT UNIQUE, "local_id" INTEGER UNIQUE)`,
|
2021-08-20 09:47:53 +02:00
|
|
|
`ALTER TABLE local_id RENAME TO local_task`,
|
2021-08-21 20:47:29 +02:00
|
|
|
`ALTER TABLE local_task ADD COLUMN local_update TEXT`,
|
2021-08-25 06:52:48 +02:00
|
|
|
`ALTER TABLE task ADD COLUMN local_id INTEGER`,
|
|
|
|
`ALTER TABLE task ADD COLUMN local_update TEXT`,
|
|
|
|
`UPDATE task SET local_id = (SELECT local_id FROM local_task WHERE local_task.id=task.id)`,
|
|
|
|
`UPDATE task SET local_update = (SELECT local_update FROM local_task WHERE local_task.id=task.id)`,
|
|
|
|
`DROP TABLE local_task`,
|
2021-09-01 06:52:21 +02:00
|
|
|
`ALTER TABLE task ADD COLUMN local_status TEXT`,
|
|
|
|
`UPDATE task SET local_status = "fetched"`,
|
2021-06-25 09:14:27 +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")
|
|
|
|
)
|
|
|
|
|
|
|
|
type SqliteConfig struct {
|
|
|
|
DBPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sqlite is an sqlite implementation of LocalRepository
|
|
|
|
type Sqlite struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSqlite(conf *SqliteConfig) (*Sqlite, error) {
|
|
|
|
db, err := sql.Open("sqlite", conf.DBPath)
|
|
|
|
if err != nil {
|
|
|
|
return &Sqlite{}, fmt.Errorf("%w: %v", ErrInvalidConfiguration, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s := &Sqlite{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.migrate(sqliteMigrations); err != nil {
|
|
|
|
return &Sqlite{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sqlite) LatestSync() (time.Time, error) {
|
2021-11-01 10:19:32 +01:00
|
|
|
rows, err := s.db.Query(`SELECT strftime('%s', latest_sync) FROM system`)
|
2021-06-25 09:14:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
rows.Next()
|
|
|
|
var latest int64
|
|
|
|
if err := rows.Scan(&latest); err != nil {
|
|
|
|
return time.Time{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Unix(latest, 0), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Sqlite) SetTasks(tasks []*task.Task) error {
|
2021-11-01 10:19:32 +01:00
|
|
|
tx, err := s.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2021-08-25 06:52:48 +02:00
|
|
|
oldTasks, err := s.FindAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
2021-08-25 06:52:48 +02:00
|
|
|
newTasks := MergeNewTaskSet(oldTasks, tasks)
|
2021-06-25 09:14:27 +02:00
|
|
|
|
2021-08-25 06:52:48 +02:00
|
|
|
if _, err := s.db.Exec(`DELETE FROM task`); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
2021-08-24 07:26:58 +02:00
|
|
|
}
|
2021-08-25 06:52:48 +02:00
|
|
|
for _, t := range newTasks {
|
2021-06-25 09:14:27 +02:00
|
|
|
var recurStr string
|
|
|
|
if t.Recur != nil {
|
|
|
|
recurStr = t.Recur.String()
|
|
|
|
}
|
2021-08-25 06:52:48 +02:00
|
|
|
|
2021-06-25 09:14:27 +02:00
|
|
|
_, err := s.db.Exec(`
|
|
|
|
INSERT INTO task
|
2021-09-01 06:52:21 +02:00
|
|
|
(id, local_id, version, folder, action, project, due, recur, local_update, local_status)
|
2021-06-25 09:14:27 +02:00
|
|
|
VALUES
|
2021-09-01 06:52:21 +02:00
|
|
|
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
|
|
t.Id, t.LocalId, t.Version, t.Folder, t.Action, t.Project, t.Due.String(), recurStr, t.LocalUpdate, t.LocalStatus)
|
2021-07-24 14:11:51 +02:00
|
|
|
|
2021-08-25 06:52:48 +02:00
|
|
|
if err != nil {
|
2021-07-14 07:17:53 +02:00
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
|
|
|
|
2021-11-01 10:19:32 +01:00
|
|
|
if _, err := s.db.Exec(`UPDATE system SET latest_sync=DATETIME('now')`); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
2021-06-25 09:14:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-25 06:52:48 +02:00
|
|
|
func (s *Sqlite) FindAll() ([]*task.LocalTask, error) {
|
2021-06-25 09:14:27 +02:00
|
|
|
rows, err := s.db.Query(`
|
2021-09-01 06:52:21 +02:00
|
|
|
SELECT id, local_id, version, folder, action, project, due, recur, local_update, local_status
|
2021-08-25 06:52:48 +02:00
|
|
|
FROM task`)
|
2021-06-25 09:14:27 +02:00
|
|
|
if err != nil {
|
2021-08-20 09:06:35 +02:00
|
|
|
return []*task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
|
|
|
|
2021-09-01 06:52:21 +02:00
|
|
|
tasks := []*task.LocalTask{}
|
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
var id, folder, action, project, due, recur, localStatus string
|
|
|
|
var localId, version int
|
|
|
|
var localUpdate task.LocalUpdate
|
|
|
|
if err := rows.Scan(&id, &localId, &version, &folder, &action, &project, &due, &recur, &localUpdate, &localStatus); err != nil {
|
|
|
|
return []*task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
tasks = append(tasks, &task.LocalTask{
|
|
|
|
Task: task.Task{
|
|
|
|
Id: id,
|
|
|
|
Version: version,
|
|
|
|
Folder: folder,
|
|
|
|
Action: action,
|
|
|
|
Project: project,
|
|
|
|
Due: task.NewDateFromString(due),
|
|
|
|
Recur: task.NewRecurrer(recur),
|
|
|
|
},
|
|
|
|
LocalId: localId,
|
|
|
|
LocalUpdate: &localUpdate,
|
|
|
|
LocalStatus: localStatus,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return tasks, nil
|
2021-06-25 09:14:27 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 09:06:35 +02:00
|
|
|
func (s *Sqlite) FindById(id string) (*task.LocalTask, error) {
|
2021-09-01 06:52:21 +02:00
|
|
|
var folder, action, project, due, recur, localStatus string
|
2021-08-20 09:30:49 +02:00
|
|
|
var localId, version int
|
2021-08-21 20:47:29 +02:00
|
|
|
var localUpdate task.LocalUpdate
|
2021-07-10 12:30:38 +02:00
|
|
|
row := s.db.QueryRow(`
|
2021-09-01 06:52:21 +02:00
|
|
|
SELECT local_id, version, folder, action, project, due, recur, local_update, local_status
|
2021-07-10 12:30:38 +02:00
|
|
|
FROM task
|
2021-08-20 09:30:49 +02:00
|
|
|
WHERE task.id = ?
|
2021-07-10 12:30:38 +02:00
|
|
|
LIMIT 1`, id)
|
2021-09-01 06:52:21 +02:00
|
|
|
if err := row.Scan(&localId, &version, &folder, &action, &project, &due, &recur, &localUpdate, &localStatus); err != nil {
|
2021-08-20 09:06:35 +02:00
|
|
|
return &task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
2021-07-10 12:30:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 09:06:35 +02:00
|
|
|
return &task.LocalTask{
|
|
|
|
Task: task.Task{
|
|
|
|
Id: id,
|
|
|
|
Version: version,
|
|
|
|
Folder: folder,
|
|
|
|
Action: action,
|
|
|
|
Project: project,
|
|
|
|
Due: task.NewDateFromString(due),
|
|
|
|
Recur: task.NewRecurrer(recur),
|
2021-08-20 09:30:49 +02:00
|
|
|
},
|
2021-08-21 20:47:29 +02:00
|
|
|
LocalId: localId,
|
|
|
|
LocalUpdate: &localUpdate,
|
2021-09-01 06:52:21 +02:00
|
|
|
LocalStatus: localStatus,
|
2021-08-20 09:30:49 +02:00
|
|
|
}, nil
|
2021-07-10 12:30:38 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 09:06:35 +02:00
|
|
|
func (s *Sqlite) FindByLocalId(localId int) (*task.LocalTask, error) {
|
2021-07-14 07:17:53 +02:00
|
|
|
var id string
|
2021-08-25 06:52:48 +02:00
|
|
|
row := s.db.QueryRow(`SELECT id FROM task WHERE local_id = ?`, localId)
|
2021-07-14 07:17:53 +02:00
|
|
|
if err := row.Scan(&id); err != nil {
|
2021-08-20 09:06:35 +02:00
|
|
|
return &task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
2021-07-14 07:17:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
t, err := s.FindById(id)
|
|
|
|
if err != nil {
|
2021-08-20 09:06:35 +02:00
|
|
|
return &task.LocalTask{}, nil
|
2021-07-14 07:17:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2021-09-04 12:20:35 +02:00
|
|
|
func (s *Sqlite) SetLocalUpdate(id string, update *task.LocalUpdate) error {
|
2021-08-21 20:47:29 +02:00
|
|
|
if _, err := s.db.Exec(`
|
2021-08-25 06:52:48 +02:00
|
|
|
UPDATE task
|
2021-09-01 06:52:21 +02:00
|
|
|
SET local_update = ?, local_status = ?
|
2021-09-04 12:20:35 +02:00
|
|
|
WHERE id = ?`, update, task.STATUS_UPDATED, id); err != nil {
|
2021-08-21 20:47:29 +02:00
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-03 09:19:36 +02:00
|
|
|
func (s *Sqlite) MarkDispatched(localId int) error {
|
|
|
|
if _, err := s.db.Exec(`
|
|
|
|
UPDATE task
|
|
|
|
SET local_status = ?
|
|
|
|
WHERE local_id = ?`, task.STATUS_DISPATCHED, localId); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-04 12:20:35 +02:00
|
|
|
func (s *Sqlite) Add(update *task.LocalUpdate) (*task.LocalTask, error) {
|
|
|
|
rows, err := s.db.Query(`SELECT local_id FROM task`)
|
|
|
|
if err != nil {
|
|
|
|
return &task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
var used []int
|
|
|
|
for rows.Next() {
|
|
|
|
var localId int
|
|
|
|
if err := rows.Scan(&localId); err != nil {
|
|
|
|
return &task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
used = append(used, localId)
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
|
|
|
|
tsk := &task.LocalTask{
|
|
|
|
Task: task.Task{
|
|
|
|
Id: uuid.New().String(),
|
|
|
|
Version: 0,
|
|
|
|
Folder: task.FOLDER_NEW,
|
|
|
|
},
|
|
|
|
LocalId: NextLocalId(used),
|
|
|
|
LocalStatus: task.STATUS_UPDATED,
|
|
|
|
LocalUpdate: update,
|
|
|
|
}
|
|
|
|
|
|
|
|
var recurStr string
|
|
|
|
if tsk.LocalUpdate.Recur != nil {
|
|
|
|
recurStr = tsk.LocalUpdate.Recur.String()
|
|
|
|
}
|
|
|
|
if _, err := s.db.Exec(`
|
|
|
|
INSERT INTO task
|
|
|
|
(id, local_id, version, folder, action, project, due, recur, local_status, local_update)
|
|
|
|
VALUES
|
|
|
|
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
|
|
tsk.Id, tsk.LocalId, tsk.Version, tsk.Folder, tsk.Action, tsk.Project,
|
|
|
|
tsk.Due.String(), recurStr, tsk.LocalStatus, tsk.LocalUpdate); err != nil {
|
|
|
|
return &task.LocalTask{}, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tsk, nil
|
|
|
|
}
|
|
|
|
|
2021-06-25 09:14:27 +02:00
|
|
|
func (s *Sqlite) migrate(wanted []sqliteMigration) error {
|
|
|
|
// 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 := []sqliteMigration{}
|
|
|
|
for rows.Next() {
|
|
|
|
var query string
|
|
|
|
if err := rows.Scan(&query); err != nil {
|
|
|
|
return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
|
|
|
|
}
|
|
|
|
existing = append(existing, sqliteMigration(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 []sqliteMigration) ([]sqliteMigration, error) {
|
|
|
|
needed := []sqliteMigration{}
|
|
|
|
if len(wanted) < len(existing) {
|
|
|
|
return []sqliteMigration{}, 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 []sqliteMigration{}, fmt.Errorf("%w: %v", ErrIncompatibleSQLMigration, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return needed, nil
|
|
|
|
}
|