/
CompareSchemaCommand.php
133 lines (117 loc) · 4.58 KB
/
CompareSchemaCommand.php
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
<?php
namespace Concrete\Core\Console\Command;
use Concrete\Core\Block\BlockType\BlockTypeList;
use Concrete\Core\Console\Command;
use Concrete\Core\Database\DatabaseStructureManager;
use Concrete\Core\Database\Schema\Schema;
use Concrete\Core\Foundation\Environment;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\Support\Facade\Package;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class CompareSchemaCommand extends Command
{
protected function configure()
{
$okExitCode = static::SUCCESS;
$errExitCode = static::FAILURE;
$this
->setName('c5:compare-schema')
->setDescription('Compares db.xml in Concrete XML schema, Concrete entities, and all installed package schemas and entities with the contents of the database and prints the difference.')
->addEnvOption()
->setHelp(<<<EOT
Returns codes:
$okExitCode operation completed successfully
$errExitCode errors occurred
EOT
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$db = \Database::connection();
$em = $db->getEntityManager();
$cacheDriver = $em->getConfiguration()->getMetadataCacheImpl();
$cacheDriver->flushAll();
$tool = new \Doctrine\ORM\Tools\SchemaTool($em);
$schemas = [];
$sm = $db->getSchemaManager();
$dbSchema = $sm->createSchema();
// core xml tables
$schemas[] = Schema::getCoreXMLSchema();
// core entities
$sm = new DatabaseStructureManager($em);
$entities = $sm->getMetadatas();
$schemas[] = $tool->getSchemaFromMetadata($entities);
// core, application and package block types
$env = Environment::get();
$list = new BlockTypeList();
$list->includeInternalBlockTypes();
foreach ($list->get() as $bt) {
$r = $env->getRecord(DIRNAME_BLOCKS . '/' . $bt->getBlockTypeHandle() . '/' . FILENAME_BLOCK_DB, $bt->getPackageHandle());
if ($r->exists()) {
$parser = Schema::getSchemaParser(simplexml_load_file($r->file));
$parser->setIgnoreExistingTables(false);
$schemas[] = $parser->parse($db);
}
}
// packages
$packages = Package::getInstalledList();
foreach ($packages as $pkg) {
$xmlFile = $pkg->getPackagePath() . '/' . FILENAME_BLOCK_DB;
if (file_exists($xmlFile)) {
$parser = Schema::getSchemaParser(simplexml_load_file($xmlFile));
$parser->setIgnoreExistingTables(false);
$schemas[] = $parser->parse($db);
}
}
// Finalize output.
$comparator = new \Doctrine\DBAL\Schema\Comparator();
$saveQueries = [];
foreach ($schemas as $schema) {
$schemaDiff = $comparator->compare($dbSchema, $schema);
$saveQueries = array_merge($saveQueries, $schemaDiff->toSaveSql($db->getDatabasePlatform()));
}
$saveQueries = $this->filterQueries($saveQueries);
if (count($saveQueries)) {
$output->writeln(t2('%s query found', '%s queries found', count($saveQueries)));
$i = 1;
foreach ($saveQueries as $query) {
$output->writeln(sprintf('%s: %s', $i, $query));
++$i;
}
} else {
$output->writeln(t('No differences found between schema and database.'));
}
return static::SUCCESS;
}
/**
* Filter out all the queries that are platform specific that
* Doctrine doens't give us a good way to deal with. This is mostly
* index lengths that are set in installation that Doctrine doesn't
* support.
*
* @param string[] $queries
*
* @return string[]
*/
protected function filterQueries($queries)
{
$app = Application::getFacadeApplication();
$config = $app->make('config');
$textIndexDrops = [];
foreach ($config->get('database.text_indexes') as $indexTable => $indexDefinition) {
foreach (array_keys($indexDefinition) as $indexName) {
$textIndexDrops[] = strtolower("DROP INDEX {$indexName} ON {$indexTable}");
}
}
$returnQueries = [];
foreach ($queries as $query) {
$queryLowerCase = strtolower($query);
if (!in_array($queryLowerCase, $textIndexDrops, true)) {
$returnQueries[] = $query;
}
}
return $returnQueries;
}
}