Passed
Push — dbal ( b6c9ce...9b470f )
by Greg
06:51
created

UpdateDatabaseSchema::process()   B

Complexity

Conditions 6
Paths 15

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 21
nc 15
nop 2
dl 0
loc 39
rs 8.9617
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2023 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\Middleware;
21
22
use Fisharebest\Webtrees\DB;
23
use Fisharebest\Webtrees\DB\WebtreesSchema;
24
use Fisharebest\Webtrees\Services\MigrationService;
25
use Fisharebest\Webtrees\Webtrees;
26
use PDO;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\MiddlewareInterface;
30
use Psr\Http\Server\RequestHandlerInterface;
31
32
use function str_contains;
33
34
/**
35
 * Middleware to update the database automatically, after an upgrade.
36
 */
37
class UpdateDatabaseSchema implements MiddlewareInterface
38
{
39
    private MigrationService $migration_service;
40
41
    /**
42
     * @param MigrationService $migration_service
43
     */
44
    public function __construct(MigrationService $migration_service)
45
    {
46
        $this->migration_service = $migration_service;
47
    }
48
49
    /**
50
     * Update the database schema, if necessary.
51
     *
52
     * @param ServerRequestInterface  $request
53
     * @param RequestHandlerInterface $handler
54
     *
55
     * @return ResponseInterface
56
     */
57
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
58
    {
59
        $this->migration_service
60
            ->updateSchema('\Fisharebest\Webtrees\Schema', 'WT_SCHEMA_VERSION', Webtrees::SCHEMA_VERSION);
61
62
        $platform = DB::connection()->getDatabasePlatform();
63
        $platform->registerDoctrineTypeMapping(dbType: 'enum', doctrineType: 'string');
64
65
        $schema_manager = DB::connection()->createSchemaManager();
66
        $comparator     = $schema_manager->createComparator();
67
        $source         = $schema_manager->introspectSchema();
68
        $target         = WebtreesSchema::schema();
69
70
        // doctrine/dbal 4.0 does not have the concept of "saveSQL"
71
        foreach ($source->getTables() as $table) {
72
            if (!$target->hasTable($table->getName())) {
73
                $source->dropTable($table->getName());
74
            }
75
        }
76
77
        $schema_diff    = $comparator->compareSchemas(oldSchema: $source, newSchema: $target);
78
        $queries        = $platform->getAlterSchemaSQL($schema_diff);
79
80
        foreach ($queries as $query) {
81
            if (str_contains($query, 'RENAME INDEX')) {
82
                //continue;
83
            }
84
85
            if (str_contains($query, 'CHANGE')) {
86
                echo '<p><b>', $query, '</b></p>';
87
            } else {
88
                echo '<p>', $query, '</p>';
89
            }
90
            //DB::connection()->executeStatement($query);
91
        }
92
93
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
94
95
        return $handler->handle($request);
0 ignored issues
show
Unused Code introduced by
return $handler->handle($request) is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
96
    }
97
}
98