|
| 1 | +import cx_Oracle |
| 2 | + |
| 3 | +class DatabaseManager(object): |
| 4 | + def __init__(self, connection_string): |
| 5 | + self.__connection_string = connection_string |
| 6 | + |
| 7 | + def __enter__(self): |
| 8 | + self.__db = cx_Oracle.Connection(self.__connection_string) |
| 9 | + self.__cursor = self.__db.cursor() |
| 10 | + return self |
| 11 | + |
| 12 | + def __exit__(self, type, value, traceback): |
| 13 | + self.__db.close() |
| 14 | + |
| 15 | + def return_user_objects(self): |
| 16 | + """ |
| 17 | + Returns dictionary of database objects within user tablepace |
| 18 | + dictionary format: |
| 19 | + { |
| 20 | + object_type1: [object_name1, object_name2, ... ], |
| 21 | + object_type2: [object_name1, object_name2, ... ], |
| 22 | + ... |
| 23 | + } |
| 24 | + """ |
| 25 | + select_sql = """SELECT OBJECT_NAME, OBJECT_TYPE FROM USER_OBJECTS""" |
| 26 | + |
| 27 | + self.__db.begin() |
| 28 | + self.__cursor.execute(select_sql) |
| 29 | + self.cursor_output = self.__cursor.fetchall() |
| 30 | + |
| 31 | + self.dictonary_output = dict() |
| 32 | + |
| 33 | + for each_sql_output in self.cursor_output: |
| 34 | + if each_sql_output[1] not in self.dictonary_output.keys(): |
| 35 | + self.dictonary_output[each_sql_output[1]] = '' |
| 36 | + |
| 37 | + for each_key in self.dictonary_output.keys(): |
| 38 | + values_array = [] |
| 39 | + for each_sql_output in self.cursor_output: |
| 40 | + if each_key == each_sql_output[1]: |
| 41 | + values_array.append(each_sql_output[0]) |
| 42 | + self.dictonary_output[each_key] = values_array |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + base_connection_string = "user/pass@hostname:port/sid" |
| 46 | + secondary_connection_string = "user/pass@hostname:port/sid" |
| 47 | + |
| 48 | + with DatabaseManager(base_connection_string) as db_instance: |
| 49 | + db_instance.return_user_objects() |
| 50 | + base_results = db_instance.dictonary_output |
| 51 | + |
| 52 | + with DatabaseManager(secondary_connection_string) as db_instance: |
| 53 | + db_instance.return_user_objects() |
| 54 | + secondary_results = db_instance.dictonary_output |
| 55 | + |
| 56 | + for key in secondary_results.keys(): |
| 57 | + if key in base_results.keys(): |
| 58 | + for each_value in secondary_results[key]: |
| 59 | + if not each_value in [each_value_1 for each_value_1 in base_results[key]]: |
| 60 | + print 'Could not find [Object type: %s][Object name: %s]' % (key, each_value) |
| 61 | + else: |
| 62 | + |
| 63 | + print 'Need to add: %s : %s' % (key, secondary_results[key]) |
| 64 | + |
0 commit comments