"""Snowflake connector class for the tasks of the YouTube Video workflow.""" from snowflake_connector.etl_connector import SQLLoader from feed_ingestion.common.youtube_sf.base_executor import YouTubeExecutor from feed_ingestion.flows.youtube_video import config # Load SQL templates sql_loader = SQLLoader(__file__) class YoutubeVideo(YouTubeExecutor): """Helper class to abstract Snowflake operations.""" sql_loader = SQLLoader(__file__) @property def staging_raw_table(self): """Name of the staging_raw table for the feed. Returns: str: staging_raw_{feed} table name. """ return config.snowflake['staging_raw'] @property def channel_owner(self): """Name of the channel owner. Returns: str: channel_owner. """ return 'MCN' def create_temp_staging_raw_table(self, temp_staging_raw_table): """Create a temporary staging raw table. Args: temp_staging_raw_table (str): A table name in Snowflake. """ params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], temp_staging_raw_table=temp_staging_raw_table) self.execute_query(sql_loader, 'create_temp_staging_raw', params) def clean_staging_raw_table(self, licensor): """Delete rows from previous unsuccessful workflow run. Args: licensor: Name of the licensor """ params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], staging_raw_table=self.staging_raw_table, licensor=licensor) self.execute_query(sql_loader, 'delete_from_staging_raw', params) def load_staging_raw_table(self, temp_staging_raw_table): """Delete rows from previous unsuccessful workflow run. Args: temp_staging_raw_table (str): A table name in Snowflake. """ params = dict( db=self.sf_config['db'], schema=self.sf_config['schema'], temp_staging_raw_table=temp_staging_raw_table, staging_raw_table=self.staging_raw_table) return self.execute_query(sql_loader, 'load_staging_raw', params) def update_staging_raw_table(self, date, licensor): """Update staging_raw_table table.""" return self.execute_query( sql_loader, 'update_staging_raw', params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], staging_raw_table=self.staging_raw_table, download_date=date, licensor=licensor)) def update_mnc_and_owner_columns(self, date, channel_names_table_name, licensor): """Update staging_raw_table table.""" return self.execute_query( sql_loader, 'update_mnc_and_owner_columns', params=dict( db=self.sf_config['db'], schema=self.sf_config['schema'], staging_raw_table=self.staging_raw_table, channel_names_table=channel_names_table_name, download_date=date, licensor=licensor))