Database migration to Zend Framework: Akrabat_Db_Schema_Manager

In the process of working on a big project in Zend Framework, the need arose to migrate the databases and move between versions, i.e. except for update, was necessary for the so-called downdate. A little googling came across an interesting article by Rob Alan (further the Author) "Akrabat_Db_Schema_Manager: Zend Framework database migrations". This article is not a translation of the original, but rather a synthesis of his and any problems encountered. This will be the discussion.

After each change in the database (add columns, tables, indexes, keys, etc.), you need to create a separate migration file. Similar files can be placed in the directory ./scripts/migrations. The author proposes to name the migration file as 001-CreateUsersTable.php. The number determines the order in which the files will run, i.e. in fact, there is [sequence number]-[brief description of the migration].php. But this is not so much informative. So we decided to name migration as [- audit]-[brief description of the migration].php. For those who use hg can use the sequence number of the changeset. In this regard, we had to rewrite the class Akrabat_Db_Schema_Manager, because in one revision might include multiple migrations and the file names are overridden by. Method _getMigrationFiles() returns not an array:
the
$files["v$versionNumber"] = array(
'path'=>$path,
'filename'= > $entry,
'version'=>$versionNumber,
'classname'=>$className);
But an array of arrays:
$files["v$versionNumber"][] = array(
'path'=>$path,
'filename'= > $entry,
'version'=>$versionNumber,
'classname'=>$className);

Accordingly, changes were made to the method _processFile($migration, $direction), which was passed the parameter $migration not as an array, but an array of arrays. Anyone interested, source code of classes will put on GitHub, while I tell you about the basic algorithm of modernization.

The migration file contains a class inherited from Akrabat_Db_Schema_AbstractChange and contains two methods: up() and down(). The up() method runs the update process of the database, and down() — in case of a rollback the audit. Here's a slightly modified example, which leads the Author:
the
class CreateUsersTable 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)
)";
$this->_db->query($sql);
$data = array();
$data['username'] = 'admin';
$data['password'] = sha1('password');
$data['roles'] = 'user,admin';
$this->_db->insert('users', $data);
}

function down()
{
$tableName = $this- > _tablePrefix . 'users';
$sql = "DROP TABLE IF EXISTS $tableName";
$this->_db->query($sql);
}
}

In the example introduced support for table prefix, which is specified in the configuration file, the key resources.db.table_prefix. But again, there is 1 minus, the class name matches [brief description of the migration], and if we have at least 2 of the same description, it will fly Fatal error: Cannot redeclare class. We define the array index 'classname'
the
'classname' => $className.$versionNumber

and similarly referred to as the class — this will allow us to avoid mistakes.
And now — the most tasty. Fasten this beauty to zend Tool. How to install Zend Tool, I will not tell, about it already wrote a lot. To begin, decide where to store Akrabat ZF Library, I prefer to have all the PHP libraries are stored in /usr/share/php/, this means that the path will be /usr/share/php/Akrabat. Then everything is simple:
the
$ zf --setup storage-directory
$ zf --setup config-file
$ echo "`php -r 'echo get_include_path().PATH_SEPARATOR;"/usr/share/php/Akrabat">~/.zf.ini
$ echo ‘basicloader.classes.0 = "Akrabat_Tool_DatabaseSchemaProvider"’>>~/.zf.ini

Now verify that everything works:
the
$ zf ? database-schema

Should see something like:
the
Zend Framework  Command  Line Console Tool v1.11.11
Actions  supported  by provider "DatabaseSchema"
DatabaseSchema
zf update database-schema env[=development] dir[=./scripts/migrations]
zf update-to database-schema version env[=development] dir[=./scripts/migrations]
zf decrement database schema versions[=1] env[=development] dir[=./scripts/migrations]
zf increment database schema versions[=1] env[=development] dir[=./scripts/migrations]
zf current database-schema env[=development] dir[=./migrations]


Sources

    the
  1. Article Author
  2. the
  3. Akrabat ZF Library on GitHub.com
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

Car navigation in detail

PostgreSQL: Analytics for DBA

Google has launched an online training course advanced search