Skip to content

Commit 0466650

Browse files
committed
Apply better schema
1 parent aaf697b commit 0466650

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

bot/migrations/V7__snippets.sql

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- Revision Version: V7
2+
-- Revises: V6
3+
-- Creation Date: 2024-06-25 22:08:13.554817 UTC
4+
-- Reason: snippets
5+
6+
CREATE TABLE IF NOT EXISTS snippets (
7+
id SERIAL PRIMARY KEY,
8+
name TEXT,
9+
content TEXT,
10+
uses INTEGER DEFAULT (0),
11+
owner_id BIGINT,
12+
location_id BIGINT,
13+
created_at TIMESTAMPTZ DEFAULT (now() at time zone 'utc')
14+
);
15+
16+
-- Create indices to speed up regular and trigram searches
17+
CREATE INDEX IF NOT EXISTS snippets_name_idx ON snippets (name);
18+
CREATE INDEX IF NOT EXISTS snippets_location_id_idx ON snippets (location_id);
19+
CREATE INDEX IF NOT EXISTS snippets_name_trgm_idx ON snippets USING GIN (name gin_trgm_ops);
20+
CREATE INDEX IF NOT EXISTS snippets_name_lower_idx ON snippets (LOWER(name));
21+
CREATE UNIQUE INDEX IF NOT EXISTS snippets_uniq_idx ON snippets (LOWER(name), location_id);
22+
23+
CREATE TABLE IF NOT EXISTS snippets_lookup (
24+
id SERIAL PRIMARY KEY,
25+
name TEXT,
26+
location_id BIGINT,
27+
owner_id BIGINT,
28+
created_at TIMESTAMPTZ DEFAULT (now() at time zone 'utc'),
29+
snippets_id INTEGER REFERENCES snippets (id) ON DELETE CASCADE ON UPDATE NO ACTION
30+
);
31+
32+
CREATE INDEX IF NOT EXISTS snippets_lookup_name_idx ON snippets_lookup (name);
33+
CREATE INDEX IF NOT EXISTS snippets_lookup_location_id_idx ON snippets_lookup (location_id);
34+
CREATE INDEX IF NOT EXISTS snippets_lookup_name_trgm_idx ON snippets_lookup USING GIN (name gin_trgm_ops);
35+
CREATE INDEX IF NOT EXISTS snippets_lookup_name_lower_idx ON snippets_lookup (LOWER(name));
36+
CREATE UNIQUE INDEX IF NOT EXISTS snippets_lookup_uniq_idx ON snippets_lookup (LOWER(name), location_id);

docker/pg/init.sh

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ set -e
44
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
55
CREATE ROLE rodhaj WITH LOGIN PASSWORD '$RODHAJ_PASSWORD';
66
CREATE DATABASE rodhaj OWNER rodhaj;
7+
CREATE EXTENSION IF NOT EXISTS pg_trgm;
78
EOSQL

docs/dev-guide/intro.rst

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ The following SQL queries can be used to create the user and database:
9696
9797
CREATE ROLE rodhaj WITH LOGIN PASSWORD 'somepass';
9898
CREATE DATABASE rodhaj OWNER rodhaj;
99+
CREATE EXTENSION IF NOT EXISTS pg_trgm;
99100
100101
.. note::
101102

0 commit comments

Comments
 (0)