justusbot/migrations/sqlite/0001_initial_state.sql

200 lines
6.2 KiB
SQL

-- @up
create table chats (
id integer primary key,
name text not null
) strict;
create table chat_config (
chatid integer,
timezone text not null default 'America/Denver',
cron text not null default 'inherit',
scripture_daily text not null default '0 8 * * *',
scripture_daily_enable boolean not null default true,
birthdays_weekly text not null default '30 7 * * 0',
birthdays_weekly_enable boolean not null default true,
birthdays_daily text not null default '30 7 * * 1-6',
birthdays_daily_enable boolean not null default true,
foreign key (chatid) references chats(id)
);
-- Overrides settings.toml.
create table system_config (
config text not null,
value text not null,
volatile boolean default false not null,
primary key (config)
);
-- SQL-based Bible parsing.
insert into system_config values ('config.sql.enable', 'false', true);
insert into system_config (config, value) values ('service.method', 'webhook');
insert into system_config (config, value) values ('service.webhook.uri', 'https://example.com/bot');
insert into system_config (config, value) values ('service.webhook.port', '2020');
insert into system_config (config, value) values ('service.webhook.host', '::');
insert into system_config (config, value) values ('system.timezone', 'America/Denver');
insert into system_config (config, value) values ('translations.basedir', 'data/translations');
insert into system_config (config, value) values ('translations.active', 'bsb');
create table users (
id integer primary key,
username text not null
);
insert into users values (1, 'system');
-- Bible metadata.
create table translations (
id integer primary key autoincrement,
-- Translations "code" or ID. For some translations this will be 3 characters. Others may use a longer identifier.
code text not null,
short_name text not null,
-- Helloao endpoint for this translation.
api_endpoint text default "" not null,
title text not null
);
create unique index translations_code_idx on translations(code);
insert into translations (
code, short_name, api_endpoint, title
) values (
'bsb', 'BSB', '/api/BSB', 'Berean Standard Bible'
), (
'kjv', 'KJV', '/api/eng_kjv', 'King James (Authorized) Version'
);
create table verses (
id integer primary key autoincrement,
added text,
book text not null,
chapter int not null,
verse_start int not null,
verse_stop int default 0 not null
);
create table verses_submitter (
verseid integer not null,
chatid integer not null,
submitterid integer not null,
foreign key (verseid) references verses(id),
foreign key (chatid) references chats(id),
foreign key (submitterid) references users(id),
primary key (verseid, chatid, submitterid)
);
create table verse_rotation (
verseid integer,
chatid integer,
-- Indicaes how many times this verse has been used in the rotation. As
-- verses are used in any given chat, their count is incremented. Only those
-- verses with a count < MAX(count) are considered for the random rotation
-- until no more verses are found.
count integer default 0 not null,
-- Votes indicates how many times a given verse has been submitted by users
-- in the channel. This counter is only increments if the user and verse
-- identifiers are not present in `verse_votes`. The system user cannot
-- increment this value.
votes integer default 0 not null,
foreign key (verseid) references verses(id),
foreign key (chatid) references chats(id)
);
create table verse_votes (
userid integer,
verseid integer,
voted_on date default now,
foreign key (userid) references users(id),
foreign key (verseid) references verses(id),
primary key (userid, verseid)
);
create table verse_rotation_translations (
chatid integer,
verseid integer,
translationid integer not null,
votes integer default 0 not null,
foreign key (chatid) references chats(id),
foreign key (verseid) references verses(id),
foreign key (translationid) references translations(id),
primary key (chatid, verseid, translationid)
);
create table verse_rotation_translation_votes (
chatid integer,
verseid integer,
translationid integer not null,
voted_on date default now,
foreign key (chatid) references chats(id),
foreign key (verseid) references verses(id),
foreign key (translationid) references translations(id),
primary key (chatid, verseid, translationid)
);
create table scheduled_verses (
verseid integer not null,
chatid integer not null,
translationid integer not null,
scheduled_for date not null,
foreign key (verseid) references verses(id),
foreign key (chatid) references chats(id),
foreign key (translationid) references translations(id),
primary key (verseid, chatid, translationid)
);
-- Enable with PRAGMA foreign_keys = ON;
create table books (
id integer primary key autoincrement,
translationid integer not null,
-- 3 letter book ID/code.
code text not null,
chapter_count integer default 0 not null,
verse_count integer default 0 not null,
apocrypha boolean default false not null,
-- 0 = no Testament (apocryphal, pseudepigraphical)
-- 1 = Old Testament
-- 2 = New Testament
-- 3 = other
testament integer default 0 not null,
-- 0 = default variant reading (OT = Masoretic Text, NT = Alexandrian)
-- 1 = alternate reading #1 (OT = DSS, NT = Byzantine/Majority Text)
-- 2 = alternate reading #2 (OT = LXX, NT = Western/Latin)
variant integer default 0 not null,
foreign key (translationid) references translations(id)
);
create unique index books_code_idx on books(code);
create table chapters (
id integer primary key autoincrement,
num integer default 0 not null,
bookid integer default 0 not null,
verse_count integer default 0 not null,
-- Mapping code is used to adjust the chapter count, such as for the Septuagint. Mapping codes currently only function for adjusting the chapter count to adjust for the Masoretic Text reading.
mapping_code integer default 0 not null,
foreign key (bookid) references book(id)
);
create unique index chapters_id_num_idx on chapters(id, num);