1
0
mirror of synced 2024-12-15 15:46:02 +03:00
doctrine2/manual/docs/en/migration.txt

103 lines
3.2 KiB
Plaintext
Raw Normal View History

2007-09-20 20:01:33 +04:00
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
2007-09-21 00:24:38 +04:00
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
2007-09-20 20:01:33 +04:00
<code type="php">
2007-09-21 00:24:38 +04:00
// 001_add_table.class.php
class AddTable extends Doctrine_Migration
2007-09-20 20:01:33 +04:00
{
public function up()
{
2007-09-20 20:11:59 +04:00
$this->createTable('migration_test', array('field1' => array('type' => 'string')));
2007-09-20 20:01:33 +04:00
}
public function down()
{
2007-09-20 20:11:59 +04:00
$this->dropTable('migration_test');
2007-09-20 20:01:33 +04:00
}
}
2007-09-21 00:24:38 +04:00
// 002_add_column.class.php
class AddColumn extends Doctrine_Migration
2007-09-20 20:01:33 +04:00
{
public function up()
{
2007-09-20 20:11:59 +04:00
$this->addColumn('migration_test', 'field1', 'string');
2007-09-20 20:01:33 +04:00
}
public function down()
{
2007-09-20 20:11:59 +04:00
$this->renameColumn('migration_test', 'field1', 'field2');
2007-09-20 20:01:33 +04:00
}
}
2007-09-21 00:24:38 +04:00
// 003_change_column.class.php
class ChangeColumn extends Doctrine_Migration
2007-09-20 20:01:33 +04:00
{
public function up()
{
2007-09-20 20:11:59 +04:00
$this->changeColumn('migration_test', 'field1', 'integer');
2007-09-20 20:01:33 +04:00
}
public function down()
{
2007-09-20 20:11:59 +04:00
$this->changeColumn('migration_test', 'field1', 'string');
2007-09-20 20:01:33 +04:00
}
}
</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">
2007-09-21 00:24:38 +04:00
// XXX_add_user.class.php
class AddUser extends Doctrine_Migration
2007-09-20 20:01:33 +04:00
{
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');
2007-09-20 20:01:33 +04:00
// Assume current version is 0
$migration->migrate(3); // takes you from 0 to 3
$migration->migrate(0); // takes you from 3 to 0
2007-09-20 20:01:33 +04:00
echo $migration->getCurrentVersion(); // 0
2007-09-20 20:01:33 +04:00
</code>