Skip to content

re-redo #4592

re-redo #4592 #115

name: Directory API - Apply Data Changes
on:
pull_request:
branches:
- main
paths:
- .github/workflows/directory-migrate.yml
- services/Directory/data/**
- services/Directory/FilterLists.Directory.Infrastructure.Migrations/**
jobs:
add_migration:
name: Add EF Core Migration
runs-on: ubuntu-latest
permissions:
contents: write
env:
USER_NAME: github-actions[bot]
USER_EMAIL: github-actions[bot]@users.noreply.github.com
LINT_MESSAGE: "chore(dir-data): 🎨 lint PR #${{ github.event.number }}"
MIGRATION_MESSAGE: "chore(dir-data): ♻️ migrate PR #${{ github.event.number }}"
TEST_MSSQL_IMAGE: mcr.microsoft.com/mssql/server:2022-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
repository: ${{ github.event.pull_request.head.repo.full_name }}
ref: ${{ github.event.pull_request.head.ref }}
fetch-depth: 50 # guess max depth for data migration branch to avoid fetching all while still allowing sync w/upstream
- name: Lint
run: ./lint.sh
working-directory: ./services/Directory/data
- name: git config
run: |
git config --global user.name "$USER_NAME"
git config --global user.email "$USER_EMAIL"
- name: git commit
run: |
LINT_PENDING_CHANGES=$(git status -s -- .)
if [ -n "$LINT_PENDING_CHANGES" ]; then
git commit -am "$LINT_MESSAGE"
fi
- name: Sync with Upstream
run: |
git remote add upstream https://github.com/${{ github.repository }}.git
git fetch upstream
git merge upstream/${{ github.event.pull_request.base.ref }}
- name: Revert Any Previous Migration for PR
run: |
PREVIOUS_MIGRATION_HASH=$(git log --grep="$MIGRATION_MESSAGE" --pretty=format:"%H" -n 1)
if [ -n "$PREVIOUS_MIGRATION_HASH" ]; then
git revert --no-edit $PREVIOUS_MIGRATION_HASH
fi
# Disabled GH Actions cache for Dependabot. https://github.com/dependabot/dependabot-core/issues/1303
# - name: Setup .NET
# uses: actions/setup-dotnet@v4
# with:
# cache-dependency-path: services/Directory/FilterLists.Directory.Api/packages.lock.json
# cache: true
- name: Install dotnet-ef
run: dotnet tool install --global dotnet-ef
# - name: Restore
# run: dotnet restore --locked-mode
# working-directory: ./services/Directory/FilterLists.Directory.Api
- name: Add Migration
run: >
dotnet ef migrations add ${{ github.event.number }}
-p FilterLists.Directory.Infrastructure.Migrations
-s FilterLists.Directory.Api
working-directory: ./services/Directory
env:
DOTNET_RUNNING_EF_CORE_TOOLS: true
- name: Commit Effective Migration
run: |
MIGRATION_CHANGED_FILE_COUNT=$(git status -s -- . | wc -l)
if [ "$MIGRATION_CHANGED_FILE_COUNT" -eq 3 ]; then
git add .
git commit -m "$MIGRATION_MESSAGE"
else
git clean -fd
fi
- name: Run Test SQL Server
run: >
docker run --name sql-server
-e 'ACCEPT_EULA=Y'
-e 'SA_PASSWORD=Your_password123'
-p 1433:1433
-d $TEST_MSSQL_IMAGE
- name: Wait for Test SQL Server
run: |
echo "Waiting for SQL Server to start..."
for i in {1..30}; do
if docker exec sql-server /opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P Your_password123 -Q "SELECT 1" &> /dev/null; then
echo "SQL Server is up and running."
exit 0
fi
echo "Waiting..."
sleep 2
done
echo "SQL Server did not start in time."
exit 1
- name: Test Migrations
run: >
dotnet ef database update
--connection "Server=localhost,1433;Database=DirectoryDb;User Id=sa;Password=Your_password123;TrustServerCertificate=True;"
-p FilterLists.Directory.Infrastructure.Migrations
-s FilterLists.Directory.Api
working-directory: ./services/Directory
- name: git push
run: git push