Skip to content

Commit

Permalink
Apply better schema
Browse files Browse the repository at this point in the history
  • Loading branch information
No767 committed Jun 26, 2024
1 parent aaf697b commit 0466650
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
File renamed without changes.
36 changes: 36 additions & 0 deletions bot/migrations/V7__snippets.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- Revision Version: V7
-- Revises: V6
-- Creation Date: 2024-06-25 22:08:13.554817 UTC
-- Reason: snippets

CREATE TABLE IF NOT EXISTS snippets (
id SERIAL PRIMARY KEY,
name TEXT,
content TEXT,
uses INTEGER DEFAULT (0),
owner_id BIGINT,
location_id BIGINT,
created_at TIMESTAMPTZ DEFAULT (now() at time zone 'utc')
);

-- Create indices to speed up regular and trigram searches
CREATE INDEX IF NOT EXISTS snippets_name_idx ON snippets (name);
CREATE INDEX IF NOT EXISTS snippets_location_id_idx ON snippets (location_id);
CREATE INDEX IF NOT EXISTS snippets_name_trgm_idx ON snippets USING GIN (name gin_trgm_ops);
CREATE INDEX IF NOT EXISTS snippets_name_lower_idx ON snippets (LOWER(name));
CREATE UNIQUE INDEX IF NOT EXISTS snippets_uniq_idx ON snippets (LOWER(name), location_id);

CREATE TABLE IF NOT EXISTS snippets_lookup (
id SERIAL PRIMARY KEY,
name TEXT,
location_id BIGINT,
owner_id BIGINT,
created_at TIMESTAMPTZ DEFAULT (now() at time zone 'utc'),
snippets_id INTEGER REFERENCES snippets (id) ON DELETE CASCADE ON UPDATE NO ACTION
);

CREATE INDEX IF NOT EXISTS snippets_lookup_name_idx ON snippets_lookup (name);
CREATE INDEX IF NOT EXISTS snippets_lookup_location_id_idx ON snippets_lookup (location_id);
CREATE INDEX IF NOT EXISTS snippets_lookup_name_trgm_idx ON snippets_lookup USING GIN (name gin_trgm_ops);
CREATE INDEX IF NOT EXISTS snippets_lookup_name_lower_idx ON snippets_lookup (LOWER(name));
CREATE UNIQUE INDEX IF NOT EXISTS snippets_lookup_uniq_idx ON snippets_lookup (LOWER(name), location_id);
1 change: 1 addition & 0 deletions docker/pg/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ set -e
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE ROLE rodhaj WITH LOGIN PASSWORD '$RODHAJ_PASSWORD';
CREATE DATABASE rodhaj OWNER rodhaj;
CREATE EXTENSION IF NOT EXISTS pg_trgm;
EOSQL
1 change: 1 addition & 0 deletions docs/dev-guide/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ The following SQL queries can be used to create the user and database:
CREATE ROLE rodhaj WITH LOGIN PASSWORD 'somepass';
CREATE DATABASE rodhaj OWNER rodhaj;
CREATE EXTENSION IF NOT EXISTS pg_trgm;
.. note::

Expand Down

0 comments on commit 0466650

Please sign in to comment.