Compare commits

..

No commits in common. "c4c02533e77d0ba4a6c2e49d5dcb076121c6d36d" and "c0a8c50dbb04cacde59d984728bd9d218cd2825f" have entirely different histories.

3 changed files with 2 additions and 71 deletions

View File

@ -1,12 +0,0 @@
#!/bin/bash
set -e
dbpath="/town/var/town.db"
srcpath="/town/src/town"
rm -f "$dbpath"
sqlite3 < "${srcpath}/sql/create_town_db.sql" "$dbpath"
chown root:admin "$dbpath"
chmod o-r "$dbpath"
chmod g+w "$dbpath"

View File

@ -9,17 +9,16 @@ CREATE TABLE IF NOT EXISTS users (
FOREIGN KEY (signupid) REFERENCES signups(signupid) FOREIGN KEY (signupid) REFERENCES signups(signupid)
); );
-- TODO address /should/ be unique but leaving it duplicable for now since i can think of some cases where there might be >1 account for the same human
CREATE TABLE IF NOT EXISTS emails ( CREATE TABLE IF NOT EXISTS emails (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
address TEXT, address TEXT UNIQUE,
userid INTEGER, userid INTEGER,
FOREIGN KEY (userid) REFERENCES users(userid) FOREIGN KEY (userid) REFERENCES users(userid)
); );
CREATE TABLE IF NOT EXISTS user_notes ( CREATE TABLE IF NOT EXISTS user_notes (
noteid INTEGER, noteid INTEGER,
userid INTEGER, userid INTEGER,
PRIMARY KEY (noteid, userid), PRIMARY KEY (noteid, userid),

View File

@ -59,7 +59,6 @@ func (n *AdminNote) Insert(db *sql.DB) error {
type TownUser struct { type TownUser struct {
ID int64 ID int64
Created time.Time
Emails []string Emails []string
Username string Username string
Notes []AdminNote Notes []AdminNote
@ -67,61 +66,6 @@ type TownUser struct {
IsAdmin bool IsAdmin bool
} }
func (u *TownUser) Insert(db *sql.DB) (err error) {
var tx *sql.Tx
var stmt *sql.Stmt
var result sql.Result
var liid int64
if tx, err = db.Begin(); err != nil {
return err
}
defer func() {
if err != nil {
tx.Rollback()
}
}()
u.Created = time.Now()
if stmt, err = tx.Prepare(`
INSERT INTO notes (created, username, state, admin)
VALUES ( ?, ?, ?, ?)`); err != nil {
return err
}
if result, err = stmt.Exec(
u.Created.Unix(),
u.Username,
u.State,
u.IsAdmin); err != nil {
return err
}
if liid, err = result.LastInsertId(); err != nil {
return err
}
u.ID = liid
if len(u.Emails) > 0 {
for _, e := range u.Emails {
if stmt, err = tx.Prepare(`
INSERT INTO emails (address, userid)
VALUES (?, ?)`); err != nil {
return err
}
if result, err = stmt.Exec(e, u.ID); err != nil {
return err
}
}
}
tx.Commit()
return nil
}
func ConnectDB() (*sql.DB, error) { func ConnectDB() (*sql.DB, error) {
db, err := sql.Open("sqlite3", dsn) db, err := sql.Open("sqlite3", dsn)
if err != nil { if err != nil {