Repository: dj-stripe/dj-stripe

Issue:
migrate djstripe zero leaves migration entry in django_migrations table
### Background:
I recently found the `DJSTRIPE_USE_NATIVE_JSONFIELD` setting and since I'm using PostgreSQL wanted to change it to `True`. I figured I needed to un-apply and then re-apply the djstripe migrations in order to make this happen. The re-apply step is where I noticed this issue. I'm not sure if this is a django bug related to squashed migrations or not, and I don't know how to check, so feel free to tell me to sod off if this is a core django issue.

### Expected behavior:
`manage.py migrate djstripe zero` undoes all `djstripe` migrations and removes them from the migration history in the `django_migrations` table.

### Actual behavior:
The actual tables are removed and any related field migrations (like a foreign key to a djstripe model) are undone, but a entry for the `0003_auto_20181117_2328_squashed_0004_auto_20190227_2114` migration remains in the `django_migrations` table.

This leads to a `django.db.migrations.exceptions.InconsistentMigrationHistory: Migration djstripe.0003_auto_20181117_2328_squashed_0004_auto_20190227_2114 is applied before its dependency djstripe.0002_auto_20180627_1121 on database 'default'.` error when trying to re-apply the migrations.

![image](https://user-images.githubusercontent.com/2704075/56098731-35bbbc00-5eb8-11e9-9ff5-14403278f360.png)

```
> pipenv run python .\manage.py migrate
Loading .env environment variables…
Traceback (most recent call last):
  File ".\manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Nukesforbreakfast\.virtualenvs\django-matsite-yaAYvxvg\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Nukesforbreakfast\.virtualenvs\django-matsite-yaAYvxvg\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Nukesforbreakfast\.virtualenvs\django-matsite-yaAYvxvg\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Nukesforbreakfast\.virtualenvs\django-matsite-yaAYvxvg\lib\site-packages\django\core\management\base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Nukesforbreakfast\.virtualenvs\django-matsite-yaAYvxvg\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\Nukesforbreakfast\.virtualenvs\django-matsite-yaAYvxvg\lib\site-packages\django\core\management\commands\migrate.py", line 85, in handle
    executor.loader.check_consistent_history(connection)
  File "C:\Users\Nukesforbreakfast\.virtualenvs\django-matsite-yaAYvxvg\lib\site-packages\django\db\migrations\loader.py", line 297, in check_consistent_history
    connection.alias,
django.db.migrations.exceptions.InconsistentMigrationHistory: Migration djstripe.0003_auto_20181117_2328_squashed_0004_auto_20190227_2114 is applied before its dependency djstripe.0002_auto_20180627_1121 on database 'default'.
```

```
> pipenv run python .\manage.py showmigrations
djstripe
 [ ] 0001_initial
 [ ] 0002_auto_20180627_1121
 [ ] 0003_auto_20181117_2328_squashed_0004_auto_20190227_2114 (2 squashed migrations)
```

### Reproduction steps:
1. Run `manage.py migrate djstripe`
2. Run `manage.py migrate djstripe zero`
3. Run `manage.py migrate djstripe`
4. observe error.

### Versions:
```
- dj-stripe [required: Any, installed: 2.0.0]
    - Django [required: >=2.0, installed: 2.1.8]
      - pytz [required: Any, installed: 2019.1]
    - jsonfield [required: >=2.0.2, installed: 2.0.2]
      - Django [required: >=1.8.0, installed: 2.1.8]
        - pytz [required: Any, installed: 2019.1]
    - stripe [required: >=2.3.0, installed: 2.24.1]
      - requests [required: >=2.20, installed: 2.21.0]
        - certifi [required: >=2017.4.17, installed: 2019.3.9]
        - chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]
        - idna [required: >=2.5,<2.9, installed: 2.8]
        - urllib3 [required: >=1.21.1,<1.25, installed: 1.24.1]

PostgreSQL version 10.3
```

### Notes:
I'm noticing that it appears to add an additional migration entry to `django_migrations` when running `manage.py migrate djstripe`. It's the last entry below. This additional entry is the one that does not get cleaned up when then running `manage.py migrate djstripe zero`.

![image](https://user-images.githubusercontent.com/2704075/56098793-330d9680-5eb9-11e9-8f16-797714bb415a.png)

### Workaround:
Manually deleting the entry from the `djagno_migrations` table after a `manage.py migrate djstripe zero` will allow the re-apply of migrations. Since the tables all appear to be removed properly, I do not think there will be any adverse side effects.


Discussion:
Interesting, thanks for the report.

That sounds like it might be a django issue?
I wasn't sure because the squashed migration in question had some extra stuff in it that I hadn't seen in a migration before (`postgres_migration_prep`) and I thought that maybe there was some custom migration stuff that was borking things. I'll also ask on the Django user group and see if a bug needs to be opened up over on Django itself.
Actually, it appears to be an issue that the original migrations no longer exist in the migrations folder but the squashed migration still says it "replaces" some migrations. [The django migration docs](https://docs.djangoproject.com/en/2.2/topics/migrations/#squashing-migrations) seem to say that in order to transition the squashed migration to a normal one, and ostensibly allow the old migration files to be deleted, you have to remove the `replaces` attribute from the migration. Once I do this, I don't get this error anymore. I'll open a PR if that's alright.

Resolving patch:
diff --git a/djstripe/migrations/0003_auto_20181117_2328_squashed_0004_auto_20190227_2114.py b/djstripe/migrations/0003_auto_20181117_2328_squashed_0004_auto_20190227_2114.py
--- a/djstripe/migrations/0003_auto_20181117_2328_squashed_0004_auto_20190227_2114.py
+++ b/djstripe/migrations/0003_auto_20181117_2328_squashed_0004_auto_20190227_2114.py
@@ -103,11 +103,6 @@ def __init__(self, *args, **kwargs):
 
 		super().__init__(*args, **kwargs)
 
-	replaces = [
-		("djstripe", "0003_auto_20181117_2328"),
-		("djstripe", "0004_auto_20190227_2114"),
-	]
-
 	dependencies = [("djstripe", "0002_auto_20180627_1121")]
 
 	operations = [


Test patch: