49 lines
1.5 KiB
Lua
49 lines
1.5 KiB
Lua
|
|
||
|
local sql = require("lsqlite3")
|
||
|
|
||
|
local queries = require("queries")
|
||
|
|
||
|
local db = {}
|
||
|
local oldconfigure = configure
|
||
|
function configure(...)
|
||
|
db.conn = sqlassert(sql.open("data/posts.db"))
|
||
|
|
||
|
--Create sql tables
|
||
|
assert(db.conn:exec(queries.create_table_authors))
|
||
|
--Create a fake "anonymous" user, so we don't run into trouble
|
||
|
--so that no one runs into trouble being able to paste under this account.
|
||
|
assert(db.conn:exec(queries.insert_anon_author))
|
||
|
--If/when an author delets their account, all posts
|
||
|
--and comments by that author are also deleted (on
|
||
|
--delete cascade) this is intentional. This also
|
||
|
--means that all comments by other users on a post
|
||
|
--an author makes will also be deleted.
|
||
|
--
|
||
|
--Post text uses zlib compression
|
||
|
assert(db.conn:exec(queries.create_table_posts))
|
||
|
--Store the raw text so people can download it later, maybe
|
||
|
--we can use it for "download as image" or "download as pdf"
|
||
|
--in the future too. Stil stored zlib compressed
|
||
|
assert(db.conn:exec(queries.create_table_raw_text))
|
||
|
--Maybe we want to store images one day?
|
||
|
assert(db.conn:exec(queries.create_table_images))
|
||
|
--Comments on a post
|
||
|
assert(db.conn:exec(queries.create_table_comments))
|
||
|
--Tags for a post
|
||
|
assert(db.conn:exec(queries.create_table_tags))
|
||
|
--Index for tags
|
||
|
assert(db.conn:exec(queries.create_index_tags))
|
||
|
--Store a cookie for logged in users. Logged in users can edit
|
||
|
--their own posts, and edit their biographies.
|
||
|
assert(db:exec(queries.create_table_session))
|
||
|
print("Created db tables")
|
||
|
|
||
|
return configure(...)
|
||
|
end
|
||
|
|
||
|
function db.close()
|
||
|
db.conn:close()
|
||
|
end
|
||
|
|
||
|
return db
|