2020-03-31 07:38:45 -06:00
|
|
|
CREATE EXTENSION IF NOT EXISTS pg_trgm;
|
|
|
|
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
|
|
|
|
|
|
|
DROP TABLE IF EXISTS entries;
|
|
|
|
DROP TABLE IF EXISTS users;
|
|
|
|
|
|
|
|
CREATE TABLE users (
|
|
|
|
user_id BIGSERIAL PRIMARY KEY,
|
|
|
|
created_at timestamp NOT NULL DEFAULT NOW(),
|
|
|
|
updated_at timestamp,
|
|
|
|
first_name text NOT NULL,
|
|
|
|
last_name text NOT NULL,
|
|
|
|
username text NOT NULL UNIQUE,
|
|
|
|
hash text NOT NULL,
|
|
|
|
email text NOT NULL,
|
2020-04-05 09:33:04 -06:00
|
|
|
token text NOT NULL default encode(digest(gen_random_uuid()::text || now(), 'sha256'), 'hex') UNIQUE,
|
2020-03-31 07:38:45 -06:00
|
|
|
token_expires timestamp NOT NULL DEFAULT NOW() + INTERVAL '3 days'
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE entries (
|
2020-04-02 06:29:59 -06:00
|
|
|
entry_id UUID NOT NULL default gen_random_uuid() PRIMARY KEY UNIQUE,
|
|
|
|
user_id BIGSERIAL NOT NULL REFERENCES users ON DELETE CASCADE,
|
2020-03-31 07:38:45 -06:00
|
|
|
created_at timestamp NOT NULL DEFAULT NOW(),
|
|
|
|
updated_at timestamp,
|
|
|
|
title text NOT NULL DEFAULT '',
|
|
|
|
body text NOT NULL DEFAULT ''
|
|
|
|
);
|
|
|
|
|
2020-04-02 06:29:59 -06:00
|
|
|
CREATE INDEX body_trgm_idx ON entries USING gist (body gist_trgm_ops);
|
2020-03-31 07:38:45 -06:00
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION hash(password text) RETURNS text AS $$
|
|
|
|
SELECT crypt(password, gen_salt('bf', 10));
|
|
|
|
$$ LANGUAGE SQL;
|
2020-04-05 09:33:04 -06:00
|
|
|
|
|
|
|
-- CREATE OR REPLACE FUNCTION similar_entries(user_id bigserial, body text, OUT entry_id UUID, OUT similarity float, OUT headline text) AS $$
|
|
|
|
-- $$ LANGUAGE SQL;
|