-
Notifications
You must be signed in to change notification settings - Fork 876
fix: use pg_depend ownership instead of name guessing for SERIAL detection #10167
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
kundansable
wants to merge
4
commits into
pgadmin-org:master
Choose a base branch
from
kundansable:fix-10100-10101-serial-pgdepend
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.
+155
−18
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
da9395c
fix: use pg_depend ownership instead of name guessing for SERIAL dete…
kundansable da072dc
fix: require default's own sequence dependency to match owned sequence
kundansable 8f1ca94
style: fix pycodestyle violations in serial-detection unit test
kundansable 55d5cd5
fix: restrict defseqrelid to sequence relations and collapse to a scalar
kundansable 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
109 changes: 109 additions & 0 deletions
109
...erver_groups/servers/databases/schemas/tables/columns/tests/test_serial_detection_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,109 @@ | ||
| ########################################################################## | ||
| # | ||
| # pgAdmin 4 - PostgreSQL Tools | ||
| # | ||
| # Copyright (C) 2013 - 2026, The pgAdmin Development Team | ||
| # This software is released under the PostgreSQL Licence | ||
| # | ||
| ########################################################################## | ||
|
|
||
| """Unit tests for get_formatted_columns()'s SERIAL-column detection using | ||
| mocks. | ||
|
|
||
| These verify the ownership-vs-default-sequence comparison (#10100, | ||
| #10101) without requiring a running PostgreSQL server. | ||
| """ | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
| from pgadmin.utils.route import BaseTestGenerator | ||
|
|
||
| UTILS_MODULE = ('pgadmin.browser.server_groups.servers.databases.schemas.' | ||
| 'tables.columns.utils') | ||
|
|
||
|
|
||
| def _make_column(**overrides): | ||
| """A properties.sql result row, as returned for a single column.""" | ||
| defaults = dict( | ||
| name='id', atttypid=23, attlen=4, attnum=1, attndims=0, | ||
| atttypmod=-1, attacl=None, attnotnull=False, attoptions=None, | ||
| attfdwoptions=None, attstattarget=-1, attstorage='p', | ||
| attidentity='', defval=None, typname='integer', | ||
| displaytypname='integer', cltype='integer', | ||
| inheritedfrom=None, inheritedid=None, elemoid=23, | ||
| typnspname='pg_catalog', | ||
| defaultstorage='p', description=None, indkey=None, isdup=False, | ||
| collspcname='', is_fk=False, seclabels=None, is_sys_column=False, | ||
| colconstype='n', genexpr=None, relname='t', is_view_only=False, | ||
| attcompression=None, seqrelid=None, defseqrelid=None, | ||
| ) | ||
| defaults.update(overrides) | ||
| return defaults | ||
|
|
||
|
|
||
| class TestSerialColumnDetection(BaseTestGenerator): | ||
| """Unit tests for ServerModule.get_formatted_columns() SERIAL | ||
| detection using mock rows.""" | ||
|
|
||
| scenarios = [ | ||
| ('Split ownership/default keeps explicit nextval default', | ||
| dict(test_method='test_split_ownership_default_not_serial')), | ||
| ('Genuine SERIAL column is reprojected', | ||
| dict(test_method='test_genuine_serial_is_detected')), | ||
| ('Identity column is never reprojected', | ||
| dict(test_method='test_identity_column_not_serial')), | ||
| ] | ||
|
|
||
| @patch(UTILS_MODULE + '.render_template', return_value='SELECT 1;') | ||
| def runTest(self, mock_render): | ||
| getattr(self, self.test_method)() | ||
|
|
||
| def _run(self, column_row): | ||
| from pgadmin.browser.server_groups.servers.databases.schemas.\ | ||
| tables.columns.utils import get_formatted_columns | ||
|
|
||
| conn = MagicMock() | ||
| conn.execute_dict.return_value = (True, {'rows': [column_row]}) | ||
| conn.execute_2darray.return_value = (True, {'rows': []}) | ||
|
|
||
| with patch(UTILS_MODULE + '.column_formatter'): | ||
| data = get_formatted_columns( | ||
| conn, tid=1, data={}, other_columns=[], | ||
| table_or_type='table', template_path='columns/sql/default') | ||
|
|
||
| return data['columns'][0] | ||
|
|
||
| def test_split_ownership_default_not_serial(self): | ||
| # Column owns sequence 100 (pg_depend deptype='a'), but its | ||
| # DEFAULT's nextval() call references a different sequence, 200 | ||
| # (pg_depend deptype='n' from the pg_attrdef entry). SERIAL must | ||
| # not be applied - ownership and default disagree. | ||
| col = _make_column( | ||
| defval="nextval('seq_used'::regclass)", | ||
| seqrelid=100, defseqrelid=200, | ||
| ) | ||
| result = self._run(col) | ||
| self.assertEqual(result['typname'], 'integer') | ||
| self.assertEqual(result['cltype'], 'integer') | ||
| self.assertEqual(result['defval'], "nextval('seq_used'::regclass)") | ||
|
|
||
| def test_genuine_serial_is_detected(self): | ||
| # Ownership and default both point at the same sequence (100) - | ||
| # this is what a real SERIAL column looks like. | ||
| col = _make_column( | ||
| defval="nextval('t_id_seq'::regclass)", | ||
| seqrelid=100, defseqrelid=100, | ||
| ) | ||
| result = self._run(col) | ||
| self.assertEqual(result['typname'], 'serial') | ||
| self.assertEqual(result['cltype'], 'serial') | ||
| self.assertEqual(result['defval'], '') | ||
|
|
||
| def test_identity_column_not_serial(self): | ||
| # Identity columns can carry an internal sequence dependency on | ||
| # both sides, but must never be reprojected as SERIAL. | ||
| col = _make_column( | ||
| defval=None, seqrelid=100, defseqrelid=100, attidentity='a', | ||
| ) | ||
| result = self._run(col) | ||
| self.assertEqual(result['typname'], 'integer') | ||
| self.assertEqual(result['defval'], None) |
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.
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.