49 lines
1.8 KiB
Lua
49 lines
1.8 KiB
Lua
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)")
|