-
-
Notifications
You must be signed in to change notification settings - Fork 96
Added schema field to OrmarConfig #1493
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
Open
hk3dva
wants to merge
13
commits into
ormar-orm:master
Choose a base branch
from
hk3dva:feature_add_schema_to_ormar_config
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
8321a18
Added schema field to OrmarConfig
hk3dva 4b39c87
Update sqlalchemy.py
hk3dva 0b92c12
Merge branch 'master' into feature_add_schema_to_ormar_config
hk3dva a526a83
edit .gitignore, Added description to readme
hk3dva b6cbdba
Merge branch 'feature_add_schema_to_ormar_config' of https://github.c…
hk3dva f733e67
remove dotenv
hk3dva 6c8d240
Merge branch 'master' into feature_add_schema_to_ormar_config
hk3dva 04f49df
Merge branch 'master' into feature_add_schema_to_ormar_config
hk3dva 816cd08
add tests: filter, join, self-join, prefetch_related
hk3dva 00f9b1d
Merge branch 'master' into feature_add_schema_to_ormar_config
hk3dva d22d91a
fix formating
hk3dva 5fd9f6a
Merge branch 'feature_add_schema_to_ormar_config' of https://github.c…
hk3dva 0f91b4b
Merge branch 'master' into feature_add_schema_to_ormar_config
hk3dva 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| DATABASE_URL=postgresql://pguser:pgpassword@localhost:10011/postgres | ||
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 |
|---|---|---|
|
|
@@ -18,3 +18,4 @@ profile.py | |
| *.db-journal | ||
| *coverage.xml | ||
| .benchmarks/ | ||
| .env | ||
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
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
Empty file.
33 changes: 33 additions & 0 deletions
33
tests/test_schema/test_create_table_fk_between_differrent_schema.py
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,33 @@ | ||
| import ormar | ||
| import pytest | ||
| import sqlalchemy | ||
|
|
||
| from tests.lifespan import init_tests | ||
| from tests.settings import create_config | ||
|
|
||
| base_ormar_config = create_config() | ||
|
|
||
|
|
||
| class User(ormar.Model): | ||
| ormar_config = base_ormar_config.copy(tablename="users", schema='s1') | ||
|
|
||
| id = ormar.Integer(primary_key=True) | ||
|
|
||
| class Profile(ormar.Model): | ||
| ormar_config = base_ormar_config.copy(tablename="profiles", schema='s2') | ||
|
|
||
| id = ormar.Integer(primary_key=True) | ||
| user = ormar.ForeignKey(User) | ||
|
|
||
| create_test_database = init_tests(base_ormar_config) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_fk_cross_schema(): | ||
| async with base_ormar_config.database: | ||
| insp = sqlalchemy.inspect(base_ormar_config.engine) | ||
| fks = insp.get_foreign_keys("profiles", schema="s2") | ||
|
|
||
| assert fks[0]["referred_schema"] == "s1" | ||
| assert fks[0]["referred_table"] == "users" | ||
|
|
||
|
|
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,26 @@ | ||
| import ormar | ||
| import pytest | ||
| import sqlalchemy | ||
|
|
||
| from tests.lifespan import init_tests | ||
| from tests.settings import create_config | ||
|
|
||
| base_ormar_config = create_config() | ||
|
|
||
|
|
||
| class Category(ormar.Model): | ||
| ormar_config = base_ormar_config.copy(tablename="categories", schema='test') | ||
|
|
||
| id: int = ormar.Integer(primary_key=True) | ||
| name: str = ormar.String(max_length=50, unique=True, index=True) | ||
| code: int = ormar.Integer() | ||
|
|
||
|
|
||
| create_test_database = init_tests(base_ormar_config) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_table_in_schema(): | ||
| async with base_ormar_config.database: | ||
| insp = sqlalchemy.inspect(base_ormar_config.engine) | ||
| assert insp.has_table("categories", schema="test") |
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,32 @@ | ||
| import ormar | ||
| import pytest | ||
| import sqlalchemy | ||
|
|
||
| from tests.lifespan import init_tests | ||
| from tests.settings import create_config | ||
|
|
||
| base_ormar_config = create_config() | ||
|
|
||
|
|
||
| class User(ormar.Model): | ||
| ormar_config = base_ormar_config.copy(tablename="users", schema='s1') | ||
|
|
||
| id = ormar.Integer(primary_key=True) | ||
|
|
||
| class Profile(ormar.Model): | ||
| ormar_config = base_ormar_config.copy(tablename="profiles", schema='s1') | ||
|
|
||
| id = ormar.Integer(primary_key=True) | ||
| user = ormar.ForeignKey(User) | ||
|
|
||
| create_test_database = init_tests(base_ormar_config) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_fk_resolves_schema(): | ||
| async with base_ormar_config.database: | ||
| insp = sqlalchemy.inspect(base_ormar_config.engine) | ||
| fks = insp.get_foreign_keys("profiles", schema="s1") | ||
|
|
||
| assert len(fks) == 1 | ||
| assert fks[0]["referred_table"] == "users" | ||
| assert fks[0]["referred_schema"] == "s1" |
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,75 @@ | ||
| from typing import Optional | ||
|
|
||
| import ormar | ||
| import pytest | ||
|
|
||
| from tests.lifespan import init_tests | ||
| from tests.settings import create_config | ||
|
|
||
| base_ormar_config = create_config() | ||
|
|
||
|
|
||
| class Author(ormar.Model): | ||
| ormar_config = base_ormar_config.copy(tablename="authors", schema='s1') | ||
|
|
||
| id = ormar.Integer(primary_key=True) | ||
| name = ormar.String(max_length=100) | ||
|
|
||
|
|
||
| class Book(ormar.Model): | ||
| ormar_config = base_ormar_config.copy(tablename="books", schema='s2') | ||
|
|
||
| id = ormar.Integer(primary_key=True) | ||
| title = ormar.String(max_length=100) | ||
| author: Optional[Author] = ormar.ForeignKey(Author) | ||
|
|
||
|
|
||
| create_test_database = init_tests(base_ormar_config) | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_filter_with_schema(): | ||
| async with base_ormar_config.database: | ||
| author1 = await Author.objects.create(name="Tolkien") | ||
| author2 = await Author.objects.create(name="Rowling") | ||
|
|
||
| await Book.objects.create(title="LOTR", author=author1) | ||
| await Book.objects.create(title="Hobbit", author=author1) | ||
| await Book.objects.create(title="HP", author=author2) | ||
|
|
||
| # фильтрация через join к таблице в другой схеме | ||
| books = await Book.objects.filter(author__name="Tolkien").all() | ||
|
|
||
| titles = sorted(b.title for b in books) | ||
| assert titles == ["Hobbit", "LOTR"] | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_filter_with_schema_and_in_lookup(): | ||
| async with base_ormar_config.database: | ||
| author1 = await Author.objects.create(name="Asimov") | ||
| author2 = await Author.objects.create(name="Clarke") | ||
|
|
||
| await Book.objects.create(title="Foundation", author=author1) | ||
| await Book.objects.create(title="Robots", author=author1) | ||
| await Book.objects.create(title="Odyssey", author=author2) | ||
|
|
||
| books = await Book.objects.filter( | ||
| author__name__in=["Asimov"] | ||
| ).all() | ||
|
|
||
| assert {b.title for b in books} == {"Foundation", "Robots"} | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_filter_with_schema_and_null_fk(): | ||
| async with base_ormar_config.database: | ||
| author = await Author.objects.create(name="Orphan Author") | ||
|
|
||
| await Book.objects.create(title="With Author", author=author) | ||
| await Book.objects.create(title="Without Author", author=None) | ||
|
|
||
| books = await Book.objects.filter(author__isnull=True).all() | ||
|
|
||
| assert len(books) == 1 | ||
| assert books[0].title == "Without Author" |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Definitely do not commit a .env file.
If you need to rely on a .env file, create a template (e.g.,
.env.example) and add.envinto the gitignore.It would also require some form of documentation in the README to explain the purpose of this.
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.
Thanks for the note, I didn’t notice that the original project did not have a preset ignoring env files
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.
For me personally, this functionality is necessary to build a system built for data analysis in a company. I have many different users who have their own schemes with their own rights. I would like to give them access only to their schemas, without generating configs for each of them
I also saw the issues in discussions about schema support, but no one promoted it