-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathDBManager.php
More file actions
113 lines (91 loc) · 3.99 KB
/
DBManager.php
File metadata and controls
113 lines (91 loc) · 3.99 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
<?php namespace DBDiff\DB;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Database\Events\StatementPrepared;
use Illuminate\Events\Dispatcher;
use DBDiff\DB\Adapters\AdapterFactory;
use DBDiff\DB\Adapters\DBAdapterInterface;
use DBDiff\Exceptions\DBException;
class DBManager {
protected Capsule $capsule;
protected DBAdapterInterface $adapter;
protected string $driver = 'mysql';
function __construct() {
$this->capsule = new Capsule;
$dispatcher = new Dispatcher();
$dispatcher->listen(StatementPrepared::class, function ($event) {
$event->statement->setFetchMode(\PDO::FETCH_ASSOC);
});
$this->capsule->setEventDispatcher($dispatcher);
// Default adapter; overridden by connect() once params are available.
$this->adapter = AdapterFactory::create('mysql');
}
public function connect($params): void {
$this->driver = $params->driver ?? 'mysql';
$this->adapter = AdapterFactory::create($this->driver);
foreach ($params->input as $key => $input) {
if ($key === 'kind') {
continue;
}
// SQLite uses the db value as a file path; no server block required.
if ($this->driver === 'sqlite') {
$config = $this->adapter->buildConnectionConfig([], $input['db']);
} else {
$server = $params->{$input['server']};
// Allow top-level params like --supabase to add sslmode to server cfg.
if (isset($params->sslmode)) {
$server['sslmode'] = $params->sslmode;
}
$config = $this->adapter->buildConnectionConfig($server, $input['db']);
}
$this->capsule->addConnection($config, $key);
}
}
public function testResources($params): void {
$this->testResource($params->input['source'], 'source');
$this->testResource($params->input['target'], 'target');
}
public function testResource($input, string $res): void {
try {
$this->capsule->getConnection($res);
} catch (\Exception $e) {
throw new DBException("Can't connect to $res database: " . $e->getMessage());
}
if (!empty($input['table'])) {
try {
$this->capsule->getConnection($res)->table($input['table'])->first();
} catch (\Exception $e) {
throw new DBException("Can't access table `{$input['table']}` on $res: " . $e->getMessage());
}
}
}
public function getDB(string $res) {
return $this->capsule->getConnection($res);
}
public function getAdapter(): DBAdapterInterface {
return $this->adapter;
}
public function getDriver(): string {
return $this->driver;
}
// -------------------------------------------------------------------------
// Convenience wrappers (delegate to the active adapter)
// -------------------------------------------------------------------------
public function getTables(string $connection): array {
return $this->adapter->getTables($this->getDB($connection));
}
public function getColumns(string $connection, string $table): array {
return $this->adapter->getColumns($this->getDB($connection), $table);
}
public function getKey(string $connection, string $table): array {
return $this->adapter->getPrimaryKey($this->getDB($connection), $table);
}
public function getCreateStatement(string $connection, string $table): string {
return $this->adapter->getCreateStatement($this->getDB($connection), $table);
}
public function getTableSchema(string $connection, string $table): array {
return $this->adapter->getTableSchema($this->getDB($connection), $table);
}
public function getDBVariable(string $connection, string $variable): ?string {
return $this->adapter->getDBVariable($this->getDB($connection), $variable);
}
}