summaryrefslogtreecommitdiffstats
path: root/library/Icinga/Application/ProvidedHook/DbMigration.php
blob: 899dbf6acc7bd2e73847df1c731ed60143dca0a9 (plain)
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
<?php

/* Icinga Web 2 | (c) 2023 Icinga GmbH | GPLv2+ */

namespace Icinga\Application\ProvidedHook;

use Icinga\Application\Hook\DbMigrationHook;
use Icinga\Common\Database;
use Icinga\Model\Schema;
use ipl\Orm\Query;
use ipl\Sql\Connection;

class DbMigration extends DbMigrationHook
{
    use Database {
        getDb as private getWebDb;
    }

    public function getDb(): Connection
    {
        return $this->getWebDb();
    }

    public function getName(): string
    {
        return $this->translate('Icinga Web');
    }

    public function providedDescriptions(): array
    {
        return [];
    }

    public function getVersion(): string
    {
        if ($this->version === null) {
            $conn = $this->getDb();
            $schemaQuery = $this->getSchemaQuery()
                ->orderBy('id', SORT_DESC)
                ->limit(2);

            if (static::getColumnType($conn, $schemaQuery->getModel()->getTableName(), 'success')) {
                /** @var Schema $schema */
                foreach ($schemaQuery as $schema) {
                    if ($schema->success) {
                        $this->version = $schema->version;

                        break;
                    }
                }

                if (! $this->version) {
                    $this->version = '2.12.0';
                }
            } elseif (static::tableExists($conn, $schemaQuery->getModel()->getTableName())
                || static::getColumnCollation($conn, 'icingaweb_user_preference', 'username') === 'utf8mb4_unicode_ci'
            ) {
                $this->version = '2.11.0';
            } elseif (static::tableExists($conn, 'icingaweb_rememberme')) {
                $randomIvType = static::getColumnType($conn, 'icingaweb_rememberme', 'random_iv');
                if ($randomIvType === 'varchar(32)') {
                    $this->version = '2.9.1';
                } else {
                    $this->version = '2.9.0';
                }
            } else {
                $usernameType = static::getColumnType($conn, 'icingaweb_group_membership', 'username');
                if ($usernameType === 'varchar(254)') {
                    $this->version = '2.5.0';
                } else {
                    $this->version = '2.0.0';
                }
            }
        }

        return $this->version;
    }

    protected function getSchemaQuery(): Query
    {
        return Schema::on($this->getDb());
    }
}