-
-
Save ademkocamaz/41e88e8a74013f8cf0d137e90a82c098 to your computer and use it in GitHub Desktop.
Start from scratch for database migrations in Django
This file contains hidden or 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/sh | |
set -e | |
die () { | |
echo >&2 "$@" | |
exit 1 | |
} | |
[ "$#" -eq 1 ] || die "Database name argument is required" | |
read -p "Re-create the '$1' database? " -n 1 -r | |
echo | |
echo "Dropping '$1'..." | |
dropdb --if-exists $1 | |
echo "'$1' dropped!" | |
echo | |
echo "Creating '$1'..." | |
createdb $1 | |
echo "'$1' created!" | |
migration_files=`find . -path "*/migrations/*.py" -type f -not -name "__init__.py" -not -path "./.venv/*"` | |
echo | |
echo "All migration files:" | |
echo "$migration_files" | |
echo | |
read -p "Delete migration files listed above? " -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]] | |
then | |
echo "Deleting all migration files..." | |
find . -path "*/migrations/*.py" -type f -not -name "__init__.py" -not -path "./.venv/*" -delete | |
find . -path "*/migrations/*.pyc" -type f -not -name "__init__.py" -not -path "./.venv/*" -delete | |
echo "All migration files deleted!" | |
fi | |
echo | |
echo "Creating new migration files..." | |
poetry run python manage.py makemigrations | |
echo "New migration files created!" | |
echo | |
echo "Migrating the database..." | |
poetry run python manage.py migrate | |
echo "Database has been migrated!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment