Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to 'factory-reset' any migrations

What is the best way to reset the database along with the migrations.

Here is what I have tried.

  1. Delete all migrations along with deleting all the database tables. Then running

php bin/console doctrine:mi:diff

php bin/console doctrine:mi:mi

That does not work.

  1. Try running single version at a time like so

php bin/console doctrine:migrations:migrate 'DoctrineMigrations\Version20200722104913'

  1. I tried this:

php bin/console doctrine:database:drop --force

php bin/console doctrine:database:create

php bin/console doctrine:mi:mi

The Problem (in detail):

Everything I do leads me to the same result.

Doctrine thinks that I still have some tables to generate which are long gone (Not in the Enitity anymore)

That's why I have this error:

An exception occurred while executing 'DROP TABLE greetings_card_category':

SQLSTATE[42S02]: Base table or view not found: 1051 Unknown table 'symfony.greetings_card_category'

I also get this warning

[WARNING] You have 6 previously executed migrations in the database that are not registered migrations.

In my migrations Directory I only have two migrations:

Version20200722104913.php
Version20200722143619.php

Here is the status if it somehow helps.

bin/console do:mi:status

| Versions             | Previous             | DoctrineMigrations\Version20200717093052                               |
|                      | Current              | DoctrineMigrations\Version20200722150530                               |
|                      | Next                 | DoctrineMigrations\Version20200722104913                               |
|                      | Latest               | DoctrineMigrations\Version20200722143619                               |
|--------------------------------------------------------------------------------------------------------
| Migrations           | Executed             | 6                                                                      |
|                      | Executed Unavailable | 6                                                                      |
|                      | Available            | 2                                                                      |
|                      | New                  | 2

At this point I would really just love to have 1 clean database and 1 migration.

How to achieve this?

like image 547
MewTwo Avatar asked Oct 11 '25 16:10

MewTwo


1 Answers

Be aware that having one migration script isn't the best way, if you work in a team or if application was already deployed. But, in the case that you are the only one developer or if you want to rebase some commit, you can do it.

If you really want to have only one migration script, here is a solution. But first of all, it is always a bad idea to drop data and table manually because of the migration_table that is used by doctrine. This table contains data used by doctrine to know the current state of your database. So, you drop tables manually, your database will be unsynchronized with the migration table, and your scripts will failed. To avoid your current error, you now have to truncate the migration_table. If it isn't enough, drop migration table, if it isn't enough drop and create the database (for the last time, because below is a solution to avoid this kind of mismatch.

Step1: Downgrade your database via doctrine:migrate

php bin/console doctrine:migrations:migrate first -n

At this step your database is empty. (The tips is the keyword "first")

Step2: Delete (after a backup) all files in the migration directory

Step3: Create a new migration

php bin/console make:migration

(or you can use symfony console doctrine:migrations:diff if you do not use the make-bundle)

Step4: Verify and manually edit the file created if necessary.

Step5: Upgrade your database

php bin/console doctrine:migration:migrate -n

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!