Yii 1.1: dbupgrader

Yii DbUpgrader Component can be used to automate upgrading a database to a new version(s).
6 followers

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) called dbversion.
  • 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);

GitHub

GitHub Repo

Total 4 comments

#12559 report it
mrkmg at 2013/03/28 02:58pm
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

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.

#12544 report it
kernel32ddl at 2013/03/28 01:18am
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?

#12537 report it
mrkmg at 2013/03/27 01:16pm
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:

  1. No yiic, can be called from a controller if wanted.
  2. Do not need to "know" the version number of upgrade, can be autodetected
  3. Supports raw .sql files (which allows for a simple way to implement very complicated upgrades)
  4. Requires no controllers to be made to run the upgrade, just a numbered SQL file.
  5. Can be extended for special use cases. (For instance I actually extended it to send out email reports on upgrades, as well as do checks afterwards on data integrity across our master/slave arrays)

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.

#12536 report it
Borales at 2013/03/27 12:23pm
What's the point?

Did you read thе guide? database.migration

What's the difference?

Leave a comment

Please to leave your comment.

Create extension