Skip to content

Migrations diff and postgres schema problem with recreating existing tables #85

@seticzech

Description

@seticzech
Package Version
doctrine/annotations v1.6.0
doctrine/dbal v2.9.2
doctrine/migrations v1.8.1
doctrine/orm v2.6.3
laravel-doctrine/migrations 1.2.0
laravel-doctrine/orm 1.4.9
laravel/framework v5.7.27
PostgreSQL 9.6.11
PHP 7.2.15

Summary

Running doctrine:migrations:diff multiple times without any changes and with existing table in database always generate the same code trying to create table with schema and delete the very same table without schema.

How to reproduce

On any project create entity:

/**
 * @ORM\Entity(repositoryClass = "App\Domain\Repositories\UserRepository")
 * @ORM\Table(name = "users", schema = "public")
 */
class User
{

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy = "IDENTITY")
     * @ORM\Column(type = "integer")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length = 64, nullable = false, unique = true)
     * @var string
     */
    protected $username;

}

Run doctrine:migrations:diff

Generated migration is ok now:

$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

$this->addSql('CREATE TABLE public.users (id SERIAL NOT NULL, username VARCHAR(64) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE UNIQUE INDEX UNIQ_2552C48DF85E0677 ON public.users (username)');

Run doctrine:migrations:migrate and table is created succesfully in database.

Run doctrine:migrations:diff and new migration is generated:

$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

$this->addSql('CREATE TABLE public.users (id SERIAL NOT NULL, username VARCHAR(64) NOT NULL, PRIMARY KEY(id))');
$this->addSql('CREATE UNIQUE INDEX UNIQ_2552C48DF85E0677 ON public.users (username)');
$this->addSql('DROP TABLE users');

Of course next running doctrine:migrations:migrate ends with error

relation "users" already exists

Comment

I don't know if this behavior is related to laravel-doctrine or doctrine itself so I also created bug report in doctrine issue tracker

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions