namespace Application\Migration;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Auto-generated Migration: Please modify to your needs!
*/
class Version20160704163301 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
$sql = <<<sql
ALTER TABLE `FOO`
ADD COLUMN `bar` TINYINT NOT NULL DEFAULT 1 AFTER `baz`;
sql;
try {
$this->addSql($sql);
} catch (\Doctrine\DBAL\Exception\NonUniqueFieldNameException $e) {
} catch (\Doctrine\DBAL\Driver\PDOException $e) {
} catch (\PDOException $e) {
}
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
}
}
How to make it silently executed if column already exists ? catch above still got error:
[Doctrine\DBAL\Exception\NonUniqueFieldNameException]
An exception occurred while executing ' ALTER TABLE `FOO`
ADD COLUMN `bar` TINYINT NOT NULL DEFAULT 1 AFTER `baz`;
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'bar'
[Doctrine\DBAL\Driver\PDOException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'bar'
[PDOException]
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'bar'
I took a short look and reproduced it.
Doctrine\DBAL\Migrations\Version::execute
does handle the Exceptions and it doesn't look like those can be caught in your migration file. The Version class will be instantiated inDoctrine\DBAL\Migrations\Configuration\Configuration::registerMigration
and execute will be invoked inDoctrine\DBAL\Migrations\Migration::migrate
.I'm sorry I can't be any help for you. During the time I looked at this, I saw no possibility to catch that. Maybe I'll be able to take a closer look later or at the weekend.