Alex Mikhalev
4 years ago
2 changed files with 65 additions and 0 deletions
@ -0,0 +1,64 @@
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env bash |
||||
|
||||
shopt -s nullglob extglob |
||||
set -e |
||||
|
||||
usage() { |
||||
echo "Usage: $0 <migration_name>" |
||||
} |
||||
|
||||
SCRIPT=$(realpath "$0") |
||||
SCRIPTPATH=$(dirname "$SCRIPT") |
||||
|
||||
PROJECT_ROOT=$(realpath --relative-to="$PWD" "$SCRIPTPATH/..") |
||||
|
||||
MIGRATION_NAME="$1" |
||||
if [[ -z "$MIGRATION_NAME" ]]; then |
||||
usage |
||||
exit 1 |
||||
fi |
||||
|
||||
MIGRATIONS_DIR="$PROJECT_ROOT/src/migrations" |
||||
|
||||
# echo "MIGRATIONS_DIR: $MIGRATIONS_DIR" |
||||
|
||||
pushd "$MIGRATIONS_DIR" >/dev/null |
||||
SQL_FILES=(+([0-9])*.sql) |
||||
popd >/dev/null |
||||
|
||||
# echo "SQL_FILES: " |
||||
# echo "${SQL_FILES[@]}" |
||||
|
||||
# Remove anything after first numbers |
||||
MIGRATION_NUMBERS=("${SQL_FILES[@]%%*([!0-9])}") |
||||
|
||||
# echo "MIGRATION_NUMBERS: " |
||||
# echo "${MIGRATION_NUMBERS[@]}" |
||||
|
||||
MIGRATION_MAX=0 |
||||
for num in "${MIGRATION_NUMBERS[@]}"; do |
||||
if (( num > MIGRATION_MAX )) |
||||
then |
||||
MIGRATION_MAX=$num |
||||
fi |
||||
done |
||||
|
||||
# echo "MIGRATION_MAX: $MIGRATION_MAX" |
||||
|
||||
NEXT_MIGRATION=$(( MIGRATION_MAX + 1 )) |
||||
NEXT_MIGRATION_PREFIX=$( printf "%04d" "$NEXT_MIGRATION" ) |
||||
|
||||
# echo "NEXT_MIGRATION: $NEXT_MIGRATION" |
||||
# echo "NEXT_MIGRATION_PREFIX: $NEXT_MIGRATION_PREFIX" |
||||
|
||||
for SUFFIX in "up" "down"; do |
||||
migration_file="$MIGRATIONS_DIR/$NEXT_MIGRATION_PREFIX-$MIGRATION_NAME-$SUFFIX.sql" |
||||
echo "Creating migration file $migration_file" |
||||
touch "$migration_file" |
||||
done |
||||
|
||||
LINE_TO_INSERT=" migs.add(include_file_migration!($NEXT_MIGRATION, \"$NEXT_MIGRATION_PREFIX-$MIGRATION_NAME\"));" |
||||
|
||||
MIGRATIONS_RS="$PROJECT_ROOT/src/db.rs" |
||||
echo "Inserting line in $MIGRATIONS_RS" |
||||
sed -i "/INSERT MIGRATION ABOVE/i $LINE_TO_INSERT" "$MIGRATIONS_RS" |
Loading…
Reference in new issue