#NEWLINE# def run_table_upsert( self , dataset_id , table_resource , project_id = None ) : #NEWLINE# #INDENT# '\n        creates a new, empty table in the dataset;\n        If the table already exists, update the existing table.\n        Since BigQuery does not natively allow table upserts, this is not an\n        atomic operation.\n        :param dataset_id: the dataset to upsert the table into.\n        :type dataset_id: str\n        :param table_resource: a table resource. see https://cloud.google.com/bigquery/docs/reference/v2/tables#resource\n        :type table_resource: dict\n        :param project_id: the project to upsert the table into.  If None,\n        project will be self.project_id.\n        :return:\n        ' #NEWLINE# table_id = table_resource [ 'tableReference' ] [ 'tableId' ] #NEWLINE# table_exists = False #NEWLINE# project_id = ( project_id if ( project_id is not None ) else self . project_id ) #NEWLINE# tables_list_resp = self . service . tables ( ) . list ( projectId = project_id , datasetId = dataset_id ) . execute ( ) #NEWLINE# if ( 'tables' in tables_list_resp ) : #NEWLINE# #INDENT# for table in tables_list_resp [ 'tables' ] : #NEWLINE# #INDENT# if ( table [ 'tableReference' ] [ 'tableId' ] == table_id ) : #NEWLINE# #INDENT# table_exists = True #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if table_exists : #NEWLINE# #INDENT# logging . info ( 'table %s:%s.%s exists, updating.' , project_id , dataset_id , table_id ) #NEWLINE# return self . service . tables ( ) . update ( projectId = project_id , datasetId = dataset_id , tableId = table_id , body = table_resource ) . execute ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# logging . info ( 'table %s:%s.%s does not exist. creating.' , project_id , dataset_id , table_id ) #NEWLINE# return self . service . tables ( ) . insert ( projectId = project_id , datasetId = dataset_id , body = table_resource ) . execute ( )