Add id column to User table

We used rowid before, but an explicit ID column is cleaner.
This commit is contained in:
Simon Ser 2020-10-24 14:52:55 +02:00
parent 28cf1147e8
commit b3e136e3b7
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

20
db.go
View File

@ -61,8 +61,9 @@ type Channel struct {
const schema = `
CREATE TABLE User (
username VARCHAR(255) PRIMARY KEY,
password VARCHAR(255) NOT NULL,
id INTEGER PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255),
admin INTEGER NOT NULL DEFAULT 0
);
@ -103,6 +104,17 @@ var migrations = []string{
"ALTER TABLE Network ADD COLUMN sasl_external_cert BLOB DEFAULT NULL",
"ALTER TABLE Network ADD COLUMN sasl_external_key BLOB DEFAULT NULL",
"ALTER TABLE User ADD COLUMN admin INTEGER NOT NULL DEFAULT 0",
`
CREATE TABLE UserNew (
id INTEGER PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255),
admin INTEGER NOT NULL DEFAULT 0
);
INSERT INTO UserNew SELECT rowid, username, password, admin FROM User;
DROP TABLE User;
ALTER TABLE UserNew RENAME TO User;
`,
}
type DB struct {
@ -190,7 +202,7 @@ func (db *DB) ListUsers() ([]User, error) {
db.lock.RLock()
defer db.lock.RUnlock()
rows, err := db.db.Query("SELECT rowid, username, password, admin FROM User")
rows, err := db.db.Query("SELECT id, username, password, admin FROM User")
if err != nil {
return nil, err
}
@ -220,7 +232,7 @@ func (db *DB) GetUser(username string) (*User, error) {
user := &User{Username: username}
var password *string
row := db.db.QueryRow("SELECT rowid, password, admin FROM User WHERE username = ?", username)
row := db.db.QueryRow("SELECT id, password, admin FROM User WHERE username = ?", username)
if err := row.Scan(&user.ID, &password, &user.Admin); err != nil {
return nil, err
}