Completed
Push — master ( 70ac5a...66ce1a )
by Tim
02:18
created

DateFieldUpdate::getIdentifier()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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