Skip to content

Commit

Permalink
Migration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Simyon264 committed Aug 26, 2024
1 parent d2bd94f commit f9b8935
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
45 changes: 42 additions & 3 deletions .github/workflows/build_and_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
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
23 changes: 23 additions & 0 deletions Tools/check_model_pending_changes.py
Original file line number Diff line number Diff line change
@@ -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!')

0 comments on commit f9b8935

Please sign in to comment.