Skip to content

Instantly share code, notes, and snippets.

@danielmackay
Created May 17, 2024 07:01
Show Gist options
  • Save danielmackay/ed06708d14a14c8a3e77512df5fd71fb to your computer and use it in GitHub Desktop.
Save danielmackay/ed06708d14a14c8a3e77512df5fd71fb to your computer and use it in GitHub Desktop.
Up PowerShell Script - Docker Compose, DB Creation, Migration, and Seeding
# Originally Created by William Liebenberg
Param(
[switch]$skipDeploy = $false,
[string]$apiDbConnectionString = "Server=.,2100;Database=MoMo;User Id=sa;Password=Password!##3;MultipleActiveResultSets=true;TrustServerCertificate=True;"
)
$upScriptPath = $Script:MyInvocation.MyCommand.Path | Split-Path
$srcPath = Join-Path -Path $upScriptPath -ChildPath "src"
Write-Host "🚢 Starting Docker Compose"
docker compose up -d
if (-not $skipDeploy) {
Write-Host "⚙️ Restore dotnet tools"
dotnet tool restore
Set-Location $srcPath
$projects = @()
$projects += @{
ProjectName = "Timesheet Module"
ProjectFileName = "Timesheets.Infrastructure"
ProjectPath = Join-Path -Path "Modules" -ChildPath "Timesheets" | Join-Path -ChildPath "Timesheets.Infrastructure"
DBContext = "TimesheetDbContext"
StartUpProject = "MoMo.Api"
ConnectionString = "$apiDbConnectionString"
}
foreach ($project in $projects) {
$projectPath = Join-Path -Path $srcPath -ChildPath $project.ProjectPath
Write-Host
Write-Host "Project Path: $($projectPath)" -ForegroundColor Green
$projectFile = Join-Path -Path $projectPath -ChildPath "$($project.ProjectFileName).csproj"
Write-Host
Write-Host "Project File: $($projectFile)" -ForegroundColor Green
Write-Host
Write-Host "StartUpProject Path: $($project.StartUpProject)" -ForegroundColor Green
$platform = 'win-x64'
$bundleExe = Join-Path -Path $projectPath -ChildPath "$($project.DBContext)_MigrationBundle.exe"
$windows = $env:OS -eq "Windows_NT"
if (-not $windows) {
$platform = 'linux-x64'
$bundleExe = Join-Path -Path $projectPath -ChildPath "$($project.DBContext)_MigrationBundle"
}
Write-Host
Write-Host "Bundle File: $($bundleExe)" -ForegroundColor Green
Write-Host
Write-Host "⚙️ Building $($project.ProjectName) Database $($project.DBContext) Bundle" -ForegroundColor Green
dotnet restore $projectFile --runtime $platform
Write-Host
Write-Host "⚙️ Generating Migration Bundle" -ForegroundColor Green
dotnet ef migrations bundle --project $projectFile --startup-project "$($project.StartUpProject)" --verbose --force --context "$($project.DBContext)" --output $bundleExe
Write-Host
Write-Host "🚀 Deploying MoMo Database $($project.DBContext) Bundle ($($bundleExe))" -ForegroundColor Green
$connectionStringArg = '--connection "' + $project.ConnectionString + '"'
Start-Process -WorkingDirectory $projectPath -NoNewWindow -Wait -FilePath $bundleExe -ArgumentList $connectionStringArg
}
Set-Location $upScriptPath
}
@danielmackay
Copy link
Author

Originally created by @william-liebenberg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment