elder-yudao-pro/deploy/local/initialize.ps1

25 lines
1.8 KiB
PowerShell
Raw Permalink Normal View History

2026-10-05 22:02:14 +08:00
param([string]$SecretsFile = (Join-Path $PSScriptRoot 'secrets.psd1'))
. (Join-Path $PSScriptRoot 'common.ps1')
$settings = Read-LocalSettings $SecretsFile
$databaseName = $settings.DatabaseName
$schemaExists = [int]@(Invoke-LocalMySql $settings "SELECT COUNT(*) FROM information_schema.SCHEMATA WHERE SCHEMA_NAME='$databaseName';")[0]
if ($schemaExists -eq 0) {
Invoke-LocalMySql $settings "CREATE DATABASE ``$databaseName`` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" | Out-Null
}
$tableCount = [int]@(Invoke-LocalMySql $settings "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA='$databaseName';")[0]
if ($tableCount -gt 0) {
throw "Database $databaseName already has $tableCount tables. Initialization is skipped to preserve existing data; run start.ps1 if it is already initialized."
}
$sqlPath = Join-Path $repoPath 'mysql_script/ruoyi-vue-pro.sql'
$sql = Get-Content -LiteralPath $sqlPath -Raw -Encoding UTF8
if ($sql -match '(?im)^\s*(USE\s|CREATE\s+DATABASE\s|DROP\s+DATABASE\s)') {
throw 'The seed script changes the target database; review it before importing.'
}
$sourcePath = $sqlPath.Replace('\', '/')
Invoke-LocalMySql $settings "SOURCE $sourcePath;" $databaseName | Out-Null
# Use the seeded database file store instead of a demonstration cloud bucket.
Invoke-LocalMySql $settings 'UPDATE infra_file_config SET master = (id = 4) WHERE deleted = 0;' $databaseName | Out-Null
$tableCount = @(Invoke-LocalMySql $settings "SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA='$databaseName';")[0]
$adminCount = @(Invoke-LocalMySql $settings "SELECT COUNT(*) FROM system_users WHERE username='admin' AND deleted=0 AND tenant_id=1;" $databaseName)[0]
Write-Output "Initialized $databaseName with $tableCount tables and $adminCount administrator account. Existing databases were not imported into."