2025-01-16 22:53:04 +00:00
|
|
|
package core
|
2025-01-16 19:46:37 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2025-01-17 05:11:07 +00:00
|
|
|
"embed"
|
2025-01-16 21:46:30 +00:00
|
|
|
"fmt"
|
2025-01-17 05:11:07 +00:00
|
|
|
"log"
|
2025-01-16 19:46:37 +00:00
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
)
|
|
|
|
|
2025-01-16 21:46:30 +00:00
|
|
|
type Item struct {
|
|
|
|
source string
|
|
|
|
id string
|
|
|
|
created int
|
|
|
|
active bool
|
|
|
|
title string
|
|
|
|
author string
|
|
|
|
body string
|
|
|
|
link string
|
|
|
|
time int
|
|
|
|
}
|
|
|
|
|
2025-01-17 05:11:07 +00:00
|
|
|
//go:embed sql/*.sql
|
|
|
|
var migrations embed.FS
|
|
|
|
|
|
|
|
// Idempotently initialize the database. Safe to call unconditionally.
|
2025-01-16 19:46:37 +00:00
|
|
|
func InitDatabase(db *sql.DB) error {
|
2025-01-17 05:11:07 +00:00
|
|
|
rows, err := db.Query(`
|
|
|
|
select exists (
|
|
|
|
select 1
|
|
|
|
from sqlite_master
|
|
|
|
where type = 'table'
|
|
|
|
and name = 'migrations'
|
|
|
|
)
|
2025-01-16 19:46:37 +00:00
|
|
|
`)
|
2025-01-17 05:11:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-01-16 19:46:37 +00:00
|
|
|
|
2025-01-17 05:11:07 +00:00
|
|
|
var exists bool
|
|
|
|
for rows.Next() {
|
|
|
|
rows.Scan(&exists)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ApplyMigration(db, "0000_baseline.sql")
|
|
|
|
return err
|
2025-01-16 19:46:37 +00:00
|
|
|
}
|
|
|
|
|
2025-01-17 05:11:07 +00:00
|
|
|
// Get the names of existing migrations that haven't been applied yet.
|
|
|
|
func GetPendingMigrations(db *sql.DB) ([]string, error) {
|
|
|
|
allMigrations, err := migrations.ReadDir("sql")
|
2025-01-16 19:46:37 +00:00
|
|
|
if err != nil {
|
2025-01-17 05:11:07 +00:00
|
|
|
return nil, err
|
2025-01-16 19:46:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
complete := map[string]bool{}
|
2025-01-17 05:11:07 +00:00
|
|
|
for _, mig := range allMigrations {
|
|
|
|
complete[mig.Name()] = false
|
|
|
|
}
|
2025-01-16 19:46:37 +00:00
|
|
|
|
2025-01-17 05:11:07 +00:00
|
|
|
rows, err := db.Query("select name from migrations")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-01-16 19:46:37 +00:00
|
|
|
for rows.Next() {
|
|
|
|
var name string
|
2025-01-17 05:11:07 +00:00
|
|
|
rows.Scan(&name)
|
2025-01-16 19:46:37 +00:00
|
|
|
complete[name] = true
|
|
|
|
}
|
|
|
|
|
2025-01-17 05:11:07 +00:00
|
|
|
var pending []string
|
|
|
|
for name, isComplete := range complete {
|
|
|
|
if !isComplete {
|
|
|
|
pending = append(pending, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pending, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply a migration by name.
|
|
|
|
func ApplyMigration(db *sql.DB, name string) error {
|
|
|
|
data, err := migrations.ReadFile("sql/" + name)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Missing migration %s", name)
|
|
|
|
}
|
|
|
|
log.Printf("Applying migration %s", name)
|
|
|
|
_, err = db.Exec(string(data))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = db.Exec("insert into migrations (name) values (?)", name)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply all pending migrations.
|
|
|
|
func MigrateDatabase(db *sql.DB) error {
|
|
|
|
pending, err := GetPendingMigrations(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, name := range pending {
|
|
|
|
err = ApplyMigration(db, name)
|
2025-01-16 19:46:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2025-01-16 21:46:30 +00:00
|
|
|
|
|
|
|
func AddSource(db *sql.DB, name string) error {
|
|
|
|
_, err := db.Exec(`
|
|
|
|
insert into sources (name)
|
|
|
|
values (?)
|
|
|
|
`, name)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteSource(db *sql.DB, name string) error {
|
|
|
|
_, err := db.Exec(`
|
|
|
|
delete from sources
|
|
|
|
where name = ?
|
|
|
|
`, name)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddItem(
|
|
|
|
db *sql.DB,
|
|
|
|
source string,
|
|
|
|
id string,
|
|
|
|
title string,
|
|
|
|
author string,
|
|
|
|
body string,
|
|
|
|
link string,
|
|
|
|
time int,
|
|
|
|
) error {
|
|
|
|
_, err := db.Exec(`
|
|
|
|
insert into items (source, id, active, title, author, body, link, time)
|
|
|
|
values (?, ?, ?, ?, ?, ?, ?, ?)
|
|
|
|
`, source, id, true, title, author, body, link, time)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeactivateItem(db *sql.DB, source string, id string) error {
|
|
|
|
res, err := db.Exec(`
|
|
|
|
update items
|
|
|
|
set active = 0
|
|
|
|
where source = ? and id = ?
|
|
|
|
`, source, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
num, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if num == 0 {
|
|
|
|
return fmt.Errorf("item %s/%s not found", source, id)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetActiveItems(db *sql.DB, source string) ([]Item, error) {
|
|
|
|
rows, err := db.Query(`
|
|
|
|
select
|
|
|
|
source,
|
|
|
|
id,
|
|
|
|
created,
|
|
|
|
active,
|
|
|
|
title,
|
|
|
|
author,
|
|
|
|
body,
|
|
|
|
link,
|
|
|
|
time
|
|
|
|
from items
|
|
|
|
where
|
|
|
|
source = ?
|
|
|
|
and active <> 0
|
|
|
|
`, source)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var items []Item
|
|
|
|
for rows.Next() {
|
|
|
|
var item Item
|
|
|
|
rows.Scan(&item.source, &item.id, &item.created, &item.active, &item.title, &item.author, &item.body, &item.link, &item.time)
|
|
|
|
items = append(items, item)
|
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|