|
10 | 10 | branches:
|
11 | 11 | - 'main'
|
12 | 12 | - 'release-**'
|
| 13 | + - 'develop' |
13 | 14 | # paths-ignore:
|
14 | 15 | # - 'docs/**'
|
15 | 16 | # - '.github/'
|
|
42 | 43 | PRNUM: ${{ github.event.pull_request.number }}
|
43 | 44 | TITLE: ${{ github.event.pull_request.title }}
|
44 | 45 | run: |
|
45 |
| - set -x |
46 | 46 | echo "base or target repo : ${{ github.event.pull_request.base.repo.full_name }}"
|
47 | 47 | echo "head or source repo : ${{ github.event.pull_request.head.repo.full_name }}"
|
48 | 48 | if [[ ${{ github.event.pull_request.head.repo.full_name }} == ${{ github.event.pull_request.base.repo.full_name }} ]]; then
|
@@ -169,78 +169,105 @@ jobs:
|
169 | 169 | pr_no: ${{ github.event.pull_request.number }}
|
170 | 170 | GH_TOKEN: ${{ github.token }}
|
171 | 171 | run: |
|
172 |
| - |
173 | 172 | # Fetch the latest changes from the main branch
|
174 | 173 | git fetch origin main
|
175 |
| -
|
| 174 | + |
176 | 175 | # Get the list of changed files
|
177 | 176 | git diff origin/main...HEAD --name-only > diff
|
178 |
| -
|
| 177 | + |
| 178 | + # Specify the directory containing migration files |
| 179 | + MIGRATION_DIR="scripts/sql" |
| 180 | + ls |
| 181 | + pwd |
| 182 | + |
| 183 | + # Print changed files |
179 | 184 | echo "Changed files:"
|
180 | 185 | cat diff
|
181 |
| -
|
182 |
| - echo "Changed SQL files-:" |
183 |
| - # Filter SQL files from the list of changed files |
184 |
| - awk '/scripts\/sql\//' diff |
185 |
| -
|
186 |
| - # Count the number of changed SQL files in the 'scripts/sql' directory |
187 |
| - count=$(awk '/scripts\/sql\//' diff | wc -l) |
188 |
| -
|
189 |
| - # Check if no SQL files were changed |
190 |
| - if [[ $count == "0" ]]; then |
191 |
| - echo "No SQL files were added, Exiting from this action." |
192 |
| - exit 0 |
193 |
| - fi |
194 |
| -
|
195 |
| - # Iterate through each changed SQL file |
196 |
| - for filename in $(awk '/scripts\/sql\//' diff); do |
197 |
| - echo "Checking File: $filename" |
| 186 | + |
| 187 | + changed_files="" |
| 188 | + while IFS= read -r file; do |
| 189 | + if [[ $file == $MIGRATION_DIR/* && $file == *.up.sql ]]; then |
| 190 | + changed_files+="$file\n" |
| 191 | + fi |
| 192 | + done < diff |
| 193 | + |
| 194 | + # Print the filtered .up.sql files |
| 195 | + echo "Filtered .up.sql files:" |
| 196 | + echo -e "$changed_files" |
| 197 | + |
| 198 | + # Check if there are any .up.sql migration files in the changed files list |
| 199 | + if [ -z "$changed_files" ]; then |
| 200 | + echo "No .up.sql migration files found in the changes." |
| 201 | + else |
| 202 | + # Extract unique migration numbers from the directory (considering only .up.sql files) |
| 203 | + existing_migrations=$(ls $MIGRATION_DIR | grep -E "\.up\.sql$" | grep -oE "[0-9]{3}[0-9]{3}[0-9]{2}" | sort | uniq) |
198 | 204 |
|
199 |
| - # Check if the SQL file name is in the correct format (i.e., it ends with either '.up.sql' or '.down.sql') |
200 |
| - if [[ "$filename" =~ \.(up|down)\.sql$ ]]; then |
201 |
| -
|
202 |
| - # Print a message that the file name is in the correct format |
203 |
| - echo "File name: $filename is in the correct format" |
204 |
| - else |
205 |
| - # Print an error message |
206 |
| - echo "Error: The SQL file name is not in the correct format: $filename." |
207 |
| -
|
208 |
| - # Post a comment on a GitHub pull request with the error message |
209 |
| - gh pr comment $pr_no --body "The SQL file name: $filename is not in the correct format." |
210 |
| -
|
211 |
| - # Exit the script with a non-zero status code |
212 |
| - exit 1 |
213 |
| - fi |
214 |
| -
|
215 |
| - # Navigate to the SQL files directory |
216 |
| - sql_dir="scripts/sql" |
217 |
| - echo "Current directory: $(pwd)" |
218 |
| - cd "$sql_dir" |
219 |
| - echo "SQL files directory: $(pwd)" |
| 205 | + # Exclude migration numbers from changed files in existing_migrations |
| 206 | + while read -r file; do |
| 207 | + migration_number=$(basename "$file" | grep -oE "[0-9]{3}[0-9]{3}[0-9]{2}") |
| 208 | + existing_migrations=$(echo "$existing_migrations" | grep -v "$migration_number") |
| 209 | + done <<< "$changed_files" |
220 | 210 |
|
221 |
| - # Extract the migration number from the SQL file name |
222 |
| - migration_no=$(echo "$filename" | cut -d "/" -f 3 | cut -d "_" -f 1) |
223 |
| - echo "Migration Number: $migration_no" |
| 211 | + # Validate each changed .up.sql migration file |
| 212 | + is_valid=true |
| 213 | + processed_migrations=() |
| 214 | + while read -r file; do |
| 215 | + # Extract migration number from the filename |
| 216 | + migration_number=$(basename "$file" | grep -oE "[0-9]{3}[0-9]{3}[0-9]{2}") |
224 | 217 |
|
225 |
| - # Count the number of files with the same migration number |
226 |
| - migration_files_present_of_this_no=$(ls | cut -d "_" -f 1 | grep -w -c "$migration_no") |
227 |
| -
|
228 |
| - # Navigate back to the original directory |
229 |
| - cd ../.. |
| 218 | + # Check if the filename has the full XXXPPPNN format |
| 219 | + if [[ ! $(basename "$file") =~ ^[0-9]{3}[0-9]{3}[0-9]{2}_ ]]; then |
| 220 | + echo "Error: Migration file $file does not have the complete XXXPPPNN format." |
| 221 | + is_valid=false |
| 222 | + continue |
| 223 | + fi |
230 | 224 |
|
231 |
| - # Check the conditions based on the number of files with the same migration number |
232 |
| - if [[ $migration_files_present_of_this_no == "2" ]]; then |
233 |
| - echo "All looks good for this migration number." |
234 |
| - elif [[ $migration_files_present_of_this_no == "1" ]]; then |
235 |
| - # Only one file is present for this migration number |
236 |
| - echo "Only single migration file was present for migration no.: $migration_no. either up or down migration is missing! EXITING" |
237 |
| - gh pr comment $pr_no --body "Error: Only a single migration file was present for this number: $migration_no." |
238 |
| - exit 1 |
239 |
| - else |
240 |
| - # Migration number is repeated |
241 |
| - echo "Error: Migration number is repeated." |
242 |
| - gh pr comment $pr_no --body "Error: The SQL file number: $migration_no is duplicated" |
| 225 | + if [ -z "$migration_number" ]; then |
| 226 | + echo "Warning: Could not extract migration number from $file." |
| 227 | + continue |
| 228 | + fi |
| 229 | + |
| 230 | + # Check if this migration number has already been processed |
| 231 | + if [[ " ${processed_migrations[@]} " =~ " $migration_number " ]]; then |
| 232 | + continue |
| 233 | + fi |
| 234 | + processed_migrations+=("$migration_number") |
| 235 | + |
| 236 | + # Check if the migration number is unique |
| 237 | + if echo "$existing_migrations" | grep -q "$migration_number"; then |
| 238 | + echo "Error: Migration number $migration_number already exists." |
| 239 | + is_valid=false |
| 240 | + fi |
| 241 | + |
| 242 | + # Check if the migration number is greater than previous ones |
| 243 | + last_migration=$(echo "$existing_migrations" | tail -n 1) |
| 244 | + if [ "$migration_number" -le "$last_migration" ]; then |
| 245 | + echo "Error: Migration number $migration_number is not greater than the latest ($last_migration)." |
| 246 | + is_valid=false |
| 247 | + fi |
| 248 | + |
| 249 | + # Check for sequential hotfix requirement (if NN > 01, check for NN-1) |
| 250 | + hotfix_number=$(echo "$migration_number" | grep -oE "[0-9]{2}$") |
| 251 | + if [ "$hotfix_number" -gt "01" ]; then |
| 252 | + previous_hotfix=$(printf "%02d" $((10#$hotfix_number - 1))) |
| 253 | + expected_previous_number="${migration_number:0:6}$previous_hotfix" |
| 254 | + if ! echo "$existing_migrations" | grep -q "$expected_previous_number"; then |
| 255 | + echo "Error: Previous hotfix migration $expected_previous_number not found for $migration_number." |
| 256 | + is_valid=false |
| 257 | + fi |
| 258 | + fi |
| 259 | + |
| 260 | + done <<< "$changed_files" |
| 261 | + |
| 262 | + if [ "$is_valid" = false ]; then |
| 263 | + echo "Validation failed. Please fix the errors before merging." |
| 264 | + gh pr comment $pr_no --body "The Migration files providede inside of the PR does not pass the criteria!!" |
243 | 265 | exit 1
|
244 | 266 | fi
|
245 |
| - done |
| 267 | + |
| 268 | + echo "All .up.sql migration file validations passed." |
| 269 | + gh pr comment $pr_no --body "The migration files have successfully passed the criteria!!" |
| 270 | + fi |
| 271 | + |
| 272 | + |
246 | 273 |
|
0 commit comments