-
Notifications
You must be signed in to change notification settings - Fork 88
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
fix: Add support for SQLite and PostgreSQL for inserting data in SQLA… #467
base: dev
Are you sure you want to change the base?
Conversation
Warning Rate limit exceeded@dexters1 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 15 minutes and 51 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (1)
WalkthroughThe changes focus on the Changes
Sequence DiagramsequenceDiagram
participant Caller
participant SQLAlchemyAdapter
participant SQLAlchemyCore
participant Database
Caller->>SQLAlchemyAdapter: insert_data(table_name, data)
SQLAlchemyAdapter->>SQLAlchemyCore: Prepare insert statement
alt Data is not empty
SQLAlchemyCore->>Database: Execute insert
Database-->>SQLAlchemyCore: Return row count
SQLAlchemyCore-->>SQLAlchemyAdapter: Return row count
else Data is empty
SQLAlchemyAdapter-->>Caller: Return 0
end
SQLAlchemyAdapter-->>Caller: Return number of inserted rows
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (18)
- GitHub Check: run_notebook_test / test
- GitHub Check: run_notebook_test / test
- GitHub Check: run_notebook_test / test
- GitHub Check: run_multimedia_example_test / test
- GitHub Check: run_dynamic_steps_example_test / test
- GitHub Check: run_notebook_test / test
- GitHub Check: test
- GitHub Check: docker-compose-test
- GitHub Check: test
- GitHub Check: windows-latest
- GitHub Check: test
- GitHub Check: test
- GitHub Check: test
- GitHub Check: test
- GitHub Check: test
- GitHub Check: test
- GitHub Check: test
- GitHub Check: profiler
🔇 Additional comments (1)
cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py (1)
80-85
:⚠️ Potential issuePotential breaking change due to altered method signature.
The
insert_data
method's signature has been changed from:async def insert_data(self, schema_name: str, table_name: str, data: list[dict])to:
async def insert_data(self, table_name: str, data: list[dict], schema_name: Optional[str] = "public") -> intThis reorders parameters and adds a default value for
schema_name
. If any existing calls to this method use positional arguments, this change can lead to unexpected behavior or runtime errors. It's important to ensure that all invocations of this method throughout the codebase have been updated to reflect this new signature.Please run the following script to identify any usages of
insert_data
that may need to be updated:This script searches for all calls to
insert_data
outside the file where it's defined, helping to locate and review them for necessary updates.
except Exception as e: | ||
logger.error(f"Insert failed: {str(e)}") | ||
raise e # Re-raise for error handling upstream |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider handling specific exceptions and avoid re-raising generic exceptions.
Catching and re-raising the general Exception
class might obscure the root cause and make debugging harder. It's better to catch specific exception types that you expect might occur (e.g., SQLAlchemyError
) and handle them accordingly. Also, consider defining custom exceptions for your application to provide more context.
Here's how you might adjust the exception handling:
try:
# ... (code that might raise an exception)
-except Exception as e:
+except SQLAlchemyError as e:
logger.error(f"Insert failed: {str(e)}")
- raise e # Re-raise for error handling upstream
+ raise DatabaseInsertError(f"Failed to insert data into {table_name}") from e
You'll need to:
- Import
SQLAlchemyError
fromsqlalchemy.exc
. - Define a
DatabaseInsertError
custom exception in yourexceptions
module to provide more specific error handling upstream.
Committable suggestion skipped: line range outside the PR's diff.
There was a problem hiding this 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
♻️ Duplicate comments (1)
cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py (1)
111-113
:⚠️ Potential issueConsider handling specific exceptions and avoid re-raising generic exceptions.
Catching and re-raising the general
Exception
class might obscure the root cause and make debugging harder.As mentioned in the previous review, consider:
- Catching specific exceptions (e.g.,
SQLAlchemyError
)- Using custom exceptions for better error context
- Preserving the original exception chain using
raise ... from
+from sqlalchemy.exc import SQLAlchemyError +from cognee.infrastructure.databases.exceptions import DatabaseInsertError + try: # ... (code that might raise an exception) -except Exception as e: +except SQLAlchemyError as e: logger.error(f"Insert failed: {str(e)}") - raise e # Re-raise for error handling upstream + raise DatabaseInsertError(f"Failed to insert data into {table_name}") from e
🧹 Nitpick comments (2)
cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py (2)
80-89
: Enhance the docstring with more details.While the docstring provides basic information, it could be more comprehensive by including:
- Parameter descriptions
- Return value details
- Example usage
Consider expanding the docstring like this:
- """ - Insert data into specified table using SQLAlchemy Core with batch optimization - Returns number of inserted rows - """ + """ + Insert data into specified table using SQLAlchemy Core with batch optimization. + + Args: + table_name (str): Name of the table to insert data into + data (list[dict]): List of dictionaries containing the data to insert + schema_name (Optional[str], optional): Database schema name. Defaults to "public". + Ignored for SQLite. + + Returns: + int: Number of rows successfully inserted + + Example: + data = [{"column1": "value1"}, {"column1": "value2"}] + rows_inserted = await insert_data("my_table", data) + """
98-101
: Consider setting SQLite foreign key pragma at connection level.While enabling foreign keys for SQLite is crucial, setting the pragma for each operation is redundant and could be moved to the connection setup.
Consider moving this to the engine creation in
__init__
:def __init__(self, connection_string: str): self.db_path: str = None self.db_uri: str = connection_string - self.engine = create_async_engine(connection_string) + connect_args = {} + if connection_string.startswith('sqlite'): + connect_args['pragma_foreign_keys'] = 'ON' + + self.engine = create_async_engine( + connection_string, + connect_args=connect_args + )
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (19)
- GitHub Check: run_notebook_test / test
- GitHub Check: run_notebook_test / test
- GitHub Check: run_simple_example_test / test
- GitHub Check: run_dynamic_steps_example_test / test
- GitHub Check: run_notebook_test / test
- GitHub Check: run_notebook_test / test
- GitHub Check: test
- GitHub Check: test
- GitHub Check: run_multimedia_example_test / test
- GitHub Check: test
- GitHub Check: test
- GitHub Check: test
- GitHub Check: windows-latest
- GitHub Check: test
- GitHub Check: test
- GitHub Check: test
- GitHub Check: test
- GitHub Check: docker-compose-test
- GitHub Check: profiler
🔇 Additional comments (1)
cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py (1)
90-92
: LGTM! Good use of early return pattern.The empty data check with appropriate logging is a good practice.
…lchemyAdapter
Description
DCO Affirmation
I affirm that all code in every commit of this pull request conforms to the terms of the Topoteretes Developer Certificate of Origin
Summary by CodeRabbit
New Features
Bug Fixes
Refactor