def stat(self,in_time_id): if not super(DatabaseName,self).stat(in_time_id): logger.error("Return from DN.stat False from super.stat()") return False sn_sets=self.get_dependants(True) if not sn_sets: logger.error("Return from DN.stat No dependants returned") return False for sn_set in sn_sets: sn=SchemaName(self.db_conn,self.prod_conn,sn_set) if not sn.stat(in_time_id): logger.error("DN.stat false from sn.stat()") return True
def discover_schemas(self): if not self.get_self_db_conn(): return cur=self.prod_conn.cursor() 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 get schema info for database {0}".format(self.db_fields['db_name'])) cur.close() return prod_schs=cur.fetchall() cur.close() self.cursor.execute("SELECT obj_oid,sch_name,id FROM schema_name WHERE dn_id={0} AND alive".format(self.id)) local_schs=self.cursor.fetchall() 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]: break else: old.sch=SchemaName(l_sch[2]) old_sch.retire() logger.info("Retired schema {0} in database {1}".format(l_sch[1],self.db_fields['db_name'])) 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() new_sch.set_fields(dn_id=self.id,obj_oid=p_sch[0],sch_name=p_sch[1]) new_sch.create() new_sch.truncate() logger.info("Create new schema {0} in database {1}".format(p_sch[1],self.db_fields['db_name']))
work_cursor.execute("SELECT id FROM host_cluster WHERE alive AND observable") 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)
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
exit(1) hc_ids=cur.fetchall() cur.close() for hc_id in hc_ids: hc=HostCluster(conn,hc_id[0]) if not hc.discover_cluster_params(): continue hc.stat(lt_id) for dbs in hc.get_dependants(True): db_dsn=dbs['db_dsn'] dn=DatabaseName(conn,db_dsn,dbs['id']) dn.stat(lt_id) for sn in dn.get_dependants(True): sn=SchemaName(conn,db_dsn,sn) sn.discover_tables() sn.discover_functions() for tbl_id in sn.get_tables(): tn=TableName(conn,db_dsn,tbl_id) tn.stat(lt_id) tn.va_stat(lt.get_id()) tn.discover_indexes() for ind_id in tn.get_dependants(): ind=IndexName(conn,db_dsn,ind_id) ind.stat(lt_id) tn.discover_toast() toast_id=tn.get_toast_id() if toast_id: ttn=TableToastName(conn,db_dsn,toast_id)