Last active
January 4, 2022 16:59
-
-
Save baxeico/4b5ceb2eee14fe776c34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ -z $1 ]; then | |
echo "Usage: $0 app_label" | |
exit 1 | |
fi | |
app=$1 # this is the name of the app where we want to update the last migration | |
# get the list of known migrations for the app | |
migrations=(`./manage.py showmigrations $app | awk '{print $2}' | tail -2`) | |
if [ ${#migrations[@]} == 1 ]; then | |
# there is just one migration in the list | |
# here we are updating the initial migration | |
previous_migration=zero | |
current_migration=${migrations[0]} # should be 0001_initial | |
else | |
# there is more than one migration in the list | |
# get the previous one to go back to | |
# and the current one to update | |
previous_migration=${migrations[0]} | |
current_migration=${migrations[1]} | |
fi | |
# go back to the previous migration | |
./manage.py migrate $app $previous_migration | |
# remove the current, outdated migration | |
rm $app/migrations/${current_migration}.* | |
# create a new migration | |
./manage.py makemigrations $app | |
# migrate the DB to the new migration | |
./manage.py migrate $app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment