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
All checks were successful
Build and copy to prod / build-and-copy (push) Successful in 36s
This commit is contained in:
parent
49d33b2366
commit
0ea09941a5
@ -6,10 +6,9 @@ import (
|
||||
)
|
||||
|
||||
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,
|
||||
name TEXT NOT NULL,
|
||||
url TEXT NOT NULL
|
||||
name TEXT NOT NULL
|
||||
);`
|
||||
|
||||
func CreateTable(db *sql.DB) {
|
||||
@ -19,23 +18,17 @@ func CreateTable(db *sql.DB) {
|
||||
}
|
||||
|
||||
type Site struct {
|
||||
id uint64
|
||||
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) {
|
||||
if s.id != 0 {
|
||||
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)
|
||||
}
|
||||
db.Exec(upsertQuery, s.Url, s.Name)
|
||||
}
|
||||
|
BIN
test.sqlite3
BIN
test.sqlite3
Binary file not shown.
Loading…
Reference in New Issue
Block a user