Files
public/Tools/mysql_model/gen.ps1

20 lines
882 B
PowerShell

# gen.ps1 - 支持参数版
param(
[Parameter(Mandatory=$true)]
[string]$dbName
)
& .\gentool.exe `
-dsn "root:gR9pV4tY7zR6qL3e@tcp(127.0.0.1:3306)/${dbName}?charset=utf8mb4&parseTime=True&loc=Local" `
-fieldSignable `
-outPath "./${dbName}/query"
Get-ChildItem ./${dbName}/model/*.gen.go | ForEach-Object {
$c = Get-Content $_.FullName -Raw -Encoding UTF8
if ($c -match '\bSn\s+int64\b' -and $c -notmatch 'BeforeCreate') {
$c -match 'type\s+(\w+)\s+struct' | Out-Null
$n = $matches[1]
$c = $c -replace '(?s)(import\s*\([^)]*)', "`$1`t`"git.hlsq.asia/mmorpg/service-common/utils`"`n"
$hook = "`n`n// Auto sn`nfunc (m *$n) BeforeCreate(_ *gorm.DB) error {`n`tif m.Sn == 0 {`n`t`tm.Sn = utils.SnowflakeInstance().Generate().Int64()`n`t}`n`treturn nil`n}"
Set-Content $_.FullName ($c.TrimEnd() + $hook) -Encoding UTF8
}
}