Skip to content

Commit

Permalink
[AdminTools] adds migration plan view and the ability to run migratio…
Browse files Browse the repository at this point in the history
…ns from the UI
  • Loading branch information
jontsai committed Aug 10, 2024
1 parent 0f0b952 commit d1d16fd
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
71 changes: 71 additions & 0 deletions admintools/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

# Django Imports
from django.conf import settings
from django.contrib import messages
from django.shortcuts import redirect

# HTK Imports
from htk.admintools.utils import retrieve_migrations
Expand All @@ -14,6 +16,9 @@
from htk.view_helpers import render_custom as _r


# isort: off


def migrations_view(
request,
template='admintools/migrations.html',
Expand All @@ -32,6 +37,72 @@ def migrations_view(
return response


def migration_plan_view(
request,
template='admintools/migration_plan.html',
data=None,
renderer=_r,
):
if request.method == 'POST':
# Run the 'migrate' command
result = subprocess.run(
['venv/bin/python', 'manage.py', 'migrate'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)

# Get the output from the command
migration_output = result.stdout
error_message = result.stderr

messages.info(request, migration_output)
if error_message:
messages.error(request, error_message)

response = redirect(request.path)
else:
try:
# Run the 'showmigrations --plan' command
result = subprocess.run(
['venv/bin/python', 'manage.py', 'showmigrations', '--plan'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
)

# Get the output from the command
migration_plan = result.stdout
migrations = migration_plan.split('\n')
num_migrations_run = len(
[
migration
for migration in migrations
if migration.startswith('[X]')
]
)
num_migrations_to_apply = len(
[
migration
for migration in migrations
if migration.startswith('[ ]')
]
)

data['migration_plan'] = migration_plan
data['num_migrations_run'] = num_migrations_run
data['num_migrations_to_apply'] = num_migrations_to_apply
except subprocess.CalledProcessError as e:
# Handle the error case
error_message = f"An error occurred while fetching the migration plan: {e.stderr}"
data['error_message'] = error_messagea

response = _r(request, template, data=data)

return response


def todos_view(
request,
template='admintools/todos.html',
Expand Down
17 changes: 17 additions & 0 deletions templates/htk/admintools/fragments/migration_plan.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% load htk_tags %}

<p>
<b>Number of Migrations Run:</b> {{ num_migrations_run }}<br/>
<b>Number of Migrations to Apply:</b> {{ num_migrations_to_apply }}<br/>
</p>

{% if num_migrations_to_apply > 0 %}
<p>
<form id="apply_migrations_form" method="POST">
{% csrf_token %}
<button type="submit" id="apply_migrations" class="btn btn-danger">Apply Migrations</button>
</form>
</p>
{% endif %}

<pre>{{ migration_plan }}</pre>
19 changes: 19 additions & 0 deletions templates/htk/admintools/fragments/migration_plan_js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script>
$(function() {
$('#apply_migrations').click(function(e) {
e.preventDefault();

swal('Are you sure you want to apply migrations?', {
buttons: [
"Cancel",
"Apply"
]
}).then((value) => {
if (value) {
form = $('#apply_migrations_form');
$('#apply_migrations_form').submit();
}
});
});
});
</script>

0 comments on commit d1d16fd

Please sign in to comment.