Merge pull request #178 from dmarcelino/migrate

Add Migrate command
This commit is contained in:
Diogo Marques 2018-03-14 15:13:00 +00:00 committed by GitHub
commit a323535582
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 113 additions and 2 deletions

View file

@ -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
<https://github.com/infoportugal/wagtail-modeltranslation/issues/175#issuecomment-368046055>`_ 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

View file

@ -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=[

View file

@ -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)

View file

@ -0,0 +1,3 @@
# coding: utf-8
default_app_config = 'wagtail_modeltranslation.migrate.apps.MigrateConfig'

View file

@ -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"

View file

@ -0,0 +1,5 @@
from wagtail_modeltranslation.management.commands.migrate_translation import Command as MigrateCommand
class Command(MigrateCommand):
pass

View file

@ -0,0 +1,5 @@
from django.core.management.commands.migrate import Command as MigrateCommand
class Command(MigrateCommand):
pass