Add ping and status table
This commit is contained in:
parent
0ea09941a5
commit
fe8dcfe3e3
11
main.go
11
main.go
@ -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
58
ping/ping.go
Normal 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) {
|
||||||
|
|
||||||
|
}
|
BIN
test.sqlite3
BIN
test.sqlite3
Binary file not shown.
Loading…
Reference in New Issue
Block a user