Skip to content

Commit

Permalink
part 1: only truncate existing tables
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-rp committed Nov 7, 2024
1 parent 17847f1 commit c669e15
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions dlt/destinations/impl/bigquery/sql_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,20 @@ def _make_database_exception(cls, ex: Exception) -> Exception:
# anything else is transient
return DatabaseTransientException(ex)

def truncate_tables(self, *tables: str) -> None:
statements: List[str] = ["DECLARE table_exists BOOL;"]
for t in tables:
table_name = self.make_qualified_table_name(t)
statements.append(
"SET table_exists = (SELECT COUNT(*) > 0 FROM"
f" `{self.project_id}.{self.dataset_name}.INFORMATION_SCHEMA.TABLES` WHERE"
f" table_name = '{t}');"
)
statements.append(
f"IF table_exists THEN EXECUTE IMMEDIATE 'TRUNCATE TABLE `{table_name}`'; END IF;"
)
self.execute_many(statements)

@staticmethod
def _get_reason_from_errors(gace: api_core_exceptions.GoogleAPICallError) -> Optional[str]:
errors: List[StrAny] = getattr(gace, "errors", None)
Expand Down

0 comments on commit c669e15

Please sign in to comment.