-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathDBDiffComprehensivePostgresTest.php
More file actions
135 lines (117 loc) · 4.35 KB
/
DBDiffComprehensivePostgresTest.php
File metadata and controls
135 lines (117 loc) · 4.35 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
require_once __DIR__ . '/AbstractComprehensiveTest.php';
/**
* PostgreSQL implementation of the comprehensive test suite.
* All test methods live in AbstractComprehensiveTest.
*
* Skips automatically when:
* - pdo_pgsql is not loaded
* - DB_HOST_POSTGRES is not set (not running inside a Postgres CLI container)
*/
class DBDiffComprehensivePostgresTest extends AbstractComprehensiveTest
{
private $host;
private $port = 5432;
private $user = 'dbdiff';
private $pass = 'rootpass';
private $defaultDb = 'diff1'; // pre-created by POSTGRES_DB in compose
/** @var \PDO Connection used for DDL (CREATE / DROP DATABASE) */
private $adminDb;
/** @var int Detected Postgres major version */
private $pgMajorVersion;
// ── Abstract method implementations ───────────────────────────────────
protected function connectAndBootstrap(): void
{
if (!extension_loaded('pdo_pgsql')) {
$this->markTestSkipped('pdo_pgsql extension not loaded — skipping Postgres comprehensive tests.');
}
$this->host = getenv('DB_HOST_POSTGRES') ?: null;
if (!$this->host) {
$this->markTestSkipped('DB_HOST_POSTGRES env var not set — skipping Postgres comprehensive tests.');
}
$this->db1 = 'dbdiff_comp1';
$this->db2 = 'dbdiff_comp2';
$this->adminDb = $this->connectWithRetry(
"pgsql:host={$this->host};port={$this->port};dbname={$this->defaultDb}",
$this->user,
$this->pass
);
// Detect major version early so getVersionSuffix() works during the test
$row = $this->adminDb->query(
"SELECT current_setting('server_version_num') AS v"
)->fetch(PDO::FETCH_ASSOC);
$this->pgMajorVersion = intdiv((int) ($row['v'] ?? 0), 10000);
$this->adminDb->exec("DROP DATABASE IF EXISTS {$this->db1}");
$this->adminDb->exec("DROP DATABASE IF EXISTS {$this->db2}");
$this->adminDb->exec("CREATE DATABASE {$this->db1}");
$this->adminDb->exec("CREATE DATABASE {$this->db2}");
}
protected function getVersionSuffix(): string
{
return 'pgsql_' . $this->pgMajorVersion;
}
protected function loadFixture(string $fixtureName): void
{
foreach (['db1' => $this->db1, 'db2' => $this->db2] as $key => $dbName) {
$file = "tests/fixtures/{$fixtureName}/{$key}-pgsql.sql";
if (!file_exists($file)) {
$this->fail("Postgres fixture not found: $file");
}
$pdo = new PDO(
"pgsql:host={$this->host};port={$this->port};dbname={$dbName}",
$this->user,
$this->pass
);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec(file_get_contents($file));
}
}
protected function driverArgs(): array
{
return [
'--driver=pgsql',
"--server1={$this->user}:{$this->pass}@{$this->host}:{$this->port}",
];
}
protected function dbInputArg(): string
{
return "server1.{$this->db1}:server1.{$this->db2}";
}
protected function tableInputArg(string $table): ?string
{
return "server1.{$this->db1}.{$table}:server1.{$this->db2}.{$table}";
}
protected function getServerConfig(): array
{
return [
'user' => $this->user,
'password' => $this->pass,
'host' => $this->host,
'port' => $this->port,
];
}
protected function configDefaults(): array
{
return [
'driver' => 'pgsql',
'type' => 'all',
'include' => 'all',
'nocomments' => true,
];
}
protected function tearDownDatabases(): void
{
if (!$this->adminDb) {
return;
}
// Terminate active connections before dropping
foreach ([$this->db1, $this->db2] as $db) {
$this->adminDb->exec(
"SELECT pg_terminate_backend(pid) " .
"FROM pg_stat_activity " .
"WHERE datname = '$db' AND pid <> pg_backend_pid()"
);
$this->adminDb->exec("DROP DATABASE IF EXISTS $db");
}
}
}