Skip to content

feat: Permit SQL based caches to drop dependent objects when swapping temp tables with final ones #629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions airbyte/_processors/sql/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class PostgresConfig(SqlConfig):
database: str
username: str
password: SecretString | str
drop_cascade: bool = False
"""Whether to use CASCADE when dropping tables."""

@overrides
def get_sql_alchemy_url(self) -> SecretString:
Expand Down Expand Up @@ -73,3 +75,33 @@ class PostgresSqlProcessor(SqlProcessorBase):

normalizer = PostgresNormalizer
"""A Postgres-specific name normalizer for table and column name normalization."""

def _swap_temp_table_with_final_table(
self,
stream_name: str,
temp_table_name: str,
final_table_name: str,
) -> None:
"""Merge the temp table into the main one.

This implementation requires MERGE support in the SQL DB.
Databases that do not support this syntax can override this method.
"""
if final_table_name is None:
raise exc.PyAirbyteInternalError(message="Arg 'final_table_name' cannot be None.")
if temp_table_name is None:
raise exc.PyAirbyteInternalError(message="Arg 'temp_table_name' cannot be None.")

_ = stream_name
deletion_name = f"{final_table_name}_deleteme"
commands = "\n".join(
[
f"ALTER TABLE {self._fully_qualified(final_table_name)} RENAME "
f"TO {deletion_name};",
f"ALTER TABLE {self._fully_qualified(temp_table_name)} RENAME "
f"TO {final_table_name};",
f"DROP TABLE {self._fully_qualified(deletion_name)}"
f"{(' CASCADE' if self.sql_config.drop_cascade else '')};",
]
)
self._execute_sql(commands)
Loading