Skip to content

Instantly share code, notes, and snippets.

@panasenco
Created June 7, 2021 23:45
Show Gist options
  • Save panasenco/31d4c12f0cadce91f576a818018970a0 to your computer and use it in GitHub Desktop.
Save panasenco/31d4c12f0cadce91f576a818018970a0 to your computer and use it in GitHub Desktop.
Converts dbt manifest.json to PlantUML ERD diagram. It's not pretty but it's readable.
#!/usr/bin/env pwsh
<#
.Synopsis
Creates a PlantUML ERD diagram from a manifest.json file.
.Parameter Path
Path to the manifest.json file. Defaults to .\target\manifest.json
#>
[CmdletBinding()]
param (
[string] $Path = ".\target\manifest.json"
)
$ErrorActionPreference = "Stop"
function Sanitize-ColumnName {
param (
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[string] $ColumnName
)
process {
# Strip text in leading parentheses
$ColumnName -replace '^\s*\([^\)]*\)\s*'
}
}
"@startuml"
"hide circle"
"skinparam linetype ortho"
# "left to right direction"
$Manifest = Get-Content -Raw -Path $Path | ConvertFrom-Json
$Manifest.nodes.PSObject.Properties | where { $_.MemberType -eq 'NoteProperty' } | foreach {
$Value = $_.Value
switch -Regex ($_.Name) {
'model\..*' {
"entity `"$($Value.name)`" as $($Value.name) {"
$Value.columns.PSObject.Properties | foreach {
" $($_.Value.name | Sanitize-ColumnName) : $($_.Value.type)"
}
"}"
}
'test\.[^\.]+\.relationships_.*' {
$OutTable = $Value.refs[1]
$OutColumn = $Value.column_name | Sanitize-ColumnName
$InTable = $Value.refs[0]
$InColumn = ($Value.name -split '__')[-2]
"$OutTable::$OutColumn ---> $InTable::$InColumn"
}
}
}
"@enduml"
@ciejer
Copy link

ciejer commented Jun 23, 2021

Hi @panasenco - seems we're thinking on the same path. I have an interactive catalog for dbt under development, and one of my upcoming features is in this space - have you put thought into what a good looking solution to this could look like?
ciejer/tangata_local#6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment