Repository: starryrbs/django-comment-migrate (language: python)

Issue:
treat auth.User when AUTH_USER_MODEL is specified for another
你好!

When `AUTH_USER_MODEL` is defined for another model, Django doesn't create `auth_user` table during migration. But `auth.User` exists in `app_models`. Because the migrations have information of `auth.User`. So it should be dropped.


Discussion / hints:
Thank you for your pull request @jbking.
Can you add a test to your pull request?


Fix patch:
diff --git a/django_comment_migrate/apps.py b/django_comment_migrate/apps.py
index a91114c..e91e25d 100644
--- a/django_comment_migrate/apps.py
+++ b/django_comment_migrate/apps.py
@@ -1,4 +1,5 @@
 from django.apps import AppConfig, apps
+from django.contrib.auth import get_user_model
 from django.db import DEFAULT_DB_ALIAS
 from django.db.models.signals import post_migrate
 
@@ -7,8 +8,13 @@
 
 
 def handle_post_migrate(app_config, using=DEFAULT_DB_ALIAS, **kwargs):
-    migrations = (migration for migration, _ in kwargs.get('plan', []))
+    from django.contrib.auth.models import User
+
+    migrations = (migration for migration, rollback in kwargs.get('plan', []) if not rollback)
     app_models = get_migrations_app_models(migrations, apps, using)
+    # another user model is specified instead.
+    if get_user_model() != User:
+        app_models -= {User}
     migrate_app_models_help_text_to_database(app_models, using)
 
 
diff --git a/setup.py b/setup.py
index ca0f167..cf0ac81 100644
--- a/setup.py
+++ b/setup.py
@@ -4,7 +4,7 @@
 
 setup(
     name='django-comment-migrate',
-    version='0.0.9',
+    version='0.1.0',
     description="""An app that provides Django model comment migration""",
     long_description=readme,
     author='starryrbs',


Test patch:
diff --git a/tests/models.py b/tests/models.py
index 87a662e..e222489 100644
--- a/tests/models.py
+++ b/tests/models.py
@@ -1,4 +1,5 @@
 from django.db import models
+from django.contrib.auth import models as auth_models
 
 
 class CommentModel(models.Model):
@@ -10,3 +11,8 @@ class CommentModel(models.Model):
     class Meta:
         app_label = 'tests'
         db_table = 'user'
+
+
+class AnotherUserModel(auth_models.AbstractBaseUser):
+    class Meta:
+        app_label = 'tests'
diff --git a/tests/test_migrate_comment.py b/tests/test_migrate_comment.py
index 030949e..4575467 100644
--- a/tests/test_migrate_comment.py
+++ b/tests/test_migrate_comment.py
@@ -3,7 +3,7 @@
 from django.apps.registry import Apps
 from django.conf import settings
 from django.core import management
-from django.test import TestCase
+from django.test import TransactionTestCase, TestCase
 from django.db import connections, migrations, models
 from django.utils.module_loading import import_string
 
@@ -97,6 +97,20 @@ def test_migrate_command_without_app_label(self):
         self.assertIn('migrate app tests successful', out.getvalue())
 
 
+class TestCommandWithAnotherCustomUser(TransactionTestCase):
+    def test_migrate_command_with_custom_auth_user(self):
+        # rollback migrations auth and related
+        management.call_command('migrate', app_label='contenttypes', migration_name='zero')
+        management.call_command('migrate', app_label='auth', migration_name='zero')
+        with self.settings(AUTH_USER_MODEL="tests.AnotherUserModel"):
+            out = io.StringIO()
+            # migrate auth and related again in customize auth_user_model context
+            management.call_command('migrate', app_label='auth')
+            management.call_command('migrate', app_label='contenttypes')
+            management.call_command('migratecomment', stdout=out)
+            self.assertIn('migrate app tests successful', out.getvalue())
+
+
 class TestUtil(TestCase):
     def test_get_migrations_app_models(self):
         new_apps = Apps(['tests'])