How to avoid DROP DEFAULT reports using Doctrine 2 Migrations diff on first run?

I had an existing PostgreSQL database with a table created as follows:

CREATE TABLE product (id SERIAL PRIMARY KEY, name VARCHAR(100) DEFAULT NULL)

This table is described in the YML Doctrine2 file in the Symfony2 project:

Acme\DemoBundle\Entity\Product:
    type: entity
    table: product
    fields:
        id:
            id: true
            type: integer
            nullable: false
            generator:
                strategy: SEQUENCE
        name:
            type: string
            length: 100
            nullable: true

When I start the task "Accounting for Migration Migration" for the first time, I should get a version control file without data in upand methods down. But instead, I get the following:

// ...

class Version20120807125808 extends AbstractMigration
{
    public function up(Schema $schema)
    {
        // this up() migration is autogenerated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != "postgresql");

        $this->addSql("ALTER TABLE product ALTER id DROP DEFAULT");
    }

    public function down(Schema $schema)
    {
        // this down() migration is autogenerated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != "postgresql");

        $this->addSql("CREATE SEQUENCE product_id_seq");
        $this->addSql("SELECT setval('product_id_seq', (SELECT MAX(id) FROM product))");
        $this->addSql("ALTER TABLE product ALTER id SET DEFAULT nextval('product_id_seq')");
    }
}

Why are differences found? How can i avoid this? I tried several sequence strategies without success.

+5
source share
4 answers

A small update on this.

Doctrine 2.4, IDENTITY:

Acme\DemoBundle\Entity\Product: type: entity table: product id: type: integer generator: strategy: IDENTITY fields: name: type: string length: 100 nullable: true

DROP DEFAULT , , default - . , , , .

"DEFAULT NOW()" , :

Acme\DemoBundle\Entity\Product: type: entity table: product id: type: integer generator: strategy: IDENTITY fields: creation_date: type: datetime nullable: false options: default: CURRENT_TIMESTAMP

+4

Doctrine 2.0 SQL DEFAULT postgres.

, .

+2

, .

If I use the strategy: IDENTITY, then everything is okay during the transition, but in the doctrine of moving down try CREATE SEQUENCE:

<!-- language: php -->
<?php

namespace Application\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;

/**
 * Auto-generated Migration: Please modify to your needs!
 */
class Version20141017144825 extends AbstractMigration
{
    public function up(Schema $schema)
    {
        // this up() migration is auto-generated, please modify it to your needs

    }

    public function down(Schema $schema)
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

        $this->addSql('CREATE SEQUENCE table_1_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
        $this->addSql('CREATE SEQUENCE table_2_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
        $this->addSql('CREATE SEQUENCE table_3_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
    }
}

How to disable CREATE SEQUENCE generation when migrating down?

I use:

  • Symfony v2.5.5
  • Doctrine v1.2.0
  • doctrine-migrations-bundle dev-master 81575a4
0
source

All Articles