Completed
Push — master ( 7d8618...7f8cfb )
by Tim
06:46
created

executeUpdateForTableFieldDateTimeMigration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HDNET\Calendarize\Updates;
6
7
use TYPO3\CMS\Core\Database\ConnectionPool;
8
use TYPO3\CMS\Core\Database\Query\QueryBuilder;
9
use TYPO3\CMS\Core\Utility\GeneralUtility;
10
use TYPO3\CMS\Install\Updates\UpgradeWizardInterface;
11
12
class DateFieldUpdate implements UpgradeWizardInterface
13
{
14
    public function getIdentifier(): string
15
    {
16
        return 'calendarizeDateField';
17
    }
18
19
    public function getTitle(): string
20
    {
21
        return 'Calendarize Date Field';
22
    }
23
24
    public function getDescription(): string
25
    {
26
        return '';
27
    }
28
29
    protected $migrationMap = [
30
        'tx_calendarize_domain_model_configuration' => [
31
            'start_date',
32
            'end_date',
33
        ],
34
        'tx_calendarize_domain_model_index' => [
35
            'start_date',
36
            'end_date',
37
        ],
38
    ];
39
40
    public function executeUpdate(): bool
41
    {
42
        foreach ($this->migrationMap as $table => $fields) {
43
            foreach ($fields as $field) {
44
                if ($this->updateNecessaryForTableFieldDateTimeMigration($table, $field)) {
45
                    $this->executeUpdateForTableFieldDateTimeMigration($table, $field);
46
                }
47
            }
48
        }
49
50
        return true;
51
    }
52
53
    protected function executeUpdateForTableFieldDateTimeMigration(string $tableName, string $fieldName)
54
    {
55
        $tmpField = $fieldName . '_' . GeneralUtility::shortMD5((string)microtime(), 5);
56
57
        $sqlQueries = [
58
            'ALTER TABLE ' . $tableName . ' ADD COLUMN ' . $tmpField . ' int(11) NOT NULL DEFAULT \'0\'',
59
            'UPDATE ' . $tableName . ' SET ' . $tmpField . ' = ' . $fieldName,
60
            'ALTER TABLE ' . $tableName . ' CHANGE ' . $fieldName . ' ' . $fieldName . ' int(11) DEFAULT NULL',
61
            'UPDATE ' . $tableName . ' SET ' . $fieldName . ' = null',
62
            'ALTER TABLE ' . $tableName . ' CHANGE ' . $fieldName . ' ' . $fieldName . ' DATE NULL default NULL',
63
            'UPDATE ' . $tableName . ' SET ' . $fieldName . ' = FROM_UNIXTIME(' . $tmpField . ')',
64
            'UPDATE ' . $tableName . ' SET ' . $fieldName . ' = NULL WHERE ' . $fieldName . ' = \'1970-01-01\'',
65
        ];
66
67
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
68
        foreach ($sqlQueries as $sqlQuery) {
69
            $connection->executeQuery($sqlQuery);
70
        }
71
    }
72
73
    public function updateNecessary(): bool
74
    {
75
        foreach ($this->migrationMap as $table => $fields) {
76
            foreach ($fields as $field) {
77
                if ($this->updateNecessaryForTableFieldDateTimeMigration($table, $field)) {
78
                    return true;
79
                }
80
            }
81
        }
82
83
        return false;
84
    }
85
86
    protected function updateNecessaryForTableFieldDateTimeMigration(string $tableName, string $fieldName)
87
    {
88
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
89
        /** @var QueryBuilder $queryBuilder */
90
        $schemaManager = $queryBuilder->getConnection()->getSchemaManager();
91
        if (!$schemaManager->tablesExist($tableName)) {
92
            return false;
93
        }
94
95
        $columns = $schemaManager->listTableColumns($tableName);
96
        if (!isset($columns[$fieldName]) || !($columns[$fieldName] instanceof \Doctrine\DBAL\Schema\Column)) {
97
            return false;
98
        }
99
        /** @var \Doctrine\DBAL\Schema\Column $checkField */
100
        $checkField = $columns[$fieldName];
101
        return !($checkField->getType() instanceof \Doctrine\DBAL\Types\DateType);
102
    }
103
104
    public function getPrerequisites(): array
105
    {
106
        // No DB Update is needed
107
        return [];
108
    }
109
}
110