def discover_schemas(self): cur=self._get_p_cursor() if not cur: logger.error("Return from DN discover_schemas. No p_cur obtained") return False try: cur.execute("SELECT oid,nspname FROM pg_namespace WHERE nspname NOT IN ('pg_catalog', 'information_schema') AND nspname !~ '^pg_toast' AND nspname !~ '^pg_temp'") except Exception as e: logger.error("Canot execute schema discovery query on Prod {0}".format(e.pgerror)) cur.close() return False prod_schs=cur.fetchall() cur.close() cur=self.db_conn.cursor() try: cur.execute("SELECT obj_oid,sch_name,id FROM schema_name WHERE dn_id={0} AND alive".format(self.id)) except Exception as e: logger.error("Canot execute schema discovery query on Local {0}".format(e.pgerror)) cur.close() return False local_schs=cur.fetchall() cur.close() for l_sch in local_schs: for p_sch in prod_schs: if l_sch[0]==p_sch[0] and l_sch[1]==p_sch[1]: old_sch=SchemaName(self.db_conn,self.prod_conn,l_sch[2]) if not old_sch.discover_tables(): logger.error("DN.discover_schemas() False from discover_tables for old") if not old_sch.discover_functions(): logger.error("DN.discover_schemas() False from discover_functions for old") break else: old_sch=SchemaName(self.db_conn,self.prod_conn,l_sch[2]) if old_sch.retire(): logger.info("Retired schema {0} in database {1}".format(l_sch[1],self.db_fields['db_name'])) else: logger.error("DN.discover_schemas() Cannot retire old") for p_sch in prod_schs: for l_sch in local_schs: if l_sch[0]==p_sch[0] and l_sch[1]==p_sch[1]: break else: new_sch=SchemaName(self.db_conn,self.prod_conn) new_sch.set_fields(dn_id=self.id,obj_oid=p_sch[0],sch_name=p_sch[1]) if new_sch._create(): if not new_sch.discover_tables(): logger.error("DN.discover_schemas() False from discover_tables for new") if not new_sch.discover_functions(): logger.error("DN.discover_schemas() False from discover_functions for new") logger.info("Create new schema {0} in database {1}".format(p_sch[1],self.db_fields['db_name'])) else: logger.error("DN.discover_schemas() Cannot create new") return True
for hc_id in work_cursor.fetchall(): hc=HostCluster(hc_id[0]) hc.discover_cluster_params() hc.stat(lt.id) for dbs in hc.get_dependants(obs=True): db_conn_string=hc.return_conn_string(dbs['db_name']) dn=DatabaseName(dbs['id'],db_conn_string) dn.stat(lt.id) db_cursor=dn.get_prod_cursor() if db_cursor: for sn_id in dn.get_dependants(obs=True): sn=SchemaName(sn_id) sn.discover_tables(db_cursor) sn.discover_functions(db_cursor) for tbl_id in sn.get_tables(): tn=TableName(tbl_id) tn.stat(lt.id,db_cursor) tn.va_stat(lt.id,db_cursor) tn.discover_indexes(db_cursor) for ind_id in tn.get_dependants(): ind=IndexName(ind_id) ind.stat(lt.id,db_cursor) tn.discover_toast(db_cursor) toast_id=tn.get_toast_id() if toast_id: ttn=TableToastName(toast_id) ttn.stat(lt.id,db_cursor) ttn.discover_index(db_cursor)