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 (
|
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
BIN
test.sqlite3
BIN
test.sqlite3
Binary file not shown.
Loading…
Reference in New Issue
Block a user