From f9b893520258397b38fad61f9a65d0edb6af71a5 Mon Sep 17 00:00:00 2001 From: Simon <63975668+Simyon264@users.noreply.github.com> Date: Mon, 26 Aug 2024 02:22:33 +0200 Subject: [PATCH] Migration tests --- .github/workflows/build_and_test.yml | 45 ++++++++++++++++++++++++++-- Tools/check_model_pending_changes.py | 23 ++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 Tools/check_model_pending_changes.py diff --git a/.github/workflows/build_and_test.yml b/.github/workflows/build_and_test.yml index f9d0cce..4de970f 100644 --- a/.github/workflows/build_and_test.yml +++ b/.github/workflows/build_and_test.yml @@ -6,11 +6,28 @@ on: jobs: build: runs-on: ubuntu-latest - + env: Solution_Name: ReplayBrowser.sln - + + services: + postgres: + image: postgres:latest + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: ReplayBrowser + ports: + - 5432:5432 + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + steps: + # SETUP + - name: Checkout uses: actions/checkout@v3 @@ -22,8 +39,30 @@ jobs: - name: Verify .NET Core version run: dotnet --version + - name: Install dotnet-ef + run: dotnet tool install --global dotnet-ef + + - name: Verify dotnet-ef version + run: dotnet ef --version + + - name: Setup Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Restore NuGet Packages run: dotnet restore ./ReplayBrowser/ReplayBrowser.csproj + - name: Write appsettings.Secret.json file # This file is used to store the connection string for the database + run: echo "{\"ConnectionStrings\":{\"DefaultConnection\":\"Host=postgres;Port=5432;Database=ReplayBrowser;Username=postgres;Password=postgres\"}}" > ./ReplayBrowser/appsettings.Secret.json + + # BUILD AND TEST + - name: Build Solution - run: dotnet build ./ReplayBrowser/ReplayBrowser.csproj \ No newline at end of file + run: dotnet build ./ReplayBrowser/ReplayBrowser.csproj + + - name: Check pending migrations + run: python ./Tools/check_model_pending_changes.py # Exits with 1 if there are pending migrations + + - name: Run Migrations + run: dotnet ef database update --project ./ReplayBrowser/ReplayBrowser.csproj \ No newline at end of file diff --git a/Tools/check_model_pending_changes.py b/Tools/check_model_pending_changes.py new file mode 100644 index 0000000..539c971 --- /dev/null +++ b/Tools/check_model_pending_changes.py @@ -0,0 +1,23 @@ +# This script checks if there are any pending changes in the model and if so, it will fail. +# This is useful to prevent committing changes to the model that are not intended. +# It uses "dotnet ef migrations has-pending-model-changes" +# If it exits with "No changes have been made to the model since the last migration.", it means there are no pending changes. Hooraay! +# If anything else, it means there are pending changes and the script will fail. Booo! +# Note: The command will output some other output as well, but we are only interested in the message above. + +import subprocess +import sys + +def main(): + command = ['dotnet', 'ef', 'migrations', 'has-pending-model-changes'] + result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + if result.returncode != 0: + print(f'Error: {result.stderr.decode("utf-8")}') + sys.exit(1) + + output = result.stdout.decode("utf-8") + if 'No changes have been made to the model since the last migration.' not in output: + print(f'Error: There are pending changes in the model.') + sys.exit(1) + + print('No pending changes in the model. Good to go!') \ No newline at end of file