@@ -130,53 +130,74 @@ jobs:
130130 pr_no : ${{ github.event.pull_request.number }}
131131 GH_TOKEN : ${{ github.token }}
132132 run : |
133- echo "GitHub Token: $GH_TOKEN"
134- git fetch origin main
135- git diff origin/main...HEAD --name-only > mydiff
136- echo "Changed SQL files:"
137- cat mydiff
138- file_list=$(git diff origin/main...HEAD --name-only)
139- awk '/scripts\/sql\//' mydiff
140- count=$(awk '/scripts\/sql\//' mydiff | wc -l)
141- if [[ $count == "0" ]] ; then
142- echo "No Sql file is added .exiting from this action"
143- exit 0
133+ echo "GitHub Token: $GH_TOKEN"
134+
135+ # Fetch the latest changes from the main branch
136+ git fetch origin main
137+ # Get the list of changed files
138+ git diff origin/main...HEAD --name-only > mydiff
139+
140+ echo "Changed files:"
141+ cat mydiff
142+
143+ echo "Changed SQL files:"
144+
145+ # Filter SQL files from the list of changed files
146+ awk '/scripts\/sql\//' mydiff
147+
148+ # Count the number of changed SQL files
149+ count=$(awk '/scripts\/sql\//' mydiff | wc -l)
150+
151+ if [[ $count == "0" ]]; then
152+ echo "No SQL files were added. Exiting from this action."
153+ exit 0
154+ fi
155+
156+ # Iterate through each changed SQL file
157+ for filename in $(awk '/scripts\/sql\//' mydiff); do
158+ echo "Checking File: $filename"
159+
160+ # Check if the SQL file name is in the correct format
161+ if [[ "$filename" =~ \.(up|down)\.sql$ ]]; then
162+ echo "File name is in the correct format: $filename"
163+ else
164+ echo "Error: The SQL file name is not in the correct format: $filename."
165+ gh pr comment $pr_no --body "Error: The SQL file name is not in the correct format: $filename."
166+ exit 1
144167 fi
145- for filename in $file_list; do
146- echo "Checking File: $filename"
147-
148- if [[ "$filename" =~ \.(up|down)\.sql$ ]]; then
149- echo "File name is in the correct format: $filename"
150- else
151- echo "Error: File name is not in the correct format: $filename."
152- gh pr comment $pr_no --body "Error: The SQL file name is not in the correct format: $filename."
153- exit 1
154- fi
155- echo "Current directory: $(pwd)"
156-
157- cd scripts/sql/
158- echo "SQL files directory: $(pwd)"
159-
160- migration_no=$(echo $filename | cut -d "/" -f 3 | cut -d "_" -f 1)
161- echo "Migration Number: $migration_no"
162-
163- migration_files_of_this_no=$(ls | cut -d "_" -f 1 | grep -w -c $migration_no )
164- cd ../../
165- echo "Current directory: $(pwd)"
166-
167- echo "Migration Number: $migration_no"
168- echo "Number of files with this migration number: $migration_files_of_this_no"
169-
170- if [[ $migration_files_of_this_no == "2" ]] ; then
171- echo "All looks good for this migration number."
172- elif [[ $migration_files_of_this_no == "1" ]] ; then
173- echo "Not Looks good . only $filename this file present."
174- echo "Only single migration file was present for this No- $migration_no"
175- gh pr comment $pr_no --body "Error: Only Single migration file was present for this no -: $migration_no."
176- exit 1
177- else
178- echo "Error: Migration number is repeated."
179- gh pr comment $pr_no --body "Error: The SQL file number is duplicated: $migration_no."
180- exit 1
181- fi
182- done
168+
169+ # Navigate to the SQL files directory
170+ sql_dir="scripts/sql"
171+ echo "Current directory: $(pwd)"
172+ cd "$sql_dir"
173+ echo "SQL files directory: $(pwd)"
174+
175+ # Extract the migration number from the SQL file name
176+ migration_no=$(echo "$filename" | cut -d "/" -f 3 | cut -d "_" -f 1)
177+ echo "Migration Number: $migration_no"
178+
179+ # Count the number of files with the same migration number
180+ migration_files_present_of_this_no=$(ls | cut -d "_" -f 1 | grep -w -c "$migration_no")
181+
182+ # Navigate back to the original directory
183+ cd -
184+
185+ echo "Current directory: $(pwd)"
186+ echo "Migration Number: $migration_no"
187+ echo "Number of files with this migration number: $migration_files_present_of_this_no"
188+
189+ # Check the conditions based on the number of files with the same migration number
190+ if [[ $migration_files_present_of_this_no == "2" ]]; then
191+ echo "All looks good for this migration number."
192+ elif [[ $migration_files_present_of_this_no == "1" ]]; then
193+ echo "Issue detected: Only $filename is present for this migration number."
194+ echo "A single migration file was expected for this No-$migration_no"
195+ gh pr comment $pr_no --body "Error: Only a single migration file was present for this number: $migration_no."
196+ exit 1
197+ else
198+ echo "Error: Migration number is repeated."
199+ gh pr comment $pr_no --body "Error: The SQL file number is duplicated: $migration_no."
200+ exit 1
201+ fi
202+ done
203+
0 commit comments