def delete_table_not_in_metastore(schema_id, table_names, session=None): db_tables = get_table_by_schema_id(schema_id, session=session) with session.no_autoflush: for data_table in db_tables: if data_table.name not in table_names: table_id = data_table.id delete_table(table_id=table_id, commit=False, session=session) delete_es_table_by_id(table_id) LOG.info(f"deleted table {table_id}") session.commit()
def delete_schema_not_in_metastore(metastore_id, schema_names, session=None): for data_schema in iterate_data_schema(metastore_id, session=session): LOG.info("checking schema %d" % data_schema.id) if data_schema.name not in schema_names: # We delete table 1 by 1 since we need to delete it for elasticsearch # Maybe we can optimize it to allow batch deletion for table in data_schema.tables: table_id = table.id delete_table(table_id=table_id, commit=False, session=session) delete_es_table_by_id(table_id) delete_schema(id=data_schema.id, commit=False, session=session) LOG.info("deleted schema %d" % data_schema.id) session.commit()
def sync_delete_table(self, schema_name, table_name, session=None): """Given a full qualified table name, Remove the table if it exists Arguments: schema_name {str} -- the schema name table_name {str} -- the table name """ # Double check if the table is indeed removed table_exists = self.check_if_table_exists(schema_name, table_name) # If no schema, then table doesn't exist schema = get_schema_by_name(schema_name, self.metastore_id, session=session) if not table_exists and schema is not None: table = get_table_by_schema_id_and_name( schema.id, table_name, session=session ) if table: delete_table(table_id=table.id, session=session)