Add ping and status table

This commit is contained in:
Lewis Dale 2024-05-09 07:12:24 +01:00
parent 0ea09941a5
commit fe8dcfe3e3
3 changed files with 62 additions and 7 deletions

11
main.go
View File

@ -5,20 +5,17 @@ import (
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"lewisdale.dev/oopsie/data" "lewisdale.dev/oopsie/data"
"lewisdale.dev/oopsie/ping"
"lewisdale.dev/oopsie/sites" "lewisdale.dev/oopsie/sites"
) )
const createSitesTable = `CREATE TABLE IF NOT EXISTS sites (
id INTEGER NOT NULL PRIMARY KEY,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
name TEXT NOT NULL,
url TEXT NOT NULL
);`
func main() { func main() {
db := data.Connect("test.sqlite3") db := data.Connect("test.sqlite3")
db.Exec("PRAGMA foreign_keys = ON;")
sites.CreateTable(db) sites.CreateTable(db)
ping.CreateTable(db)
site := sites.Site{Name: "Lewisdale.dev", Url: "https://lewisdale.dev"} site := sites.Site{Name: "Lewisdale.dev", Url: "https://lewisdale.dev"}
site.Save(db) site.Save(db)

58
ping/ping.go Normal file
View File

@ -0,0 +1,58 @@
package ping
import (
"database/sql"
"lewisdale.dev/oopsie/sites"
)
type Status int16
const (
Success Status = iota
Failure
)
type Ping struct {
Site sites.Site
Timestamp string
Status Status
}
const createQuery = `CREATE TABLE IF NOT EXISTS statuses (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS ping (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
site TEXT NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
status INTEGER NOT NULL,
FOREIGN KEY (site) REFERENCES sites(url),
FOREIGN KEY (status) REFERENCES statuses(id)
);`
const seedStatusQuery = `INSERT INTO statuses (id, name) VALUES (?, ?)
ON CONFLICT (id) DO NOTHING`
func CreateTable(db *sql.DB) {
if _, err := db.Exec(createQuery); err != nil {
panic(err)
}
seedStatuses(db)
}
func seedStatuses(db *sql.DB) {
if _, err := db.Exec(seedStatusQuery, Success, "Success"); err != nil {
panic(err)
}
if _, err := db.Exec(seedStatusQuery, Failure, "Failure"); err != nil {
panic(err)
}
}
func (p *Ping) Save(db *sql.DB) {
}

Binary file not shown.