Skip to content

Commit e14aeee

Browse files
authored
Merge pull request #60 from webmd-health-services/feature/PFORM-3233
Export-Migration will now include extended properties on schemas, views, and view columns.
2 parents 6d8587f + ecae420 commit e14aeee

File tree

7 files changed

+205
-41
lines changed

7 files changed

+205
-41
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2+
# 0.18.0
3+
4+
* `Export-Migration` will now include extended properties on schemas, views, and view columns.
5+
6+
27
# 0.17.0
38

49
## Changes

Rivet/Functions/Export-Migration.ps1

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,12 +1163,18 @@ where
11631163
-- SCHEMAS
11641164
select
11651165
sys.schemas.name,
1166-
sys.sysusers.name as owner
1166+
sys.sysusers.name as owner,
1167+
sys.extended_properties.value as description
11671168
from
11681169
sys.schemas
11691170
join
11701171
sys.sysusers
1171-
on sys.schemas.principal_id = sys.sysusers.uid'
1172+
on sys.schemas.principal_id = sys.sysusers.uid
1173+
left join
1174+
sys.extended_properties
1175+
on sys.extended_properties.class = 3
1176+
and sys.extended_properties.major_id = sys.schemas.schema_id
1177+
and sys.extended_properties.name = ''MS_Description'''
11721178
function Export-Schema
11731179
{
11741180
param(
@@ -1187,9 +1193,14 @@ from
11871193
{
11881194
return
11891195
}
1196+
$description = $schema.description
1197+
if( $description )
1198+
{
1199+
$description = ' -Description ''{0}''' -f ($description -replace '''','''''')
1200+
}
11901201

11911202
Write-ExportingMessage -Schema $Object.schema_name -Type Schema
1192-
' Add-Schema -Name ''{0}'' -Owner ''{1}''' -f $schema.name,$schema.owner
1203+
' Add-Schema -Name ''{0}'' -Owner ''{1}''{2}' -f $schema.name,$schema.owner, $description
11931204
$exportedSchemas[$schema.name] = $true
11941205
Push-PopOperation ('Remove-Schema -Name ''{0}''' -f $schema.name)
11951206
}
@@ -1534,8 +1545,22 @@ where
15341545
Write-ExportingMessage -Schema $Object.schema_name -Name $Object.name -Type View
15351546
if( $view -match $createPreambleRegex )
15361547
{
1548+
$description = $Object.description
1549+
if( $description )
1550+
{
1551+
$description = ' -Description ''{0}''' -f ($description -replace '''','''''')
1552+
}
1553+
15371554
$view = $view -replace $createPreambleRegex,''
1538-
' Add-View{0} -Name ''{1}'' -Definition @''{2}{3}{2}''@' -f $schema,$Object.name,[Environment]::NewLine,$view
1555+
' Add-View{0} -Name ''{1}''{2} -Definition @''{3}{4}{3}''@' -f $schema,$Object.name,$description,[Environment]::NewLine,$view
1556+
1557+
# Get view's columns that have extended properties
1558+
$viewColumns = Invoke-Query -Query $columnsQuery | Where-Object { $_.object_id -eq $Object.object_id -and $_.description }
1559+
foreach( $column in $viewColumns )
1560+
{
1561+
$colDescription = ' -Description ''{0}''' -f ($column.description -replace '''','''''')
1562+
' Add-ExtendedProperty -SchemaName ''{0}'' -ViewName ''{1}'' -ColumnName ''{2}'' -Value {3}' -f $Object.schema_name,$Object.object_name,$column.column_name,$colDescription
1563+
}
15391564
}
15401565
else
15411566
{

Rivet/Functions/Operations/Add-Description.ps1

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function Add-Description
33
{
44
<#
55
.SYNOPSIS
6-
Adds the `MS_Description` extended property to a table or column.
6+
Adds the `MS_Description` extended property to schemas, tables, columns, views, and view columns.
77
88
.DESCRIPTION
99
The `sys.sp_addextendedproperty` stored procedure is used to set a table/column's description (i.e. the `MS_Description` extended property), but the syntax is weird. This function hides that weirdness from you. You're welcome.
@@ -22,33 +22,62 @@ function Add-Description
2222
Add-Description -Description 'Whoseit's whatsits table.' -TableName WhoseitsWhatsits -ForTable
2323
2424
PowerShell v2.0 doesn't parse the parameters correctly when setting a table name, so you have to explicitly tell it what to do. Upgrade to PowerShell 3!
25+
26+
.EXAMPLE
27+
Add-Description -Description 'This is an extended property on a schema' -SchemaName 'test'
28+
29+
Adds a description (i.e. the `MS_Description` extended property) on the `test` schema.
30+
31+
.EXAMPLE
32+
Add-Description -Description 'This is an extended property on a view' -SchemaName 'test' -ViewName 'testVw'
33+
34+
Adds a description (i.e. the `MS_Description` extended property) on the `testVw` view.
35+
36+
.EXAMPLE
37+
Add-Description -Description 'This is an extended property on a view column' -SchemaName 'test' -ViewName 'testVw' -ColumnName 'ID'
38+
39+
Adds a description (i.e. the `MS_Description` extended property) on the `ID` column in the 'testVw' view.
2540
#>
2641
[CmdletBinding()]
2742
param(
28-
[Parameter(Mandatory=$true,Position=0)]
29-
[string]
3043
# The value for the MS_Description extended property.
31-
$Description,
44+
[Parameter(Mandatory, Position=0)]
45+
[String] $Description,
3246

33-
[Alias('Schema')]
34-
[string]
3547
# The schema. Defaults to `dbo`.
36-
$SchemaName = 'dbo',
48+
[Parameter(ParameterSetName='ForSchema')]
49+
[Parameter(ParameterSetName='ForTable')]
50+
[Parameter(ParameterSetName='ForView')]
51+
[Parameter(ParameterSetName='ForColumn')]
52+
[Alias('Schema')]
53+
[String] $SchemaName = 'dbo',
3754

38-
[Parameter(Mandatory=$true)]
39-
[Alias('Table')]
40-
[string]
4155
# The name of the table where the extended property is getting set.
42-
$TableName,
56+
[Parameter(Mandatory, ParameterSetName='ForTable')]
57+
[Parameter(Mandatory, ParameterSetName='ForColumn')]
58+
[Alias('Table')]
59+
[String] $TableName,
60+
61+
# The name of the view where the extended property is getting set.
62+
[Parameter(Mandatory, ParameterSetName='ForView')]
63+
[Alias('View')]
64+
[String] $ViewName,
4365

44-
[Parameter(ParameterSetName='ForColumn')]
45-
[Alias('Column')]
46-
[string]
4766
# The name of the column where the extended property is getting set.
48-
$ColumnName
67+
[Parameter(Mandatory, ParameterSetName='ForColumn')]
68+
[Alias('Column')]
69+
[String] $ColumnName
4970
)
5071

5172
$optionalArgs = @{ }
73+
if( $TableName )
74+
{
75+
$optionalArgs.TableName = $TableName
76+
}
77+
if( $ViewName )
78+
{
79+
$optionalArgs.ViewName = $ViewName
80+
}
5281
if( $ColumnName )
5382
{
5483
$optionalArgs.ColumnName = $ColumnName
@@ -57,6 +86,5 @@ function Add-Description
5786
Add-ExtendedProperty -Name ([Rivet.Operations.ExtendedPropertyOperation]::DescriptionPropertyName) `
5887
-Value $Description `
5988
-SchemaName $SchemaName `
60-
-TableName $TableName `
6189
@optionalArgs
6290
}

Rivet/Functions/Operations/Add-Schema.ps1

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,45 @@ function Add-Schema
66
Creates a new schema.
77
88
.DESCRIPTION
9-
The `Add-Schema` operation creates a new schema in a database. It does so in an idempotent way, i.e. it only creates the schema if it doesn't exist.
9+
The `Add-Schema` operation creates a new schema in a database. It does so in an idempotent way, i.e. it only
10+
creates the schema if it doesn't exist. If -Description is provided an extended property named 'MS_Description'
11+
will be added to the schema with the description as the value.
1012
1113
.EXAMPLE
1214
Add-Schema -Name 'rivetexample'
1315
1416
Creates the `rivetexample` schema.
17+
18+
.EXAMPLE
19+
Add-Schema -Name 'rivetTest' -Description 'This is an extended property'
20+
21+
Creates the `rivetTest` schema with the `MS_Description` extended property.
1522
#>
1623
[CmdletBinding()]
1724
param(
18-
[Parameter(Mandatory=$true)]
19-
[Alias('SchemaName')]
20-
[string]
2125
# The name of the schema.
22-
$Name,
26+
[Parameter(Mandatory)]
27+
[Alias('SchemaName')]
28+
[String] $Name,
2329

24-
[Alias('Authorization')]
25-
[string]
2630
# The owner of the schema.
27-
$Owner
31+
[Alias('Authorization')]
32+
[String] $Owner,
33+
34+
# A description of the schema.
35+
[String] $Description
2836
)
2937

3038
Set-StrictMode -Version 'Latest'
39+
40+
$schemaOp = New-Object 'Rivet.Operations.AddSchemaOperation' $Name, $Owner
41+
42+
if( $Description )
43+
{
44+
$schemaDescriptionOp = Add-Description -SchemaName $Name -Description $Description
45+
$schemaOp.ChildOperations.Add($schemaDescriptionOp)
46+
}
3147

32-
New-Object 'Rivet.Operations.AddSchemaOperation' $Name, $Owner
48+
$schemaOp | Write-Output
49+
$schemaOp.ChildOperations | Write-Output
3350
}

Rivet/Functions/Operations/Add-View.ps1

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,47 @@
55
Creates a new view.
66
77
.DESCRIPTION
8-
Creates a new view.
8+
Creates a new view. If -Description is provided an extended property named 'MS_Description' will be added to the
9+
schema with the description as the value.
910
1011
.EXAMPLE
1112
Add-View -SchemaName 'rivet' 'ReadMigrations' 'AS select * from rivet.Migrations'
1213
1314
Creates a view to read all the migrations from Rivet's Migrations table. Don't do this in real life.
15+
16+
.EXAMPLE
17+
Add-View -Name 'rivetVw' -Description 'This is an extended property'
18+
19+
Creates the `rivetVw` view with the `MS_Description` extended property.
1420
#>
1521
[CmdletBinding()]
1622
param(
17-
[Parameter(Mandatory=$true,Position=0)]
18-
[string]
1923
# The name of the view.
20-
$Name,
24+
[Parameter(Mandatory, Position=0)]
25+
[String] $Name,
2126

22-
[Parameter()]
23-
[string]
2427
# The schema name of the view. Defaults to `dbo`.
25-
$SchemaName = 'dbo',
28+
[Parameter()]
29+
[String] $SchemaName = 'dbo',
2630

27-
[Parameter(Mandatory=$true,Position=1)]
28-
[string]
2931
# The definition of the view. Everything after the `create view [schema].[name]` clause.
30-
$Definition
32+
[Parameter(Mandatory, Position=1)]
33+
[String] $Definition,
34+
35+
# A description of the view.
36+
[String] $Description
3137
)
3238

3339
Set-StrictMode -Version 'Latest'
3440

35-
New-Object 'Rivet.Operations.AddViewOperation' $SchemaName,$Name,$Definition
41+
$viewOp = New-Object 'Rivet.Operations.AddViewOperation' $SchemaName,$Name,$Definition
42+
43+
if( $Description )
44+
{
45+
$viewDescriptionOp = Add-Description -SchemaName $SchemaName -ViewName $Name -Description $Description
46+
$viewOp.ChildOperations.Add($viewDescriptionOp)
47+
}
48+
49+
$viewOp | Write-Output
50+
$viewOp.ChildOperations | Write-Output
3651
}

Rivet/Rivet.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
RootModule = 'Rivet.psm1'
1212

1313
# Version number of this module.
14-
ModuleVersion = '0.17.0'
14+
ModuleVersion = '0.18.0'
1515

1616
# ID used to uniquely identify this module
1717
GUID = '8af34b47-259b-4630-a945-75d38c33b94d'

Test/Export-Migration.Tests.ps1

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,3 +1236,77 @@ function Pop-Migration
12361236
ThenMigration -HasContent 'Add-Index -TableName ''Table1'' -ColumnName ''AnotherID'''
12371237
}
12381238

1239+
Describe 'Export-Migration.when schema has an extended property' {
1240+
Init
1241+
GivenMigrationContent @'
1242+
function Push-Migration
1243+
{
1244+
Add-Schema 'snap'
1245+
Add-ExtendedProperty -Name 'MS_Description' -Value 'This is the MS Description for the schema snap' -SchemaName 'snap'
1246+
Add-Table -Schema 'snap' -Name 'SnapTable' -Column {
1247+
int 'ID' -NotNull
1248+
}
1249+
}
1250+
1251+
function Pop-Migration
1252+
{
1253+
Remove-Table -Schema 'snap' -Name 'SnapTable'
1254+
Remove-Schema -Name 'snap'
1255+
}
1256+
'@
1257+
1258+
WhenExporting
1259+
ThenMigration -HasContent 'Add-Schema -Name ''snap'' -Owner ''dbo'' -Description ''This is the MS Description for the schema snap'''
1260+
ThenMigration -HasContent 'Add-Table -SchemaName ''snap'' -Name ''SnapTable'''
1261+
}
1262+
1263+
Describe 'Export-Migration.when view has an extended property' {
1264+
Init
1265+
GivenMigrationContent @'
1266+
function Push-Migration
1267+
{
1268+
Add-Schema 'crackle'
1269+
Add-View -SchemaName 'crackle' -Name 'CrackleView' -Definition 'as select 1 as one'
1270+
Add-ExtendedProperty -Name 'MS_Description' -SchemaName 'crackle' -ViewName 'CrackleView' -Value 'This is the MS Description for the view CrackleView'
1271+
}
1272+
1273+
function Pop-Migration
1274+
{
1275+
Remove-View -SchemaName 'crackle' -Name 'CrackleView'
1276+
Remove-Schema 'crackle'
1277+
}
1278+
'@
1279+
1280+
WhenExporting
1281+
ThenMigration -HasContent 'Add-Schema -Name ''crackle'''
1282+
ThenMigration -HasContent 'Add-View -SchemaName ''crackle'' -Name ''CrackleView'''
1283+
ThenMigration -HasContent 'Add-View -SchemaName ''crackle'' -Name ''CrackleView'' -Description ''This is the MS Description for the view CrackleView'''
1284+
}
1285+
1286+
Describe 'Export-Migration.when view column has an extended property' {
1287+
Init
1288+
GivenMigrationContent @'
1289+
function Push-Migration
1290+
{
1291+
Add-Schema 'pop'
1292+
Add-Table -Schema 'pop' -Name 'PopTable' -Column {
1293+
int 'ID' -NotNull
1294+
}
1295+
Add-View -Name 'PopView' -SchemaName 'pop' -Definition 'as select * from PopTable'
1296+
Add-ExtendedProperty -Name 'MS_Description' -SchemaName 'pop' -ViewName 'PopView' -ColumnName 'ID' -Value 'This is the MS Description for column ID in the view PopView'
1297+
}
1298+
1299+
function Pop-Migration
1300+
{
1301+
Remove-Table -SchemaName 'pop' -Name 'PopTable'
1302+
Remove-View -SchemaName 'pop' -Name 'PopView'
1303+
Remove-Schema 'pop'
1304+
}
1305+
'@
1306+
1307+
WhenExporting
1308+
ThenMigration -HasContent 'Add-Schema -Name ''pop'''
1309+
ThenMigration -HasContent 'Add-Table -SchemaName ''pop'' -Name ''PopTable'''
1310+
ThenMigration -HasContent 'Add-View -SchemaName ''pop'' -Name ''PopView'''
1311+
ThenMigration -HasContent 'Add-ExtendedProperty -SchemaName ''pop'' -ViewName ''PopView'' -ColumnName ''ID'' -Value -Description ''This is the MS Description for column ID in the view PopView'''
1312+
}

0 commit comments

Comments
 (0)