Ilya Baybikov's home page

Database migrations made easy

Easy-Migration - simple SQL Server migrations with PowerShell, I’ve had this script sitting in my toolbox for quite a while, and now it’s finally time to share it. Why didn’t I do it earlier? Probably because I didn’t have a good name for it.

In short, it executes database migration scripts in a defined order and keeps track of what has already been executed. For full documentation, check out the GitHub repository.

Let’s take a quick look.

If you already have SQL Server and SSMS installed and running, you can skip the first two steps.

Examples for PowerShell 7.0+

To run the examples, open a PowerShell prompt in the example directory

Dry Run mode

 1. (Join-Path $PSScriptRoot '..' 'functions-ps7.0.ps1')
 2
 3Invoke-EasyMigration `
 4	-ConnStr 'Data Source=(local);Initial Catalog=tempdb;Connection Timeout=5;Encrypt=False;
 5		User Id=sa;Password=P1s-Unsee-Me;Application Name=easy-migration;' `
 6	-BasePath $PSScriptRoot `
 7	-Phase 'phase01' `
 8	-ForceScripts 'job007\000-kill-all-user-processes.sql' `
 9	-Verbose `
10	-WhatIf

Output

What if: Performing the operation "Invoke-EasyMigration" on target "DataSource: (local), InitialCatalog: tempdb, Phase: phase01".
VERBOSE: Dry run
VERBOSE: Running migration script: 000-fix-this.sql
VERBOSE: Migration completed
VERBOSE: Running migration script: 001-fix-that.sql
VERBOSE: Migration completed
VERBOSE: Forcing migration script: job007\000-kill-all-user-processes.sql
VERBOSE: Running migration script: job007\000-kill-all-user-processes.sql
VERBOSE: Migration completed
VERBOSE: Easy as that!

Execute mode

1. (Join-Path $PSScriptRoot '..' 'functions-ps7.0.ps1')
2
3Invoke-EasyMigration `
4	-ConnStr 'Data Source=(local);Initial Catalog=tempdb;Connection Timeout=5;Encrypt=False;
5		User Id=sa;Password=P1s-Unsee-Me;Application Name=easy-migration;' `
6	-BasePath $PSScriptRoot `
7	-Phase 'phase01' `
8	-ForceScripts 'job007\000-kill-all-user-processes.sql' `
9	-Verbose

Output

VERBOSE: Performing the operation "Invoke-EasyMigration" on target "DataSource: (local), InitialCatalog: tempdb, Phase: phase01".
VERBOSE: Running migration script: 000-fix-this.sql
VERBOSE: I'm
VERBOSE: Going
VERBOSE: To
VERBOSE: Fix
VERBOSE: This
VERBOSE: Migration completed
VERBOSE: Running migration script: 001-fix-that.sql
VERBOSE: I'm going to fix that
VERBOSE: Migration completed
VERBOSE: Forcing migration script: job007\000-kill-all-user-processes.sql
VERBOSE: Running migration script: job007\000-kill-all-user-processes.sql
VERBOSE: Just kidding
VERBOSE: Migration completed
VERBOSE: Easy as that!

Hopefully it was easy to read and will be easy to use.

Let me know what you think in the comments below the LinkedIn post.

See you later!

#Powershell