diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/schema_diff_table_utils.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/schema_diff_table_utils.py index 4d15dc19e43..64962cccaec 100644 --- a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/schema_diff_table_utils.py +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/schema_diff_table_utils.py @@ -38,6 +38,15 @@ class SchemaDiffTableCompare(SchemaDiffObjectCompare): column_keys_to_ignore = ['atttypid', 'edit_types', 'elemoid', 'seqrelid', 'indkey', 'seqtypid', 'defseqrelid'] + # 'serial_defval' keeps the nextval() default that reprojecting a + # column as SERIAL empties (#10292). It names the sequence with its + # schema, so it differs whenever schemas with different names are + # compared, even though the columns are the same. Whether a column is + # SERIAL is already compared through its type. It cannot go in + # column_keys_to_ignore, as compare_target_cols() deletes those keys, + # and the SQL for converting a column to SERIAL needs this one. + serial_keys_to_ignore = ['serial_defval'] + # 'conkey' and 'confkey' are attribute numbers, which differ between # databases whenever a column has been dropped from one of the tables, # even though the columns they name are the same. The columns are @@ -55,8 +64,8 @@ class SchemaDiffTableCompare(SchemaDiffObjectCompare): index_keys_to_ignore = ['indrelid', 'indclass'] keys_to_ignore = table_keys_to_ignore + column_keys_to_ignore + \ - constraint_keys_to_ignore + trigger_keys_to_ignore + \ - index_keys_to_ignore + serial_keys_to_ignore + constraint_keys_to_ignore + \ + trigger_keys_to_ignore + index_keys_to_ignore def compare(self, **kwargs): """ @@ -175,6 +184,16 @@ def table_col_comp(source, target): return different + @staticmethod + def _compared_column(column): + """ + Return the properties of a column that decide whether it differs, + leaving out serial_keys_to_ignore. Those stay in the column itself + for generating the SQL. + """ + return {key: value for key, value in column.items() + if key not in SchemaDiffTableCompare.serial_keys_to_ignore} + @staticmethod def compare_target_cols(source, target_cols, added, updated): """ @@ -198,7 +217,8 @@ def compare_target_cols(source, target_cols, added, updated): tmp = copy.deepcopy(item) source['attnum'] = tmp['attnum'] - if tmp and source != tmp: + compared = SchemaDiffTableCompare._compared_column + if tmp and compared(source) != compared(tmp): # check column level grants acl_dict = dict() if 'attacl' in source and 'attacl' in tmp and \ @@ -225,7 +245,7 @@ def compare_target_cols(source, target_cols, added, updated): updated.append(source) target_cols.remove(tmp) - elif tmp and source == tmp: + elif tmp: target_cols.remove(tmp) elif tmp is None: added.append(source) @@ -239,6 +259,22 @@ def _fk_column_pairs(foreign_key): return [(col.get('local_column'), col.get('referenced')) for col in foreign_key.get('columns', [])] + @staticmethod + def _fk_referenced_schema(foreign_key): + """ + Return the schema of a foreign key's referenced table, or None when + it is the schema of the foreign key's own table. + + Schemas with different names can be compared, so a foreign key to a + table in its own schema must compare equal on both sides even + though the schema names differ (#10762). A reference to any other + schema is still compared by name. + """ + refnsp = foreign_key.get('refnsp') + if refnsp is not None and refnsp == foreign_key.get('fknsp'): + return None + return refnsp + @staticmethod def table_constraint_comp(source_table, target_table): """ @@ -295,9 +331,18 @@ def table_constraint_comp(source_table, target_table): tmp_tar.pop('oid') if tmp_tar and tmp_src != tmp_tar: tmp_updated = copy.deepcopy(source) + ref_schema = \ + SchemaDiffTableCompare._fk_referenced_schema for key in non_editable_keys[constraint]: - if key in tmp_updated and \ - tmp_updated[key] != tmp_tar[key]: + if key not in tmp_updated: + continue + src_val = tmp_updated[key] + tar_val = tmp_tar[key] + if constraint == 'foreign_key' and \ + key == 'refnsp': + src_val = ref_schema(tmp_updated) + tar_val = ref_schema(tmp_tar) + if src_val != tar_val: added.append(source) deleted.append(tmp_updated) tmp_updated = None diff --git a/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_schema_diff_cross_schema_unit.py b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_schema_diff_cross_schema_unit.py new file mode 100644 index 00000000000..b128b1c9f17 --- /dev/null +++ b/web/pgadmin/browser/server_groups/servers/databases/schemas/tables/tests/test_schema_diff_cross_schema_unit.py @@ -0,0 +1,177 @@ +########################################################################## +# +# pgAdmin 4 - PostgreSQL Tools +# +# Copyright (C) 2013 - 2026, The pgAdmin Development Team +# This software is released under the PostgreSQL Licence +# +########################################################################## + +"""Unit tests for comparing tables in schemas with different names (#10762). + +A table's own schema name shows up in the nextval() default of a SERIAL +column and in the referenced schema of a foreign key to a table in the +same schema. When two schemas with different names are compared, neither +may make otherwise identical tables compare as different, or make Schema +Diff drop and recreate an identical foreign key. +""" + +import copy + +from pgadmin.browser.server_groups.servers.databases.schemas.tables.\ + schema_diff_table_utils import SchemaDiffTableCompare +from pgadmin.tools.schema_diff.directory_compare import \ + are_dictionaries_identical +from pgadmin.utils.route import BaseTestGenerator + + +def _serial_column(schema, **overrides): + """An 'id' column as returned by get_formatted_columns() for a SERIAL + column of a table in the given schema.""" + column = dict( + name='id', cltype='serial', typname='serial', + displaytypname='serial', atttypid=23, attnum=1, elemoid=23, + seqtypid=23, seqrelid=16400, defseqrelid=16400, indkey=None, + defval='', attnotnull=True, attacl=[], + serial_defval="nextval('{0}.admin_user_emails_id_seq'" + "::regclass)".format(schema), + ) + column.update(overrides) + return column + + +def _foreign_key(schema, refnsp=None, **overrides): + """A foreign key as returned by get_foreign_keys() for a table in the + given schema, referencing admin_users in refnsp (by default the same + schema).""" + refnsp = refnsp or schema + foreign_key = dict( + oid=16420, name='fk_admin_user_emails_admin', + condeferrable=False, condeferred=False, confupdtype='a', + confdeltype='a', confmatchtype=False, conkey=[2], confkey=[1], + confrelid=16390, fknsp=schema, fktab='admin_user_emails', + refnspoid=16385, refnsp=refnsp, reftab='admin_users', comment=None, + convalidated=False, conislocal=True, + columns=[{'local_column': 'admin', 'references': 16390, + 'referenced': 'id', + 'references_table_name': refnsp + '.admin_users'}], + remote_schema=refnsp, remote_table='admin_users', + coveringindex=None, autoindex=True, hasindex=False, + ) + foreign_key.update(overrides) + return foreign_key + + +def _table(schema, columns, foreign_keys): + return dict(name='admin_user_emails', schema=schema, columns=columns, + foreign_key=foreign_keys) + + +class TestSchemaDiffCrossSchemaUnit(BaseTestGenerator): + """Unit tests for SchemaDiffTableCompare across schema names.""" + + scenarios = [ + ('Tables differing only in their schema name are identical', + dict(test_method='test_tables_identical')), + ('A SERIAL column whose default names another schema is not ' + 'reported as changed', + dict(test_method='test_serial_column_not_changed')), + ('A column becoming SERIAL is still reported as changed', + dict(test_method='test_column_becoming_serial_changed')), + ('A foreign key to its own schema is not recreated', + dict(test_method='test_fk_to_own_schema_not_recreated')), + ('A foreign key to the same other schema is not recreated', + dict(test_method='test_fk_to_other_schema_not_recreated')), + ('A foreign key referencing a different schema is recreated', + dict(test_method='test_fk_to_different_schema_recreated')), + ('A foreign key with a different referenced table is recreated', + dict(test_method='test_fk_to_different_table_recreated')), + ] + + def runTest(self): + getattr(self, self.test_method)() + + def test_tables_identical(self): + # The case from #10762: identical tables in schemas named + # novinator and novinator_prod. + source = _table('novinator', [_serial_column('novinator')], + [_foreign_key('novinator', oid=16420)]) + target = _table('novinator_prod', [_serial_column('novinator_prod')], + [_foreign_key('novinator_prod', oid=17420)]) + + self.assertTrue(are_dictionaries_identical( + source, target, SchemaDiffTableCompare.keys_to_ignore, False)) + + def test_serial_column_not_changed(self): + source = _serial_column('novinator') + target_cols = [_serial_column('novinator_prod')] + + added = [] + updated = [] + SchemaDiffTableCompare.compare_target_cols( + source, target_cols, added, updated) + + self.assertEqual(added, []) + self.assertEqual(updated, []) + self.assertEqual(target_cols, []) + + def test_column_becoming_serial_changed(self): + # The default the source column was reprojected from must still + # reach the SQL that converts the target column to SERIAL (#10292). + source = _serial_column('novinator') + target = _serial_column( + 'novinator_prod', cltype='integer', typname='integer', + displaytypname='integer', seqrelid=None, defseqrelid=None, + defval=None) + del target['serial_defval'] + target_cols = [target] + + added = [] + updated = [] + SchemaDiffTableCompare.compare_target_cols( + source, target_cols, added, updated) + + self.assertEqual(len(updated), 1) + self.assertEqual(updated[0]['serial_defval'], + "nextval('novinator.admin_user_emails_id_seq'" + "::regclass)") + + def _compare_foreign_keys(self, source_fk, target_fk): + source = _table(source_fk['fknsp'], [], [source_fk]) + target = _table(target_fk['fknsp'], [], [target_fk]) + return SchemaDiffTableCompare.table_constraint_comp( + copy.deepcopy(source), copy.deepcopy(target))['foreign_key'] + + def test_fk_to_own_schema_not_recreated(self): + diff = self._compare_foreign_keys( + _foreign_key('novinator', oid=16420, confrelid=16390), + _foreign_key('novinator_prod', oid=17420, confrelid=17390)) + + self.assertEqual(diff['added'], []) + self.assertEqual(diff['deleted'], []) + + def test_fk_to_other_schema_not_recreated(self): + diff = self._compare_foreign_keys( + _foreign_key('novinator', refnsp='common'), + _foreign_key('novinator_prod', refnsp='common')) + + self.assertEqual(diff['added'], []) + self.assertEqual(diff['deleted'], []) + + def test_fk_to_different_schema_recreated(self): + # The target references a table in another schema, so it is not + # the same foreign key, even though the table names match. + diff = self._compare_foreign_keys( + _foreign_key('novinator'), + _foreign_key('novinator_prod', refnsp='novinator')) + + self.assertEqual(len(diff['added']), 1) + self.assertEqual(len(diff['deleted']), 1) + + def test_fk_to_different_table_recreated(self): + diff = self._compare_foreign_keys( + _foreign_key('novinator'), + _foreign_key('novinator_prod', reftab='other_users')) + + self.assertEqual(len(diff['added']), 1) + self.assertEqual(len(diff['deleted']), 1)