diff --git a/.github/workflows/check-migration-file-edited.yml b/.github/workflows/check-migration-file-edited.yml new file mode 100644 index 0000000000..e94a573c61 --- /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 "Exiting migration files cannot be modified." + 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}`,