yeg
2026-09-18 f5ede132185909c530a4a3f84b8a27d3935cba8a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Drops every table in the MySQL test schema so the next test run rebuilds it from db/migration.
#
# Why this exists instead of "drop database":
#   * the checkout has no mysql client and no Docker, so the script uses the JDBC driver that the
#     application already depends on;
#   * dropping and recreating the schema through Flyway is the only supported way to get back in
#     sync after a migration was edited, and it is fast because the test profile switches off the
#     100k-row bank-branch import (see docs/agents/testing.md).
#
# Usage:
#   .\scripts\reset-test-db.ps1
#   .\scripts\reset-test-db.ps1 -Database talent_card_test -Port 8134 -User root -Password 123456
#
# The dev schema (talent_card) is refused on purpose: it holds tables that no migration creates
# (hand-made in the shared dev database), so dropping them loses data that cannot be rebuilt from
# db/migration. See "不要重建开发库" in docs/agents/testing.md.
param(
    [string] $Database = '',
    [string] $DatabaseHost = '',
    [int]    $Port = 0,
    [string] $User = '',
    [string] $Password = ''
)
 
$ErrorActionPreference = 'Stop'
 
function Get-DefaultDatabase {
    if ($env:TEST_DATASOURCE_URL -and $env:TEST_DATASOURCE_URL -match '/[^/?]+\?') {
        return [regex]::Match($env:TEST_DATASOURCE_URL, '/([^/?]+)\?').Groups[1].Value
    }
    return 'talent_card_test'
}
 
function Get-DefaultHost {
    if ($env:TEST_DATASOURCE_URL -and $env:TEST_DATASOURCE_URL -match '//([^:/]+)') {
        return [regex]::Match($env:TEST_DATASOURCE_URL, '//([^:/]+)').Groups[1].Value
    }
    return '127.0.0.1'
}
 
function Get-DefaultPort {
    if ($env:TEST_DATASOURCE_URL -and $env:TEST_DATASOURCE_URL -match '//[^:/]+:(\d+)') {
        return [int][regex]::Match($env:TEST_DATASOURCE_URL, '//[^:/]+:(\d+)').Groups[1].Value
    }
    return 8134
}
 
function Find-MySqlDriver {
    $roots = @()
    if ($env:MAVEN_REPO_LOCAL) { $roots += $env:MAVEN_REPO_LOCAL }
    $settings = Join-Path $env:USERPROFILE '.m2\settings.xml'
    if (Test-Path $settings) {
        try {
            [xml] $document = Get-Content $settings
            if ($document.settings.localRepository) { $roots += $document.settings.localRepository }
        } catch {
            Write-Host "Could not read $settings; falling back to the default repository path." -ForegroundColor Yellow
        }
    }
    $roots += (Join-Path $env:USERPROFILE '.m2\repository')
 
    foreach ($root in $roots) {
        if (-not $root -or -not (Test-Path $root)) { continue }
        $jar = Get-ChildItem -Path $root -Recurse -Filter 'mysql-connector-j-*.jar' -ErrorAction SilentlyContinue |
            Where-Object { $_.Name -notmatch 'sources|javadoc' } |
            Sort-Object FullName -Descending |
            Select-Object -First 1
        if ($jar) { return $jar.FullName }
    }
    return $null
}
 
function Get-JavaExecutable {
    $candidates = @()
    if ($env:JAVA_HOME) { $candidates += (Join-Path $env:JAVA_HOME 'bin\java.exe') }
    $candidates += 'java'
    foreach ($candidate in $candidates) {
        $command = Get-Command $candidate -ErrorAction SilentlyContinue
        if ($command) { return $command.Source }
    }
    throw 'No java executable found. Set JAVA_HOME or add java to PATH.'
}
 
if (-not $Database)   { $Database = Get-DefaultDatabase }
if (-not $DatabaseHost) { $DatabaseHost = Get-DefaultHost }
if ($Port -le 0)      { $Port = Get-DefaultPort }
if (-not $User)       { $User = if ($env:TEST_DATASOURCE_USERNAME) { $env:TEST_DATASOURCE_USERNAME } else { 'root' } }
if (-not $Password)   { $Password = if ($env:TEST_DATASOURCE_PASSWORD) { $env:TEST_DATASOURCE_PASSWORD } else { '123456' } }
 
if ($Database -eq 'talent_card') {
    throw "拒绝重置开发库 talent_card:它含有迁移从未创建的历史表(如 dependent_card_apply、merchant_benefit_rule),删表会丢数据且无法从 db/migration 重建。详见 docs/agents/testing.md。"
}
 
$driver = Find-MySqlDriver
if (-not $driver) {
    throw 'mysql-connector-j was not found in the Maven local repository. Run ".\mvnw.cmd -o test-compile" once so Maven downloads the project dependencies.'
}
 
$java = Get-JavaExecutable
$program = Join-Path $PSScriptRoot 'ResetTestDatabase.java'
$url = "jdbc:mysql://${DatabaseHost}:${Port}/${Database}?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&useSSL=false"
 
Write-Host "Resetting $Database on ${DatabaseHost}:${Port} ..." -ForegroundColor Cyan
& $java --class-path $driver $program $url $User $Password
if ($LASTEXITCODE -ne 0) { throw "Reset failed (exit $LASTEXITCODE). Driver: $driver" }