-
Couldn't load subscription status.
- Fork 246
feat: add PostgreSQL schema-qualified table name support #462
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
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| ruby 3.4.4 | ||
| ruby 3.4.7 |
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,67 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'test_helper' | ||
|
|
||
| describe SchemaType do | ||
| before do | ||
| skip 'PostgreSQL only' unless postgresql? | ||
| end | ||
|
|
||
| def assert_lineage(parent, child) | ||
| assert_equal parent, child.parent | ||
| assert_equal [child, parent], child.self_and_ancestors | ||
|
|
||
| # make sure reloading doesn't affect the self_and_ancestors: | ||
| child.reload | ||
| assert_equal [child, parent], child.self_and_ancestors | ||
| end | ||
|
|
||
| it 'properly handles schema-qualified table names' do | ||
| assert_equal 'test_schema.schema_types', SchemaType.table_name | ||
| assert_equal 'test_schema.schema_type_hierarchies', SchemaTypeHierarchy.table_name | ||
| end | ||
|
|
||
| it 'finds self and parents when children << is used' do | ||
| parent = SchemaType.new(name: 'Electronics') | ||
| child = SchemaType.new(name: 'Phones') | ||
| parent.children << child | ||
| parent.save | ||
| assert_lineage(parent, child) | ||
| end | ||
|
|
||
| it 'finds self and parents properly if the constructor is used' do | ||
| parent = SchemaType.create(name: 'Electronics') | ||
| child = SchemaType.create(name: 'Phones', parent: parent) | ||
| assert_lineage(parent, child) | ||
| end | ||
|
|
||
| it 'creates hierarchy records in the schema-qualified table' do | ||
| parent = SchemaType.create!(name: 'Electronics') | ||
| child = SchemaType.create!(name: 'Phones', parent: parent) | ||
|
|
||
| hierarchy = SchemaTypeHierarchy.where(ancestor_id: parent.id, descendant_id: child.id).first | ||
| refute_nil hierarchy | ||
| assert_equal 1, hierarchy.generations | ||
| end | ||
|
|
||
| it 'fixes self_and_ancestors properly on reparenting' do | ||
| a = SchemaType.create! name: 'Electronics' | ||
| b = SchemaType.create! name: 'Phones' | ||
| assert_equal([b], b.self_and_ancestors.to_a) | ||
| a.children << b | ||
| assert_equal([b, a], b.self_and_ancestors.to_a) | ||
| end | ||
|
|
||
| it 'supports tree operations with schema-qualified tables' do | ||
| root = SchemaType.create!(name: 'Electronics') | ||
| child1 = SchemaType.create!(name: 'Computers', parent: root) | ||
| child2 = SchemaType.create!(name: 'Phones', parent: root) | ||
| grandchild = SchemaType.create!(name: 'Laptops', parent: child1) | ||
|
|
||
| assert_equal 2, root.children.count | ||
| assert_equal 1, child1.children.count | ||
| assert_equal 0, child2.children.count | ||
| assert_equal [grandchild, child1, root], grandchild.self_and_ancestors | ||
| assert_equal [root, child1, child2, grandchild], root.self_and_descendants.order(:name) | ||
| end | ||
| end |
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,10 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class SchemaType < ApplicationRecord | ||
| self.table_name = 'test_schema.schema_types' | ||
| has_closure_tree order: :name | ||
|
|
||
| def to_s | ||
| name | ||
| end | ||
| end |
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
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.
The schema_type_hierarchies table is manually created, but the PR's purpose is to auto-generate hierarchy tables with schema prefixes. If the library should automatically create this table, consider removing this manual definition to ensure the feature works as intended and to avoid duplication between manual and auto-generated tables.