1
0
mirror of synced 2024-12-13 22:56:04 +03:00
doctrine2/manual/docs/en/migration.txt
2007-10-17 21:16:49 +00:00

103 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('/path/to/migration_classes');
// Assume current version is 0
$migration->migrate(3); // takes you from 0 to 3
$migration->migrate(0); // takes you from 3 to 0
echo $migration->getCurrentVersion(); // 0
</code>