-
Notifications
You must be signed in to change notification settings - Fork 648
Proposal: Add path
column to categories table as ltree
type to make tree traversing easier
#1122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bors-voyager
merged 22 commits into
rust-lang:master
from
erewok:category_tree_experimental
Aug 30, 2018
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7350391
New categories `path` column as `ltree` type
erewok 876bd05
Add test and doc function for parent_categories
erewok a7223a8
Link to parent category from category page
kivikakk 619b512
Parent cat not in name since parents included
kivikakk ead2857
Add Ltree to schema.
kivikakk ca72109
bump diesel_ltree
kivikakk 95dd777
diesel_ltree 0.1.3
kivikakk cd11d54
Pass a11y
kivikakk 51ed1aa
add aria-label with ember-component-attributes
kivikakk 4d063b5
use insert_into(…).values
kivikakk 1d20870
Fix up the other tests too
kivikakk e3f0a40
extract some SQL
kivikakk 570abc5
fmt
kivikakk 6f4a396
master merge fixup
kivikakk e3c3625
extract some more SQL
kivikakk a5c9f14
Add path column to categories table
carols10cents 6240588
Add sql_types to categories' table! in the schema
carols10cents 1ad3f9d
Explicitly never select categories.path column
carols10cents 13a1f9c
Commit package lock changes
carols10cents 4434413
Merge remote-tracking branch 'upstream/master' into category_tree_exp…
carols10cents 8e099ed
Cargo fmt
carols10cents 81dff1e
Fix Cargo.lock
carols10cents File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP TRIGGER set_category_path_update ON categories; | ||
|
||
DROP TRIGGER set_category_path_insert ON categories; | ||
|
||
DROP FUNCTION IF EXISTS set_category_path_to_slug(); | ||
|
||
DROP INDEX path_categories_idx; | ||
DROP INDEX path_gist_categories_idx; | ||
|
||
ALTER TABLE categories | ||
DROP COLUMN path; | ||
|
||
DROP EXTENSION ltree; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
-- Your SQL goes here | ||
CREATE EXTENSION ltree; | ||
|
||
-- Create the new column which will represent our category tree. | ||
-- Fill it with values from `slug` column and then set to non-null | ||
ALTER TABLE categories | ||
ADD COLUMN path ltree; | ||
|
||
-- Unfortunately, hyphens (dashes) are not allowed... | ||
UPDATE categories | ||
SET path = text2ltree('root.' || trim(replace(replace(slug, '-', '_'), '::', '.'))) | ||
WHERE path is NULL; | ||
|
||
ALTER TABLE CATEGORIES | ||
ALTER COLUMN path SET NOT NULL; | ||
|
||
-- Create some indices that allow us to use GIST operators: '@>', etc | ||
CREATE INDEX path_gist_categories_idx ON categories USING GIST(path); | ||
CREATE INDEX path_categories_idx ON categories USING btree(path); | ||
|
||
-- Create procedure and trigger to auto-update path | ||
CREATE OR REPLACE FUNCTION set_category_path_to_slug() | ||
RETURNS trigger AS | ||
$BODY$ | ||
BEGIN | ||
NEW.path = text2ltree('root.' || trim(replace(replace(NEW.slug, '-', '_'), '::', '.'))); | ||
RETURN NEW; | ||
END; | ||
$BODY$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER set_category_path_insert | ||
BEFORE INSERT | ||
ON categories | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE set_category_path_to_slug(); | ||
|
||
CREATE TRIGGER set_category_path_update | ||
BEFORE UPDATE OF slug ON categories | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE set_category_path_to_slug(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason to have both a gist and a btree index? The postgres docs say:
which looks like gist indexes take care of the same operators that btree indexes take care of?
All of this is probably moot anyway since the categories table is so smalll :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, I've just seen everyone do it that way, so I figured it was a best practice, if not necessary.