|
| 1 | +using System; |
| 2 | +using DbUp; |
| 3 | +using DotNetEnv; |
| 4 | +using Microsoft.Data.SqlClient; |
| 5 | + |
| 6 | +namespace Todo.Database.Deploy |
| 7 | +{ |
| 8 | + class Program |
| 9 | + { |
| 10 | + static int Main(string[] args) |
| 11 | + { |
| 12 | + // This will load the content of .env file and create related environment variables |
| 13 | + DotNetEnv.Env.Load(); |
| 14 | + |
| 15 | + // Connection string for deploying the database (high-privileged account as it needs to be able to CREATE/ALTER/DROP) |
| 16 | + var connectionString = Environment.GetEnvironmentVariable("ConnectionString"); |
| 17 | + |
| 18 | + var csb = new SqlConnectionStringBuilder(connectionString); |
| 19 | + Console.WriteLine($"Deploying database: {csb.InitialCatalog}"); |
| 20 | + |
| 21 | + Console.WriteLine("Testing connection..."); |
| 22 | + var conn = new SqlConnection(csb.ToString()); |
| 23 | + conn.Open(); |
| 24 | + conn.Close(); |
| 25 | + |
| 26 | + Console.WriteLine("Starting deployment..."); |
| 27 | + var dbup = DeployChanges.To |
| 28 | + .SqlDatabase(csb.ConnectionString) |
| 29 | + .WithScriptsFromFileSystem("../sql") |
| 30 | + .JournalToSqlTable("dbo", "$__dbup_journal") |
| 31 | + .LogToConsole() |
| 32 | + .Build(); |
| 33 | + |
| 34 | + var result = dbup.PerformUpgrade(); |
| 35 | + |
| 36 | + if (!result.Successful) |
| 37 | + { |
| 38 | + Console.WriteLine(result.Error); |
| 39 | + return -1; |
| 40 | + } |
| 41 | + |
| 42 | + Console.WriteLine("Success!"); |
| 43 | + return 0; |
| 44 | + } |
| 45 | + } |
| 46 | +} |
0 commit comments