Created
September 17, 2025 18:53
-
-
Save Husseinbeygi/48ecf362f70b85068711f33acaffe222 to your computer and use it in GitHub Desktop.
Apply Migration File-based App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #:package [email protected] | |
| #:package [email protected] | |
| using System; | |
| using System.IO; | |
| using System.Linq; | |
| using System.Diagnostics; | |
| using System.Collections.Generic; | |
| Console.WriteLine("=== Apply Migrations started ==="); | |
| var root = Directory.GetCurrentDirectory(); | |
| string startupProject = Environment.GetEnvironmentVariable("STARTUP_PROJECT"); | |
| string connectionString = Environment.GetEnvironmentVariable("CONNECTION_STRING"); | |
| string projectFilter = Environment.GetEnvironmentVariable("PROJECT_FILTER"); | |
| var csprojs = Directory.EnumerateFiles(root, "*.csproj", SearchOption.AllDirectories) | |
| .Where(p => string.IsNullOrWhiteSpace(projectFilter) || | |
| p.Contains(projectFilter, StringComparison.OrdinalIgnoreCase)); | |
| (int exitCode, string stdout, string stderr) Run(string file, string args, string workingDir) | |
| { | |
| var psi = new ProcessStartInfo | |
| { | |
| FileName = file, | |
| Arguments = args, | |
| WorkingDirectory = workingDir, | |
| RedirectStandardOutput = true, | |
| RedirectStandardError = true, | |
| UseShellExecute = false | |
| }; | |
| using var proc = Process.Start(psi)!; | |
| string outp = proc.StandardOutput.ReadToEnd(); | |
| string errp = proc.StandardError.ReadToEnd(); | |
| proc.WaitForExit(); | |
| return (proc.ExitCode, outp, errp); | |
| } | |
| foreach (var proj in csprojs) | |
| { | |
| var projDir = Path.GetDirectoryName(proj)!; | |
| var migrationsDirs = Directory.EnumerateDirectories(projDir, "Migrations", SearchOption.AllDirectories); | |
| if (!migrationsDirs.Any()) | |
| { | |
| Console.WriteLine($"[SKIP] {proj}: no Migrations folder found anywhere under project."); | |
| continue; | |
| } | |
| Console.WriteLine($"[INFO] {proj}: found {migrationsDirs.Count()} Migrations folder(s)."); | |
| Console.WriteLine($"\n--- Project: {proj} ---"); | |
| // list migrations | |
| string listArgs = $"ef migrations list --project {proj}"; | |
| if (!string.IsNullOrWhiteSpace(startupProject)) | |
| listArgs += $" --startup-project {startupProject}"; | |
| if (!string.IsNullOrWhiteSpace(connectionString)) | |
| listArgs += $" --connection {connectionString}"; | |
| Console.WriteLine($"[WARN] Arg list for migrations {listArgs}"); | |
| var listRes = Run("dotnet", listArgs, projDir); | |
| if (listRes.exitCode != 0) | |
| { | |
| Console.WriteLine($"[WARN] migrations list failed for {proj}, stderr:\n{listRes.stderr}"); | |
| continue; | |
| } | |
| if (string.IsNullOrWhiteSpace(listRes.stdout) | |
| || listRes.stdout.Contains("No migrations were found", StringComparison.OrdinalIgnoreCase)) | |
| { | |
| Console.WriteLine($"[SKIP] {proj}: no migrations found in output."); | |
| continue; | |
| } | |
| Console.WriteLine($"[FOUND] migrations in {proj}:\n{listRes.stdout}"); | |
| // run update | |
| string updateArgs = $"ef database update --project \"{proj}\""; | |
| if (!string.IsNullOrWhiteSpace(startupProject)) | |
| updateArgs += $" --startup-project \"{startupProject}\""; | |
| if (!string.IsNullOrWhiteSpace(connectionString)) | |
| updateArgs += $" --connection \"{connectionString}\""; | |
| var upd = Run("dotnet", updateArgs, projDir); | |
| if (upd.exitCode != 0) | |
| { | |
| Console.Error.WriteLine($"[ERROR] database update failed for {proj}:\n{upd.stderr}"); | |
| Environment.Exit(upd.exitCode); | |
| } | |
| else | |
| { | |
| Console.WriteLine($"[SUCCESS] migrations applied for {proj}."); | |
| } | |
| } | |
| Console.WriteLine("\n=== finished ApplyMigrations.cs ==="); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment