Browse Source

Add script for adding a migration

master
Alex Mikhalev 4 years ago
parent
commit
6361ebbe76
  1. 64
      scripts/add_migration.sh
  2. 1
      src/db.rs

64
scripts/add_migration.sh

@ -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"

1
src/db.rs

@ -14,5 +14,6 @@ pub fn create_migrations() -> Migrations {
let mut migs = Migrations::new(); let mut migs = Migrations::new();
migs.add(include_file_migration!(1, "0001-table_sections")); migs.add(include_file_migration!(1, "0001-table_sections"));
migs.add(include_file_migration!(2, "0002-section_rows")); migs.add(include_file_migration!(2, "0002-section_rows"));
// INSERT MIGRATION ABOVE -- DO NOT EDIT THIS COMMENT
migs migs
} }

Loading…
Cancel
Save