diff --git a/.github/resources/rename_migration_files.py b/.github/resources/rename_migration_files.py new file mode 100644 index 000000000..5dd266728 --- /dev/null +++ b/.github/resources/rename_migration_files.py @@ -0,0 +1,26 @@ +import os +from datetime import datetime, timedelta + +def rename_migrations(): + migration_folder = "./backend/src/db/migrations" + with open("added_files.txt", "r") as file: + changed_files = file.readlines() + + # Find the latest file among the changed files + latest_timestamp = datetime.now() # utc time + for file_path in changed_files: + file_path = file_path.strip() + # each new file bump by 1s + latest_timestamp = latest_timestamp + timedelta(seconds=1) + + new_filename = os.path.join(migration_folder, latest_timestamp.strftime("%Y%m%d%H%M%S") + f"_{file_path.split('_')[1]}") + old_filename = os.path.join(migration_folder, file_path) + os.rename(old_filename, new_filename) + print(f"Renamed {old_filename} to {new_filename}") + + if len(changed_files) == 0: + print("No new files added to migration folder") + +if __name__ == "__main__": + rename_migrations() + diff --git a/.github/workflows/update-be-new-migration-latest-timestamp.yml b/.github/workflows/update-be-new-migration-latest-timestamp.yml new file mode 100644 index 000000000..03dbd1177 --- /dev/null +++ b/.github/workflows/update-be-new-migration-latest-timestamp.yml @@ -0,0 +1,39 @@ +name: Rename Migrations + +on: + pull_request: + paths: + - 'backend/src/db/migrations/**' + +jobs: + rename: + runs-on: ubuntu-latest + steps: + - name: Check out repository + uses: actions/checkout@v2 + + - name: Install GH CLI - Local Testing Using Nektos Act + if: ${{ env.ACT }} + run: | + sudo apt update + sudo apt install gh + + - name: Checkout PR + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: gh pr checkout ${{ github.event.pull_request.number }} + + - 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 + + - name: Script to rename migrations + run: python .github/resources/rename_migration_files.py + + - name: Commit and push changes + run: | + git config user.name github-actions + git config user.email github-actions@github.com + git add ./backend/src/db/migrations + git commit -m "chore: renamed new migration files to latest timestamp (gh-action)" + git push +