justusbot/migrations/base/sqlite/base_initial-layout.sql

252 lines
8.3 KiB
MySQL
Raw Permalink Normal View History

-- @up
2026-10-30 14:25:05 -06:00
-- @version=0003
-- Chats.
2026-10-30 14:25:05 -06:00
--
-- Several tables are dependent upon chats.id so it's created first.
create table chats (
id integer primary key,
name text not null
) strict;
insert into chats values (1, 'all chats');
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)
);
2026-10-30 14:25:05 -06:00
-- Members and member metadata.
create table anniversaries (
id integer primary key autoincrement,
month integer not null,
day integer not null,
year integer default 0 not null,
title text default '' not null
);
create index anniversaries_month_day_idx on anniversaries (month, day);
create table announcements (
id integer primary key autoincrement,
chatid integer not null,
scheduled_for datetime not null,
-- If enabled, reminders will be generated a week before and a day before the announcement itself.
reminders boolean default true not null,
title text default '' not null,
content text default '' not null,
foreign key (chatid) references chats(id)
);
create index announcements_chatid_idx on announcements (chatid);
create index announcements_scheduled_for_idx on announcements (scheduled_for);
create table birthdays (
id integer primary key autoincrement,
-- User ID if applicable (or given).
userid integer default null,
chatid integer not null,
name text default '' not null,
month integer not null default 1 check (month >= 1 and month <= 12),
-- Basic sanity checks. Application-level code will check valid values for
-- each month with special handling for February birthdays.
day integer not null default 1 check (day >= 1 and day <= 31),
-- If the birthday falls on a leap year and is February 29th, offer the user
-- an opportunity to apply an adjustment of either -1 (February 28th) or +1
-- (March 1st). 0 = no delta (non-leap day) or no preference (no preference
-- is handled the same as -1).
leap_delta integer not null check (leap_delta >= -1 or leap_delta <= 1),
announce boolean default true not null,
foreign key (userid) references users(id),
foreign key (chatid) references chats(id)
);
create index birthdays_chatid_idx on birthdays (chatid);
create index birthdays_chatid_name_idx on birthdays (chatid, name);
-- System
-- Overrides settings.toml.
create table settings (
config text not null,
value text not null,
volatile boolean default false not null,
primary key (config)
);
-- SQL-based Bible parsing.
insert into settings values ('bible.sql.enable', 'false', true);
-- Enable SQL configuration override. Requires setting the connection DSN via an
-- environment variable (DBCONN).
insert into settings values ('config.sql.enable', 'false', true);
insert into settings (config, value) values ('service.method', 'webhook');
insert into settings (config, value) values ('service.webhook.uri', 'https://example.com/bot');
insert into settings (config, value) values ('service.webhook.port', '2020');
insert into settings (config, value) values ('service.webhook.host', '::');
insert into settings (config, value) values ('system.timezone', 'America/Denver');
insert into settings (config, value) values ('translations.basedir', 'data/translations');
insert into settings (config, value) values ('translations.active', 'bsb');
insert into settings (config, value) values ('translations.default', 'bsb');
create table users (
id integer primary key,
username text not null,
firstname text not null,
lastname text default '' not null
);
create unique index users_username_idx on users(username);
create index users_firstname_idx on users(firstname);
insert into users (id, username, firstname) values (1, 'system', 'system');
-- Bible metadata.
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);
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,
path text default '' not null,
enabled boolean default true not null,
title text not null
);
create unique index translations_code_idx on translations(code);
insert into translations (
code, path, title
) values (
'bsb', 'bsb', 'Berean Standard Bible'
), (
'kjv', 'kjv', 'King James (Authorized) Version'
);
create table api_translations (
translationid integer not null primary key,
servicename text default '' not null,
api_endpoint text default '' not null,
foreign key (translationid) references translations(id)
);
insert into api_translations (
translationid, servicename, api_endpoint
) values (
1, 'helloao', '/api/BSB'
), (
2, 'helloao', '/api/eng_kjv'
);
create table verses (
id integer primary key autoincrement,
added datetime default current_timestamp not null,
book text not null,
chapter int not null,
verse_start int not null,
verse_stop int default 0 not null
);
create unique index verses_book_chapter_verses_idx on verses (book, chapter, verse_start, verse_stop);
-- Verses added to the rotation will slowly atrophy the number available in
-- `verses`. Clients should delete entries from this table once the intersection
-- of verses available and verses loaded here fully coincide.
create table verse_rotation (
verseid integer primary key,
foreign key (verseid) references verses(id)
);
create unique index verse_rotation_verseid_chatid_idx on verse_rotation (verseid);
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_translations (
verseid integer not null,
translationid integer not null,
foreign key (verseid) references verses(id),
foreign key (translationid) references translations(id),
primary key (verseid, translationid)
);
create table verse_log (
id integer not null primary key autoincrement,
verseid integer not null,
posted datetime default current_timestamp not null,
foreign key (verseid) references verses(id)
);
create index verse_log_posted_idx on verse_log (posted);
create table scheduled_verses (
verseid integer not null,
-- translationid should only be specified if the scheduled verse must derive
-- from a specific translation.
translationid integer default null,
scheduled_for date not null,
foreign key (verseid) references verses(id),
foreign key (translationid) references translations(id),
primary key (verseid, translationid)
);