mysql-to-sqlite3 icon indicating copy to clipboard operation
mysql-to-sqlite3 copied to clipboard

:sparkles: add table creation order computation with FK constraints

Open techouse opened this issue 6 months ago β€’ 4 comments

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_order function. 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.
  • Schema metadata extraction:

    • Introduced the fetch_schema_metadata function to retrieve table names and foreign key relationships from the MySQL schema. This ensures robust handling of various row formats and edge cases.

Codebase improvements:

  • Topological sorting for table creation:
    • Added the topo_sort_tables function 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.

Unit test enhancements:

  • Expanded test coverage for new functionality:
    • Added tests for topo_sort_tables to verify behavior with acyclic, cyclic, and empty dependency graphs.
    • Added tests for fetch_schema_metadata to ensure robust handling of diverse row formats, including tuples, lists, objects, dictionaries, and invalid types.
    • Added tests for compute_creation_order to validate table ordering and cycle detection in schemas with and without circular dependencies.
    • Enhanced transfer method tests to verify foreign key checks and exception handling during SQLite operations.

techouse avatar Jun 08 '25 18:06 techouse

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.

❀️ Share
πŸͺ§ 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 @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @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 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 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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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 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.

coderabbitai[bot] avatar Jun 08 '25 18:06 coderabbitai[bot]

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.

codecov[bot] avatar Jun 08 '25 18:06 codecov[bot]

@coderabbitai review

techouse avatar Jun 08 '25 19:06 techouse

βœ… 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.

coderabbitai[bot] avatar Jun 08 '25 19:06 coderabbitai[bot]