Tool for copy records from one Mysql database to another with all dependencies.
Additional features:
- Build array/json 2-level tree for representation database
Via Composer
Add information about new package in your composer.json
"repositories": [
...
{
"type": "vcs",
"url": "[email protected]:operations/database-minifier.git",
"name": "paunin/database-minifier"
}
],
"require-dev": {
"paunin/database_minifier": ">=0.0.1"
},
- Database minifier allows you to create dump files with
INSERTqueries for database with respect to references. That means you will be able to extract data from database and apply it on another one with the same structure. - All records which are referenced by rows you want to move will be copied as well as DB should keep consistency.
- Optionally you can copy records which have references to target rows (
copyReferencedByoption in all directives) - Minifier supports multi-source extraction. You can describe links between 2 separate databases and the script will extract data like they have foreign keys across two databases
- For multi-source mode Minifier can dump
INSERTqueries into different files. By default all queries will be dumped instdout
As Minifier has multi-source mode each function accepts table names with namespaces only (e.g. connection_name:tableA), where namespace is a connection name you have in configuration for minifier.
- Copy
minifier.json.disttominifier.jsonand configure it:-
connections- sources you want to explore{ "source1": { # use this name as namespace for tables in the connection "dbname": "%dbname%", "username": "%user%", "password": "%pwd%", "host": "%host%", "driver": "mysql", # only mysql is supported "out_file": "php://stdout", # any file for dumping SQL for this connection }/* , ... * / } -
relations- links between different sources/databases or inside one.{ "%table%": [ "%table%": [ ["%fk%": "%pk%" /* , ... * /] # keep in mind complex PKs /* , ... * / # if we have more then one reference to the same table ] /* , ... * / # more links to other tables ] /* , ... * / }
-
- Add more
directivesin array format["method": "%method%", "arguments": [%arg1%, %arg2%, ... ]]. All directives described bellow. - Run command
php run-minifier.php [%config_json%]whereconfig_jsonfile with configurations (minifier.jsonby default)
Create new object:
$dm = new \Paunin\DatabaseMinifier\DatabaseMinifier($connections, $relations);
Where $connections and $relations are options in format described for json config.
You can build your database tree and use it in your purposes
[
"%table%": [
"primary_key": ["%PK1%", "%PK2%" /* , ... * /],
"references": [
"%table%": [
["%fk%": "%pk%" /* , ... * /] # keep in mind complex PKs
/* , ... * / # if we have more then one reference to the same table
] /* , ... * / # more links to other tables
],
"referenced_by": [
"%table%": [
["%fk%": "%pk%" /* , ... * /] # keep in mind complex PKs
/* , ... * / # if we have more then one reference to the same table
] /* , ... * / # more links to other tables
]
] /* , ... * /
]
Returns Json object like DatabaseMinifier::buildArrayTree()
Copy all records (with all dependencies) from master db to salve.
$tableName- table name with namespace (e.g.connection_name:table_name)$copyReferencedBy- if istrueit will also copy all records depend on found records.$limit- can be integer or string in format{LIMIT}, {OFFSET}
$tableName- table name with namespace (e.g.connection_name:table_name)$pk- primary key value or array of value for complex primary keys$copyReferencedBy- if istrueit will also copy all records depend on found.
$tableName- table name with namespace (e.g.connection_name:table_name)$pks- array of primary keys for functioncopyRecordsByPk$copyReferencedBy- if istrueit will also copy all records depend on found records.
- Start environment with docker-compose
docker-compose build - And run tests
docker-compose run application ./vendor/phpunit/phpunit/phpunit