Skip to content

Commit

Permalink
✨ Database notebook script can now create a static readme too
Browse files Browse the repository at this point in the history
  • Loading branch information
brianary authored Dec 14, 2023
1 parent eab13bc commit 7a14b85
Showing 1 changed file with 60 additions and 11 deletions.
71 changes: 60 additions & 11 deletions Initialize-DatabaseNotebook.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,74 @@ Adds cells to the current Polyglot Notebook that generates a header, ER diagram,
#Requires -Version 7
#Requires -Modules dbatools
[CmdletBinding()] Param(
# The name of a server (and optional instance) to connect and use for the query.
[Parameter(Position=0,Mandatory=$true)][ValidatePattern('\A[^'']+\z')][string] $ServerInstance,
# The the database to connect to on the server.
[Parameter(Position=1,Mandatory=$true)][ValidatePattern('\A[^'']+\z')][string] $DatabaseName,
# By default, an encryped connection is used. This disables that for certain compatibility issues.
[switch] $DisableEncryption,
[switch] $AllowWrites
# By default, a read-only connection is used. This disables that and allows read/write operations,
# for certain compatibility issues or other needs.
[switch] $AllowWrites,
# The path to a static Markdown README.md file to create in parallel since the notebook readme
# isn't fully supported yet.
[string] $Readme
)

Import-CharConstants.ps1 NL -Scope Script

function Add-MainHeader
{
@"
@"
$DatabaseName database
======================
"@ |Add-NotebookCell.ps1
if(!$Readme) {Write-Information 'Skipping generation of README.md'; return}
@"
$DatabaseName database
======================
<!--
This content is generated by README.ipynb and copied here because otherwise the native GitHub Jupyter Notebook
renderer doesn't show it by default for the directory, doesn't render the Mermaid content, and doesn't fold the
input code cells away (making it very noisy). Hopefully one day GitHub will address these issues, and/or
Polyglot Notebooks will provide an export/CLI to generate flat CommonMark, or the much cleaner Polyglot .dib
Notebook type will include output cells when saved and rendered.
You can use this PowerShell command to convert a copied notebook output cell into a CommonMark table:
"`$(Get-Clipboard)" -replace '(?m)<style.*</style>' |HtmlToMarkdown\Convert-HtmlToMarkdown |Set-Clipboard
-->
"@ |Out-File $Readme utf8BOM
}

function Add-SqlSupport
{
'#r "nuget: Microsoft.DotNet.Interactive.SqlServer, *-*"' |Add-NotebookCell.ps1 -Language csharp
'#r "nuget: Microsoft.DotNet.Interactive.SqlServer, *-*"' |Add-NotebookCell.ps1 -Language csharp
}

function Add-ErDiagramGenerator
{
@"
# generate erDiagram
Get-DbaDatabase -SqlInstance '$ServerInstance' -Database '$DatabaseName' |
Get-DbaDbTable |
Where-Object Name -NotIn dtproperties,__MigrationLog,__SchemaSnapshot |
Export-MermaidER.ps1 |
Add-NotebookCell.ps1 -Language
Get-DbaDbTable |
Where-Object Name -NotIn dtproperties,__MigrationLog,__SchemaSnapshot |
Export-MermaidER.ps1 |
Add-NotebookCell.ps1 -Language mermaid
"Last updated `$(Get-Date)"
"@ |Add-NotebookCell.ps1 -Language pwsh
if(!$Readme) {return}
@"
``````mermaid
$(Get-DbaDatabase -SqlInstance $ServerInstance -Database $DatabaseName |
Get-DbaDbTable |
Where-Object Name -NotIn dtproperties,__MigrationLog,__SchemaSnapshot |
Export-MermaidER.ps1)
``````
"@ |Out-File $Readme -Append
}

function Format-TableQuery([Parameter(Mandatory=$true,ValueFromPipeline=$true)][Microsoft.SqlServer.Management.Smo.Table] $Table)
Expand Down Expand Up @@ -88,21 +123,35 @@ select '$($Table.Schema -eq 'dbo' ? '' : $Table.Schema + '.')$($Table.Name)', co

function Add-TableDetails
{
$connstr = "Data Source=$ServerInstance; Initial Catalog=$DatabaseName; Encrypt=$($DisableEncryption ? 'False' : 'True'); ApplicationIntent=$($AllowWrites ? 'ReadWrite' : 'ReadOnly'); Integrated Security=True"
@"
#!connect mssql --kernel-name $($DatabaseName -replace '\W+') "Server=$ServerInstance; Initial Catalog=$DatabaseName; Encrypt=$($DisableEncryption ? 'False' : 'True'); ApplicationIntent=$($AllowWrites ? 'ReadWrite' : 'ReadOnly'); Integrated Security=True"
#!connect mssql --kernel-name $($DatabaseName -replace '\W+') "$connstr"
"@ |Add-NotebookCell.ps1 -Language csharp
$Local:OFS = [Environment]::NewLine
$query = @"
$(Get-DbaDatabase -SqlInstance $ServerInstance -Database $DatabaseName |
Get-DbaDbTable |
Where-Object Name -NotIn dtproperties,__MigrationLog,__SchemaSnapshot |
Format-TableQuery);
"@
@"
#!sql-$($DatabaseName -replace '\W+')
-- be sure to remove duplicates as needed
$(Get-DbaDatabase -SqlInstance $ServerInstance -Database $DatabaseName |Get-DbaDbTable |Where-Object Name -NotIn dtproperties,__MigrationLog,__SchemaSnapshot |Format-TableQuery);
$query
"@ |Add-NotebookCell.ps1 -Language sql
if(!$Readme) {return}
@"
| Table | # Records | Oldest record | Newest record |
|-------|----------:|:-------------:|:-------------:|
$(Invoke-DbaQuery -Query $query -SqlInstance (Connect-DbaInstance -ConnectionString $connstr) -As DataRow |
ForEach-Object {"| $($_.Table) | $($_.'# Records') | $($_.'Oldest record') | $($_.'Newest record') |"})
"@ |Out-File $Readme -Append
}

function Add-DatabaseDetails
{
Add-MainHeader
Add-SqlSupport
Add-MainHeader
Add-SqlSupport
Add-ErDiagramGenerator
Add-TableDetails
}
Expand Down

0 comments on commit 7a14b85

Please sign in to comment.