Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get database deployments working #136

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
86deafd
Removed automated deployments on startup, added section to readme
noremacskich Aug 17, 2024
0f911f5
Created Github Action to Deploy DB Changes
noremacskich Aug 17, 2024
ad6b8a2
Addressed Linter Errors, Tweaked Workflow itself
noremacskich Aug 17, 2024
5924a77
Another shot at getting the ef tools working
noremacskich Aug 17, 2024
7bbda39
Fixed remove command, Fixed Casing on Secrets
noremacskich Aug 17, 2024
0ad5a11
Fixed typo
noremacskich Aug 17, 2024
b55f4f0
Fixed Action Lint findings
noremacskich Aug 17, 2024
e8c0793
Fixed Remove IP from Firewall run
noremacskich Aug 17, 2024
9b5a874
Pipeline says I should use --yes
noremacskich Aug 17, 2024
c8b103e
Realized I needed flexible-server, not server in commands
noremacskich Aug 17, 2024
f68ee24
Fixed server parameter
noremacskich Aug 17, 2024
9e8b633
After consulting docs, got correct parameter for db name
noremacskich Aug 17, 2024
d3b00da
Fixed rulename parameter
noremacskich Aug 17, 2024
abd615f
Switching to the Access Token method for password
noremacskich Aug 18, 2024
5613235
Trying the connection string from in app
noremacskich Sep 4, 2024
1c04f42
Merge branch 'main' into GetDatabaseDeploymentsWorking
noremacskich Sep 4, 2024
61fbe60
Set dummy connection string for efcore bundle
noremacskich Sep 4, 2024
b059dfa
Fixed config issue
noremacskich Sep 4, 2024
77cebb3
Got a proper name on the environment variable
noremacskich Sep 4, 2024
0d0a743
Should now be getting the token correctly
noremacskich Sep 4, 2024
176ab21
Trying new connection string
noremacskich Sep 4, 2024
bc389f7
Forgot to add in SSL mode
noremacskich Sep 4, 2024
677843e
Trying different token strategy
noremacskich Sep 4, 2024
8d18ab2
Trying direct call on connection
noremacskich Sep 4, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions .github/workflows/DeployDatabaseChanges.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---

name: Deploy Database Changes

on:
push:
branches: ["main"]
paths:
- 'api/ExpressedRealms.DB/Migrations/**'
pull_request:
branches: ["main"]

jobs:
build:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./api
permissions:
packages: write
contents: read
attestations: write
id-token: write
steps:
- uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 8.0.x

- name: Restore dependencies
run: dotnet restore

- name: Build
run: dotnet build --no-restore

- name: Install dotnet-ef tool
run: dotnet tool install --global dotnet-ef

- name: Add dotnet tools to PATH
run: echo "$HOME/.dotnet/tools" >> "$GITHUB_PATH"

- name: Restore Tools
run: dotnet tool restore

- name: Build Bundle
run: dotnet ef migrations bundle --self-contained --project ExpressedRealms.DB --startup-project ExpressedRealms.Server
env:
ConnectionStrings__DefaultConnection: "Foo"

- name: Get Public IP
id: ip
uses: haythem/public-ip@v1.3

- name: Print Public IP
run: |
echo ${{ steps.ip.outputs.ipv4 }}
echo ${{ steps.ip.outputs.ipv6 }}

- name: Azure Login
uses: azure/login@v1
with:
client-id: ${{ secrets.EXPRESSEDREALMS_DATABASE_AZURE_CLIENT_ID }}
tenant-id: ${{ secrets.EXPRESSEDREALMS_DATABASE_AZURE_TENANT_ID }}
subscription-id: ${{ secrets.EXPRESSEDREALMS_DATABASE_AZURE_SUBSCRIPTION_ID }}

- name: Add IP to Firewall
run: az postgres flexible-server firewall-rule create --resource-group "${RESOURCE_GROUP}" --name "${SQL_SERVER}" --rule-name GitHubIP --start-ip-address "${IP_ADDRESS}" --end-ip-address "${IP_ADDRESS}"
env:
SQL_SERVER: ${{ secrets.POSTGRES_SERVER_NAME }}
IP_ADDRESS: ${{ steps.ip.outputs.ipv4 }}
RESOURCE_GROUP: ${{ secrets.POSTGRES_RESOURCE_GROUP }}

- name: Get Access Token
id: get_token
run: |
token=$(az account get-access-token --resource https://ossrdbms-aad.database.windows.net/ --query accessToken -o tsv)
echo "TOKEN=$token" >> $GITHUB_ENV

- name: Run Migrations
run: ./efbundle --connection "${{secrets.DB_DEPLOYMENT_CONNECTION_STRING}};Password=${{ env.TOKEN}};Ssl Mode=Require;"
working-directory: ./api

- name: Remove IP from Firewall
if: always()
run: az postgres flexible-server firewall-rule delete --resource-group "${RESOURCE_GROUP}" --name "${SQL_SERVER}" --rule-name GitHubIP --yes
env:
SQL_SERVER: ${{ secrets.POSTGRES_SERVER_NAME }}
RESOURCE_GROUP: ${{ secrets.POSTGRES_RESOURCE_GROUP }}
11 changes: 11 additions & 0 deletions api/ExpressedRealms.DB/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@ the application has. See the CharacterConfiguration class for an example.
* [Seeding](https://code-maze.com/migrations-and-seed-data-efcore/)
* [Type Configuration](https://stackoverflow.com/questions/46978332/use-ientitytypeconfiguration-with-a-base-entity)


## Applying Migrations Locally
Create the efbundle.exe:
```shell
dotnet ef migrations bundle --self-contained --project ExpressedRealms.DB --startup-project ExpressedRealms.Server
```

Run the bundle
```shell
./efbundle --connection="Host=localhost:5432;Port=5432;Database=expressedRealms;Username=user;Password=password"
```
18 changes: 0 additions & 18 deletions api/ExpressedRealms.Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,6 @@
Log.Information("Building the App");
var app = builder.Build();

// Migrate latest database changes during startup
Log.Information("Checking if Migrations Need to Be Run");
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<ExpressedRealmsDbContext>();

if (dbContext.Database.GetPendingMigrations().Any())
{
Log.Information("DB is missing migrations, running them now");
dbContext.Database.Migrate();
Log.Information("Successfully ran all migrations!");
}
else
{
Log.Information("No Migrations are needed");
}
}

if (app.Environment.IsProduction())
{
Log.Information("Setting Up Forwarded Headers");
Expand Down
Loading