yiiDbUpgrader ¶
Yii DbUpgrader Component can be used to automate upgrading a database to a new version(s).
Installation ¶
Copy DbUpgrader.php
to protected/components/
Usage ¶
- Create a folder in your Yii data folder (
protected/data
) calleddbversion
. - Each time you make changes to the database, place the sql code for those changes into numbered sql files in that directory. The follow are both valid:
1.sql
2.sql
3.sql
4.sql
001.sql
002.sql
003.sql
004.sql
- Somewhere in your code, you can run the following to actually perform the check and upgrade.
$dbu = new DbUpgrader;
$dbu->runUpgrade();
- If you want a verbose output (useful for cli access and logging)
$dbu = new DbUpgrader;
$dbu->runUpgrade(true);
What's the point?
Did you read thе guide? database.migration
What's the difference?
Re: What's the point?
I wanted something that was extendable, yet simple. I found the built in system quite cumbersome, and way more powerful than I needed. Also I have this automated across many instances.
The main differences:
I guess if you like the built in migration stuff, use it. If you want something simpler, try this or one of the many other database versioning and migration tools out there.
Example of *.sql
Could you please show example of 1-N.sql files.
If I insert CREATE TABLE command can extension analyze it and create ALTER commands for changed fields only?
RE: Example of *.sql
The extension does no analysis of the sql or the current database, it meerly just executes the SQL files. Below is an example of an *.sql file
[sql] DROP TABLE IF EXISTS `department`; CREATE TABLE IF NOT EXISTS `department` ( `id` int(4) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(512) NOT NULL, `active` tinyint(1) NOT NULL DEFAULT '1', PRIMARY KEY (`id`), KEY `name` (`name`(255)), KEY `active` (`active`) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; INSERT INTO `department` (`id`, `name`, `active`) VALUES ('1', 'Administrative', '1'); ALTER TABLE `task` ADD `department` int(4) unsigned NULL;
The following creates a department table and modifies the tasks table to support having a department attached to it, as well as creates an initial department.
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.