1
0
mirror of synced 2024-12-15 23:56:02 +03:00
doctrine2/manual/new/docs/en/migration.txt
2007-09-20 20:24:38 +00:00

107 lines
3.2 KiB
Plaintext

The Doctrine Migration tools allow you to migrate databases and it issues alter table statements directly to your databases when you need to deploy database changes.
++ Writing Migration Classes
Migration classes consist of a simple class that extends from Doctrine_Migration. You can define a public up() and down() method that is meant for doing and undoing changes to a database for that migration step. The class name is completely arbitrary, but the name of the file which contains the class must have a prefix containing the number it represents in the migration process. Example: XXX_representative_name.class.php
<code type="php">
// 001_add_table.class.php
class AddTable extends Doctrine_Migration
{
public function up()
{
$this->createTable('migration_test', array('field1' => array('type' => 'string')));
}
public function down()
{
$this->dropTable('migration_test');
}
}
// 002_add_column.class.php
class AddColumn extends Doctrine_Migration
{
public function up()
{
$this->addColumn('migration_test', 'field1', 'string');
}
public function down()
{
$this->renameColumn('migration_test', 'field1', 'field2');
}
}
// 003_change_column.class.php
class ChangeColumn extends Doctrine_Migration
{
public function up()
{
$this->changeColumn('migration_test', 'field1', 'integer');
}
public function down()
{
$this->changeColumn('migration_test', 'field1', 'string');
}
}
</code>
+++ Methods
Here is a list of the available methods you can use to alter your database in your migration classes
<code type="php">
public function createTable($tableName, array $fields = array(), array $options = array())
public function dropTable($tableName)
public function renameTable($oldTableName, $newTableName)
public function addColumn($tableName, $columnName, $type, array $options = array())
public function renameColumn($tableName, $oldColumnName, $newColumnName)
public function changeColumn($tableName, $columnName, $type, array $options = array())
public function removeColumn($tableName, $columnName)
public function addIndex($tableName, $indexName, array $options = array())
public function removeIndex($tableName, $indexName)
</code>
+++ Altering Data
You can alter table directly in your up() and down() methods like you normally would by creating new model instances and calling save() or creating queries and deleting data.
<code type="php">
// XXX_add_user.class.php
class AddUser extends Doctrine_Migration
{
public function up()
{
// Add new user
$user = new User();
$user->loginname = 'jwage';
$user->save();
}
public function down()
{
// Delete the user we added in the up
$query = new Doctrine_Query();
$query->delete('User')->from('User u')->where('u.loginname = ?', 'jwage')->execute();
}
}
</code>
++ Performing Migrations
<code type="php">
$migration = new Doctrine_Migration('migration_classes');
$migration->migrate(0, 1);
$migration->migrate(1, 2);
$migration->migrate(2, 3);
// Then revert back to version 1
$migration->migrate(3, 2);
$migration->migrate(2, 1);
$migration->migrate(1, 0);
echo $migration->getCurrentVersion(); // should echo 0
</code>