Change database schema

This commit is contained in:
Andinus 2020-03-27 14:38:31 +05:30
parent 5d65b69f75
commit f22adf24e8
Signed by: andinus
GPG Key ID: B67D55D482A799FD
2 changed files with 18 additions and 47 deletions

View File

@ -11,7 +11,7 @@ import (
// Register takes in registration details and returns an error. If
// error doesn't equal nil then the registration was unsuccessful.
// regInfo should have username, password & ip.
// regInfo should have username & password.
func Register(db *sqlite3.DB, regInfo map[string]string) error {
u := user.User{}
u.SetID(genID(64))
@ -29,11 +29,11 @@ func Register(db *sqlite3.DB, regInfo map[string]string) error {
db.Mu.Lock()
defer db.Mu.Unlock()
err = insertRecords(db, u, regInfo)
err = insertRegRecords(db, u)
return err
}
func insertRecords(db *sqlite3.DB, u user.User, regInfo map[string]string) error {
func insertRegRecords(db *sqlite3.DB, u user.User) error {
// Start the transaction
tx, err := db.Conn.Begin()
if err != nil {
@ -42,26 +42,8 @@ func insertRecords(db *sqlite3.DB, u user.User, regInfo map[string]string) error
return err
}
// Insert the record into registration table
regStmt, err := db.Conn.Prepare(`
INSERT INTO registration(id, username, reg_time, reg_ip) values(?, ?, ?, ?)`)
if err != nil {
log.Printf("auth/register.go: %s\n",
"Failed to prepare statement")
return err
}
defer regStmt.Close()
_, err = regStmt.Exec(u.ID(), u.Username(), time.Now().UTC(), regInfo["ip"])
if err != nil {
log.Printf("auth/register.go: %s\n",
"Failed to execute statement")
return err
}
// Insert the record into users table
usrStmt, err := db.Conn.Prepare(`
INSERT INTO users(id, username, password) values(?, ?, ?)`)
INSERT INTO users(id, username, password, regTime) values(?, ?, ?, ?)`)
if err != nil {
log.Printf("auth/register.go: %s\n",
"Failed to prepare statement")
@ -69,7 +51,7 @@ INSERT INTO users(id, username, password) values(?, ?, ?)`)
}
defer usrStmt.Close()
_, err = usrStmt.Exec(u.ID(), u.Username(), u.Password())
_, err = usrStmt.Exec(u.ID(), u.Username(), u.Password(), time.Now().UTC())
if err != nil {
log.Printf("auth/register.go: %s\n",
"Failed to execute statement")

View File

@ -41,32 +41,21 @@ func Init(db *DB) {
}
sqlstmt := []string{
// Create users table, this will hold information on
// account like id, type & other user specific
// information. We are using id because later we may
// want to add username change or account delete
// functionality. username here is not unique because
// if user deletes account then we'll change it to
// "ghost" or something. This doesn't mean usernames
// shouldn't be unique, registration table requires
// them to be unique so it'll fail if they aren't
// unique.
// Users can login with multiple devices and so
// multiple tokens will be created. This shouldn't be
// used for login, logins should be verified with
// users table only.
`CREATE TABLE IF NOT EXISTS access (
id TEXT NOT NULL,
token TEXT NOT NULL,
genTime TEXT NOT NULL);`,
`CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
type TEXT NOT NULL DEFAULT notadmin,
username TEXT NOT NULL,
password TEXT NOT NULL);`,
// Create registration table, this will hold user
// account details like registration time, ip &
// similar details. This is the only place that will
// relate the username to id even after deletion.
// usernames must be unique in this table.
`CREATE TABLE IF NOT EXISTS registration (
id TEXT PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
reg_time TEXT NOT NULL,
reg_ip TEXT NOT NULL);`,
type TEXT NOT NULL DEFAULT user,
username VARCHAR(128) NOT NULL UNIQUE,
password TEXT NOT NULL,
regTime TEXT NOT NULL);`,
}
// We range over statements and execute them one by one, this