mysql实例自动化迁移,如何自动化PHP / MySQL应用程序的迁移(架构和数据)

I have an application in PHP/MySQL. I am searching for an automated way upgrading database behind the application.

I don't need to have the compatibility with older versions once it is upgraded.

I have read jeff's and K. Scott Allen's articles on this.

I am still not sure how to implement this for a PHP/MySQL application.

Is there any simple and good process for this?

解决方案

I have a "Schema" object that I use - but you could do the same without classes..

What you want to do is create a 'db_schema_versions' table:

CREATE TABLE db_schema_versions (

`table` varchar(255) NOT NULL PRIMARY KEY,

`version` INT NOT NULL

)

After your database can track what version # it is on - it can do SQL upgrades automatically.

You should lock your schema table while upgrading schema. This way you wont have two requests at the same moment trying to upgrade your schema.

So - keep track of the version you are upgrading from - build a big switch - something like this:

class SNTrack_Db_Schema extends MW_Db_Schema_Abstract {

protected $table = "sntrack_db_schema";

protected $version = 5;

protected function upgrade($fromVersion) {

// don't break

switch($fromVersion) {

case 0:

$this->db->query('CREATE TABLE sntrack_inbound_shipment (

`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,

`from` VARCHAR(255) NOT NULL,

`date` DATE NOT NULL,

`invoice` VARCHAR(255) NOT NULL,

`notes` TEXT

)');

$this->setVersion(1);

case 1:

$this->db->query('ALTER TABLE sntrack_details ADD `shipment_id` INT');

$this->db->query('ALTER TABLE sntrack_product ADD `inventory` INT NOT NULL DEFAULT 0');

$this->db->query('CREATE TABLE sntrack_inventory_shipment (

`shipment_id` INT NOT NULL,

`product_id` INT NOT NULL,

`qty` INT NOT NULL,

PRIMARY KEY (`shipment_id`, `product_id`)

)');

$this->setVersion(2);

...etc

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值