-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathMigrateMongo.ps1
30 lines (25 loc) · 1.23 KB
/
MigrateMongo.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
param($server, $database, $backupLocation, $migrationsAssemblyPath)
# Backup location is base\server\timestamp
$backupLocation = join-path (join-path $backupLocation $server) $(get-date -f yyyy_MM_dd_HH_mm_ss)
# Backup current database
mongodump -h $server -d $database -o $backupLocation
# Load assembly with migrations and MongoMigrations framework assembly
$migrationsAssembly = [System.Reflection.Assembly]::LoadFrom($migrationsAssemblyPath)
$migrationFrameworkAssemblyPath = join-path ([IO.Path]::GetDirectoryName($migrationsAssemblyPath)) 'MongoMigrations.dll'
[System.Reflection.Assembly]::LoadFrom($migrationFrameworkAssemblyPath)
# Create migration runner and load migrations
$runner = new-object MongoMigrations.MigrationRunner(('mongodb://' + $server), $database)
$runner.MigrationLocator.LookForMigrationsInAssembly($migrationsAssembly)
Try
{
$runner.UpdateToLatest()
}
Catch [MongoMigrations.MigrationException]{
# Attempt restore on failure
echo "Migrations failed: "
Write-Host $_.Exception.ToString()
echo "Attempting restore from " + $backupLocation
$restoreLocation = join-path $backupLocation $database
mongorestore -h $server -d $database -drop $restoreLocation
throw
}