
CREATE DATABASE dragon_fire;

USE dragon_fire;

-- DROP TABLE posts;
-- DROP TABLE path_access_counts;
-- DROP TABLE path_errcodes;
-- DROP TABLE feed_cache;

CREATE TABLE posts (
	post_id INTEGER AUTO_INCREMENT,

	host VARCHAR(64) NOT NULL,

	post_path VARCHAR(255) NOT NULL,
	post_path_depth INTEGER NOT NULL DEFAULT 0,

	post_created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
	post_updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,

	post_view_count INTEGER DEFAULT 0,

	post_title 	VARCHAR(1024),
	post_tags 	VARCHAR(1024),
	post_brief  TEXT(2048),

	post_metadata JSON DEFAULT NULL,
	post_settings_cache JSON DEFAULT NULL,

	PRIMARY KEY(post_id),
	CONSTRAINT unique_post UNIQUE (host, post_path),

	INDEX(host, post_path),
	INDEX(host, post_path_depth, post_path),

	INDEX(host, post_created_at),
	INDEX(host, post_updated_at),

	FULLTEXT(post_tags)
);

CREATE TABLE post_markdown (
	post_id INTEGER,

	post_markdown TEXT,

	PRIMARY KEY(post_id),
	FOREIGN KEY(post_id) REFERENCES posts(post_id)
		ON DELETE CASCADE,

	FULLTEXT(post_markdown)
);

CREATE TABLE path_access_counts (
	access_time DATETIME NOT NULL,
	host VARCHAR(64) NOT NULL,

	post_path VARCHAR(255),
	agent VARCHAR(255),
	referrer VARCHAR(255),
	
	path_access_count INTEGER DEFAULT 0,
	path_processing_time DOUBLE PRECISION DEFAULT 0,

	PRIMARY KEY(access_time, host, post_path, agent, referrer)
);

CREATE TABLE path_errcodes (
	access_timestamp DATETIME NOT NULL,
	
	host VARCHAR(64) NOT NULL,

	post_path VARCHAR(255),
	agent VARCHAR(255),
	referrer VARCHAR(255),
	error VARCHAR(1024),
);

CREATE TABLE feed_cache (
	host VARCHAR(64) NOT NULL,
	search_path VARCHAR(255),
	export_type VARCHAR(255),

	feed_created_on DATETIME DEFAULT CURRENT_TIMESTAMP,

	feed_content MEDIUMTEXT,

	PRIMARY KEY(host, search_path, export_type)
);

INSERT INTO posts (post_path, post_path_depth, post_metadata, post_content)
VALUES (
	'/about',
	0,
'
{
	"tags": ["test", "test2", "hellorld"],
	"brief": "This is a simple test indeed",
	"type": "text/markdown",
	"title": "About the dergen"
}
',
'
# About the dergs indeed

This is just a simple test. Might be nice, though!
'
), (
	'/about/neira',
	1,
'
{
	"tags": ["test", "test2", "hellorld", "neira"],
	"brief": "This is a soft grab of Neira",
	"type": "text/markdown",
	"title": "About her"
}
',
'
# Nothing here yet!

Sorry for this. She is working hard :>
'
), (
	'/about/xasin',
	1,
'
{
	"tags": ["test", "test2", "hellorld", "xasin"],
	"brief": "This is a soft grab of Xasin",
	"type": "text/markdown",
	"title": "About her"
}
',
'
# Nothing here yet!

Sorry for this. He is working hard :>
'
), (
	'/about/mesh',
	1,
'
{
	"tags": ["test", "test2", "hellorld", "mesh"],
	"brief": "This is a soft grab of Mesh",
	"type": "text/markdown",
	"title": "About her"
}
',
'
# Nothing here yet!

Sorry for this. Shi is working hard :>
'
), (
	'/about/alviere',
	1,
'
{
	"tags": ["test", "test2", "hellorld", "mesh"],
	"brief": "SHE GRABS",
	"type": "text/markdown",
	"title": "SHE GRABS"
}
',
'
# Nothing here yet!

Sorry for this. She GRABS A LOT

----

## And now, for the lorem:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Eleifend mi in nulla posuere sollicitudin aliquam ultrices sagittis orci. Risus commodo viverra maecenas accumsan lacus vel facilisis. Sed viverra tellus in hac habitasse. Nulla malesuada pellentesque elit eget gravida cum. Posuere sollicitudin aliquam ultrices sagittis orci a. Libero nunc consequat interdum varius sit amet. Bibendum arcu vitae elementum curabitur vitae nunc sed velit. Amet mauris commodo quis imperdiet massa tincidunt nunc pulvinar. Sed adipiscing diam donec adipiscing. Laoreet id donec ultrices tincidunt arcu non sodales. Id semper risus in hendrerit gravida rutrum quisque non. Ut venenatis tellus in metus vulputate eu.

Risus sed vulputate odio ut enim blandit volutpat. Placerat in egestas erat imperdiet. Non curabitur gravida arcu ac tortor dignissim convallis aenean. Neque aliquam vestibulum morbi blandit cursus risus at. Elementum integer enim neque volutpat ac tincidunt vitae semper. Eu ultrices vitae auctor eu augue ut. In mollis nunc sed id semper risus in hendrerit gravida. Lectus arcu bibendum at varius vel pharetra vel turpis nunc. In pellentesque massa placerat duis. Non quam lacus suspendisse faucibus. Vitae aliquet nec ullamcorper sit amet risus nullam. Accumsan lacus vel facilisis volutpat est velit egestas dui.

Risus feugiat in ante metus dictum at tempor commodo. Duis ut diam quam nulla. Nunc aliquet bibendum enim facilisis gravida neque convallis. Tincidunt augue interdum velit euismod in pellentesque. Praesent semper feugiat nibh sed pulvinar proin gravida hendrerit lectus. Non odio euismod lacinia at quis risus sed vulputate odio. Nunc sed blandit libero volutpat sed cras ornare arcu. Adipiscing enim eu turpis egestas pretium aenean pharetra magna. Ut tristique et egestas quis ipsum suspendisse. Blandit cursus risus at ultrices mi tempus imperdiet nulla malesuada.
'
);