:sparkles: add table creation order computation with FK constraints
This pull request introduces enhancements to the MySQL-to-SQLite data transfer process, focusing on foreign key handling, schema metadata extraction, and table creation order computation. Additionally, unit tests have been expanded to ensure robust functionality and edge case coverage.
Enhancements to MySQL-to-SQLite data transfer:
-
Foreign key handling during transfer:
- Added computation of table creation order to respect foreign key constraints using the new
compute_creation_orderfunction. Tables are processed in a dependency-safe order, and cyclic dependencies are logged as warnings. - Implemented a post-transfer check for foreign key constraint violations in SQLite using
PRAGMA foreign_key_check. Violations are logged for debugging purposes.
- Added computation of table creation order to respect foreign key constraints using the new
-
Schema metadata extraction:
- Introduced the
fetch_schema_metadatafunction to retrieve table names and foreign key relationships from the MySQL schema. This ensures robust handling of various row formats and edge cases.
- Introduced the
Codebase improvements:
- Topological sorting for table creation:
- Added the
topo_sort_tablesfunction to perform topological sorting of tables based on foreign key dependencies. This ensures tables are created in a foreign key-safe order. Cyclic dependencies are identified and returned for further analysis.
- Added the
Unit test enhancements:
- Expanded test coverage for new functionality:
- Added tests for
topo_sort_tablesto verify behavior with acyclic, cyclic, and empty dependency graphs. - Added tests for
fetch_schema_metadatato ensure robust handling of diverse row formats, including tuples, lists, objects, dictionaries, and invalid types. - Added tests for
compute_creation_orderto validate table ordering and cycle detection in schemas with and without circular dependencies. - Enhanced
transfermethod tests to verify foreign key checks and exception handling during SQLite operations.
- Added tests for
Walkthrough
New utilities for extracting MySQL schema metadata and determining table creation order respecting foreign key constraints were introduced. The transfer method in the transporter now uses this order, logs cycles, and checks for foreign key violations after transfer. Comprehensive unit tests were added for both the utility functions and the updated transfer logic.
Changes
| File(s) | Change Summary |
|---|---|
| src/mysql_to_sqlite3/mysql_utils.py | Added functions: fetch_schema_metadata, topo_sort_tables, and compute_creation_order for FK-aware ordering. |
| src/mysql_to_sqlite3/transporter.py | Modified transfer method to use FK-aware table order, handle cycles, and check FK constraints post-transfer. |
| tests/unit/test_mysql_utils.py | Added tests for topological sorting, schema metadata extraction, and creation order computation. |
| tests/unit/test_transporter.py | Enhanced and added tests for transfer logic, table ordering, cycle handling, and FK constraint checks. |
Sequence Diagram(s)
sequenceDiagram
participant User
participant MySQLtoSQLite
participant MySQLConn
participant SQLiteConn
User->>MySQLtoSQLite: transfer()
MySQLtoSQLite->>MySQLConn: fetch_schema_metadata()
MySQLConn-->>MySQLtoSQLite: tables, FK edges
MySQLtoSQLite->>MySQLtoSQLite: topo_sort_tables()
alt Cyclic dependencies detected
MySQLtoSQLite->>MySQLtoSQLite: Log warning
end
MySQLtoSQLite->>SQLiteConn: PRAGMA foreign_keys=OFF
loop For each table in order
MySQLtoSQLite->>SQLiteConn: Transfer table data
end
MySQLtoSQLite->>SQLiteConn: PRAGMA foreign_keys=ON
MySQLtoSQLite->>SQLiteConn: PRAGMA foreign_key_check
SQLiteConn-->>MySQLtoSQLite: FK violations (if any)
MySQLtoSQLite->>MySQLtoSQLite: Log FK violations
Suggested labels
mariadb
Poem
In the warren of tables, dependencies entwine,
Now sorted with care, in a foreign-key line.
If cycles appear, a warning will sound,
And after the hop, constraint checks abound.
With tests all a-flutter, our codeβs in fine shapeβ
A database journey, with nary escape!
πβ¨
π Recent review details
Configuration used: CodeRabbit UI Review profile: CHILL Plan: Pro
π₯ Commits
Reviewing files that changed from the base of the PR and between adf7c06fd4fc431d0f676e931af24a88ac5b1444 and de6cff24a0d4a20228217e0cc816f783cebe941d.
π Files selected for processing (1)
src/mysql_to_sqlite3/transporter.py(3 hunks)
π§ Files skipped from review as they are similar to previous changes (1)
- src/mysql_to_sqlite3/transporter.py
β° Context from checks skipped due to timeout of 90000ms (4)
- GitHub Check: Codacy Static Code Analysis
- GitHub Check: Analyze
- GitHub Check: Analyze (python)
- GitHub Check: Analyze (actions)
β¨ Finishing Touches
- [ ] π Generate Docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
πͺ§ Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>, please review it.Explain this complex logic.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai explain this code block.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin 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 explain its main purpose.@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.
Support
Need help? Create a ticket on our support page for assistance with any issues or questions.
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 pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile 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.
Codecov Report
Attention: Patch coverage is 90.66667% with 7 lines in your changes missing coverage. Please review.
Project coverage is 94.11%. Comparing base (
ab601c3) to head (de6cff2).
| Files with missing lines | Patch % | Lines |
|---|---|---|
| src/mysql_to_sqlite3/mysql_utils.py | 88.67% | 6 Missing :warning: |
| src/mysql_to_sqlite3/transporter.py | 95.45% | 1 Missing :warning: |
Additional details and impacted files
@@ Coverage Diff @@
## master #98 +/- ##
==========================================
- Coverage 94.40% 94.11% -0.29%
==========================================
Files 8 8
Lines 643 714 +71
==========================================
+ Hits 607 672 +65
- Misses 36 42 +6
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
@coderabbitai review
β Actions performed
Review triggered.
Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.