From 7f627555772719514a03c7e3cbc3f03860fa52cb Mon Sep 17 00:00:00 2001 From: Jon Beilke Date: Tue, 16 Oct 2018 12:56:44 -0500 Subject: [PATCH 1/3] add djangomod.migrate --- salt/modules/djangomod.py | 48 ++++++++++++++++++++++++++++ tests/unit/modules/test_djangomod.py | 22 +++++++++++++ 2 files changed, 70 insertions(+) diff --git a/salt/modules/djangomod.py b/salt/modules/djangomod.py index 1167eb78ef73..7c10316eec28 100644 --- a/salt/modules/djangomod.py +++ b/salt/modules/djangomod.py @@ -88,6 +88,9 @@ def syncdb(settings_module, minion the ``migrate`` option can be passed as ``True`` calling the migrations to run after the syncdb completes + NOTE: The syncdb command was deprecated in Django 1.7 and removed in Django 1.9. + For Django versions 1.9 or higher use the `migrate` command instead. + CLI Example: .. code-block:: bash @@ -112,6 +115,51 @@ def syncdb(settings_module, *args, **kwargs) +def migrate(settings_module, + app_label=None, + migration_name=None, + bin_env=None, + database=None, + pythonpath=None, + env=None, + noinput=True, + runas=None): + ''' + Run migrate + + Execute the Django-Admin migrate command (requires Django 1.7 or higher). + + CLI Example: + + .. code-block:: bash + + salt '*' django.migrate + salt '*' django.migrate + salt '*' django.migrate + ''' + args = [] + kwargs = {} + if database: + kwargs['database'] = database + if noinput: + args.append('noinput') + + if app_label and migration_name: + cmd = "migrate {0} {1}".format(app_label, migration_name) + elif app_label: + cmd = "migrate {0}".format(app_label) + else: + cmd = 'migrate' + + return command(settings_module, + cmd, + bin_env, + pythonpath, + env, + runas, + *args, **kwargs) + + def createsuperuser(settings_module, username, email, diff --git a/tests/unit/modules/test_djangomod.py b/tests/unit/modules/test_djangomod.py index 52489c96b264..952a2211c512 100644 --- a/tests/unit/modules/test_djangomod.py +++ b/tests/unit/modules/test_djangomod.py @@ -51,6 +51,16 @@ def test_syncdb(self): with patch.dict(djangomod.__salt__, {'cmd.run': mock}): self.assertTrue(djangomod.syncdb('DJANGO_SETTINGS_MODULE')) + # 'migrate' function tests: 1 + + def test_migrate(self): + ''' + Test if it runs the Django-Admin migrate command + ''' + mock = MagicMock(return_value=True) + with patch.dict(djangomod.__salt__, {'cmd.run': mock}): + self.assertTrue(djangomod.migrate('DJANGO_SETTINGS_MODULE')) + # 'createsuperuser' function tests: 1 def test_createsuperuser(self): @@ -190,6 +200,18 @@ def test_django_admin_cli_syncdb_migrate(self): runas=None ) + def test_django_admin_cli_migrate(self): + mock = MagicMock() + with patch.dict(djangomod.__salt__, + {'cmd.run': mock}): + djangomod.migrate('settings.py') + mock.assert_called_once_with( + 'django-admin.py migrate --settings=settings.py --noinput', + python_shell=False, + env=None, + runas=None + ) + def test_django_admin_cli_createsuperuser(self): mock = MagicMock() with patch.dict(djangomod.__salt__, From c1b4be5df2870fca7c98b0f53a5ab05bb1f00271 Mon Sep 17 00:00:00 2001 From: Jon Beilke Date: Wed, 17 Oct 2018 15:19:57 -0500 Subject: [PATCH 2/3] add version tag and document args for djangomod.migrate --- salt/modules/djangomod.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/salt/modules/djangomod.py b/salt/modules/djangomod.py index 7c10316eec28..3206f30b96f8 100644 --- a/salt/modules/djangomod.py +++ b/salt/modules/djangomod.py @@ -129,6 +129,25 @@ def migrate(settings_module, Execute the Django-Admin migrate command (requires Django 1.7 or higher). + .. versionadded:: Neon + + app_label + Specific app to run migrations for, instead of all apps. + This may involve running other apps’ migrations too, due to dependencies. + + migration_name + Named migration to be applied to a specific app. + Brings the database schema to a state where the named migration is applied, + but no later migrations in the same app are applied. This may involve + unapplying migrations if you have previously migrated past the named migration. + Use the name zero to unapply all migrations for an app. + + database + Database to migrate. Defaults to 'default'. + + noinput + Suppresses all user prompts. Defaults to True. + CLI Example: .. code-block:: bash From 89717ddb1752bc4f0670028ad818aff164f69702 Mon Sep 17 00:00:00 2001 From: Jon Beilke Date: Thu, 18 Oct 2018 08:46:42 -0500 Subject: [PATCH 3/3] document default django-admin args and cmd.run args available in djangomod.migrate --- salt/modules/djangomod.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/salt/modules/djangomod.py b/salt/modules/djangomod.py index 3206f30b96f8..f0ecfd153867 100644 --- a/salt/modules/djangomod.py +++ b/salt/modules/djangomod.py @@ -131,6 +131,12 @@ def migrate(settings_module, .. versionadded:: Neon + settings_module + Specifies the settings module to use. + The settings module should be in Python package syntax, e.g. mysite.settings. + If this isn’t provided, django-admin will use the DJANGO_SETTINGS_MODULE + environment variable. + app_label Specific app to run migrations for, instead of all apps. This may involve running other apps’ migrations too, due to dependencies. @@ -142,12 +148,38 @@ def migrate(settings_module, unapplying migrations if you have previously migrated past the named migration. Use the name zero to unapply all migrations for an app. + bin_env + Path to pip (or to a virtualenv). This can be used to specify the path + to the pip to use when more than one Python release is installed (e.g. + ``/usr/bin/pip-2.7`` or ``/usr/bin/pip-2.6``. If a directory path is + specified, it is assumed to be a virtualenv. + database Database to migrate. Defaults to 'default'. + pythonpath + Adds the given filesystem path to the Python import search path. + If this isn’t provided, django-admin will use the PYTHONPATH environment variable. + + env + A list of environment variables to be set prior to execution. + + Example: + + .. code-block:: yaml + + module.run: + - name: django.migrate + - settings_module: my_django_app.settings + - env: + - DATABASE_USER: 'mydbuser' + noinput Suppresses all user prompts. Defaults to True. + runas + The user name to run the command as. + CLI Example: .. code-block:: bash