Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. param(
  2. $SqlInstance = 'SQLSRV01',
  3. $Database = "AdventureWorks2017"
  4. )
  5.  
  6. import-module dbatools;
  7.  
  8. if ( $SqlInstance.GetType() -ne 'Server' ) {
  9. $inst = Connect-DbaInstance -SqlInstance $SqlInstance.ToString()
  10. }
  11. else {
  12. $inst = $SqlInstance;
  13. }
  14.  
  15.  
  16. $db = Get-DbaDatabase -SqlInstance $inst -Database $Database
  17.  
  18. $colors = @(
  19. "#62A9FF", "#06DCFB", "#01FCEF", "#03EBA6", "#01F33E", "#62D0FF"
  20. , "#75B4FF", "#24E0FB", "#1FFEF3", "#03F3AB", "#0AFE47", "#75D6FF"
  21. , "#86BCFF", "#3DE4FC", "#5FFEF7", "#33FDC0", "#4BFE78", "#8ADCFF"
  22. , "#99C7FF", "#63E9FC", "#74FEF8", "#62FDCE", "#72FE95", "#99E0FF"
  23. , "#99C7FF", "#75ECFD", "#92FEF9", "#7DFDD7", "#8BFEA8", "#A8E4FF"
  24. , "#A8CFFF", "#8CEFFD", "#A5FEFA", "#8FFEDD", "#A3FEBA", "#BBEBFF"
  25. , "#BBDAFF", "#ACF3FD", "#B5FFFC", "#A5FEE3", "#B5FFC8", "#CEF0FF"
  26. , "#D0E6FF", "#C0F7FE", "#CEFFFD", "#BEFEEB", "#CAFFD8", "#D9F3FF"
  27. , "#DBEBFF", "#C0F7FE", "#E1FFFE", "#BDFFEA", "#EAFFEF", "#ECFAFF"
  28. , "#ECF4FF", "#E6FCFF", "#F2FFFE", "#CFFEF0", "#EAFFEF", "#F9FDFF"
  29. , "#F9FCFF", "#F9FEFF", "#FDFFFF", "#F7FFFD", "#F9FFFB", "#FDFEFF"
  30. );
  31.  
  32. function Get-TblDataType {
  33. <#
  34. .SYNOPSIS
  35.  
  36. .DESCRIPTION
  37.  
  38. .PARAMETER ComputerName
  39.  
  40. .EXAMPLE
  41.  
  42. .INPUTS
  43.  
  44. .OUTPUTS
  45. #>
  46. [CmdletBinding()]
  47. Param(
  48. [Parameter(ValueFromPipeline=$True,
  49. Mandatory=$True,
  50. ValueFromPipelineByPropertyName=$True)]
  51. [Microsoft.SqlServer.Management.Smo.Column]
  52. $Column
  53. )
  54.  
  55. Begin{}
  56.  
  57. Process{
  58. $dt = $Column.DataType.ToString();
  59. switch -regex ($Column.DataType.ToString()) {
  60. "(.*)char" {
  61. $dt += "(" ;
  62. if ( $Column.DataType.MaximumLength -eq "-1") {
  63. $dt += "MAX" ;
  64. }
  65. else {
  66. $dt += "$($Column.DataType.MaximumLength)" ;
  67. }
  68. $dt += ")" ;
  69. break;
  70. }
  71. "datetime2" { $dt += "(" + $Column.DataType.NumericScale.ToString() + ")"; }
  72. "decimal" { $dt += "($($Column.DataType.NumericPrecision),$($Column.DataType.NumericScale))"; }
  73.  
  74. }
  75.  
  76. return $dt
  77. }
  78.  
  79. End {}
  80. }
  81.  
  82.  
  83. [string]$uml = '';
  84. $uml += "`n@startuml`nskinparam linetype ortho`nhide circle`nskinparam roundcorner 20"
  85.  
  86. [int]$ColorIndex = 0;
  87. $db.Tables | Group-Object -Property Schema | %{
  88. # $uml += "`nnamespace $($_.Name) {"
  89. $SchemaColor = $colors[$ColorIndex];
  90. $ColorIndex++;
  91. $_.Group | %{
  92. $tbl = $_;
  93.  
  94. $uml += "`nEntity $($tbl.Schema).$($tbl.Name) $SchemaColor {"
  95.  
  96. # Key Columns
  97. $tbl.Columns | ?{ $_.InPrimaryKey } | %{
  98.  
  99. $uml += "`n`t* $($_.Name) : $( Get-TblDataType $_ )"
  100. }
  101.  
  102. $uml += "`n`t--"
  103.  
  104. #Non-Key Columns
  105. $tbl.Columns | ?{ -Not $_.InPrimaryKey } | %{
  106. $uml += "`n`t$(if ( $_.Nullable ){ '* ' } else { ' ' })$($_.Name) : $( Get-TblDataType $_ )"
  107. }
  108.  
  109. $uml += "`n}"
  110.  
  111.  
  112. }
  113. # $uml += "`n}"
  114. } #Group
  115.  
  116. #Relationships
  117. $db.Tables | ?{$_.ForeignKeys.Count -gt 0} | %{
  118. $tbl = $_;
  119. $tbl.ForeignKeys | %{
  120. $uml += "`n$($tbl.Schema).$($tbl.Name) - $($_.ReferencedTableSchema).$($_.ReferencedTable)"
  121. }
  122.  
  123. }
  124.  
  125. #Layout - Hidden links to try and stretch out the layout
  126. $uml += "`n`n'Add hidden links to force the model to stretch out"
  127. for ($i = 0; $i -lt [int]($db.Tables.Count / 5); $i++ ){
  128. # Pick a random table
  129. $t = $db.Tables[(Get-Random -Minimum 0 -Maximum $db.Tables.Count)];
  130.  
  131. # Link to other random tables
  132. foreach($j in 1..10) {
  133. $r = $db.Tables[(Get-Random -Minimum 0 -Maximum $db.Tables.Count)];
  134. $uml += "`n$($t.Schema).$($t.Name) -[hidden]- $($r.Schema).$($r.Name)"
  135. }
  136. }
  137.  
  138.  
  139. $uml += "`n@enduml"
  140. Set-Content -Path "./db.puml" -Value $uml
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement