Skip to content

Commit

Permalink
fix_: fix migration script check when multiple files are added in a s…
Browse files Browse the repository at this point in the history
…ingle branch
  • Loading branch information
dlipicar committed Feb 3, 2025
1 parent e109ec1 commit 7c849a8
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions _assets/scripts/migration_check.sh
Original file line number Diff line number Diff line change
@@ -1,48 +1,46 @@
#!/usr/bin/env bash

set -e
set -o pipefail
set -euo pipefail

source _assets/scripts/colors.sh

check_migration_order() {
local prev_migration=""
for file in "$@"; do
current_migration=$(echo "$file" | cut -d'-' -f1)
current_migration=$(basename "$file")

if [[ ! -z "$prev_migration" && "$current_migration" < "$prev_migration" ]]; then
echo "migration ${current_migration} is not in order with ${prev_migration}"
echo "Error: Migration files are out of order. Please ensure migrations are added in chronological order."
echo "${YLW}migration ${RST}${current_migration} ${YLW}is not in order with ${RST}${prev_migration}"
echo "${YLW}Error: Migration files are out of order. Please ensure migrations are added in chronological order."
exit 1
fi

prev_migration="$current_migration"
done
}

git checkout origin/develop
git pull origin develop
git checkout -
committed_files=$(git ls-tree -r --name-only HEAD protocol/migrations/sqlite/*.sql | sort)
staged_files=$(git diff --name-only origin/develop protocol/migrations/sqlite/*.sql | sort)

all_files=$(echo -e "$committed_files\n$staged_files")
BASE_BRANCH=${BASE_BRANCH:-develop}
BASE_COMMIT=${1:-origin/${BASE_BRANCH}}

# protocol migrations
check_migration_order $all_files
MIGRATION_DIRS=( \
"protocol/migrations/sqlite" \
"appdatabase/migrations/sql" \
"protocol/encryption/migrations/sqlite" \
"walletdatabase/migrations/sql" \
)

committed_files=$(git ls-tree -r --name-only HEAD appdatabase/migrations/sql/*.sql | sort)
staged_files=$(git diff --name-only origin/develop appdatabase/migrations/sql/*.sql | sort)

all_files=$(echo -e "$committed_files\n$staged_files")

# account migrations
check_migration_order $all_files
git checkout ${BASE_COMMIT}
git pull origin ${BASE_BRANCH}
git checkout -

committed_files=$(git ls-tree -r --name-only HEAD protocol/encryption/migrations/sqlite/*.sql | sort)
staged_files=$(git diff --name-only origin/develop protocol/encryption/migrations/sqlite/*.sql | sort)
for MIGRATION_DIR in ${MIGRATION_DIRS[@]}; do
echo -e "${GRN}Checking migrations:${RST} ${MIGRATION_DIR}"

all_files=$(echo -e "$committed_files\n$staged_files")
base_files=$(git ls-tree -r --name-only ${BASE_COMMIT} ${MIGRATION_DIR}/*.sql | sort)
new_files=$(git diff --name-only ${BASE_COMMIT} ${MIGRATION_DIR}/*.sql | sort)
all_files=$(echo -e "$base_files\n$new_files")

# encryption migrations
check_migration_order $all_files
check_migration_order $all_files
done

exit 0

0 comments on commit 7c849a8

Please sign in to comment.