Skip to content

Instantly share code, notes, and snippets.

@ademkocamaz
Forked from adamghill/scorched_earth.sh
Created September 25, 2023 18:08
Show Gist options
  • Save ademkocamaz/41e88e8a74013f8cf0d137e90a82c098 to your computer and use it in GitHub Desktop.
Save ademkocamaz/41e88e8a74013f8cf0d137e90a82c098 to your computer and use it in GitHub Desktop.
Start from scratch for database migrations in Django
#!/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