From 506e86d666d5fe10a0740c018791e151d962c7f3 Mon Sep 17 00:00:00 2001 From: = Date: Fri, 28 Jun 2024 20:33:56 +0530 Subject: [PATCH 1/2] feat: added slugify migration file creater name and additional check to ensure migration files are not editied in PR --- .../workflows/check-migration-file-edited.yml | 25 +++++++++++++++++++ ...date-be-new-migration-latest-timestamp.yml | 6 ++--- backend/scripts/create-migration.ts | 3 ++- 3 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/check-migration-file-edited.yml diff --git a/.github/workflows/check-migration-file-edited.yml b/.github/workflows/check-migration-file-edited.yml new file mode 100644 index 0000000000..b12f8578de --- /dev/null +++ b/.github/workflows/check-migration-file-edited.yml @@ -0,0 +1,25 @@ +name: Check migration file edited + +on: + pull_request: + types: [opened, synchronize] + paths: + - 'backend/src/db/migrations/**' + +jobs: + rename: + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Check any migration files are modified, renamed or duplicated. + run: | + git diff --name-status HEAD^ HEAD backend/src/db/migrations | grep '^M\|^R\|^C' || true | cut -f2 | xargs -r -n1 basename > edited_files.txt + if [ -s edited_files.txt ]; then + echo "Migration files has been edited" + cat edited_files.txt + exit 1 + fi diff --git a/.github/workflows/update-be-new-migration-latest-timestamp.yml b/.github/workflows/update-be-new-migration-latest-timestamp.yml index 684c786541..a98a8ca860 100644 --- a/.github/workflows/update-be-new-migration-latest-timestamp.yml +++ b/.github/workflows/update-be-new-migration-latest-timestamp.yml @@ -19,18 +19,16 @@ jobs: - name: Get list of newly added files in migration folder run: | - git diff --name-status HEAD^ HEAD backend/src/db/migrations | grep '^A' | cut -f2 | xargs -n1 basename > added_files.txt + git diff --name-status HEAD^ HEAD backend/src/db/migrations | grep '^A' || true | cut -f2 | xargs -r -n1 basename > added_files.txt if [ ! -s added_files.txt ]; then echo "No new files added. Skipping" - echo "SKIP_RENAME=true" >> $GITHUB_ENV + exit 0 fi - name: Script to rename migrations - if: env.SKIP_RENAME != 'true' run: python .github/resources/rename_migration_files.py - name: Commit and push changes - if: env.SKIP_RENAME != 'true' run: | git config user.name github-actions git config user.email github-actions@github.com diff --git a/backend/scripts/create-migration.ts b/backend/scripts/create-migration.ts index 59040a37ac..34f4aca419 100644 --- a/backend/scripts/create-migration.ts +++ b/backend/scripts/create-migration.ts @@ -2,13 +2,14 @@ import { execSync } from "child_process"; import path from "path"; import promptSync from "prompt-sync"; +import slugify from "@sindresorhus/slugify" const prompt = promptSync({ sigint: true }); const migrationName = prompt("Enter name for migration: "); // Remove spaces from migration name and replace with hyphens -const formattedMigrationName = migrationName.replace(/\s+/g, "-"); +const formattedMigrationName = slugify(migrationName); execSync( `npx knex migrate:make --knexfile ${path.join(__dirname, "../src/db/knexfile.ts")} -x ts ${formattedMigrationName}`, From 8eec08356b599d897ca73e35adb1b01390d396ad Mon Sep 17 00:00:00 2001 From: Maidul Islam Date: Mon, 1 Jul 2024 20:59:56 -0400 Subject: [PATCH 2/2] update error message --- .github/workflows/check-migration-file-edited.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check-migration-file-edited.yml b/.github/workflows/check-migration-file-edited.yml index b12f8578de..e94a573c61 100644 --- a/.github/workflows/check-migration-file-edited.yml +++ b/.github/workflows/check-migration-file-edited.yml @@ -19,7 +19,7 @@ jobs: run: | git diff --name-status HEAD^ HEAD backend/src/db/migrations | grep '^M\|^R\|^C' || true | cut -f2 | xargs -r -n1 basename > edited_files.txt if [ -s edited_files.txt ]; then - echo "Migration files has been edited" + echo "Exiting migration files cannot be modified." cat edited_files.txt exit 1 fi