定制 malukenho/migratedb 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

malukenho/migratedb

Composer 安装命令:

composer require malukenho/migratedb

包简介

The simple tools to migrate data between relational DB write in PHP

README 文档

README

Build Status Latest Stable Version Total Downloads Latest Unstable Version License

MigrateDB

MigrateDB is a simple tool to migrate data between databases.

Step 1

Installing the MigrateDB is very simple using composer :3

Create the follow script composer.json

{
    "require": {
        "malukenho/migratedb": "dev-master"
    }
}

Run composer install and all's okay!

Well, we have the follow table

mysql> SELECT * FROM user;
+------+------------+---------+
| id   | name       | passwd  |
+------+------------+---------+
|    1 | Kika Pimpo | 123@456 |
|    2 | RamStrYou  | 1!#@$%6 |
+------+------------+---------+
1 row in set (0.00 sec)

And would like to migrate this datas to another table.

mysql> SELECT * FROM member;
+-----------+-----------------+------------+
| user_id   | user_name       | user_pass  |
+-----------+-----------------+------------+
|           Nothing to see here            |
+-----------+-----------------+------------+
1 row in set (0.00 sec)

Let's go!

Create the class to get data of a table. You can use the annotation @from_table to set it, and @complement to increase your query.

<?php
/**
 * @Configurations(
 *     from_table="user",
 *     to_table="member",
 *     complement="WHERE status = '1'",
 *     type="select"
 * )
 */
class User implements EnumTablesRelation
{
	const user_id = 'id';
	const user_name = 'name'
	const user_pass = 'passwd';
}

The above code generates the following query

SELECT id, name, passwd FROM user WHERE status = 1

Step 2

Create the class to relation with previous class. It's gonna insert the data selected on Step 1 on this database

<?php
/**
 * @Configurations(
 *     from_table="user",
 *     to_table="member"
 * )
 */
class InsertUser implements EnumTablesRelation
{
	const IDENTIFY = 'user_id';
	const USERNAME = 'user_name'
	const PASSWORD = 'user_pass';
}

Final Step

Create file of configuration and execute migration :3

<?php
$loader = require __DIR__.'/vendor/autoload.php';

$mySql = new PDO('...');
$mySql2 = new PDO('...');

$router = new MigrateDB(new User);
 
$result = $router->setConnection($mySql, $mySql2)
    ->MapperDatas('1');
 
$router->replyTo(new InsertUser)
    ->with($result);

You can also set it like this

<?php
$routerClient = new MigrateDB(new ClientData);

$routerClient->registerFilter(new ClientFilter)
    ->replyTo(new ClientDataReply)
    ->with(
        $routerClient->setConnection(
            $mySqlConnection, 
            $fireBirdConnection
        )->MapperDatas(
            rand(0, 9)
        )
    );

Advanced

Types

We use advanced structures for migrating information between the database, it is decided according to the annotation type is the type definada in class EnumTablesRelation. There are 3 types valid until now. They are:

  • select
  • join
  • as

The select type has see on previous examples.

join

Here is a join example:

<?php
/**
 * @Configurations(
 *     from_table="UserList",
 *     to_table="NewUserList",
 *     complement="WHERE UserList.iduser = $1",
 *     type="join"
 * )
 */
class UserRelation implements EnumTablesRelation
{
	const user_id = 'userid';
	const user_name = 'name.user_detail ON id = 1';
	const user_pass = 'passwd';
}

The previous code generate the following query:

SELECT 
    `UserList`.`userid` AS user_id, 
    `UserList`.`passwd` AS user_pass, 
    `user_detail`.`name` AS user_name 
        FROM UserList 
            INNER JOIN 
                `user_detail` ON user_detail.id = 1 
WHERE UserList.iduser = 1

as

Here is a as example:

<?php
/**
 * @Configurations(
 *     from_table="UserList",
 *     to_table="NewUserList",
 *     complement="WHERE iduser = $1",
 *     type="as"
 * )
 */
class UserRelation implements EnumTablesRelation
{
	const user_id = 'userid';
	const user_name = 'name';
	const user_pass = 'passwd';
}

The previous code generate the following query:

SELECT 
    `user_id` AS `userid`, 
    `user_name` AS `name`, 
    `user_pass` AS `passwd` 
         FROM UserList 
WHERE iduser = 1

统计信息

  • 总下载量: 50
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 31
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 30
  • Watchers: 6
  • Forks: 7
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2013-12-07

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固