天天看點

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

在産品疊代開發釋出過程中,由于業務需求的增加,資料庫難免會有結構調整等操作.

在每個版本釋出過程中怎麼控制每個版本server端程式與資料庫版本保持一緻,以及數

據庫更新、復原等操作.

由于具體需求不同,宅鳥根據自己公司的情況将mysql-php-migrations做了一些修改來滿應用!

宅鳥修改改程式後的mysql遷移程式有以下目錄:

<a target="_blank" href="http://blog.51cto.com/attachment/201312/144201680.jpg"></a>

config 配置檔案

dbscript sql腳本目錄

lib 遷移程式類庫

migrate.php 遷移指令執行入口

執行php migrate.php

可以看到如下結果

<a target="_blank" href="http://blog.51cto.com/attachment/201312/144641751.jpg"></a>

我們可以看到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檔案,改檔案格式如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

<code>&lt;?php</code>

<code>class</code> <code>Migration_2013_12_18_14_50_45 </code><code>extends</code> <code>MpmMysqliMigration</code>

<code>{</code>

<code>        </code><code>public</code> <code>function</code> <code>up(ExceptionalMysqli &amp;</code><code>$mysqli</code><code>)</code>

<code>        </code><code>{</code>

<code>                </code><code>$mysqli</code><code>-&gt;</code><code>exec</code><code>(</code><code>"DO 0"</code><code>);</code>

<code>        </code><code>}</code>

<code>        </code><code>public</code> <code>function</code> <code>down(ExceptionalMysqli &amp;</code><code>$mysqli</code><code>)</code>

<code>}</code>

<code>?&gt;</code>

把需要修改的資料庫腳本寫在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

<code>$db_config</code> <code>= (object) </code><code>array</code><code>();</code>

<code>$db_config</code><code>-&gt;host = </code><code>'127.0.0.1'</code><code>;</code>

<code>$db_config</code><code>-&gt;port = </code><code>'3306'</code><code>;</code>

<code>$db_config</code><code>-&gt;user = </code><code>'dbuser'</code><code>;</code>

<code>$db_config</code><code>-&gt;pass = </code><code>'dbpasswd'</code><code>;</code>

<code>$db_config</code><code>-&gt;name = </code><code>'dbname'</code><code>;</code>

<code>$db_config</code><code>-&gt;db_path = </code><code>'var/www/mysqlMigrations/dbscript/'</code><code>;</code>

<code>$db_config</code><code>-&gt;method = 2;   </code><code>//1 pdo,2 mysqli</code>

了解該程式基本結構後,我們來開始使用一下它:

cd mysqlMigrations

php migrate.php add test2  

在dbscript下生成 2013_12_18_15_06_14_test2.php

執行指令:php migrate.php list

可以看到版本結果:

#         Timestamp

     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

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

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

執行指令:php migrate.php down 1380427212        可以把資料版本從 1387349445 復原到 1380427212

執行php migrate.php list檢視資料庫版本復原結果

 #         Timestamp

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

如果要遷移到資料庫最大版本可以執行一下指令:

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

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

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

執行復原:

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

通過執行一下操作,檢視資料庫中資料變化

本文轉自birdinroom 51CTO部落格,原文連結:http://blog.51cto.com/birdinroom/1342147,如需轉載請自行聯系原作者