In the process of working on one huge project on the Zend Framework, it became necessary to migrate databases and move between versions, i.e. besides update, the so-called downdate was needed. A little googling came across an interesting article by Rob Alan (hereinafter the Author) "Akrabat_Db_Schema_Manager: Zend Framework database migrations". This article is not a translation of the original, but rather a synthesis of it and the problem. About this conversation will go.
After each change in the database (adding columns, tables, indexes, keys, etc.), you need to create a separate migration file. Such files can be placed in the ./scripts/migrations directory. The author proposes to name the migration file as 001-CreateUsersTable.php. The number determines the order in which these files will be launched, i.e. in fact goes
[ordinal number] - [brief description of migration] .php . But this is not so much information. Therefore, we decided to refer to migration as
[revision number] - [brief description of migration] .php . For those who use hg can use the sequence number changeset. In this regard, it was necessary to rewrite the class Akrabat_Db_Schema_Manager, because several migrations could be included in one revision and the file names were redefined. The _getMigrationFiles () method does not return an array of the form:
$files["v$versionNumber"] = array( 'path'=>$path, 'filename'=>$entry, 'version'=>$versionNumber, 'classname'=>$className); : $files["v$versionNumber"][] = array( 'path'=>$path, 'filename'=>$entry, 'version'=>$versionNumber, 'classname'=>$className);
Accordingly, the changes affected the _processFile ($ migration, $ direction) method, to which the $ migration parameter was transferred, not as an array, but as an array of arrays. To whom it is interesting, the source codes of the classes will be laid out on
GitHub , but for now I’ll tell you about the main 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 starts the process of updating the database, and down () in the case of a rollback of the audit. Here is a slightly modified example given by the Author:
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, support for table prefix is ​​introduced, which is indicated in the configuration file, in the
resources.db.table_prefix key. But again there is 1 minus, the class name is the same as
[short description of the migration] , and if we have at least 2 identical descriptions, the Fatal error: Cannot redeclare class will fly out. To do this, we define the array index 'classname' as
'classname' => $className.$versionNumber
and the class is similarly called - this will allow us to avoid mistakes.
And now - the most delicious. We fasten all this beauty to zend Tool. I will not tell you how to install the Zend Tool, they have already written a lot about it. First of all, let's decide where to store the Akrabat ZF Library, I prefer to store all PHP libraries in / usr / share / php /, which means that the path to it will be / usr / share / php / Akrabat. Then everything is simple:
$ 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 make sure that everything works:
$ zf ? database-schema
Must see something similar:
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] zf get-table-prefix database-schema
Sources
- Article Author
- Akrabat ZF Library on GitHub.com