oopsie/sites/sites.go
Lewis Dale 0ea09941a5
All checks were successful
Build and copy to prod / build-and-copy (push) Successful in 36s
Use url as the unique id for a site, rather than an id
2024-05-08 09:26:26 +01:00

35 lines
618 B
Go

package sites
import (
"database/sql"
"fmt"
)
const createSitesTable = `CREATE TABLE IF NOT EXISTS sites (
url TEXT NOT NULL PRIMARY KEY,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
name TEXT NOT NULL
);`
func CreateTable(db *sql.DB) {
if _, err := db.Exec(createSitesTable); err != nil {
fmt.Println(fmt.Errorf(err.Error()))
}
}
type Site struct {
created_at uint64
Name string
Url string
}
const upsertQuery = `INSERT INTO sites (url, name) VALUES (?, ?)
ON CONFLICT (url) DO UPDATE
SET
name = excluded.name
`
func (s *Site) Save(db *sql.DB) {
db.Exec(upsertQuery, s.Url, s.Name)
}