You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.5 KiB
66 lines
1.5 KiB
4 years ago
|
#!/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
|
||
|
|
||
4 years ago
|
MIGRATIONS_DIR="$PROJECT_ROOT/sprinklers_database/src/migrations"
|
||
4 years ago
|
|
||
|
# 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
|
||
4 years ago
|
MIGRATION_NUMBERS=("${SQL_FILES[@]%%-*.sql}")
|
||
4 years ago
|
|
||
|
# 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
|
||
|
|
||
4 years ago
|
LINE_TO_INSERT="\ \ \ \ migs.add(include_file_migration!($NEXT_MIGRATION, \"$NEXT_MIGRATION_PREFIX-$MIGRATION_NAME\"));"
|
||
4 years ago
|
|
||
4 years ago
|
MIGRATIONS_RS="$PROJECT_ROOT/sprinklers_database/src/migrations/mod.rs"
|
||
4 years ago
|
echo "Inserting line in $MIGRATIONS_RS"
|
||
4 years ago
|
sed -i "/INSERT MIGRATION ABOVE/i \
|
||
|
$LINE_TO_INSERT" "$MIGRATIONS_RS"
|