diff --git a/docs/management commands.rst b/docs/management commands.rst index b3a75fb..1f9a370 100644 --- a/docs/management commands.rst +++ b/docs/management commands.rst @@ -57,13 +57,30 @@ The ``makemigrations_translation`` Command ``wagtail-modeltranslation`` patches Wagtail's ``Page`` model and as consequence Django's original ``makemigrations`` commmand will create migrations for ``Page`` which may create conflicts with other migrations. To circumvent this issue ``makemigrations_translation`` hides any ``Page`` model changes -and creates all other migrations as usual. Use this command as an alterntive to Django's own +and creates all other migrations as usual. Use this command as an alternative to Django's own ``makemigrations`` or consider using :ref:`management_commands-makemigrations`. .. code-block:: console $ python manage.py makemigrations_translation +.. _management_commands-migrate_translation: + +The ``migrate_translation`` Command +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. versionadded:: 0.8 + +Since :ref:`management_commands-makemigrations_translation` hides any ``Page`` model changes, Django's own +``migrate`` command won't be able to update ``wagtailcore_page`` table with new translation fields. In order to +correctly update the database schema a combination of ``migrate`` followed by :ref:`sync_page_translation_fields` +is usually required. ``migrate_translation`` provides a shortcut to running these two commands. Use this +as an alternative to Django's own ``migrate`` or consider using :ref:`management_commands-migrate`. + +.. code-block:: console + + $ python manage.py migrate_translation + .. _management_commands-set_translation_url_paths: The ``set_translation_url_paths`` Command @@ -109,3 +126,41 @@ this will likely create invalid ``Page`` migrations, do this only if you know wh .. code-block:: console $ python manage.py makemigrations_original + + +.. _management_commands-wagtail_modeltranslation.migrate: + +wagtail_modeltranslation.migrate +--------------------------------- + +To use ``wagtail_modeltranslation.migrate`` module commands add ``'wagtail_modeltranslation.migrate,'`` +to ``INSTALLED_APPS``. This module adds the following management commands. + +.. _management_commands-migrate: + +The ``migrate`` Command +~~~~~~~~~~~~~~~~~~~~~~~ + +This command is a proxy for :ref:`management_commands-migrate_translation`. It has the added benefit of +overriding Django's own ``migrate`` saving the need to additionally run :ref:`sync_page_translation_fields`. +See `issue #175 +`_ to understand +how this command can be used to create translation fields in a test database. + +.. code-block:: console + + $ python manage.py migrate + +.. _management_commands-migrate_original: + +The ``migrate_original`` Command +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Since Django's ``migrate`` is overriden by ``wagtail-modeltranslation``'s version use +``migrate_original`` to run the Django's original ``migrate`` command. Please note +this will not update ``wagtailcore_page`` table with new translation fields, use +:ref:`sync_page_translation_fields` for that. + +.. code-block:: console + + $ python manage.py migrate_original diff --git a/setup.py b/setup.py index 28c8377..8c96e10 100755 --- a/setup.py +++ b/setup.py @@ -24,7 +24,10 @@ setup( 'wagtail_modeltranslation.templatetags', 'wagtail_modeltranslation.makemigrations', 'wagtail_modeltranslation.makemigrations.management', - 'wagtail_modeltranslation.makemigrations.management.commands'], + 'wagtail_modeltranslation.makemigrations.management.commands', + 'wagtail_modeltranslation.migrate', + 'wagtail_modeltranslation.migrate.management', + 'wagtail_modeltranslation.migrate.management.commands'], package_data={'wagtail_modeltranslation': ['static/wagtail_modeltranslation/css/*.css', 'static/wagtail_modeltranslation/js/*.js']}, install_requires=[ diff --git a/wagtail_modeltranslation/management/commands/migrate_translation.py b/wagtail_modeltranslation/management/commands/migrate_translation.py new file mode 100644 index 0000000..f96fa21 --- /dev/null +++ b/wagtail_modeltranslation/management/commands/migrate_translation.py @@ -0,0 +1,33 @@ +from django.core.management.commands.migrate import Command as MigrateCommand +from django.db.migrations.autodetector import MigrationAutodetector +from .sync_page_translation_fields import Command as SyncPageTranslationFieldsCommand + + +# decorate MigrationAutodetector.changes so we can silence any wagtailcore migrations missing warnings +def changes_decorator(func): + def wrapper(self, graph, trim_to_apps=None, convert_apps=None, migration_name=None): + changes = func(self, graph, trim_to_apps, convert_apps, migration_name) + if 'wagtailcore' in changes: + del changes['wagtailcore'] + return changes + return wrapper + + +class Command(MigrateCommand): + help = "Updates database schema. Manages both apps with migrations and those without. " \ + "Updates Wagtail Page translation fields" + + def handle(self, *args, **options): + old_autodetector_changes = MigrationAutodetector.changes + MigrationAutodetector.changes = changes_decorator(MigrationAutodetector.changes) + + try: + super(Command, self).handle(*args, **options) + finally: + MigrationAutodetector.changes = old_autodetector_changes + + # Run sync_page_translation_fields command + sync_page_command = SyncPageTranslationFieldsCommand() + # Update the dict of sync_page_command with the content of this one + sync_page_command.__dict__.update(self.__dict__) + sync_page_command.handle(*args, **options) diff --git a/wagtail_modeltranslation/migrate/__init__.py b/wagtail_modeltranslation/migrate/__init__.py new file mode 100644 index 0000000..416b29c --- /dev/null +++ b/wagtail_modeltranslation/migrate/__init__.py @@ -0,0 +1,3 @@ +# coding: utf-8 + +default_app_config = 'wagtail_modeltranslation.migrate.apps.MigrateConfig' diff --git a/wagtail_modeltranslation/migrate/apps.py b/wagtail_modeltranslation/migrate/apps.py new file mode 100644 index 0000000..87305df --- /dev/null +++ b/wagtail_modeltranslation/migrate/apps.py @@ -0,0 +1,7 @@ +from django.apps import AppConfig + + +class MigrateConfig(AppConfig): + name = 'wagtail_modeltranslation.migrate' + label = 'wagtail_modeltranslation_migrate' + verbose_name = "Wagtail Modeltranslation migrate" diff --git a/wagtail_modeltranslation/migrate/management/__init__.py b/wagtail_modeltranslation/migrate/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/wagtail_modeltranslation/migrate/management/commands/__init__.py b/wagtail_modeltranslation/migrate/management/commands/__init__.py new file mode 100755 index 0000000..e69de29 diff --git a/wagtail_modeltranslation/migrate/management/commands/migrate.py b/wagtail_modeltranslation/migrate/management/commands/migrate.py new file mode 100644 index 0000000..9d42d2e --- /dev/null +++ b/wagtail_modeltranslation/migrate/management/commands/migrate.py @@ -0,0 +1,5 @@ +from wagtail_modeltranslation.management.commands.migrate_translation import Command as MigrateCommand + + +class Command(MigrateCommand): + pass diff --git a/wagtail_modeltranslation/migrate/management/commands/migrate_original.py b/wagtail_modeltranslation/migrate/management/commands/migrate_original.py new file mode 100644 index 0000000..124c8f0 --- /dev/null +++ b/wagtail_modeltranslation/migrate/management/commands/migrate_original.py @@ -0,0 +1,5 @@ +from django.core.management.commands.migrate import Command as MigrateCommand + + +class Command(MigrateCommand): + pass