CREATE TABLE IF NOT EXISTS thread (
	id INTEGER PRIMARY KEY,
	title TEXT NOT NULL,
	time TIMESTAMP WITH TIME ZONE NOT NULL
);

CREATE TABLE IF NOT EXISTS post (
	thread_id INTEGER REFERENCES thread(id) ON DELETE CASCADE NOT NULL,
	id INTEGER PRIMARY KEY,
	name TEXT NOT NULL,
	tripcode TEXT,
	subject TEXT,
	time TIMESTAMP WITH TIME ZONE NOT NULL,
	body TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS link (
	link_from INTEGER REFERENCES post(id) ON DELETE CASCADE NOT NULL,
	link_to INTEGER REFERENCES post(id) ON DELETE CASCADE NOT NULL,
	PRIMARY KEY (link_from, link_to)
);

CREATE TABLE IF NOT EXISTS tag (
	post_id INTEGER REFERENCES post(id) ON DELETE CASCADE NOT NULL,
	name TEXT NOT NULL,
	PRIMARY KEY (post_id, name)
);

CREATE TABLE IF NOT EXISTS file (
	post_id INTEGER REFERENCES post(id) ON DELETE CASCADE NOT NULL,
	chan_file_name TEXT,
	original_file_name TEXT
);
