diff --git a/.gitignore b/.gitignore index 9f1eb09..44c0c11 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ logs/ nginx.conf.compiled +db.*.sqlite +.vscode/ diff --git a/schema.lua b/schema.lua new file mode 100644 index 0000000..2f50821 --- /dev/null +++ b/schema.lua @@ -0,0 +1,48 @@ +local schema = require("lapis.db.schema") +local db = require("lapis.db") + +local types = schema.types + +schema.create_table("users", { + {"id", types.integer{primary_key = true}}, + {"username", types.text{unique = true, null = false}}, + {"password_hash", types.text{null = false}}, + {"permission", types.integer{default = 0}}, + {"created_at", "INTEGER DEFAULT (unixepoch(CURRENT_TIMESTAMP))"} +}) + +schema.create_table("topics", { + {"id", types.integer{primary_key = true}}, + {"name", types.text{null = false}}, + {"slug", types.text{null = false, unique = true}} +}) + +schema.create_table("threads", { + {"id", types.integer{primary_key = true}}, + {"topic_id", "INTEGER REFERENCES topics(id) ON DELETE CASCADE"}, + {"user_id", "INTEGER REFERENCES users(id) ON DELETE SET NULL"}, + {"title", types.text{null = false}}, + {"slug", types.text{null = false, unique = true}}, + {"created_at", "INTEGER DEFAULT (unixepoch(CURRENT_TIMESTAMP))"}, +}) + +schema.create_table("posts", { + {"id", types.integer{primary_key = true}}, + {"thread_id", "INTEGER REFERENCES threads(id) ON DELETE CASCADE"}, + {"user_id", "INTEGER REFERENCES users(id) ON DELETE SET NULL"}, + {"created_at", "INTEGER DEFAULT (unixepoch(CURRENT_TIMESTAMP))"}, + {"current_revision_id", "INTEGER REFERENCES post_history(id)"}, +}) + +schema.create_table("post_history", { + {"id", types.integer{primary_key = true}}, + {"post_id", "INTEGER REFERENCES posts(id) ON DELETE CASCADE"}, + {"user_id", "INTEGER REFERENCES users(id) ON DELETE CASCADE"}, + {"content", types.text{null = false}}, + {"edited_at", "INTEGER DEFAULT (unixepoch(CURRENT_TIMESTAMP))"}, + {"is_initial_revision", "BOOLEAN DEFAULT FALSE"} +}) + +db.query("CREATE INDEX idx_threads_topic_id ON threads(topic_id)") +db.query("CREATE INDEX idx_posts_thread_id ON posts(thread_id)") +db.query("CREATE INDEX idx_post_history_post_id ON post_history(post_id)")