Created
August 16, 2010 23:26
-
-
Save jstangroome/527963 to your computer and use it in GitHub Desktop.
ConvertTo-DbSchema1.0.ps1
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
#requires -version 2.0 | |
[CmdletBinding()] | |
param ( | |
[parameter(Mandatory=$true)] | |
[ValidateScript({Test-Path -PathType Leaf -LiteralPath $_})] | |
[string] | |
$DbSchemaFile, | |
[parameter(Mandatory=$true)] | |
[string] | |
$Destination | |
) | |
$ErrorActionPreference = 'Stop' | |
Set-StrictMode -Version Latest | |
$DbSchema = (Get-Content -Path $DbSchemaFile) -as [xml] | |
if ($DbSchema.DataSchemaModel.FileFormatVersion -ne '1.2') { | |
throw 'Expected DataSchemaModel FileFormatVersion 1.2' | |
} | |
# downgrade version number | |
$DbSchema.DataSchemaModel.FileFormatVersion = '1.0' | |
$DbSchema.DataSchemaModel.SchemaVersion = '1.0' | |
# inject SqlDsp namespace into DatabaseSchemaProvider full type name | |
$DspName = $DbSchema.DataSchemaModel.DspName | |
$DspName = $DspName -replace '^(Microsoft.Data.Schema.Sql.)(Sql\d+DatabaseSchemaProvider)$', '$1SqlDsp.$2' | |
$DbSchema.DataSchemaModel.DspName = $DspName | |
# remove annotations | |
$DbSchema.DataSchemaModel.Model.Element | | |
ForEach-Object { | |
$_.SelectNodes('//Annotation[@Type="SysCommentsObjectAnnotation"]') | | |
ForEach-Object { | |
$_.PSBase.ParentNode.RemoveChild($_) | Out-Null | |
} | |
} | |
$CurrentDir = [Environment]::CurrentDirectory | |
try { | |
if ((Get-Location).Provider.Name -eq 'FileSystem') { | |
# force .NET current directory to match PS current directory | |
# to support relative paths for $Destination | |
[Environment]::CurrentDirectory = (Get-Location).ProviderPath | |
} | |
# save converted file | |
$DbSchema.Save($Destination) | |
} finally { | |
[Environment]::CurrentDirectory = $CurrentDir | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment