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

Conversation

nicob3y
Copy link

@nicob3y nicob3y commented Mar 19, 2025

In the case we use the cache as a built-in destination we might expect that table-dependent objects would be droppped during a refresh.
Use case is also described on the Postgres destination connector (https://docs.airbyte.com/integrations/destinations/postgres#creating-dependent-objects)

Summary by CodeRabbit

  • New Features
    • Introduced a configurable option allowing users to enable cascade behavior during table removal.
    • Added a process to safely merge temporary tables into final ones, featuring automatic table renaming and error handling for missing table information.

@nicob3y nicob3y changed the title Permit SQL based caches to drop dependent objects when swapping temp tables with final ones fix: Permit SQL based caches to drop dependent objects when swapping temp tables with final ones Mar 19, 2025
Copy link
Contributor

coderabbitai bot commented Mar 19, 2025

📝 Walkthrough

Walkthrough

The changes introduce a new boolean attribute drop_cascade to the PostgresConfig class, defaulting to False, which specifies if the CASCADE option should be used when dropping tables. Additionally, a new method _swap_temp_table_with_final_table is added to the PostgresSqlProcessor class. This method swaps a temporary table with a final table by renaming the final table to a temporary deletion alias, renaming the temporary table to the final table, and then dropping the deletion table with an optional CASCADE clause. The method also performs input validation, raising exceptions when necessary.

Changes

File Change Summary
airbyte/_processors/.../postgres.py - Added new attribute drop_cascade: bool = False to the PostgresConfig class.
- Added _swap_temp_table_with_final_table method to the PostgresSqlProcessor class that swaps tables and conditionally applies the CASCADE clause during table deletion.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Processor as PostgresSqlProcessor
    participant DB as Database

    Client->>Processor: _swap_temp_table_with_final_table(stream_name, temp_table_name, final_table_name)
    alt Missing table names
        Processor-->>Client: Raise Exception
    else Valid table names
        Processor->>DB: Rename final table -> deletion alias
        Processor->>DB: Rename temporary table -> final table name
        Processor->>DB: Drop deletion table (using CASCADE if drop_cascade=True)
        Processor-->>Client: Operation completed successfully
    end
Loading

Suggested reviewers

  • Should we include bleonard as a reviewer? wdyt?
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@nicob3y nicob3y changed the title fix: Permit SQL based caches to drop dependent objects when swapping temp tables with final ones feat: Permit SQL based caches to drop dependent objects when swapping temp tables with final ones Mar 19, 2025
@aaronsteers
Copy link
Contributor

aaronsteers commented Mar 22, 2025

@nicob3y - Thank you for this contribution! The change looks good to me except I don't think every SQL dialect supports CASCADE.

Could we implement this specifically on Postgres and any other specific cache implementations(s) where needed?

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🔭 Outside diff range comments (1)
airbyte/_processors/sql/postgres.py (1)

79-108: ⚠️ Potential issue

The table swapping implementation looks good, but there's a missing import.

The implementation for swapping tables is well-structured with clear error handling and SQL generation. However, there's an undefined name exc used on lines 91 and 93 for error handling, which is causing pipeline failures.

You need to import the exceptions module. Looking at the error handling pattern, you might be missing an import like:

+ from airbyte.exceptions import exc

or possibly:

+ from airbyte import exceptions as exc

Depending on where your exceptions are defined. What do you think?

Also, small note about the docstring - it mentions requiring MERGE support, but the implementation is using RENAME and DROP operations instead. Would it make sense to update the docstring to be more aligned with the actual implementation, wdyt?

🧰 Tools
🪛 Ruff (0.8.2)

91-91: Undefined name exc

(F821)


93-93: Undefined name exc

(F821)

🪛 GitHub Actions: Run Linters

[error] 91-93: Name 'exc' is not defined. (name-defined)

🧹 Nitpick comments (1)
airbyte/_processors/sql/postgres.py (1)

95-95: Unused parameter

The line _ = stream_name indicates that stream_name is unused in this method. Consider either:

  1. Removing it from the method parameters if it's truly not needed, or
  2. Adding a comment explaining why it's required as a parameter but not used in this implementation

Is the stream_name parameter needed for future extensions or for compatibility with other implementations? If not, maybe we could simplify the signature?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e235040 and 152877e.

📒 Files selected for processing (1)
  • airbyte/_processors/sql/postgres.py (2 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
airbyte/_processors/sql/postgres.py

91-91: Undefined name exc

(F821)


93-93: Undefined name exc

(F821)

🪛 GitHub Actions: Run Linters
airbyte/_processors/sql/postgres.py

[error] 91-93: Name 'exc' is not defined. (name-defined)

⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Pytest (No Creds)
  • GitHub Check: Pytest (Fast)
🔇 Additional comments (2)
airbyte/_processors/sql/postgres.py (2)

27-28: Nice addition of the drop_cascade parameter!

This configuration parameter aligns perfectly with the PR objective of permitting SQL-based caches to drop dependent objects. The default value of False maintains backward compatibility while giving users the flexibility to enable CASCADE drops when needed. The docstring is clear and concise too.


103-105: Good implementation of the conditional CASCADE

The implementation correctly uses the drop_cascade configuration to conditionally add the CASCADE option when dropping tables. This is exactly what was needed according to the PR objectives.

The ternary expression is concise and readable. This will work perfectly for users who need to drop dependent objects like views when refreshing their caches.

@nicob3y
Copy link
Author

nicob3y commented Mar 29, 2025

@aaronsteers
Among the existing cache adapters, only Postgres supports this feature.
Therefore, I have created a new option named "drop_cascade" specifically for this adapter; by default, this option is disabled.

How to use it:

from airbyte.caches import PostgresCache

cache_config = {}
cache = PostgresCache(
  **cache_config,
  drop_cascade=True
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants