Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes15kdownloads
compare.py144 linesDownload Raw Back to schema_diff
1##########################################################################2#3# pgAdmin 4 - PostgreSQL Tools4#5# Copyright (C) 2013 - 2024, The pgAdmin Development Team6# This software is released under the PostgreSQL Licence7#8##########################################################################9 10"""Schema diff object comparison."""11 12from flask import render_template13from pgadmin.utils.driver import get_driver14from config import PG_DEFAULT_DRIVER15from pgadmin.utils.ajax import internal_server_error16from pgadmin.tools.schema_diff.directory_compare import compare_dictionaries17 18 19class SchemaDiffObjectCompare:20 21    keys_to_ignore = ['oid', 'oid-2', 'is_sys_obj', 'schema']22 23    @staticmethod24    def get_schema(sid, did, scid):25        """26        This function will return the schema name.27        """28 29        driver = get_driver(PG_DEFAULT_DRIVER)30        manager = driver.connection_manager(sid)31        conn = manager.connection(did=did)32 33        ver = manager.version34        server_type = manager.server_type35 36        # Fetch schema name37        status, schema_name = conn.execute_scalar(38            render_template(39                "/".join(['schemas',40                          '{0}/#{1}#'.format(server_type, ver),41                          'sql/get_name.sql']),42                conn=conn, scid=scid43            )44        )45 46        return status, schema_name47 48    def compare(self, **kwargs):49        """50        This function is used to compare all the objects51        from two different schemas.52 53        :param kwargs:54        :return:55        """56        source_params = {'sid': kwargs.get('source_sid'),57                         'did': kwargs.get('source_did')}58        target_params = {'sid': kwargs.get('target_sid'),59                         'did': kwargs.get('target_did')}60        ignore_owner = kwargs.get('ignore_owner', False)61        ignore_whitespaces = kwargs.get('ignore_whitespaces', False)62        ignore_tablespace = kwargs.get('ignore_tablespace', False)63        ignore_grants = kwargs.get('ignore_grants', False)64 65        group_name = kwargs.get('group_name')66        source_schema_name = kwargs.get('source_schema_name', None)67        source = {}68        target = {}69 70        status, target_schema = self.get_schema(kwargs.get('target_sid'),71                                                kwargs.get('target_did'),72                                                kwargs.get('target_scid'))73        if not status:74            return internal_server_error(errormsg=target_schema)75 76        if group_name == 'Database Objects':77            source = self.fetch_objects_to_compare(**source_params)78            target = self.fetch_objects_to_compare(**target_params)79        else:80            source_params['scid'] = kwargs.get('source_scid')81            target_params['scid'] = kwargs.get('target_scid')82 83            if 'scid' in source_params and source_params['scid'] is not None:84                source = self.fetch_objects_to_compare(**source_params)85 86            if 'scid' in target_params and target_params['scid'] is not None:87                target = self.fetch_objects_to_compare(**target_params)88 89        # If both the dict have no items then return None.90        if not (source or target) or not \91            (isinstance(source, dict) and isinstance(target, dict)) or \92                (len(source) <= 0 and len(target) <= 0):93            return None94 95        return compare_dictionaries(view_object=self,96                                    source_params=source_params,97                                    target_params=target_params,98                                    target_schema=target_schema,99                                    source_dict=source,100                                    target_dict=target,101                                    node=self.node_type,102                                    node_label=self.blueprint.collection_label,103                                    group_name=group_name,104                                    ignore_keys=self.keys_to_ignore,105                                    source_schema_name=source_schema_name,106                                    ignore_owner=ignore_owner,107                                    ignore_whitespaces=ignore_whitespaces,108                                    ignore_tablespace=ignore_tablespace,109                                    ignore_grants=ignore_grants)110 111    def ddl_compare(self, **kwargs):112        """113        This function will compare object properties and114         return the difference of SQL115        """116 117        source_params = {'gid': 1,118                         'sid': kwargs.get('source_sid'),119                         'did': kwargs.get('source_did'),120                         'oid': kwargs.get('source_oid')121                         }122 123        target_params = {'gid': 1,124                         'sid': kwargs.get('target_sid'),125                         'did': kwargs.get('target_did'),126                         'oid': kwargs.get('target_oid')127                         }128 129        source_scid = kwargs.get('source_scid')130        if source_scid is not None and source_scid != 0:131            source_params['scid'] = source_scid132 133        target_scid = kwargs.get('target_scid')134        if target_scid is not None and target_scid != 0:135            target_params['scid'] = target_scid136 137        source = self.get_sql_from_diff(**source_params)138        target = self.get_sql_from_diff(**target_params)139 140        return {'source_ddl': source,141                'target_ddl': target,142                'diff_ddl': ''143                }144 
codekingpro/portable-devtools · Team Ai