dev-master
9999999-devTool to migrate from a database to another one.
MIT
The Requires
- php >=5.6.0
- illuminate/support ~5
- illuminate/database ~5
by Marcin Kozak
database laravel migration db laravel5
Wallogit.com
2017 © Pedro Peláez
Tool to migrate from a database to another one.
A simple console tool which helps to migrate database data from one to another using different DB connections., (*1)
Sometimes you can have an opportunity to make your custom database based on the existing one but in your opinion you can design it in better way for example primary keys are not names simply as id but in the <table_name>_id pattern, or columns values are not exactly what you want., (*2)
Add marcinkozak/databasemigrator to composer.json., (*3)
"marcinkozak/databasemigrator": "dev-master"
Or simply run:, (*4)
composer require marcinkozak/databasemigrator
To make it available for Laravel open the config/app.php file and add line below., (*5)
'providers' => array(
// Other service providers entries
MarcinKozak\DatabaseMigrator\DatabaseMigratorServiceProvider::class,
);
Run command, (*6)
php artisan vendor:publish --provider="MarcinKozak\DatabaseMigrator\DatabaseMigratorServiceProvider"
to publish new files in an application root:, (*7)
config/marcinkozak/databasemigrator/connections.phpdatabase/schemas/ExampleSchema.phpThe first file contains a collection of connections where each of them defines source and target DB connection, disabled/enabled state and class name of schema., (*8)
<?php
return [
[
'source' => 'mysql2',
'target' => 'mysql',
'enabled' => true,
'schema' => ExampleSchema::class,
],
];
The schema files are located in the database/schemas/ directory., (*9)
Before you start migrating you must have defined target tables, otherwise an exception will be thrown., (*10)
$table = new Table('table_name');
$table = new Table('source_table_name', 'target_table_name');
This is a mandatory step to make migration works for the selected table. You can define a single column, (*11)
$column = new Column('column_name');
$table->addColumn($column);
or for different names, (*12)
$column = new Column('source_column_name', 'target_column_name');
$table->addColumn($column);
or use the schema() method to define multi columns in single step., (*13)
$table->schema([
'column_1',
'column_2',
//...
]);
Of course you are able to define different names inside the above method., (*14)
$table->schema([
'source_column_1' => 'target_column_1',
'source_column_2' => 'target_column_2',
//...
]);
Not always we want to migrate the same value for certain column. The package supports the map() method which allows to transform value to the new one. To use it simply define new Column instance., (*15)
$column = new Column('column_name');
$column->map(function($value) {
return $value . '_some_stupid_word';
});
If you want to make some relation which has poorly designed in the source table you can follow that way, (*16)
$data = DB::table('some_table')->pluck('id', 'some_column_name');
$column = new Column('column_name');
$column->map(function($value) use($data) {
return array_get($data, $value);
});
Let's suppose that in this case $value stores identical value as the some_column_name column. By using the array_get function we can fetch the desirable primary key and use it as foreign key for the new value inside the map method., (*17)
The package has two Artisan methods., (*18)
php artisan database-migrator:populate, (*19)
php artisan database-migrator:clear, (*20)
Tool to migrate from a database to another one.
MIT
database laravel migration db laravel5