Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async def create_table(self, schema_name: str, table_name: str, table_config: li
async def delete_table(self, table_name: str, schema_name: Optional[str] = "public"):
async with self.engine.begin() as connection:
if self.engine.dialect.name == "sqlite":
# SQLite doesnt support schema namespaces and the CASCADE keyword.
# SQLite doesn't support schema namespaces and the CASCADE keyword.
# However, foreign key constraint can be defined with ON DELETE CASCADE during table creation.
await connection.execute(text(f'DROP TABLE IF EXISTS "{table_name}";'))
else:
Expand Down Expand Up @@ -327,19 +327,18 @@ async def delete_database(self):
file.write("")
else:
async with self.engine.begin() as connection:
schema_list = await self.get_schema_list()
# Create a MetaData instance to load table information
metadata = MetaData()
# Drop all tables from all schemas
for schema_name in schema_list:
# Load the schema information into the MetaData object
await connection.run_sync(metadata.reflect, schema=schema_name)
for table in metadata.sorted_tables:
drop_table_query = text(
f'DROP TABLE IF EXISTS {schema_name}."{table.name}" CASCADE'
)
await connection.execute(drop_table_query)
metadata.clear()
# Drop all tables from the public schema
schema_name = "public"
Copy link
Collaborator

@dexters1 dexters1 Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'public_staging' schema also should be deleted, it's created by dlt

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added deletion of 'public_staging' schema as well

# Load the schema information into the MetaData object
await connection.run_sync(metadata.reflect, schema=schema_name)
for table in metadata.sorted_tables:
drop_table_query = text(
f'DROP TABLE IF EXISTS {schema_name}."{table.name}" CASCADE'
)
await connection.execute(drop_table_query)
metadata.clear()
except Exception as e:
logger.error(f"Error deleting database: {e}")
raise e
Expand Down
Loading