From 736cebe036dfadd9a90dd093b8ed25291ea901fa Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 12 Mar 2024 20:44:54 +0100 Subject: [PATCH] Sql comments and salts table --- .../internal/database/migrations/0010_users.sql | 5 +++++ .../internal/database/migrations/0070_salts.sql | 15 +++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 backend/internal/database/migrations/0070_salts.sql diff --git a/backend/internal/database/migrations/0010_users.sql b/backend/internal/database/migrations/0010_users.sql index 7cb23fd..5c9d329 100644 --- a/backend/internal/database/migrations/0010_users.sql +++ b/backend/internal/database/migrations/0010_users.sql @@ -1,3 +1,7 @@ +-- Id is a surrogate key for in ternal use +-- userId is what is used for external id +-- username is what is used for login +-- password is the hashed password CREATE TABLE IF NOT EXISTS users ( id INTEGER PRIMARY KEY, userId TEXT DEFAULT (HEX(RANDOMBLOB(4))) NOT NULL UNIQUE, @@ -5,5 +9,6 @@ CREATE TABLE IF NOT EXISTS users ( password VARCHAR(255) NOT NULL ); +-- Users are commonly searched by username and userId CREATE INDEX IF NOT EXISTS users_username_index ON users (username); CREATE INDEX IF NOT EXISTS users_userId_index ON users (userId); \ No newline at end of file diff --git a/backend/internal/database/migrations/0070_salts.sql b/backend/internal/database/migrations/0070_salts.sql new file mode 100644 index 0000000..9fb0588 --- /dev/null +++ b/backend/internal/database/migrations/0070_salts.sql @@ -0,0 +1,15 @@ +-- It is unclear weather this table will be used + +-- Create the table to store hash salts +CREATE TABLE salts ( + id INTEGER PRIMARY KEY, + salt TEXT NOT NULL +); + +-- Create a trigger to automatically generate a salt when inserting a new user record +CREATE TRIGGER generate_salt_trigger +AFTER INSERT ON users +BEGIN + INSERT INTO salts (salt) VALUES (randomblob(16)); + UPDATE users SET salt_id = (SELECT last_insert_rowid()) WHERE id = new.id; +END;