summaryrefslogtreecommitdiff
path: root/app/Schema/Migration45.php
blob: da55bed172becad1e44477b16a1a1fd53accd0b8 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php

/**
 * webtrees: online genealogy
 * Copyright (C) 2026 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

declare(strict_types=1);

namespace Fisharebest\Webtrees\Schema;

use Fisharebest\Webtrees\DB;
use Illuminate\Database\Query\Expression;
use Illuminate\Database\Schema\Blueprint;
use Throwable;

/**
 * Upgrade the database schema from version 45 to version 46.
 */
final readonly class Migration45 implements MigrationInterface
{
    public function upgrade(): void
    {
        // We now write only upper-case values to these columns.
        DB::table(table: 'media_file')->update(values: [
            'multimedia_format' => new Expression(value: 'UPPER(multimedia_format)'),
        ]);

        DB::table(table: 'media_file')->update(values: [
            'source_media_type' => new Expression(value: 'UPPER(source_media_type)'),
        ]);

        if (!DB::schema()->hasColumn(table: 'gedcom', column: 'media_folder')) {
            DB::schema()->table(table: 'gedcom', callback: function (Blueprint $table): void {
                $table->string(column: 'media_folder', length: 255)->default(value: 'media/')->index();
                $table->string(column: 'title', length: 255)->default(value: 'tree')->index();
                $table->string(column: 'gedcom_filename', length: 255)->default(value: 'tree.ged')->index();
                $table->integer(column: 'imported')->default(value: 1)->index();
                $table->integer(column: 'private')->default(value: 0)->index();
                $table->integer(column: 'contact_user_id')->nullable()->index();
                $table->integer(column: 'support_user_id')->nullable()->index();

                if (DB::driverName() === DB::SQL_SERVER) {
                    // SQL-Server can't use CASCADE or SET NULL constraints here, as it can't handle multiple paths
                    $table->foreign(columns: ['contact_user_id'])->references(['user_id'])->on('user');
                    $table->foreign(columns: ['support_user_id'])->references(['user_id'])->on('user');
                } else if !(DB::driverName() === DB::FIREBIRD) {
                    $table->foreign(columns: ['contact_user_id'])->references(['user_id'])->on('user')->nullOnDelete()->cascadeOnUpdate();
                    $table->foreign(columns: ['support_user_id'])->references(['user_id'])->on('user')->nullOnDelete()->cascadeOnUpdate();                }
            });
        }

        $new_columns = [
            'MEDIA_DIRECTORY'        => 'media_folder',
            'imported'               => 'imported',
            'title'                  => 'title',
            'gedcom_filename'        => 'gedcom_filename',
            'REQUIRE_AUTHENTICATION' => 'private',
            'CONTACT_USER_ID'        => 'contact_user_id',
            'WEBMASTER_USER_ID'      => 'support_user_id',
        ];

        $rows = DB::table(table: 'gedcom_setting')
            ->whereIn(column: 'setting_name', values: array_keys($new_columns))
            ->get();

        foreach ($rows as $row) {
            $column = $new_columns[$row->setting_name];

            $value = match ($row->setting_name) {
                'imported', 'REQUIRE_AUTHENTICATION'   => (int) (bool) $row->setting_value,
                'CONTACT_USER_ID', 'WEBMASTER_USER_ID' => (int) $row->setting_value,
                default                                => mb_substr($row->setting_value, 0, 255),
            };

            try {
                DB::table(table: 'gedcom')
                    ->where(column: 'gedcom_id', operator: '=', value: $row->gedcom_id)
                    ->update(values: [$column => $value]);
            } catch (Throwable) {
                // If it violates a foreign key constraint, then just ignore it.
            }
        }

        DB::table(table: 'gedcom_setting')
            ->whereIn(column: 'setting_name', values: array_keys($new_columns))
            ->delete();

        // Old setting, no longer used.
        DB::table(table: 'gedcom_setting')
            ->whereIn(column: 'setting_name', values: ['LANGUAGE'])
            ->delete();
    }
}