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

Issue:
新功能(backend): 支持用户自定义backend



Discussion / hints:

Fix patch:
diff --git a/README-zh_CN.rst b/README-zh_CN.rst
index 5db114c..6403945 100644
--- a/README-zh_CN.rst
+++ b/README-zh_CN.rst
@@ -58,13 +58,16 @@ Django Comment Migrate
 
 现在检查数据库的table，注释已经迁移了。
 
-自定义注释的字段
+自定义配置
 --------------------
 
 在 settings.py::
 
     DCM_COMMENT_KEY='verbose_name'
     DCM_TABLE_COMMENT_KEY='verbose_name'
+    DCM_BACKEND={
+            "new-engine": "engine.path"
+    }
 
 Command
 -------
diff --git a/README.rst b/README.rst
index cfd1867..516fc55 100644
--- a/README.rst
+++ b/README.rst
@@ -58,13 +58,16 @@ Examples
 
 Now check the database table, comments have been generated.
 
-Custom comment field
---------------------
+Custom config
+---------------
 
 In settings.py::
 
     DCM_COMMENT_KEY='verbose_name'
     DCM_TABLE_COMMENT_KEY='verbose_name'
+    DCM_BACKEND={
+            "new-engine": "engine.path"
+    }
 
 Command
 -------
diff --git a/django_comment_migrate/config.py b/django_comment_migrate/config.py
index 36dc478..dc9373c 100644
--- a/django_comment_migrate/config.py
+++ b/django_comment_migrate/config.py
@@ -2,7 +2,11 @@
 
 
 class DCMConfig:
-    defaults = {"DCM_COMMENT_KEY": "help_text", "DCM_TABLE_COMMENT_KEY": "verbose_name"}
+    defaults = {
+        "DCM_COMMENT_KEY": "help_text",
+        "DCM_TABLE_COMMENT_KEY": "verbose_name",
+        "DCM_BACKEND": None,
+    }
 
     def __getattr__(self, name):
         if name in self.defaults:
diff --git a/django_comment_migrate/db_comments.py b/django_comment_migrate/db_comments.py
index 851f896..ed550be 100644
--- a/django_comment_migrate/db_comments.py
+++ b/django_comment_migrate/db_comments.py
@@ -4,26 +4,35 @@
 from django.db import connections
 from django.utils.module_loading import import_string
 
+from django_comment_migrate.config import dcm_config
+
 
 def get_migration_class_from_engine(engine):
-    backend_name = engine.split('.')[-1]
-    # This backend was renamed in Django 1.9.
-    if backend_name == 'postgresql_psycopg2':
-        backend_name = 'postgresql'
-    path = f'django_comment_migrate.backends.{backend_name}.CommentMigration'
+    engine_name = engine.split(".")[-1]
+    if (
+        dcm_config.DCM_BACKEND
+        and isinstance(dcm_config.DCM_BACKEND, dict)
+        and dcm_config.DCM_BACKEND.get(engine_name)
+    ):
+        path = dcm_config.DCM_BACKEND.get(engine_name)
+    else:
+        # This backend was renamed in Django 1.9.
+        if engine_name == "postgresql_psycopg2":
+            engine_name = "postgresql"
+        path = f"django_comment_migrate.backends.{engine_name}.CommentMigration"
 
     return import_string(path)
 
 
 def migrate_app_models_comment_to_database(app_models, using):
-    engine = settings.DATABASES[using]['ENGINE']
+    engine = settings.DATABASES[using]["ENGINE"]
     try:
         migration_class = get_migration_class_from_engine(engine)
     except ImportError:
-        warnings.warn(f'{engine} is not supported by this comment migration'
-                      f' backend.')
+        warnings.warn(
+            f"{engine} is not supported by this comment migration" f" backend."
+        )
     else:
         for model in app_models:
-            executor = migration_class(connection=connections[using],
-                                       model=model)
+            executor = migration_class(connection=connections[using], model=model)
             executor.execute()
diff --git a/setup.py b/setup.py
index b84d271..15d780b 100644
--- a/setup.py
+++ b/setup.py
@@ -4,7 +4,7 @@
 
 setup(
     name='django-comment-migrate',
-    version='0.1.4',
+    version='0.1.5',
     description="""An app that provides Django model comment migration """,
     long_description=readme,
     author='starryrbs',


Test patch:
diff --git a/tests/test_migrate_comment.py b/tests/test_migrate_comment.py
index 0327b3c..692fc12 100644
--- a/tests/test_migrate_comment.py
+++ b/tests/test_migrate_comment.py
@@ -116,7 +116,9 @@ def test_get_comments_for_model(self):
         target_sql = engine_sql_mapping[engine]
         self.assertEqual(sql, target_sql)
 
-    @override_settings(DCM_COMMENT_KEY="verbose_name", DCM_TABLE_COMMENT_KEY="verbose_name_plural")
+    @override_settings(
+        DCM_COMMENT_KEY="verbose_name", DCM_TABLE_COMMENT_KEY="verbose_name_plural"
+    )
     def test_custom_comment_key(self):
         engine = settings.DATABASES["default"]["ENGINE"]
         migration_class = get_migration_class_from_engine(engine)
@@ -126,6 +128,18 @@ def test_custom_comment_key(self):
         self.assertIn("verbose name is aaa", str(sql))
         self.assertIn("测试自定义表注释key", str(sql))
 
+    @override_settings(
+        DCM_BACKEND={
+            "new-engine": "django_comment_migrate.backends.mssql.CommentMigration"
+        }
+    )
+    def test_custom_backend(self):
+        migration_class = get_migration_class_from_engine("new-engine")
+        self.assertEqual(
+            migration_class,
+            import_string("django_comment_migrate.backends.mssql.CommentMigration"),
+        )
+
 
 class TestCommand(TestCase):
     def test_migrate_command_with_app_label(self):