Use url as the unique id for a site, rather than an id
All checks were successful
Build and copy to prod / build-and-copy (push) Successful in 36s

This commit is contained in:
Lewis Dale 2024-05-08 09:26:26 +01:00
parent 49d33b2366
commit 0ea09941a5
2 changed files with 9 additions and 16 deletions

View File

@ -6,10 +6,9 @@ import (
) )
const createSitesTable = `CREATE TABLE IF NOT EXISTS sites ( const createSitesTable = `CREATE TABLE IF NOT EXISTS sites (
id INTEGER NOT NULL PRIMARY KEY, url TEXT NOT NULL PRIMARY KEY,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
name TEXT NOT NULL, name TEXT NOT NULL
url TEXT NOT NULL
);` );`
func CreateTable(db *sql.DB) { func CreateTable(db *sql.DB) {
@ -19,23 +18,17 @@ func CreateTable(db *sql.DB) {
} }
type Site struct { type Site struct {
id uint64
created_at uint64 created_at uint64
Name string Name string
Url 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) { func (s *Site) Save(db *sql.DB) {
if s.id != 0 { db.Exec(upsertQuery, s.Url, s.Name)
query := `UPDATE SITES
SET
name=?,
url=?
WHERE id =?
`
db.Exec(query, s.Name, s.Url, s.id)
} else {
query := `INSERT INTO SITES (name, url) VALUES (?, ?)`
db.Exec(query, s.Name, s.Url)
}
} }

Binary file not shown.