Inline constraints

This commit is contained in:
Joe Nelson
2015-11-13 12:59:45 -08:00
parent 80928535b0
commit 71e6d0414d
+21 -31
View File
@@ -3,6 +3,8 @@
begin; begin;
-- comment out the role creation statements if
-- you want to run this script more than once
create role anon noinherit; create role anon noinherit;
create role author; create role author;
@@ -41,15 +43,11 @@ $$ LANGUAGE plpgsql;
create table if not exists create table if not exists
basic_auth.users ( basic_auth.users (
email text not null, email text primary key,
pass text not null, pass text not null check (length(pass) < 512),
role name not null, role name not null check (length(role) < 512),
verified boolean not null default false, verified boolean not null default false
-- If you like add more columns, or a json column -- If you like add more columns, or a json column
constraint user_pkey primary key (email),
constraint user_field_length_limits check (
length(pass) < 512
AND length(email::text) < 512)
); );
create or replace function create or replace function
@@ -120,17 +118,16 @@ create trigger send_validation
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
-- Email Validation and Password Reset -- Email Validation and Password Reset
drop type if exists token_type_enum cascade;
create type token_type_enum as enum ('validation', 'reset'); create type token_type_enum as enum ('validation', 'reset');
create table if not exists create table if not exists
basic_auth.tokens ( basic_auth.tokens (
token uuid unique, token uuid primary key,
token_type token_type_enum not null, token_type token_type_enum not null,
email text not null, email text not null references basic_auth.users (email)
created_at timestamptz not null default current_date, on delete cascade on update cascade,
constraint t_pk primary key (token), created_at timestamptz not null default current_date
constraint t_user_fk foreign key (email) references basic_auth.users
on delete cascade on update cascade
); );
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@@ -322,30 +319,23 @@ create trigger update_users
create table if not exists create table if not exists
posts ( posts (
id bigserial not null, id bigserial primary key,
title text not null, title text not null,
body text not null, body text not null,
author text not null, author text not null references basic_auth.users (email)
created_at timestamptz not null default current_date, on delete restrict on update cascade,
constraint post_pk primary key (id), created_at timestamptz not null default current_date
constraint post_user_fk foreign key (author)
references basic_auth.users
on delete restrict on update cascade
); );
create table if not exists create table if not exists
comments ( comments (
id bigserial not null, id bigserial primary key,
body text not null, body text not null,
author text not null, author text not null references basic_auth.users (email)
post bigint not null, on delete restrict on update cascade,
created_at timestamptz not null default current_date, post bigint not null references posts (id)
constraint comment_pk primary key (id), on delete cascade on update cascade,
constraint comment_user_fk foreign key (author) created_at timestamptz not null default current_date
references basic_auth.users
on delete restrict on update cascade,
constraint comment_post_fk foreign key (post) references posts
on delete cascade on update cascade
); );
------------------------------------------------------------------------------- -------------------------------------------------------------------------------