/
Version20180926070300.php
73 lines (68 loc) · 3.28 KB
/
Version20180926070300.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
<?php
namespace Concrete\Core\Updater\Migrations\Migrations;
use Concrete\Core\Entity\Express\Entity as ExpressEntity;
use Concrete\Core\Updater\Migrations\AbstractMigration;
use Concrete\Core\Updater\Migrations\RepeatableMigrationInterface;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\ORM\EntityManagerInterface;
class Version20180926070300 extends AbstractMigration implements RepeatableMigrationInterface
{
public function upgradeSchema(Schema $schema)
{
$this->fixAttributeIndexTable($schema, 'FileSearchIndexAttributes', 'fID', 'Files', 'fID');
$this->fixAttributeIndexTable($schema, 'CollectionSearchIndexAttributes', 'cID', 'Collections', 'cID');
$this->fixAttributeIndexTable($schema, 'SiteSearchIndexAttributes', 'siteID', 'Sites', 'siteID');
$this->fixAttributeIndexTable($schema, 'UserSearchIndexAttributes', 'uID', 'Users', 'uID');
$entityManager = $this->app->make(EntityManagerInterface::class);
$expressEntityRepository = $entityManager->getRepository(ExpressEntity::class);
foreach ($expressEntityRepository->findAll() as $expressEntity) {
$this->fixExpressEntityAttributeIndexTable($schema, $expressEntity);
}
}
/**
* @param \Doctrine\DBAL\Schema\Schema $schema
* @param string $indexTableName
* @param string $indexColumnName
* @param string $foreignTableName
* @param string $foreignColumn
*/
private function fixAttributeIndexTable(Schema $schema, $indexTableName, $indexColumnName, $foreignTableName, $foreignColumn)
{
$this->output("Fixing attribute index table {$indexTableName}...");
$sm = $this->connection->getSchemaManager();
$schemaTables = $sm->listTableNames();
if (in_array($indexTableName, $schemaTables)) {
$this->deleteInvalidForeignKey($indexTableName, $indexColumnName, $foreignTableName, $foreignColumn);
$indexTable = $schema->getTable($indexTableName);
$indexColumn = $indexTable->getColumn($indexColumnName);
$indexColumn->setDefault(null);
$indexTable->addForeignKeyConstraint(
$foreignTableName,
[$indexColumnName],
[$foreignColumn],
['onUpdate' => 'CASCADE', 'onDelete' => 'CASCADE']
);
} else {
$this->output(t('Could not locate table %s, skipping...', $indexTableName));
}
}
/**
* @param \Doctrine\DBAL\Schema\Schema $schema
* @param \Concrete\Core\Entity\Express\Entity $expressEntity
*/
private function fixExpressEntityAttributeIndexTable(Schema $schema, ExpressEntity $expressEntity)
{
$this->output('Fixing express index table for ' . $expressEntity->getName() . '...');
$attributeCategory = $expressEntity->getAttributeKeyCategory();
/* @var \Concrete\Core\Attribute\Category\ExpressCategory $attributeCategory */
$indexTableName = $attributeCategory->getIndexedSearchTable();
$indexTable = $schema->getTable($indexTableName);
$exEntryIDColumn = $indexTable->getColumn('exEntryID');
$exEntryIDColumn->setUnsigned(false);
$this->fixAttributeIndexTable(
$schema,
$indexTableName, 'exEntryID',
'ExpressEntityEntries', 'exEntryID'
);
}
}