Giter Site home page Giter Site logo

mysql_merge's Introduction

mysql_merge

No Maintenance Intended

This small script allows you to merge two or more mysql databases with similar schema but different dataset. As a result you'll have your data copied into specified database.

It solves some important problems with data integrity across different databases like:

  • Primary Keys conflict
  • Keeping Foreign Keys up to date (sometimes even those not marked as foreign keys in the database schema!)
  • Fixing your orphaned rows (depending on configuration: with mapping, setting to null or deleting)
  • Dealing with conflicting unique indexes
  • Minor schema differences between databases
  • ... and a few more

To use:

  1. clone this repo (obviously)
  2. copy file mysql_merge/config.py.example mysql_merge/config.py
  3. setup your database details
  4. make sure your destination db already have schema loaded - this script does not transfer a schema!
  5. run as: python run.py
  • This script will change some of your PK's. It will rollback all changes in case of any error, but you should backup all your data before using it anyway!*
  • While PK values will not remain the same, all foreign keys and looks-like-it's-FK columns will be updated accordingly*

What it does in details:

  1. Maps all tables, columns, relations and unique/primary indexes
  2. If some FK-like columns are found (*_id) but they are not marked as FK - user is asked if that should be treated as FK and if yes - where does it point to
  3. Change all tables to InnoDB
  4. Alter all tables and modify FOREIGN KEYS to ON UPDATE CASCADE
  5. Resolve orphaned rows - strategy depends on configuration
  6. Update all numerical PKs to PK + iteration_nb * increment_step[table_name] ( so they don't conflict in the destination database; increment_step is easily customizable in config.py, if table_name key is not set default will be used instead)
  7. Detect which unique values conflicts with data in the destination db
  8. Update PKs on those rows to corresponding PKs from the destination db
  9. Copy data from all tables to the destination db
  10. Rolls back 6th step

Limitations:

  • Completely not intended to work with composite primary/foreign key other than M2M intermediary tables
  • It won't rollback changes from step 7
  • Non-FK values won't change their value (like non-fk column user_id that wasn't specified in step 2; all simulated-fks)
  • Conflicting non-numeric PKs will stop the script with an appropriate error message
  • MyISAM tables will be converted to InnoDB, script won't continue on failure
  • This is not meant to deal with any triggers, please turn them off
  • ... probably there are more :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.