Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add migrate command to djangomod #50083

Merged
merged 4 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions salt/modules/djangomod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 <settings_module>
salt '*' django.migrate <settings_module> <app_label>
salt '*' django.migrate <settings_module> <app_label> <migration_name>
'''
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,
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/modules/test_djangomod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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__,
Expand Down