Completed
Push — master ( 68129f...3bc187 )
by Tim
02:15 queued 10s
created

DateFieldUpdate::updateNecessary()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 4
nc 4
nop 0
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
11
class DateFieldUpdate extends AbstractUpdate
12
{
13
    protected $title = 'Calendarize Date Field';
14
15
    protected $description = 'This wizard migrates the existing start and end dates in the ' .
16
        'database from a timestamp to a real date. This enables dates before 1970 and after 2038.';
17
18
    protected $migrationMap = [
19
        'tx_calendarize_domain_model_configuration' => [
20
            'start_date',
21
            'end_date',
22
        ],
23
        'tx_calendarize_domain_model_index' => [
24
            'start_date',
25
            'end_date',
26
        ],
27
    ];
28
29
    public function getIdentifier(): string
30
    {
31
        return 'calendarize_dateField';
32
    }
33
34
    public function executeUpdate(): bool
35
    {
36
        foreach ($this->migrationMap as $table => $fields) {
37
            foreach ($fields as $field) {
38
                if ($this->updateNecessaryForTableFieldDateTimeMigration($table, $field)) {
39
                    $this->executeUpdateForTableFieldDateTimeMigration($table, $field);
40
                }
41
            }
42
        }
43
44
        return true;
45
    }
46
47
    protected function executeUpdateForTableFieldDateTimeMigration(string $tableName, string $fieldName)
48
    {
49
        $tmpField = $fieldName . '_' . GeneralUtility::shortMD5((string)microtime(), 5);
50
51
        $sqlQueries = [
52
            'ALTER TABLE ' . $tableName . ' ADD COLUMN ' . $tmpField . ' int(11) NOT NULL DEFAULT \'0\'',
53
            'UPDATE ' . $tableName . ' SET ' . $tmpField . ' = ' . $fieldName,
54
            'ALTER TABLE ' . $tableName . ' CHANGE ' . $fieldName . ' ' . $fieldName . ' int(11) DEFAULT NULL',
55
            'UPDATE ' . $tableName . ' SET ' . $fieldName . ' = null',
56
            'ALTER TABLE ' . $tableName . ' CHANGE ' . $fieldName . ' ' . $fieldName . ' DATE NULL default NULL',
57
            'UPDATE ' . $tableName . ' SET ' . $fieldName . ' = FROM_UNIXTIME(' . $tmpField . ')',
58
            'UPDATE ' . $tableName . ' SET ' . $fieldName . ' = NULL WHERE ' . $fieldName . ' = \'1970-01-01\'',
59
        ];
60
61
        $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
62
        foreach ($sqlQueries as $sqlQuery) {
63
            $connection->executeQuery($sqlQuery);
64
        }
65
    }
66
67
    public function updateNecessary(): bool
68
    {
69
        foreach ($this->migrationMap as $table => $fields) {
70
            foreach ($fields as $field) {
71
                if ($this->updateNecessaryForTableFieldDateTimeMigration($table, $field)) {
72
                    return true;
73
                }
74
            }
75
        }
76
77
        return false;
78
    }
79
80
    protected function updateNecessaryForTableFieldDateTimeMigration(string $tableName, string $fieldName)
81
    {
82
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($tableName);
83
        /** @var QueryBuilder $queryBuilder */
84
        $schemaManager = $queryBuilder->getConnection()->getSchemaManager();
85
        if (!$schemaManager->tablesExist($tableName)) {
86
            return false;
87
        }
88
89
        $columns = $schemaManager->listTableColumns($tableName);
90
        if (!isset($columns[$fieldName]) || !($columns[$fieldName] instanceof \Doctrine\DBAL\Schema\Column)) {
91
            return false;
92
        }
93
        /** @var \Doctrine\DBAL\Schema\Column $checkField */
94
        $checkField = $columns[$fieldName];
95
96
        return !($checkField->getType() instanceof \Doctrine\DBAL\Types\DateType);
97
    }
98
99
    public function getPrerequisites(): array
100
    {
101
        // No DB Update is needed
102
        return [];
103
    }
104
}
105