-- Was meant to work on MySQL/MariaDB

 CREATE DATABASE `dwelling`
COLLATE utf8mb4_unicode_ci;

USE `dwelling`;

	CREATE USER `dweller`@`%`
IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES
   ON `dwelling`.*
   TO `dweller`@`%`;

CREATE TABLE `blog_categories` (
	`category_id` MEDIUMINT   PRIMARY KEY AUTO_INCREMENT,
	`category`    VARCHAR(40) NOT NULL    UNIQUE );

INSERT INTO `blog_categories` (`category`)
VALUES ('Update'), ('Diary');

CREATE INDEX `blog_categories_ix_category`
	ON `blog_categories` (`category`);

CREATE TABLE `blog` (
	`entry_id` SERIAL       PRIMARY KEY,
	`date`     DATETIME     DEFAULT CURRENT_TIMESTAMP NOT NULL,
	`category` MEDIUMINT                              NOT NULL,
	`title`    VARCHAR(255)                           NOT NULL,
	`body`     MEDIUMTEXT                             NOT NULL,
	FOREIGN KEY (`category`) REFERENCES `blog_categories` (`category_id`)
		 ON UPDATE CASCADE );

CREATE TABLE IF NOT EXISTS `guestbook` (
	`post_id`      SERIAL        PRIMARY KEY,
	`created`      DATETIME      DEFAULT CURRENT_TIMESTAMP NOT NULL,
	`name`         VARCHAR(80)                             NOT NULL,
	`email`        VARCHAR(255),
	`website`      VARCHAR(255),
	`message`      VARCHAR(4096)                           NOT NULL,
	`hide_website` BOOLEAN       DEFAULT TRUE              NOT NULL,
	`hide_email`   BOOLEAN       DEFAULT TRUE              NOT NULL);

CREATE INDEX IF NOT EXISTS `guestbook_ix_created`
	ON `guestbook` (`created`);

CREATE TABLE IF NOT EXISTS `guestbook_feedback` (
	`post_id` SERIAL        PRIMARY KEY,
	`created` DATETIME      DEFAULT CURRENT_TIMESTAMP NOT NULL,
	`comment` VARCHAR(4096)                           NOT NULL,
	FOREIGN KEY (`post_id`) REFERENCES `guestbook` (`post_id`)
		 ON DELETE CASCADE
		 ON UPDATE CASCADE );