-- Database generated with pgModeler (PostgreSQL Database Modeler).
-- pgModeler  version: 0.9.1
-- PostgreSQL version: 10.0
-- Project Site: pgmodeler.io
-- Model Author: Joern Muehlencord


-- Database creation must be done outside a multicommand file.
-- These commands were put in this file only as a convenience.
-- -- object: account_test | type: DATABASE --
-- -- DROP DATABASE IF EXISTS account_test;
-- CREATE DATABASE account_test
-- 	ENCODING = 'UTF8'
-- 	LC_COLLATE = 'German_Germany.1252'
-- 	LC_CTYPE = 'German_Germany.1252';
-- -- ddl-end --
-- 

-- object: public.config | type: TABLE --
-- DROP TABLE IF EXISTS public.config CASCADE;
CREATE TABLE public.config(
	config_key varchar(100) NOT NULL,
	config_key_group varchar(200),
	config_key_account uuid,
	config_value varchar(200),
	CONSTRAINT config_pk PRIMARY KEY (config_key)

);
-- ddl-end --

-- object: public.application_role | type: TABLE --
-- DROP TABLE IF EXISTS public.application_role CASCADE;
CREATE TABLE public.application_role(
	id uuid NOT NULL,
	role_name character varying(80) NOT NULL,
	role_description character varying(200) NOT NULL,
	CONSTRAINT pk_application_role_pk PRIMARY KEY (id)

);
-- ddl-end --

-- object: public.account | type: TABLE --
-- DROP TABLE IF EXISTS public.account CASCADE;
CREATE TABLE public.account(
	id uuid NOT NULL,
	username character varying(32) NOT NULL,
	emailaddress character varying(200) NOT NULL,
	firstname character varying(100) NOT NULL,
	lastname character varying(100) NOT NULL,
	account_password character(200) NOT NULL,
	last_login timestamp with time zone,
	last_failed_login timestamp with time zone,
	failure_count integer NOT NULL DEFAULT 0,
	status character varying(10) NOT NULL DEFAULT 'NEW',
	password_reset_ongoing boolean NOT NULL DEFAULT false,
	password_reset_valid_to timestamp with time zone,
	password_reset_hash character(200),
	created_on timestamp with time zone NOT NULL DEFAULT timezone('utc'::text, now()),
	created_by character varying(32) NOT NULL,
	last_updated_on timestamp with time zone NOT NULL DEFAULT timezone('utc'::text, now()),
	last_updated_by character varying(32) NOT NULL,
	CONSTRAINT pk_account PRIMARY KEY (id),
	CONSTRAINT uidx_username UNIQUE (username)

);
-- ddl-end --

-- object: public.account_history | type: TABLE --
-- DROP TABLE IF EXISTS public.account_history CASCADE;
CREATE TABLE public.account_history(
	id uuid NOT NULL,
	account_id uuid NOT NULL,
	message character varying(200),
	failure_count integer NOT NULL DEFAULT 0,
	status character varying(20) NOT NULL,
	last_updated_on timestamp with time zone NOT NULL DEFAULT timezone('utc'::text, now()),
	last_updated_by character varying(32) NOT NULL,
	CONSTRAINT pk_account_history PRIMARY KEY (id)

);
-- ddl-end --

-- object: public.account_role | type: TABLE --
-- DROP TABLE IF EXISTS public.account_role CASCADE;
CREATE TABLE public.account_role(
	account uuid NOT NULL,
	account_role uuid NOT NULL,
	CONSTRAINT pk_account_role PRIMARY KEY (account,account_role)

);
-- ddl-end --

-- object: public.application_permission | type: TABLE --
-- DROP TABLE IF EXISTS public.application_permission CASCADE;
CREATE TABLE public.application_permission(
	id uuid NOT NULL,
	permission_name character varying(80) NOT NULL,
	permission_description character varying(200) NOT NULL,
	CONSTRAINT pk_application_permission PRIMARY KEY (id),
	CONSTRAINT uidx_application_permission_name UNIQUE (permission_name)

);
-- ddl-end --

-- object: public.role_permission | type: TABLE --
-- DROP TABLE IF EXISTS public.role_permission CASCADE;
CREATE TABLE public.role_permission(
	application_role uuid NOT NULL,
	role_permission uuid NOT NULL,
	CONSTRAINT pk_role_permission_role_permission_name PRIMARY KEY (application_role,role_permission)

);
-- ddl-end --

-- object: public.mail_template | type: TABLE --
-- DROP TABLE IF EXISTS public.mail_template CASCADE;
CREATE TABLE public.mail_template(
	template_name character varying(40) NOT NULL,
	template_value text NOT NULL,
	CONSTRAINT mail_template_pk PRIMARY KEY (template_name)

);
-- ddl-end --

-- object: config_key_account_fk | type: CONSTRAINT --
-- ALTER TABLE public.config DROP CONSTRAINT IF EXISTS config_key_account_fk CASCADE;
ALTER TABLE public.config ADD CONSTRAINT config_key_account_fk FOREIGN KEY (config_key_account)
REFERENCES public.account (id) MATCH FULL
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- ddl-end --

-- object: fk_account_history_username_fk | type: CONSTRAINT --
-- ALTER TABLE public.account_history DROP CONSTRAINT IF EXISTS fk_account_history_username_fk CASCADE;
ALTER TABLE public.account_history ADD CONSTRAINT fk_account_history_username_fk FOREIGN KEY (account_id)
REFERENCES public.account (id) MATCH SIMPLE
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- ddl-end --

-- object: fk_account_role_account | type: CONSTRAINT --
-- ALTER TABLE public.account_role DROP CONSTRAINT IF EXISTS fk_account_role_account CASCADE;
ALTER TABLE public.account_role ADD CONSTRAINT fk_account_role_account FOREIGN KEY (account)
REFERENCES public.account (id) MATCH SIMPLE
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- ddl-end --

-- object: fk_account_role_role_name | type: CONSTRAINT --
-- ALTER TABLE public.account_role DROP CONSTRAINT IF EXISTS fk_account_role_role_name CASCADE;
ALTER TABLE public.account_role ADD CONSTRAINT fk_account_role_role_name FOREIGN KEY (account_role)
REFERENCES public.application_role (id) MATCH SIMPLE
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- ddl-end --

-- object: fk_role_permission_application_role | type: CONSTRAINT --
-- ALTER TABLE public.role_permission DROP CONSTRAINT IF EXISTS fk_role_permission_application_role CASCADE;
ALTER TABLE public.role_permission ADD CONSTRAINT fk_role_permission_application_role FOREIGN KEY (application_role)
REFERENCES public.application_role (id) MATCH SIMPLE
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- ddl-end --

-- object: fk_role_permission_role_permission | type: CONSTRAINT --
-- ALTER TABLE public.role_permission DROP CONSTRAINT IF EXISTS fk_role_permission_role_permission CASCADE;
ALTER TABLE public.role_permission ADD CONSTRAINT fk_role_permission_role_permission FOREIGN KEY (role_permission)
REFERENCES public.application_permission (id) MATCH SIMPLE
ON DELETE NO ACTION ON UPDATE NO ACTION;
-- ddl-end --


