php两个数据库直接数据迁移,Phinx : 简单的PHP数据库迁移工具

bc3f9276acf88283a0569b9b9ef9ee92.png

在线QQ客服:1922638

专业的SQL Server、MySQL数据库同步软件

Phinx使您的php应用程序的数据迁移过程变得非常容易。在五分钟内,您可以安装Phinx并执行数据迁移。 Phinx允许开发人员以简洁明了的方式修改和操作数据库。依靠Phinx提供了强大的API,您无需手动编写SQL,而使用PHP代码创建代码迁移。开发人员可以使用自己喜欢的SCM进行代码迁移。这允许Phinx代码迁移在不同的数据库系统之间移植。 。Phinx将跟踪已执行的代码迁移,以便您不必担心数据库状态,而专注于开发更好的软件。

功能

使用php代码进行数据迁移

部署模式下的迁移

五分钟内使用

不再担心数据库的状态

可以集成任何应用程序

特别适用于开发,测试,在线数据库字段,数据同步(博客注释)

安装

使用Composer安装

1.安装作曲家

curl-s https://getcomposer.org/installer | php

1

curl – s https : //getcomposer.org/installer | php

2.将phinx作为依赖项添加到composer.json

{

“要求”:{

” robmorgan/phinx”:” *”

}

}

1

2

3

4

5

{

” require” : {

” robmorgan/phinx” : ” *”

\\ ubsp; }

}

3.安装Phinx

php composer.phar install

1

php 作曲家 。 phar install

4.运行Phinx

PHP供应商/bin/phinx

1

php 供应商 / bin / phinx

附件:如果您还没有完全做到,则表示祝贺。当然,有多种安装方法。有关其他方法,请参阅官方文档。在实践几种安装方法的过程中遇到了一些问题,所有方法都涉及php.ini。通常,在安装过程中会出现错误提示。一种是关闭某些安全功能,另一种是关闭phar的只读项目,然后重新启动php-fpm或apache。

然后,您可以创建一个新的迁移类

php vendor/bin/phinx创建MyNewMigration

1

php 供应商 / bin / phinx 创建 MyNewMigration

phinx的默认配置文件是项目根目录中的phinx.yml,但是官方文件中我们也可以使用phinx.php,只要删除phinx.yml,phinx.php就会自动生效。在这里,我们使用phinx.php完成配置项

我们可以将数据库信息设置为在一个单独的文件中迁移,然后在phinx.php中引入它

\ lt ;的PHP

//项目根目录/settings.php

$ config =数组(

” DB_HOST” = \ gt; “本地主机”,

” DB_NAME” = \ gt; ” phinx_test”,

” DB_USER” =>” root”,

” DB_PWD” =>” devop”

);

$设置= $配置;

1

2

3

4

5

6

7

8

9

\ lt; ? php

//项目根目录/settings.php

$ 配置 = 数组 (

\; ” DB _HOST” = \ gt; ” localhost” ,

” DB_NAME” = \ gt; ” phinx_test” ,

” DB_USER” = \ gt; ” root” ,

” DB_PWD” = \ gt; ” devop”

) ;

$ 设置 = $ config ;

\ lt ;的PHP

//项目根目录/phinx.php

需要” settings.php”;

返回数组(

“路径” =”数组(

“迁移” =”>” ” db/迁移”,

“种子” =”>” ” db/种子”

),

“环境” =”>”数组(

” default_migration_table” = \ gt; ” phinxlog”,

” default_database” = \ gt; “产品”,

“产品” =”数组(

” adapter” = \ gt; ” mysql”,

“主机” = \ gt; $ settings [” DB_HOST”],

“名称” = \ gt; $个设置[” DB_NAME”],

” user” = \ gt; $ settings [” DB_USER”],

” pass” = \ gt; $设置[” DB_PWD”]

)

)

);

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

\ lt; ? php

//\\ ub26; 项目根目录/phinx.php

要求 ” settings.php” ;

return 数组 (

” path” = \ gt; 数组 (

\\ “迁移” = \ gt; /span> “数据库/迁移” ,

“种子” = \ gt; ” db/种子”

) ,

“环境” = \ gt; 数组 (

\ ” default_migration_table” = \ gt; ” phinxlog” ,

” default_database” = \ gt; ” product” ,

“产品” = \ gt; 数组 (

; ” adapter” = \ gt; ” mysql” ,

; ” host” = \ gt; $ 设置 [ ” DB_HOST” ] ,

“名称” = \ gt; $ 设置 [ ” DB_NAME” ] ,

” user” = \ gt; $ 设置 [ ” DB_USER” ] ,

class =” crayon-s”>”通过” = \ gt; $ 设置 [ ” DB_PWD” ]

)

)

) ;

接下来输入20160809174305_my_new_migration.php(此处编号是根据上面创建命令时的时间系统自动生成的),此处有3种可用方法,即更改,向上,向下,我们可以向上编写代码在执行之前,向下写下需要回滚的代码。需要特别注意的是,您不应使用change方法,因为它将影响上下代码的执行

以下是在up方法中使用的一些示例,down也被依次模拟,请参考文档以了解详细信息

\ lt ;的PHP

//项目根目录/db/migrations/20160809174305_my_new_migration.php

使用Phinx \\\\迁移\\\\ AbstractMigration;

类MyNewMigration扩展AbstractMigration

{

公共功能up()

{

$ users = $ this-\\ u>表(”用户”);

$ users-\\ u> addColumn(” id”,” string”,数组(” limit” = \ gt; 20))

->更新();

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

\ lt; ? php

\ nbsp ;

//项目根目录/db/migrations/20160809174305_my_new_migration.php

使用 Phinx \\\\ 迁移 \\\\ AbstractMigration ;

class MyNewMigration 扩展 AbstractMigration

{

\\ ubsp;

公共 函数 上 ( )

{

$ 用户 = $ 此 -\ gt; 表 ( ” users” ) ;

$ 用户 -\ gt; addColumn ( ” id” , ” string” , 数组 ( ” limit” = \ gt; 20 ) )

\ -\ gt; 更新 ( ) ;

}

}

\ lt ;的PHP

//项目根目录/db/migrations/20160809174305_my_new_migration.php

使用Phinx \\\\迁移\\\\ AbstractMigration;

类MyNewMigration扩展AbstractMigration

{

公共功能up()

{

$ data =数组(

数组(

” id” = \ gt; 11

),

数组(

” id” = \ gt; 21岁

)

);

$ posts = $ this-\\ ugt26;表(”用户”);

$ posts-\\ u>插入($ data)->救 ();

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

\ lt; ? php

\ nbsp ;

//项目根目录/db/migrations/20160809174305_my_new_migration.php

使用 Phinx \\\\ 迁移 \\\\ AbstractMigration ;

class MyNewMigration 扩展 AbstractMigration

{

\\ ubsp;

公共 函数 上 ( )

{

$ 数据 = 数组 (

\ nbsp ; 数组 (

\ ” id” \\ u002 6汤匙; = \ gt; 11 ,

) ,

数组 (

” id” \ n bsp; = 21 ,

\ ) ) span>

) ;

$ 帖子 = $ 此 -\ gt; 表 ( ” users” ) ;

$ 帖子 -\ gt; 插入 ( $ 数据 ) -\ gt; 保存 ( ) ;

}

}

编写代码后,执行以下命令

PHP供应商/bin/phinx迁移-e产品

1

php 供应商 / bin / phinx 迁移 \\ ubsp; – e 产品

除了上述up方法示例的插入数据外,我们还可以使用种子类来实现,首先执行命令来创建种子类

php vendor/bin/phinx种子:创建UserSeeder

1

php 供应商 / bin / phinx 种子 : 创建 UserSeeder

\ lt ;的PHP

//项目根目录/db/种子/UserSeeder.php

使用Phinx \\\\ Seed \\\\ AbstractSeed;

UserSeeder类扩展AbstractSeed

{

公共功能运行()

{

$ data =数组(

数组(

” id” = \ gt; 1,

),

数组(

” id” = \ gt; 2,

)

);

$ posts = $ this-\\ ugt26;表(”用户”);

$ posts-\\ u>插入($ data)->救 ();

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

\ lt; ? php

//\\ ub26; 项目根目录/db/种子/UserSeeder.php

使用 Phinx \\\\ 种子 \\\\ AbstractSeed ;

class UserSeeder 扩展 AbstractSeed

{

\\ ubsp; 公共 函数 run ( )

{

\ nbsp ; \\b $ 数据 = 数组 (

array (

\ ” id” ” id” = \ gt; 1 ,

) ,

数组 (

” id” = \\ > 2 ,

\ nbsp ; )

) ;

$ 帖子 = $ 此 -\ gt; 表 ( “用户” ) ;

$ 帖子 -\ gt; 插入 ( $ 数据 ) -\ gt; 保存 ( ) ;

}

}

编写代码后,执行以下命令

php vendor/bin/phinx seed:运行-e产品

1

php 供应商 / bin / phinx 种子 : 运行 – e 产品

有关更多使用方法,请参阅官方文档

最后再说两个字

正如我先前在功能中提到的那样,phinx特别适合于开发,测试,在线数据库同步字段信息,数据信息,测试数据的生成和同步等,因此特别适合用于团队开发过程,特别是对于新项目,只要该项目一直坚持使用phinx进行独立部署,那么每次更改数据库表时,信息团队成员就可以通过git或svn同步代码,然后执行上述执行命令来同步库表信息。这样,避免了传统开发过程中在不同开发环境中同步库信息的繁琐和错误。在phinx.php中,有一个配置项” default_migration_table” =>” phinxlog”记录了更改,这也是确保不会重复的措施,因此无需担心丢失或重复执行。命令。玩狮身人面像有很多不同的方式,所以这只是一个窍门。欢迎拍砖。

参考

github:https://github.com/robmorgan/phinx

官方文档:http://docs.phinx.org/en/latest

常用命令:http://docs.phinx.org/en/latest/commands.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值