-- @up create table library_books ( id integer primary key autoincrement, isbn integer default 0 not null, -- ISBN-10 check digit. If it is the maximum allowable value in the base-11 format (i.e. the ISBN-10 ends with "X") this will be "1". isbn_check integer default 0 not null, title text default '' not null, authors text default '' not null, meta jsonb default '{}' not null ); create unique index library_books_isbn_check_idx on library_books(isbn, isbn_check); create table library ( id integer primary key autoincrement, bookid integer, chatid integer, available boolean default true not null, foreign key (bookid) references library_books(id) ); create unique index bookid_chatid_library_idx on library(bookid, chatid); create table library_checkout ( libraryid integer default 0 not null, -- Checkout user, if available. userid integer default null, -- Checkout recipient. This should only be populated if the user ID is not -- present. recipient text default '' not null, checked_out datetime default current_timestamp not null, returned_at datetime default null, foreign key (userid) references users(id), foreign key (libraryid) references library(id) ); -- The queue table contains individuals waiting in the queue for a book that is -- currently loaned out. create table library_queue ( bookid integer default 0 not null, userid integer default null, requester text default '' not null, foreign key (bookid) references library_books(id), foreign key (userid) references users(id) ); create index library_queue_bookid_idx on library_queue (bookid); -- OpenLibrary response cache. -- -- 4xx status codes will be rechecked no sooner than 1 month if a user attempts -- to add a book with the missing ISBN to the library. -- -- 5xx status codes will be rechecked no sooner than 1 week if a user attempts -- to add a book matching an ISBN that generated the error. -- -- 3xx redirects are not recorded. These should resolve into 200 or an error. -- -- 200 OK status codes will not be rechecked. JSON data will be retrieved from -- this table instead of upstream. create table ol_cache ( isbn text default '' not null, response_code integer default 0 not null, requested datetime default current_timestamp not null, -- We cache the full response for later debugging, if needed. json_response text default '' not null ); create unique index ol_cache_isbn_idx on ol_cache(isbn); insert into settings (config, value) values ('library.lookup.useragent', 'BibleBot/1.0 (contact: $contact; using go/please)');