Skip to content

Commit

Permalink
only truncate or delete from existing tables
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-rp committed Oct 4, 2024
1 parent 543044e commit 3afacb5
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions dlt/destinations/job_client_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,11 @@ def drop_tables(self, *tables: str, delete_schema: bool = True) -> None:
delete_schema: If True, also delete all versions of the current schema from storage
"""
with self.maybe_ddl_transaction():
self.sql_client.drop_tables(*tables)
if delete_schema:
# only run drop command on existing tables
existing_tables = self.get_known_existing_storage_tables()
tables_to_drop = set(existing_tables).intersection(set(tables))
self.sql_client.drop_tables(*tables_to_drop)
if delete_schema and (self.schema.version_table_name in existing_tables):
self._delete_schema_in_storage(self.schema)

@contextlib.contextmanager
Expand Down Expand Up @@ -378,6 +381,12 @@ def get_storage_table(self, table_name: str) -> Tuple[bool, TTableSchemaColumns]
storage_table = list(self.get_storage_tables([table_name]))[0]
return len(storage_table[1]) > 0, storage_table[1]

def get_known_existing_storage_tables(self) -> List[str]:
"""Returns all tables known in dlt schema and found in destination"""
tables = self.get_storage_tables(self.schema.tables.keys())
existing_tables = [t[0] for t in tables if len(t[1]) > 0]
return existing_tables

@abstractmethod
def _from_db_type(
self, db_type: str, precision: Optional[int], scale: Optional[int]
Expand Down

0 comments on commit 3afacb5

Please sign in to comment.