2
votes

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?

2
I have tried a few times to use migrations and it's a mess. I fallback on doctrine:schema:create,drop and update. Just empty (or drop) the migrations table and avoid migrations at least until your schema more or stabilizes.Cerad
@Cerad I posted a solution to avoid this kind of mess until shema is stable. :-)Alexandre Tranchant

2 Answers

2
votes

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
0
votes

Having one migration it isn't the best way, especially when you work in a team and do some features in different branches. With the one migration, it is easy to mess up and do something wrong like in your case. So it's ok to have many migrations.

What about your error, you can manually edit your migration and fix all errors, then run diff and migrate(if needed), or you can drop your database, remove all migrations and create a new one, and then creates migrations after making changes in code.