Last active
January 22, 2024 08:54
-
-
Save chriscauley/cf0b038d055076a2a30de43526d4150e to your computer and use it in GitHub Desktop.
Django style migrations for alembic
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
#!/usr/bin/env bash | |
# This script gives alembic django style migrations | |
# Revision id is auto-incremented and autogenerate is the default behavior | |
# Alter this line to point to migrations folder | |
NEXT_ID=`ls path/to/db/versions/* | grep -P '/\d{4}_.*\.py'|wc -l` | |
NAME="" | |
FLAG="--autogenerate" | |
function usage() { | |
echo " | |
USAGE: | |
./makemigrations.sh migration_name [-e | --empty] | |
where: | |
migration_name: slug to name migration as | |
-e | --empty: create an empty migration (otherwise autogenerate from python) | |
" | |
} | |
while [ "$1" != "" ]; do | |
case $1 in | |
-e | --empty ) | |
FLAG="" | |
;; | |
-h | --help ) | |
usage | |
exit | |
;; | |
* ) | |
if [ ! -z "$NAME" ] | |
then | |
echo "Name option supplied more than once" >&2 | |
usage >&2 | |
exit 1 | |
else | |
NAME=$1 | |
fi | |
;; | |
esac | |
shift | |
done | |
alembic revision ${FLAG} -m "${NAME}" --rev-id=`printf "%04d" ${NEXT_ID}` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment