-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathDBDiff.php
More file actions
87 lines (73 loc) · 3.02 KB
/
Copy pathDBDiff.php
File metadata and controls
87 lines (73 loc) · 3.02 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
<?php namespace DBDiff;
use DBDiff\Params\ParamsFactory;
use DBDiff\DB\DiffCalculator;
use DBDiff\SQLGen\SQLGenerator;
use DBDiff\Logger;
use DBDiff\Templater;
use DBDiff\Linter\DestructiveLinter;
use DBDiff\Exceptions\DestructiveChangeException;
class DBDiff {
/**
* Legacy entry point — reads params from the CLI via aura/cli and writes
* migration.sql (or the --output path) using the Templater.
*
* Kept for backward compatibility; new code should call getDiffResult()
* directly and handle output with the format/template of their choice.
*
* @param object|null $params Pre-built params object (bypasses CLIGetter when supplied).
*/
public function run($params = null): void
{
try {
if ($params === null) {
$params = ParamsFactory::get();
}
$result = $this->getDiffResult($params);
if ($result['empty']) {
Logger::info("Identical resources");
} else {
$templater = new Templater($params, $result['up'], $result['down']);
$templater->output();
}
Logger::success("Completed");
} catch (\Exception $e) {
Logger::error($e->getMessage());
throw $e;
}
}
/**
* Compute the diff and return raw UP / DOWN SQL.
*
* This is the lower-level method used by DiffCommand so that the caller
* can apply any output format (Flyway, Liquibase, Laravel, native…) rather
* than being forced through the Templater.
*
* When $params->allowDestructive is false (the default) and the diff
* contains destructive schema changes (DROP TABLE, DROP COLUMN, etc.), a
* DestructiveChangeException is thrown before any SQL is generated.
* Pass --allow-destructive on the CLI (or set $params->allowDestructive = true)
* to bypass the check.
*
* @param object $params A params object (DefaultParams-shaped stdClass or subclass).
* @return array{empty: bool, up: string, down: string, lint?: \DBDiff\Linter\LintResult}
* @throws DestructiveChangeException
*/
public function getDiffResult(object $params): array
{
$diffCalculator = new DiffCalculator;
$diff = $diffCalculator->getDiff($params);
if (empty($diff['schema']) && empty($diff['data'])) {
return ['empty' => true, 'up' => '', 'down' => ''];
}
$allowDestructive = $params->allowDestructive ?? false;
$linter = new DestructiveLinter();
$lintResult = $linter->lint($diff);
if ($lintResult->hasErrors() && !$allowDestructive) {
throw new DestructiveChangeException($lintResult);
}
$sqlGenerator = new SQLGenerator($diff);
$up = ($params->include !== 'down') ? $sqlGenerator->getUp() : '';
$down = ($params->include !== 'up') ? $sqlGenerator->getDown() : '';
return ['empty' => false, 'up' => $up, 'down' => $down, 'lint' => $lintResult];
}
}