|
Product release in iterative development process, due to the increase in business demand, inevitably there will be a database of structural adjustment and other operations.
How each version of each controlled release process server-side programs and consistent version of the database, and the database upgrade, rollback and other operations.
The Bowen House birds will recommend you a mysql database migration tool mysql-php-migrations
Depending on the specific needs, according to their own house Bird's situation will mysql-php-migrations made some changes to full application!
Bird House to modify the program after mysql migration program has the following directories:
config configuration file
dbscript sql scripts directory
lib migration program library
migrate.php migration command entry
Execute php migrate.php
We can see the following results
We can see that there are many commands migrate.php
php migrate.php add test
result:
__ __ __ __
| / | (_ / | __ | __) | __ || __) __ | / |. _ _ _ |. _ _ _ _
| | / __) _ / | __ | | || | || (_) | (_ || _ | (_) |) _)
/ _ /
************************************************** ****************** 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
At this point add a new directory already dbscript 2013_12_18_14_50_45_test.php file, change the file format is as follows:
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");
}
}
?>
The need to modify the database script written up function:
The changes made to modify the corresponding rollback uninstallation down function
Note: It is recommended only up Change database in a production environment, not down operations, to avoid loss of data useful to the user.
Execute php migrate.php list returns the result:
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 directory
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
?>
Once you understand the basic structure of the program, we started to look at it:
cd mysqlMigrations
php migrate.php add test2
2013_12_18_15_06_14_test2.php generated at dbscript
Run: php migrate.php list
You can see the version Results:
# 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.
Explanation:
version version number of each migration
createtime Created
Whether active has been activated to take effect
The current version of the database where the current flag
Note Migration note
Run: php migrate.php up 1387349445 1380427212 from the version of the data can be migrated to 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.
Run: php migrate.php down 1380427212 1387349445 from the version of the data can be rolled back to 1380427212
Execute php migrate.php list to view the database version rollback results
# 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.
If you want to migrate to the largest database version can execute the following command:
php migrate.php up max_version
Return:
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.
************************************************** *****************************
See 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.
Rollback:
php migrate.php down 1380427212
Back at the results:
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.
By performing the operation about to view the data in the database changes |
|
|
|