def check_if_table_exists(table_name, connection): hook = PostgresHook(postgres_conn_id=connection) query = hook.get_first(f'''SELECT * from information_schema.tables where table_name = \'{table_name}\' and table_schema = \'public\'''') if query: return 'insert_new_row' else: return 'create_table'
def check_table_exist(sql_to_get_schema, sql_to_check_table_exist, table_name): """ callable function to check if table exist """ hook = PostgresHook() query = hook.get_first(sql=sql_to_check_table_exist.format(table_name.lower())) if query: return "insert_row" else: return "create_table"
class NotebookToKeepOperator(PostgresOperator): @apply_defaults def __init__(self, *args, **kwargs): super(NotebookToKeepOperator, self).__init__(*args, **kwargs) def execute(self, context): self.log.info('Executing: %s', self.sql) self.hook = PostgresHook(postgres_conn_id=self.postgres_conn_id, schema=self.database) result = self.hook.get_first(self.sql, parameters=self.parameters) if not result: raise AirflowException("The query returned None") record = result[0] self.log.info('First record: {0}'.format(record)) for output in self.hook.conn.notices: self.log.info(output) return record
def execute(self, context): hook = PostgresHook(postgres_conn_id='postgres_conn') query = hook.get_first(f'SELECT COUNT(*) from {self.table_name}') logging.info(query[0]) return query[0]