Ola Duda
Ola Duda

Reputation: 73

How to remove doctrine migrations permanently?

How do I perform a migration and have just my existing Entities visualised?

The problem is:

  1. I removed all versions files from migrations
  2. I dropped schema public
  3. I created new schema public
  4. I made a make:migration
  5. I made a doctrine:migrations:migrate

And old data appeared - I mean tables which don't have its entities and were dropped long time ago.

Schema is created, but with reduntant and unnecessary data.

How to totally reset migrations? Is there any cache which save dropped tables (not considering versions of migrations)?

Here is my latest migrations file - I want to make a migration and I don't have such tables as for instance user_to_offert_user. Don't give me manual solutions, please - I know that I can modify it before migration, but it seems pointless to me doing that every time when I will change something.

If something is not clear, ask me please. I tried to specify problem as much as I can.

Additional info:

What I am exactly doing:

  1. I removed all versions from migrations.
  2. I truncated all data
  3. I dropped my public schema
  4. I created my new public schema
  5. I run bin/console make:migration
  6. I run bin/console doctrine:migrations:migrate

And here is my huge problem: I have this kind of entities: Entities

And when I run migrate all of this tables shows: Tables

As as you can see, there are too many tables. Some of them I dropped a long time ago and they still appears in every migration. I don't know how to get by and I am sooo desperate, because I have been spending on this problem 3th day.

I have my database on Heroku - I use Postgres and when I dropped all tablesthey doesn't show in heroku: enter image description here

Here is code of this migration:

<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

/**
 * Auto-generated Migration: Please modify to your needs!
 */
final class Version20210403092535 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 SEQUENCE requirement_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
        $this->addSql('CREATE SEQUENCE skill_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
        $this->addSql('CREATE SEQUENCE "user_id_seq" INCREMENT BY 1 MINVALUE 1 START 1');
        $this->addSql('CREATE SEQUENCE user_to_offert_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
        $this->addSql('CREATE TABLE login_history (id SERIAL NOT NULL, user_id_id INT NOT NULL, login_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, login_status BOOLEAN NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE INDEX IDX_37976E369D86650F ON login_history (user_id_id)');
        $this->addSql('CREATE TABLE offert (id SERIAL NOT NULL, event_firm_id INT NOT NULL, event_name VARCHAR(100) NOT NULL, date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, description VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE INDEX IDX_44229184CF8D1CF ON offert (event_firm_id)');
        $this->addSql('CREATE TABLE requirement (id INT NOT NULL, requirement VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE TABLE requirement_user (requirement_id INT NOT NULL, user_id INT NOT NULL, PRIMARY KEY(requirement_id, user_id))');
        $this->addSql('CREATE INDEX IDX_598D28707B576F77 ON requirement_user (requirement_id)');
        $this->addSql('CREATE INDEX IDX_598D2870A76ED395 ON requirement_user (user_id)');
        $this->addSql('CREATE TABLE skill (id INT NOT NULL, skill VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE TABLE skill_user (skill_id INT NOT NULL, user_id INT NOT NULL, PRIMARY KEY(skill_id, user_id))');
        $this->addSql('CREATE INDEX IDX_CAD24AFB5585C142 ON skill_user (skill_id)');
        $this->addSql('CREATE INDEX IDX_CAD24AFBA76ED395 ON skill_user (user_id)');
        $this->addSql('CREATE TABLE "user" (id INT NOT NULL, user_details_id_id INT NOT NULL, email VARCHAR(255) NOT NULL, password VARCHAR(255) NOT NULL, account_type VARCHAR(100) NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D64912595E8 ON "user" (user_details_id_id)');
        $this->addSql('CREATE TABLE user_details (id SERIAL NOT NULL, name VARCHAR(255) NOT NULL, surname VARCHAR(255) NOT NULL, description VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE TABLE user_to_offert (id INT NOT NULL, hired BOOLEAN NOT NULL, PRIMARY KEY(id))');
        $this->addSql('CREATE TABLE user_to_offert_offert (user_to_offert_id INT NOT NULL, offert_id INT NOT NULL, PRIMARY KEY(user_to_offert_id, offert_id))');
        $this->addSql('CREATE INDEX IDX_4AA1878F8C2827E4 ON user_to_offert_offert (user_to_offert_id)');
        $this->addSql('CREATE INDEX IDX_4AA1878F3D478C97 ON user_to_offert_offert (offert_id)');
        $this->addSql('CREATE TABLE user_to_offert_user (user_to_offert_id INT NOT NULL, user_id INT NOT NULL, PRIMARY KEY(user_to_offert_id, user_id))');
        $this->addSql('CREATE INDEX IDX_7BE6CF748C2827E4 ON user_to_offert_user (user_to_offert_id)');
        $this->addSql('CREATE INDEX IDX_7BE6CF74A76ED395 ON user_to_offert_user (user_id)');
        $this->addSql('ALTER TABLE login_history ADD CONSTRAINT FK_37976E369D86650F FOREIGN KEY (user_id_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE offert ADD CONSTRAINT FK_44229184CF8D1CF FOREIGN KEY (event_firm_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE requirement_user ADD CONSTRAINT FK_598D28707B576F77 FOREIGN KEY (requirement_id) REFERENCES requirement (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE requirement_user ADD CONSTRAINT FK_598D2870A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE skill_user ADD CONSTRAINT FK_CAD24AFB5585C142 FOREIGN KEY (skill_id) REFERENCES skill (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE skill_user ADD CONSTRAINT FK_CAD24AFBA76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE "user" ADD CONSTRAINT FK_8D93D64912595E8 FOREIGN KEY (user_details_id_id) REFERENCES user_details (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE user_to_offert_offert ADD CONSTRAINT FK_4AA1878F8C2827E4 FOREIGN KEY (user_to_offert_id) REFERENCES user_to_offert (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE user_to_offert_offert ADD CONSTRAINT FK_4AA1878F3D478C97 FOREIGN KEY (offert_id) REFERENCES offert (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE user_to_offert_user ADD CONSTRAINT FK_7BE6CF748C2827E4 FOREIGN KEY (user_to_offert_id) REFERENCES user_to_offert (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
        $this->addSql('ALTER TABLE user_to_offert_user ADD CONSTRAINT FK_7BE6CF74A76ED395 FOREIGN KEY (user_id) REFERENCES "user" (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
    }

    public function down(Schema $schema) : void
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->addSql('CREATE SCHEMA public');
        $this->addSql('ALTER TABLE user_to_offert_offert DROP CONSTRAINT FK_4AA1878F3D478C97');
        $this->addSql('ALTER TABLE requirement_user DROP CONSTRAINT FK_598D28707B576F77');
        $this->addSql('ALTER TABLE skill_user DROP CONSTRAINT FK_CAD24AFB5585C142');
        $this->addSql('ALTER TABLE login_history DROP CONSTRAINT FK_37976E369D86650F');
        $this->addSql('ALTER TABLE offert DROP CONSTRAINT FK_44229184CF8D1CF');
        $this->addSql('ALTER TABLE requirement_user DROP CONSTRAINT FK_598D2870A76ED395');
        $this->addSql('ALTER TABLE skill_user DROP CONSTRAINT FK_CAD24AFBA76ED395');
        $this->addSql('ALTER TABLE user_to_offert_user DROP CONSTRAINT FK_7BE6CF74A76ED395');
        $this->addSql('ALTER TABLE "user" DROP CONSTRAINT FK_8D93D64912595E8');
        $this->addSql('ALTER TABLE user_to_offert_offert DROP CONSTRAINT FK_4AA1878F8C2827E4');
        $this->addSql('ALTER TABLE user_to_offert_user DROP CONSTRAINT FK_7BE6CF748C2827E4');
        $this->addSql('DROP SEQUENCE requirement_id_seq CASCADE');
        $this->addSql('DROP SEQUENCE skill_id_seq CASCADE');
        $this->addSql('DROP SEQUENCE "user_id_seq" CASCADE');
        $this->addSql('DROP SEQUENCE user_to_offert_id_seq CASCADE');
        $this->addSql('DROP TABLE login_history');
        $this->addSql('DROP TABLE offert');
        $this->addSql('DROP TABLE requirement');
        $this->addSql('DROP TABLE requirement_user');
        $this->addSql('DROP TABLE skill');
        $this->addSql('DROP TABLE skill_user');
        $this->addSql('DROP TABLE "user"');
        $this->addSql('DROP TABLE user_details');
        $this->addSql('DROP TABLE user_to_offert');
        $this->addSql('DROP TABLE user_to_offert_offert');
        $this->addSql('DROP TABLE user_to_offert_user');
    }
}

Upvotes: 3

Views: 6357

Answers (1)

dbrumann
dbrumann

Reputation: 17166

Doctrine MigrationsBundle has a table where it stores the information which migrations previously ran. If you reset your migrations you will also have to reset that table. You can use the rollup command for this:

php bin/console doctrine:migrations:rollup

If this does not work you might want to change the table manually, e.g. in a migration, but you should probably avoid this if possible. By default the table is called doctrine_migration_versions, but you can use the debug:config command to check if your configuration is different:

php bin/console debug:config doctrine_migrations storage

Here is how it should look by default:

doctrine_migrations:
    # ...
    storage:
        # Default (SQL table) metadata storage configuration
        table_storage:
            table_name: 'doctrine_migration_versions'
            version_column_name: 'version'
            version_column_length: 1024
            executed_at_column_name: 'executed_at'

Upvotes: 3

Related Questions