-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathDBDiff.php
More file actions
70 lines (58 loc) · 2.21 KB
/
DBDiff.php
File metadata and controls
70 lines (58 loc) · 2.21 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
<?php namespace DBDiff;
use DBDiff\Params\ParamsFactory;
use DBDiff\DB\DiffCalculator;
use DBDiff\SQLGen\SQLGenerator;
use DBDiff\Logger;
use DBDiff\Templater;
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.
*
* @param object $params A params object (DefaultParams-shaped stdClass or subclass).
* @return array{empty: bool, up: string, down: string}
*/
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' => ''];
}
$sqlGenerator = new SQLGenerator($diff);
$up = ($params->include !== 'down') ? $sqlGenerator->getUp() : '';
$down = ($params->include !== 'up') ? $sqlGenerator->getDown() : '';
return ['empty' => false, 'up' => $up, 'down' => $down];
}
}