dev-master
9999999-dev https://github.com/akrabat/AkrabatAkrabat's Zend Tool Provider for DB Migration
The Requires
- php >=5.2.7
by Rob Allen
zf1 tooling db migration
Akrabat's Zend Tool Provider for DB Migration
See Akrabat_Db_Schema_Manager: Zend Framework database migrations for full details, (*1)
To get the Zend_Tool provider working:, (*2)
Create a composer.json with the following, (*3)
{ "require": { "akrabat/zf1-db-migrate": "dev-master", "breerly/zf1": "1.11.11" }, "config": { "bin-dir": "bin/" } }
You need to have composer installed http://getcomposer.org, and run, (*4)
php composer.phar install
Update your ~/.bash_profile
to set up an alias to the zf.php script, (*5)
alias zf='export ZF_CONFIG_FILE=/path/to/my/project/bin/.zf.ini; /path/to/my/project/bin/zf.php'
Restart your terminal or source ~/.bash_profile
, (*6)
If you haven't already done so, setup the storage directory and config file:, (*7)
zf --setup storage-directory zf --setup config-file
Edit the created .zf.ini
. Change path so that it includes ZF1 and Akrabat/zf1, allow for auotoloading Akrabat and set up the provider:, (*8)
php.include_path = "/path/to/zf1/library:/path/to/Akrabat/zf1/" autoloadernamespaces.0 = "Akrabat_" basicloader.classes.0 = "Akrabat_Tool_DatabaseSchemaProvider"
zf
should provide a help screen with the DatabaseSchema
provider at the bottom., (*9)
Create migration files within migrations with the file name format of nnn-Xxxx.php. e.g. 001-Users.php
where:
nnn => any number. The lower numbered files are executed first
Xxx => any name. This is the class name within the file., (*10)
Create a class in your migrations file. Example for 001-Users.php:, (*11)
class Users extends Akrabat_Db_Schema_AbstractChange { function up() { $tableName = $this->_tablePrefix . 'users'; $sql = " CREATE TABLE IF NOT EXISTS $tableName ( id int(11) NOT NULL AUTO_INCREMENT, username varchar(50) NOT NULL, password varchar(75) NOT NULL, roles varchar(200) NOT NULL DEFAULT 'user', PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; $this->_db->query($sql); $data = array(); $data['username'] = 'admin'; $data['password'] = sha1('password'); $data['roles'] = 'user,admin'; $this->_db->insert($tableName, $data); } function down() { $tableName = $this->_tablePrefix . 'users'; $sql= "DROP TABLE IF EXISTS $tableName"; $this->_db->query($sql); } }
If you want a table prefix, add this to your application.ini
:, (*12)
resources.db.table_prefix = "prefix"
Create migration files within migrations with the file name format of nnn-Xxxx.php. e.g. 001-Users.php
where:
nnn => any number. The lower numbered files are executed first
Xxx => any name. This is the class name within the file., (*13)
Create a class in your migrations file. Example for 001-Users.php:, (*14)
class Users extends \Akrabat\Db\Schema\AbstractChange { function up() { $tableName = $this->_tablePrefix . 'users'; $sql = " CREATE TABLE IF NOT EXISTS $tableName ( id int(11) NOT NULL AUTO_INCREMENT, username varchar(50) NOT NULL, password varchar(75) NOT NULL, roles varchar(200) NOT NULL DEFAULT 'user', PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; $this->_db->query($sql); $data = array(); $data['username'] = 'admin'; $data['password'] = sha1('password'); $data['roles'] = 'user,admin'; $this->_db->insert($tableName, $data); } function down() { $tableName = $this->_tablePrefix . 'users'; $sql= "DROP TABLE IF EXISTS $tableName"; $this->_db->query($sql); } }
If you want a table prefix, add this to your application.ini
:, (*15)
resources.db.table_prefix = "prefix"
Define the phing task in the build.xml
, (*16)
<taskdef name="dbmigration" classname="phing.tasks.PhingAkrabatDbSchemaManager" />
Setup a phing target with the database options in the build.xml
, (*17)
<target name="database-migration"> <dbmigration adapter="mysqli" host="${db.host}" dbname="${db.name}" username="${db.user}" password="${db.pass}" /> </target>
Run phing using the command line, by phing database-migration
, (*18)
Akrabat's Zend Tool Provider for DB Migration
zf1 tooling db migration