-
Notifications
You must be signed in to change notification settings - Fork 159
Expand file tree
/
Copy pathDBSchema.php
More file actions
75 lines (55 loc) · 2.39 KB
/
DBSchema.php
File metadata and controls
75 lines (55 loc) · 2.39 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
<?php namespace DBDiff\DB\Schema;
use Diff\Differ\ListDiffer;
use DBDiff\Params\ParamsFactory;
use DBDiff\Diff\SetDBCollation;
use DBDiff\Diff\SetDBCharset;
use DBDiff\Diff\DropTable;
use DBDiff\Diff\AddTable;
use DBDiff\Diff\AlterTable;
class DBSchema {
protected $manager;
function __construct($manager) {
$this->manager = $manager;
}
function getDiff() {
$params = ParamsFactory::get();
$driver = $this->manager->getDriver();
$diffs = [];
// Collation & Charset — MySQL only
if ($driver === 'mysql') {
$dbName = $this->manager->getDB('target')->getDatabaseName();
$sourceCollation = $this->manager->getDBVariable('source', 'collation_database');
$targetCollation = $this->manager->getDBVariable('target', 'collation_database');
if ($sourceCollation !== $targetCollation) {
$diffs[] = new SetDBCollation($dbName, $sourceCollation, $targetCollation);
}
$sourceCharset = $this->manager->getDBVariable('source', 'character_set_database');
$targetCharset = $this->manager->getDBVariable('target', 'character_set_database');
if ($sourceCharset !== $targetCharset) {
$diffs[] = new SetDBCharset($dbName, $sourceCharset, $targetCharset);
}
}
// Tables
$tableSchema = new TableSchema($this->manager);
$sourceTables = $this->manager->getTables('source');
$targetTables = $this->manager->getTables('target');
if (isset($params->tablesToIgnore)) {
$sourceTables = array_diff($sourceTables, $params->tablesToIgnore);
$targetTables = array_diff($targetTables, $params->tablesToIgnore);
}
$addedTables = array_diff($sourceTables, $targetTables);
foreach ($addedTables as $table) {
$diffs[] = new AddTable($table, $this->manager, 'source');
}
$commonTables = array_intersect($sourceTables, $targetTables);
foreach ($commonTables as $table) {
$tableDiff = $tableSchema->getDiff($table);
$diffs = array_merge($diffs, $tableDiff);
}
$deletedTables = array_diff($targetTables, $sourceTables);
foreach ($deletedTables as $table) {
$diffs[] = new DropTable($table, $this->manager, 'target');
}
return $diffs;
}
}