Last active
October 12, 2021 09:53
-
-
Save marcingolenia/e95c33d311e2cb358c656850448224f4 to your computer and use it in GitHub Desktop.
Just place the script in a folder with sql files. Optionally use docker as well :)
This file contains 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
version: '3.7' | |
services: | |
db: | |
image: mcr.microsoft.com/mssql/server:latest | |
environment: | |
SA_PASSWORD: 'Secret!Passw0rd' | |
ACCEPT_EULA: 'Y' | |
ports: | |
- '1433:1433' | |
db-migrations: | |
build: | |
context: db/ | |
dockerfile: ./Dockerfile | |
depends_on: | |
- db | |
environment: | |
DB_CONNECTION: "Server=db;Initial Catalog=MyDb;User ID=sa;Password=Secret!Passw0rd;Connection Timeout=30;" |
This file contains 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
FROM mcr.microsoft.com/dotnet/sdk:5.0 | |
COPY * ./ | |
CMD ["sh", "-c", "dotnet fsi migrateDatabase.fsx \"${DB_CONNECTION}\""] |
This file contains 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
#r "nuget: DbUp,4.5.0" | |
open System | |
open DbUp | |
let connectionString = fsi.CommandLineArgs.[1..] | |
|> Array.tryHead | |
|> Option.defaultValue "Server=localhost,1434;Initial Catalog=MyDb;User ID=sa;Password=Secret!Passw0rd;MultipleActiveResultSets=True;Connection Timeout=30;" | |
let logAndParseEngineResult (result: Engine.DatabaseUpgradeResult) = | |
match result.Successful with | |
| true -> | |
Console.ForegroundColor <- ConsoleColor.Green | |
Console.WriteLine("Success") | |
Console.ResetColor() | |
0 | |
| false -> | |
Console.ForegroundColor <- ConsoleColor.Red | |
Console.WriteLine result.Error | |
Console.ResetColor() | |
-1 | |
EnsureDatabase.For.SqlDatabase(connectionString) | |
// Consider NullLogger for idempotent sql migrations. | |
DeployChanges | |
.To | |
.SqlDatabase(connectionString) | |
.WithScriptsFromFileSystem("./") | |
.WithTransactionPerScript() | |
.LogToConsole() | |
.Build() | |
.PerformUpgrade() | |
|> logAndParseEngineResult |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment