use bigserial for user_id on entries, update SimilarEntries to include headline

This commit is contained in:
Aaron Bieber 2020-04-02 06:29:59 -06:00
parent aa44137660
commit 9ed951eda0
2 changed files with 11 additions and 7 deletions

View File

@ -19,10 +19,14 @@ DELETE FROM entries
WHERE entry_id = $1;
-- name: SimilarEntries :many
SELECT entry_id, similarity(body, $1) AS sml
FROM entries
WHERE body % $1
ORDER BY sml DESC, body;
SELECT entry_id, similarity(body, $2) as similarity,
ts_headline('english', body, q) as headline,
title from entries,
plainto_tsquery($2) q
WHERE user_id = $1 and
similarity(body, $2) > 0.0
order by similarity DESC
LIMIT 10;
-- name: CreateUser :one
INSERT INTO users (

View File

@ -18,15 +18,15 @@ CREATE TABLE users (
);
CREATE TABLE entries (
entry_id UUID NOT NULL default gen_random_uuid() PRIMARY KEY,
user_id int NOT NULL REFERENCES users ON DELETE CASCADE,
entry_id UUID NOT NULL default gen_random_uuid() PRIMARY KEY UNIQUE,
user_id BIGSERIAL NOT NULL REFERENCES users ON DELETE CASCADE,
created_at timestamp NOT NULL DEFAULT NOW(),
updated_at timestamp,
title text NOT NULL DEFAULT '',
body text NOT NULL DEFAULT ''
);
CREATE INDEX body_idx ON entries USING GIN (body gin_trgm_ops);
CREATE INDEX body_trgm_idx ON entries USING gist (body gist_trgm_ops);
CREATE OR REPLACE FUNCTION hash(password text) RETURNS text AS $$
SELECT crypt(password, gen_salt('bf', 10));