-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathMigrationConfig.php
More file actions
344 lines (295 loc) · 12.8 KB
/
MigrationConfig.php
File metadata and controls
344 lines (295 loc) · 12.8 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php namespace DBDiff\Migration\Config;
use DBDiff\Migration\Exceptions\ConfigException;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
/**
* Loads and exposes configuration for the DBDiff migration module.
*
* Configuration is read from a YAML file (default: dbdiff.yml in the current
* working directory) and may be partially overridden by runtime options passed
* as an array to the constructor.
*
* Minimal dbdiff.yml example:
* ─────────────────────────────
* database:
* driver: mysql # mysql | pgsql | sqlite
* host: 127.0.0.1
* port: 3306
* name: mydb
* user: root
* password: secret
*
* migrations:
* dir: ./migrations # where .up.sql / .down.sql files live
* history_table: _dbdiff_migrations # tracking table name
* out_of_order: false # allow applying older versions after newer
* ─────────────────────────────
*
* You may also supply a full DSN URL instead of individual fields:
* database:
* url: postgres://user:[email protected]:5432/postgres
*
* For Supabase, use either the direct connection URL or the session-mode
* pooler URL (port 6543). SSL is enabled automatically when a Supabase host
* is detected.
*
* For SQLite, omit host/port/user/password and use `path` instead of `name`:
* database:
* driver: sqlite
* path: ./database.sqlite
*/
class MigrationConfig
{
private array $raw = [];
// ── Database ─────────────────────────────────────────────────────────────
public string $driver = 'mysql';
public string $host = '127.0.0.1';
public int $port = 3306;
public string $dbName = '';
public string $dbPath = ''; // SQLite file path
public string $user = '';
public string $password = '';
public string $sslMode = '';
/**
* Whether to configure the connection for pgbouncer / connection-pooler mode.
* Automatically set to true when a Supabase pooler URL (port 6543) is used.
* In pooler mode, server-side prepared statements are disabled.
*/
public bool $pgbouncer = false;
// ── Migrations ───────────────────────────────────────────────────────────
public string $migrationsDir = './migrations';
public string $historyTable = '_dbdiff_migrations';
public bool $outOfOrder = false;
/**
* Migration file format: 'native' (default) or 'supabase'.
* native — {version}_{desc}.up.sql + optional .down.sql
* supabase — {version}_{desc}.sql (UP-only, no DOWN concept)
*
* Auto-set to 'supabase' when a Supabase project is detected and no
* explicit format was configured. Commands use this as their default
* when no --format flag is supplied.
*/
public string $migrationFormat = 'native';
// ── Supabase project detection (Phase 1 + 4) ─────────────────────────────
/** Whether a supabase/config.toml was auto-detected in cwd or a parent. */
public bool $isSupabaseProject = false;
/** Absolute path to the directory containing supabase/config.toml. */
public string $supabaseProjectRoot = '';
// ─────────────────────────────────────────────────────────────────────────
/** Tracks whether migrationsDir was set from config file or CLI override. */
private bool $migrationsDirExplicit = false;
/** Tracks whether migrationFormat was set from config file or CLI override. */
private bool $migrationFormatExplicit = false;
/**
* @param string|null $configFile Explicit path to a YAML config file.
* Pass null to auto-detect dbdiff.yml in cwd.
* @param array $overrides Key-value pairs that override file settings.
* Keys mirror the YAML structure flattened:
* 'db_url' — full DSN URL (highest precedence)
* 'driver', 'host', 'port', 'name', 'path',
* 'user', 'password', 'sslmode', 'pgbouncer',
* 'migrations_dir', 'history_table', 'out_of_order',
* 'migration_format' — 'native' or 'supabase'
*/
public function __construct(?string $configFile = null, array $overrides = [])
{
$file = $configFile ?? $this->detect();
if ($file !== null) {
$this->loadFile($file);
}
$this->applyOverrides($overrides);
$this->detectSupabaseProject();
}
// ── Public helpers ────────────────────────────────────────────────────────
/**
* Build an Illuminate-compatible connection config array ready for
* Capsule::addConnection().
*/
public function toConnectionConfig(): array
{
switch ($this->driver) {
case 'sqlite':
return [
'driver' => 'sqlite',
'database' => $this->dbPath,
'prefix' => '',
];
case 'pgsql':
$cfg = [
'driver' => 'pgsql',
'host' => $this->host,
'port' => $this->port,
'database' => $this->dbName,
'username' => $this->user,
'password' => $this->password,
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
];
if ($this->sslMode) {
$cfg['sslmode'] = $this->sslMode;
}
// pgbouncer (transaction-mode pooler) requires server-side
// prepared statements to be disabled. Session-mode pooler
// (Supabase port 6543) works fine without this, but it's
// harmless to set in either case.
if ($this->pgbouncer) {
$cfg['options'] = [
\PDO::ATTR_EMULATE_PREPARES => true,
];
}
return $cfg;
default: // mysql
return [
'driver' => 'mysql',
'host' => $this->host,
'port' => $this->port,
'database' => $this->dbName,
'username' => $this->user,
'password' => $this->password,
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
];
}
}
/** Returns the resolved absolute path to the migrations directory. */
public function resolveMigrationsDir(): string
{
$dir = $this->migrationsDir;
if (!str_starts_with($dir, '/')) {
$dir = getcwd() . '/' . ltrim($dir, './');
}
return rtrim($dir, '/');
}
// ── Private helpers ───────────────────────────────────────────────────────
/**
* Auto-detect a Supabase project by walking up from cwd.
* Only runs when neither migrationsDir nor migrationFormat was explicitly set.
* Sets $isSupabaseProject, $supabaseProjectRoot, $migrationsDir, $migrationFormat.
*/
private function detectSupabaseProject(): void
{
$root = SupabaseProjectDetector::find();
if ($root === null) {
return;
}
$this->isSupabaseProject = true;
$this->supabaseProjectRoot = $root;
if (!$this->migrationsDirExplicit) {
$this->migrationsDir = SupabaseProjectDetector::migrationsDir($root);
}
if (!$this->migrationFormatExplicit) {
$this->migrationFormat = 'supabase';
}
}
private function detect(): ?string
{
$candidates = [
getcwd() . '/dbdiff.yml',
getcwd() . '/dbdiff.yaml',
getcwd() . '/.dbdiff.yml',
];
foreach ($candidates as $candidate) {
if (file_exists($candidate)) {
return $candidate;
}
}
return null;
}
private function loadFile(string $path): void
{
if (!file_exists($path)) {
throw new ConfigException("Config file not found: {$path}");
}
try {
$this->raw = Yaml::parseFile($path) ?? [];
} catch (ParseException $e) {
throw new ConfigException("Failed to parse config file '{$path}': " . $e->getMessage());
}
// Populate database section
$db = $this->raw['database'] ?? [];
// A `url` key takes precedence over individual fields
if (!empty($db['url'])) {
$this->applyDsn($db['url']);
}
// Individual fields override URL-parsed values if both are present
$this->driver = $db['driver'] ?? $this->driver;
$this->host = $db['host'] ?? $this->host;
$this->port = (int) ($db['port'] ?? $this->port);
$this->dbName = $db['name'] ?? $this->dbName;
$this->dbPath = $db['path'] ?? $this->dbPath;
$this->user = $db['user'] ?? $this->user;
$this->password = $db['password'] ?? $this->password;
$this->sslMode = $db['sslmode'] ?? $this->sslMode;
$this->pgbouncer = (bool) ($db['pgbouncer'] ?? $this->pgbouncer);
// Populate migrations section
$m = $this->raw['migrations'] ?? [];
if (isset($m['dir'])) {
$this->migrationsDir = $m['dir'];
$this->migrationsDirExplicit = true;
}
if (isset($m['format'])) {
$this->migrationFormat = $m['format'];
$this->migrationFormatExplicit = true;
}
$this->historyTable = $m['history_table'] ?? $this->historyTable;
$this->outOfOrder = (bool) ($m['out_of_order'] ?? $this->outOfOrder);
}
/**
* Parse a DSN URL and populate connection properties from it.
* Calls DsnParser::parse() internally — see that class for supported URL formats.
*/
private function applyDsn(string $url): void
{
$parsed = DsnParser::parse($url);
$this->driver = $parsed['driver'];
$this->host = $parsed['host'];
$this->port = $parsed['port'];
$this->dbName = $parsed['name'];
$this->dbPath = $parsed['path'];
$this->user = $parsed['user'];
$this->password = $parsed['password'];
if (!empty($parsed['sslmode'])) {
$this->sslMode = $parsed['sslmode'];
}
if ($parsed['pgbouncer']) {
$this->pgbouncer = true;
}
}
private function applyOverrides(array $overrides): void
{
// A db_url in overrides (e.g. from --db-url CLI flag) wins over everything
if (!empty($overrides['db_url'])) {
$this->applyDsn($overrides['db_url']);
unset($overrides['db_url']);
}
$map = [
'driver' => 'driver',
'host' => 'host',
'port' => 'port',
'name' => 'dbName',
'path' => 'dbPath',
'user' => 'user',
'password' => 'password',
'sslmode' => 'sslMode',
'pgbouncer' => 'pgbouncer',
'migrations_dir' => 'migrationsDir',
'history_table' => 'historyTable',
'out_of_order' => 'outOfOrder',
'migration_format' => 'migrationFormat',
];
foreach ($overrides as $key => $value) {
if (isset($map[$key]) && $value !== null) {
$prop = $map[$key];
$this->$prop = $value;
// Track explicit overrides so Supabase auto-detection doesn't clobber them
if ($key === 'migrations_dir') {
$this->migrationsDirExplicit = true;
} elseif ($key === 'migration_format') {
$this->migrationFormatExplicit = true;
}
}
}
}
}