Repository navigation
fix: order Schema Diff tables by foreign key dependencies #10888
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
dpage
merged 3 commits into
pgadmin-org:master
from
hrkn:fix/schema-diff-fk-dependency-order
Dec 2, 2026
+775
−72
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
100 changes: 100 additions & 0 deletions
100
...er_groups/servers/databases/schemas/tables/tests/test_schema_diff_fk_dependencies_unit.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,100 @@ | ||
| ########################################################################## | ||
| # | ||
| # pgAdmin 4 - PostgreSQL Tools | ||
| # | ||
| # Copyright (C) 2013 - 2026, The pgAdmin Development Team | ||
| # This software is released under the PostgreSQL Licence | ||
| # | ||
| ########################################################################## | ||
|
|
||
| """Unit tests for Schema Diff foreign key dependency extraction.""" | ||
|
|
||
| from unittest.mock import MagicMock | ||
|
|
||
| from pgadmin.browser.server_groups.servers.databases.schemas.tables import ( | ||
| TableView, | ||
| ) | ||
| from pgadmin.browser.server_groups.servers.databases.schemas.tables.\ | ||
| constraints.foreign_key import utils as fkey_utils | ||
| from pgadmin.utils.route import BaseTestGenerator | ||
|
|
||
|
|
||
| class TestSchemaDiffFkDependenciesUnit(BaseTestGenerator): | ||
| """Unit tests for get_fkey_dependencies() and | ||
| TableView.get_table_submodules_dependencies().""" | ||
|
|
||
| scenarios = [ | ||
| ('get_fkey_dependencies includes referenced table OID', | ||
| dict(test_method='test_fkey_dependencies_include_oid')), | ||
| ('get_fkey_dependencies handles tables with no foreign keys', | ||
| dict(test_method='test_fkey_dependencies_empty')), | ||
| ('get_table_submodules_dependencies removes consecutive self ' | ||
| 'references', | ||
| dict(test_method='test_submodules_dependencies_strips_self_refs')), | ||
| ] | ||
|
|
||
| def runTest(self): | ||
| getattr(self, self.test_method)() | ||
|
|
||
| def _make_conn(self, rows): | ||
| conn = MagicMock() | ||
| conn.manager.version = 170000 | ||
| conn.execute_dict.return_value = (True, {'rows': rows}) | ||
| return conn | ||
|
|
||
| def test_fkey_dependencies_include_oid(self): | ||
| conn = self._make_conn([ | ||
| { | ||
| 'refnsp': 'fk_order_repro', | ||
| 'reftab': 'z_parent', | ||
| 'confrelid': 16390, | ||
| }, | ||
| { | ||
| 'refnsp': 'shared', | ||
| 'reftab': 'lookup', | ||
| 'confrelid': 16410, | ||
| }, | ||
| ]) | ||
|
|
||
| with self.app.app_context(): | ||
| status, deps = fkey_utils.get_fkey_dependencies(conn, 16395) | ||
|
|
||
| self.assertTrue(status) | ||
| self.assertEqual(deps, [ | ||
| {'type': 'table', 'name': 'fk_order_repro.z_parent', 'oid': 16390}, | ||
| {'type': 'table', 'name': 'shared.lookup', 'oid': 16410}, | ||
| ]) | ||
|
|
||
| def test_fkey_dependencies_empty(self): | ||
| conn = self._make_conn([]) | ||
|
|
||
| with self.app.app_context(): | ||
| status, deps = fkey_utils.get_fkey_dependencies(conn, 16395) | ||
|
|
||
| self.assertTrue(status) | ||
| self.assertEqual(deps, []) | ||
|
|
||
| def test_submodules_dependencies_strips_self_refs(self): | ||
| tid = 16395 | ||
| view = TableView.__new__(TableView) | ||
| view.conn = self._make_conn([ | ||
| # Two consecutive self-referencing FKs followed by a parent FK. | ||
| {'refnsp': 'app', 'reftab': 's_self', 'confrelid': tid}, | ||
| {'refnsp': 'app', 'reftab': 's_self', 'confrelid': tid}, | ||
| {'refnsp': 'app', 'reftab': 'z_parent', 'confrelid': 16390}, | ||
| ]) | ||
| view.tables_sub_modules = [] | ||
| # Consecutive self-dependency in get_dependencies() as well. | ||
| view.get_dependencies = MagicMock(return_value=[ | ||
| {'type': 'table', 'name': 'app.s_self', 'oid': tid}, | ||
| {'type': 'type', 'name': 'app.custom_type', 'oid': 16388}, | ||
| ]) | ||
|
|
||
| raw_method = TableView.get_table_submodules_dependencies.__wrapped__ | ||
| with self.app.app_context(): | ||
| deps = raw_method(view, tid=tid) | ||
|
|
||
| self.assertEqual(deps, [ | ||
| {'type': 'type', 'name': 'app.custom_type', 'oid': 16388}, | ||
| {'type': 'table', 'name': 'app.z_parent', 'oid': 16390}, | ||
| ]) |
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
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.