Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table in default schema is recreated (when the schema is specified) #1196

Open
zerkms opened this issue Aug 29, 2021 · 4 comments
Open

Table in default schema is recreated (when the schema is specified) #1196

zerkms opened this issue Aug 29, 2021 · 4 comments
Labels

Comments

@zerkms
Copy link

zerkms commented Aug 29, 2021

Bug Report

Q A
BC Break no
Version 3.2.1

Summary

If a schema is explicitly specified - the table is recreated every migration:

    <entity name="Db\Schema\Dummy" table="dummy" schema="public">
        <id name="id" type="integer">
            <generator strategy="IDENTITY" />
        </id>
    </entity>

This generates this initial migration:

final class Version20210829230256 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
        $this->addSql('CREATE TABLE public.dummy (id SERIAL NOT NULL, PRIMARY KEY(id))');
    }

    public function down(Schema $schema): void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->addSql('DROP TABLE public.dummy');
    }
}

Then I apply the migration and run diff once again, and it generates the following migration

final class Version20210829230310 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
        $this->addSql('CREATE TABLE public.dummy (id SERIAL NOT NULL, PRIMARY KEY(id))');
        $this->addSql('DROP TABLE dummy');
    }

    public function down(Schema $schema): void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->addSql('CREATE TABLE dummy (id SERIAL NOT NULL, PRIMARY KEY(id))');
        $this->addSql('DROP TABLE public.dummy');
    }
}

From the other hand - if I don't specify the schema explicitly the second diff generates

final class Version20210829230712 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
        $this->addSql('CREATE SCHEMA public');
    }
}

which is also wrong as it should not recreate the public schema.

Current behavior

How to reproduce

Expected behavior

It should not try to recreate a table given it has not changed.

@zerkms zerkms changed the title Table in default schema is recreated Table in default schema is recreated (when the schema is specified) Aug 29, 2021
@tomasnorre
Copy link

I can confirm this.

Reported it in Symfony as I thought it was a Symfony component.
symfony/symfony#44952

@ghost
Copy link

ghost commented Feb 4, 2022

I can also confirm this behaviour.
In a current project running with Symfony 6.0.1 on PHP 8.1.0, Debian 10 x64 system I keep extending Doctrine Entities created with make:entity by hand. Whenever I run make:migration I have to manually remove the CREATE SCHEMA statement from generated migration.
The schema name was configured in database url as follows:

postgresql://symfony:supersecretpasswd@127.0.0.1:5432/techdb_dev?serverVersion=13&charset=utf8&schema=test

Interesting about this: The schema name gets ignored completely. It always adds "CREATE SCHEMA public" to the down method of the migration. So I decided to remove the schema from my database url, anyway Doctrine keeps adding it to my migrations.

@derrabus derrabus added the Bug label Feb 4, 2022
@pkly
Copy link

pkly commented Feb 22, 2022

I also encountered this, but for me it's the ALTER instructions, as we already had an existing schema on older versions.

My whole down() method in the migration is basically changing all fields to be "themselves" again, even if the migration itself only changes one or two, all tables are changed "back" (to what they already are).

In my case only 3 fields changed (new comment, migration from guid to uuid), and I got 12 lines modifying all entities and all of their fields, not only the changed ones. And my schema is on the entity as PHP attributes

@allan-simon
Copy link

I found the root issue and opened a PR doctrine/dbal#5600 with full explanation here doctrine/dbal#5609

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants