deleted column in sqlite

This commit is contained in:
Erik Winter 2024-09-09 07:32:05 +02:00
parent 5f435b7440
commit 42fb906dad
2 changed files with 13 additions and 12 deletions

View File

@ -13,11 +13,11 @@ const (
) )
type Syncable struct { type Syncable struct {
ID string ID string `json:"id"`
Kind Kind Kind Kind `json:"kind"`
Updated time.Time Updated time.Time `json:"updated"`
Deleted bool Deleted bool `json:"deleted"`
Item string Item string `json:"item"`
} }
func NewSyncable(item string) Syncable { func NewSyncable(item string) Syncable {

View File

@ -14,7 +14,7 @@ const (
) )
var migrations = []string{ var migrations = []string{
`CREATE TABLE items ("id" TEXT UNIQUE, "kind" TEXT, "updated" TIMESTAMP, "body" TEXT)`, `CREATE TABLE items ("id" TEXT UNIQUE, "kind" TEXT, "updated" TIMESTAMP, "deleted" INTEGER, "body" TEXT)`,
`PRAGMA journal_mode=WAL`, `PRAGMA journal_mode=WAL`,
`PRAGMA synchronous=NORMAL`, `PRAGMA synchronous=NORMAL`,
`PRAGMA cache_size=2000`, `PRAGMA cache_size=2000`,
@ -51,16 +51,17 @@ func NewSqlite(dbPath string) (*Sqlite, error) {
func (s *Sqlite) Update(item Syncable) error { func (s *Sqlite) Update(item Syncable) error {
if _, err := s.db.Exec(` if _, err := s.db.Exec(`
INSERT INTO items INSERT INTO items
(id, kind, updated, body) (id, kind, updated, deleted, body)
VALUES VALUES
(?, ?, ?, ?) (?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE ON CONFLICT(id) DO UPDATE
SET SET
kind=?, kind=?,
updated=?, updated=?,
deleted=?,
body=?`, body=?`,
item.ID, item.Kind, item.Updated.Format(timestampFormat), item.Item, item.ID, item.Kind, item.Updated.Format(timestampFormat), item.Deleted, item.Item,
item.Kind, item.Updated.Format(timestampFormat), item.Item); err != nil { item.Kind, item.Updated.Format(timestampFormat), item.Deleted, item.Item); err != nil {
return fmt.Errorf("%w: %v", ErrSqliteFailure, err) return fmt.Errorf("%w: %v", ErrSqliteFailure, err)
} }
return nil return nil
@ -68,7 +69,7 @@ body=?`,
func (s *Sqlite) Updated(t time.Time) ([]Syncable, error) { func (s *Sqlite) Updated(t time.Time) ([]Syncable, error) {
rows, err := s.db.Query(` rows, err := s.db.Query(`
SELECT id, kind, updated, body SELECT id, kind, updated, deleted, body
FROM items FROM items
WHERE updated > ?`, t.Format(timestampFormat)) WHERE updated > ?`, t.Format(timestampFormat))
if err != nil { if err != nil {
@ -79,7 +80,7 @@ WHERE updated > ?`, t.Format(timestampFormat))
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var item Syncable var item Syncable
if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Item); err != nil { if err := rows.Scan(&item.ID, &item.Kind, &item.Updated, &item.Deleted, &item.Item); err != nil {
return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err) return nil, fmt.Errorf("%w: %v", ErrSqliteFailure, err)
} }
result = append(result, item) result = append(result, item)