61 lines
1.0 KiB
Go
61 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
func InitDatabase(db *sql.DB) error {
|
|
db.Exec(`
|
|
create table migrations (name text) strict;
|
|
`)
|
|
|
|
return nil
|
|
}
|
|
|
|
func MigrateDatabase(db *sql.DB) error {
|
|
rows, err := db.Query(`
|
|
select name from migrations;
|
|
`)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
complete := map[string]bool{}
|
|
|
|
for rows.Next() {
|
|
var name string
|
|
err = rows.Scan(&name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
complete[name] = true
|
|
}
|
|
|
|
if !complete["0000_initial_schema"] {
|
|
_, err = db.Exec(`
|
|
create table sources(name text) strict;
|
|
create table items(
|
|
source text not null,
|
|
id text not null,
|
|
created int not null default (unixepoch()),
|
|
active int,
|
|
title text,
|
|
author text,
|
|
body text,
|
|
link text,
|
|
time int,
|
|
primary key (source, id),
|
|
foreign key (source) references sources (name) on delete cascade
|
|
) strict;
|
|
insert into migrations (name) values ('0000_initial_schema');
|
|
`)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|