/* -*-sql-*-
 *
 * Plain old SQL for setting up the tables for Odin web services.
 */

/* The various tools assume that the database is appropriate configured with
 * the SERIALIZABLE isolation level.
 */

begin;

drop table if exists odin_pastebin;
drop table if exists odin_pastebin_lang;
drop table if exists odin_pastebin_seq;

create table odin_pastebin_lang
	(lang varchar(32) primary key,
	 descr varchar(64) not null);
insert into odin_pastebin_lang (lang, descr) values ('txt', 'Plain text');

create table odin_pastebin_seq (seq int);
insert into odin_pastebin_seq (seq) values (10000);

create table odin_pastebin
	(tag varchar(16) primary key,
	 stamp bigint not null,
	 edithash varchar(128) not null,
	 owner varchar(64) not null,
	 title varchar(128) not null,
	 lang varchar(32) not null
		default 'txt'
		references odin_pastebin_lang (lang)
			on update cascade
			on delete set default
			deferrable initially deferred,
	 content text not null);
create index odin_pastebin_by_lang on odin_pastebin (lang);
create index odin_pastebin_by_owner on odin_pastebin (owner);

commit;
