You are browsing a version that is no longer maintained. |
Migration Classes
Migration classes must extend Doctrine\Migrations\AbstractMigration
and at a minimum they must implement the up
and down
methods. You can easily generate a blank migration to modify with the following command:
$ ./vendor/bin/doctrine-migrations generate
Generated new migration class to "/data/doctrine/migrations-docs-example/lib/MyProject/Migrations/Version20180601193057.php"
To run just this migration for testing purposes, you can use migrations:execute --up 'MyProject\Migrations\Version20180601193057'
To revert the migration you can use migrations:execute --down 'MyProject\Migrations\Version20180601193057'
The above command will generate a PHP class with the path to it visible like above. Here is what the blank migration looks like:
1 <?php
declare(strict_types=1);
namespace MyProject\Migrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20180601193057 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Methods to Implement
The AbstractMigration
class provides a few methods you can override to define additional behavior for the migration.
isTransactional
Override this method if you want to disable transactions in a migration. It defaults to true.
Some database platforms like MySQL or Oracle do not support DDL
statements in transactions and may or may not implicitly commit the
transaction opened by this library as soon as they encounter such a
statement, and before running it. Make sure to read the manual of
your database platform to know what is actually happening.
|
getDescription
Override this method if you want to provide a description for your migration. The value returned here
will get outputted when you run the ./vendor/bin/doctrine-migrations status --show-versions
command.
Methods to Call
The AbstractMigration
class provides a few methods you can call in your migrations to perform various functions.
addSql
You can use the addSql
method within the up
and down
methods. Internally the addSql
calls are passed
to the executeQuery method in the DBAL. This means that you can use the power of prepared statements easily and that
you don't need to copy paste the same query with different parameters. You can just pass those different parameters
to the addSql method as parameters.
throwIrreversibleMigrationException
If a migration cannot be reversed, you can use this exception in the down
method to indicate such.
The throwIrreversibleMigrationException
method accepts an optional message to output.