Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@samsonasik
Last active September 7, 2016 13:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsonasik/6272d635836832809321e8f103c3a2ed to your computer and use it in GitHub Desktop.
Save samsonasik/6272d635836832809321e8f103c3a2ed to your computer and use it in GitHub Desktop.
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'  
@SenseException
Copy link

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 in Doctrine\DBAL\Migrations\Configuration\Configuration::registerMigration and execute will be invoked in Doctrine\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.

@SenseException
Copy link

The class Doctrine\DBAL\Migrations\Configuration\Configuration itself is instantiated in the Doctrine\DBAL\Migrations\Tools\Console\Helper\ConfigurationHelper, that is created in Doctrine\DBAL\Migrations\Tools\Console\Command\AbstractCommand::getMigrationConfiguration. There seems to be a way to use a custom helper, that can create custom classes for Configuration and then Version, but that's a far fetched solution.

If you only need it temporary to fix the schemas to make Migrations in sync with the DB state again, this may not be a the solution of your choice. Ignoring Exceptions shouldn't be permanently implemented.

@samsonasik
Copy link
Author

@SenseException Thank you for the suggestion ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment