Mysql遷移工具在生產環境下的使用

在產品迭代開發發佈過程當中,因爲業務需求的增長,數據庫不免會有結構調整等操做.php

在每一個版本發佈過程當中怎麼控制每一個版本server端程序與數據庫版本保持一致,以及數mysql

據庫升級、回滾等操做.git

本博文宅鳥將向你們推薦一款mysql數據庫遷移工具mysql-php-migrationsgithub

因爲具體需求不一樣,宅鳥根據本身公司的狀況將mysql-php-migrations作了一些修改來滿應用!sql

 

宅鳥修改改程序後的mysql遷移程序有如下目錄:數據庫

config 配置文件函數

dbscript sql腳本目錄工具

lib 遷移程序類庫spa

migrate.php 遷移命令執行入口orm

 

 

執行php migrate.php

能夠看到以下結果

咱們能夠看到migrate.php有不少命令

php migrate.php add  test

結果:

      __ __         __      __

|\/|  (_ /  \|   __ |__)|__||__) __ |\/|. _  _ _ |_. _  _  _

|  |\/__)\_\/|__    |   |  ||       |  ||(_)| (_||_|(_)| )_)

   /                                    _/

******************************************************************** v2.0.1 ***

 

New migration created: file

/var/www/mysqlMigrations/dbscript/2013_12_18_14_50_45_test.php

 

*******************************************************************************

cd dbscript

total 16

-rw-r--r-- 1 www-data www-data 4837 Sep 29 09:21 2013_06_18_17_14_16_v1.php

-rw-r--r-- 1 www-data www-data  802 Sep 29 13:29 2013_09_29_12_00_12_v1.php

-rw-r--r-- 1 root     www-data  240 Dec 18 14:50 2013_12_18_14_50_45_test.php

此時dbscript目錄已經新添加一個2013_12_18_14_50_45_test.php文件,改文件格式以下:

<?php

class Migration_2013_12_18_14_50_45 extends MpmMysqliMigration

{

       public function up(ExceptionalMysqli &$mysqli)

       {

               $mysqli->exec("DO 0");

       }

       public function down(ExceptionalMysqli &$mysqli)

       {

               $mysqli->exec("DO 0");

       }

 

}

?>

 

把須要修改的數據庫腳本寫在up函數中:

把對應修改修改所作的回滾操做卸載down函數中

 

注意:在生產環境下建議只作數據庫的向上變遷,不作down操做,避免用戶有用數據丟失.

 

執行php migrate.php list 返回結果:

WARNING: Migration numbers may not be in order due to interleaving.

 

     #         Timestamp

   ========================================================================

     version           createtime              active  current note

     1371546856        2013-06-18 17:14:16     1       0       v1

     1380427212        2013-09-29 12:00:12     1       1       v1

     1387349445        2013-12-18 14:50:45     0       0       test

   Page 1 of 1, 3 migrations in all.

 

 

cd config 目錄

cat db_config.php

<?php

$db_config = (object) array();

$db_config->host = '127.0.0.1';

$db_config->port = '3306';

$db_config->user = 'dbuser';

$db_config->pass = 'dbpasswd';

$db_config->name = 'dbname';

$db_config->db_path = 'var/www/mysqlMigrations/dbscript/';

$db_config->method = 2;   //1 pdo,2 mysqli

?>

 

瞭解該程序基本結構後,咱們來開始使用一下它:

cd mysqlMigrations

php migrate.php add test2  

在dbscript下生成 2013_12_18_15_06_14_test2.php

 

執行命令:php migrate.php list

能夠看到版本結果:

#         Timestamp

   ========================================================================

     version           createtime              active  current note

     1371546856        2013-06-18 17:14:16     1       0       v1

     1380427212        2013-09-29 12:00:12     1       1       v1

     1387349445        2013-12-18 14:50:45     0       0       test

     1387350374        2013-12-18 15:06:14     0       0       test2

 

   Page 1 of 1, 4 migrations in all.

 

說明:

   version 每次遷移的版本號

   createtime 建立時間

   active  是否已經激活生效

   current 數據庫當前所在版本標誌

   note  遷移的註釋

 

執行命令:php migrate.php up 1387349445   能夠把數據版本從 1380427212 遷移到 1387349445

#         Timestamp

   ========================================================================

     version           createtime              active  current note

     1371546856        2013-06-18 17:14:16     1       0       v1

     1380427212        2013-09-29 12:00:12     1       0       v1

1387349445        2013-12-18 14:50:45     1       1       test

     1387350374        2013-12-18 15:06:14     0       0       test2

 

   Page 1 of 1, 4 migrations in all.

 

 

執行命令:php migrate.php down 1380427212        能夠把數據版本從 1387349445 回滾到 1380427212

執行php migrate.php list查看數據庫版本回滾結果

 #         Timestamp

   ========================================================================

     version           createtime              active  current note

     1371546856        2013-06-18 17:14:16     1       0       v1

    1380427212        2013-09-29 12:00:12     1       1       v1

     1387349445        2013-12-18 14:50:45     0       0       test

     1387350374        2013-12-18 15:06:14     0       0       test2

 

   Page 1 of 1, 4 migrations in all.

 

若是要遷移到數據庫最大版本能夠執行一下命令:

php migrate.php up max_version

返回結果:

Migrating to 2013-12-18 15:06:14 (ID 1387350374)...

       Performing UP migration 2013-12-18 14:50:45 (ID 1387349445)... done.

       Performing UP migration 2013-12-18 15:06:14 (ID 1387350374)... done.

 

*******************************************************************************

查看php migrate.php list

#         Timestamp

   ========================================================================

     version           createtime              active  current note

     1371546856        2013-06-18 17:14:16     1       0       v1

     1380427212        2013-09-29 12:00:12     1       0       v1

     1387349445        2013-12-18 14:50:45     1       0       test

1387350374        2013-12-18 15:06:14     1       1       test2

 

   Page 1 of 1, 4 migrations in all.

 

執行回滾:

php migrate.php down 1380427212        

返回一下結果:

Migrating to 2013-09-29 12:00:12 (ID 1380427212)...

       Performing DOWN migration 2013-12-18 15:06:14 (ID 1387350374)... done.

       Performing DOWN migration 2013-12-18 14:50:45 (ID 1387349445)... done.

 

  #         Timestamp

   ========================================================================

     version           createtime              active  current note

     1371546856        2013-06-18 17:14:16     1       0       v1

     1380427212        2013-09-29 12:00:12     1       1       v1

     1387349445        2013-12-18 14:50:45     0       0       test

     1387350374        2013-12-18 15:06:14     0       0       test2

 

   Page 1 of 1, 4 migrations in all.

 

經過執行一下操做,查看數據庫中數據變化

相關文章
相關標籤/搜索