BEGIN TRANSACTION;

PRAGMA foreign_keys = ON;

DROP TABLE IF EXISTS config;
CREATE TABLE config (
	name TEXT UNIQUE,  -- variable name
	value ANY          -- variable value
);

DROP TABLE IF EXISTS users;
CREATE TABLE users (
	user_id INTEGER PRIMARY KEY,                 -- PK
	username TEXT UNIQUE COLLATE NOCASE,         -- can be username or email
	password_hash TEXT,                          -- output of password_hash()
	timezone TEXT DEFAULT 'America/Los_Angeles'  -- for date formatting
);

DROP TABLE IF EXISTS posts;
CREATE TABLE posts (
	body TEXT,                    -- main text of the post
	created INTEGER,              -- timestamp when the post was created
	author_id INTEGER NOT NULL,   -- user_id of author
	FOREIGN KEY (author_id) REFERENCES users (user_id)
);

INSERT INTO config (name, value) VALUES ('schema_version', '20230107');

COMMIT;
