Passed
Push — dbal ( 46ec49 )
by Greg
34:07 queued 19:39
created

WebtreesSchema::tableSiteSetting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2022 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;
21
22
use Closure;
23
use Doctrine\DBAL\Connection;
24
use Doctrine\DBAL\DriverManager;
25
use Doctrine\DBAL\Query\QueryBuilder;
26
use Doctrine\DBAL\Schema\Schema;
27
use Doctrine\DBAL\Schema\SchemaConfig;
28
use Doctrine\DBAL\Schema\SchemaException;
29
use Doctrine\DBAL\Schema\Table;
30
use Doctrine\DBAL\Types\Types;
31
use ErrorException;
32
use Fisharebest\Webtrees\Factories\CacheFactory;
33
use Fisharebest\Webtrees\Factories\CalendarDateFactory;
34
use Fisharebest\Webtrees\Factories\ElementFactory;
35
use Fisharebest\Webtrees\Factories\EncodingFactory;
36
use Fisharebest\Webtrees\Factories\FamilyFactory;
37
use Fisharebest\Webtrees\Factories\FilesystemFactory;
38
use Fisharebest\Webtrees\Factories\GedcomRecordFactory;
39
use Fisharebest\Webtrees\Factories\HeaderFactory;
40
use Fisharebest\Webtrees\Factories\ImageFactory;
41
use Fisharebest\Webtrees\Factories\IndividualFactory;
42
use Fisharebest\Webtrees\Factories\LocationFactory;
43
use Fisharebest\Webtrees\Factories\MarkdownFactory;
44
use Fisharebest\Webtrees\Factories\MediaFactory;
45
use Fisharebest\Webtrees\Factories\NoteFactory;
46
use Fisharebest\Webtrees\Factories\RepositoryFactory;
47
use Fisharebest\Webtrees\Factories\ResponseFactory;
48
use Fisharebest\Webtrees\Factories\RouteFactory;
49
use Fisharebest\Webtrees\Factories\SharedNoteFactory;
50
use Fisharebest\Webtrees\Factories\SlugFactory;
51
use Fisharebest\Webtrees\Factories\SourceFactory;
52
use Fisharebest\Webtrees\Factories\SubmissionFactory;
53
use Fisharebest\Webtrees\Factories\SubmitterFactory;
54
use Fisharebest\Webtrees\Factories\SurnameTraditionFactory;
55
use Fisharebest\Webtrees\Factories\TimeFactory;
56
use Fisharebest\Webtrees\Factories\TimestampFactory;
57
use Fisharebest\Webtrees\Factories\IdFactory;
58
use Fisharebest\Webtrees\Factories\XrefFactory;
59
use Fisharebest\Webtrees\GedcomFilters\GedcomEncodingFilter;
60
use Fisharebest\Webtrees\Http\Middleware\BadBotBlocker;
61
use Fisharebest\Webtrees\Http\Middleware\BaseUrl;
62
use Fisharebest\Webtrees\Http\Middleware\BootModules;
63
use Fisharebest\Webtrees\Http\Middleware\CheckForMaintenanceMode;
64
use Fisharebest\Webtrees\Http\Middleware\CheckForNewVersion;
65
use Fisharebest\Webtrees\Http\Middleware\ClientIp;
66
use Fisharebest\Webtrees\Http\Middleware\CompressResponse;
67
use Fisharebest\Webtrees\Http\Middleware\ContentLength;
68
use Fisharebest\Webtrees\Http\Middleware\DoHousekeeping;
69
use Fisharebest\Webtrees\Http\Middleware\EmitResponse;
70
use Fisharebest\Webtrees\Http\Middleware\HandleExceptions;
71
use Fisharebest\Webtrees\Http\Middleware\LoadRoutes;
72
use Fisharebest\Webtrees\Http\Middleware\NoRouteFound;
73
use Fisharebest\Webtrees\Http\Middleware\ReadConfigIni;
74
use Fisharebest\Webtrees\Http\Middleware\RegisterGedcomTags;
75
use Fisharebest\Webtrees\Http\Middleware\Router;
76
use Fisharebest\Webtrees\Http\Middleware\SecurityHeaders;
77
use Fisharebest\Webtrees\Http\Middleware\UpdateDatabaseSchema;
78
use Fisharebest\Webtrees\Http\Middleware\UseDatabase;
79
use Fisharebest\Webtrees\Http\Middleware\UseLanguage;
80
use Fisharebest\Webtrees\Http\Middleware\UseSession;
81
use Fisharebest\Webtrees\Http\Middleware\UseTheme;
82
use Fisharebest\Webtrees\Http\Middleware\UseTransaction;
83
use Illuminate\Container\Container;
84
use Illuminate\Database\Capsule\Manager as DB;
85
use Illuminate\Database\Schema\Blueprint;
86
use Middleland\Dispatcher;
87
use Nyholm\Psr7\Factory\Psr17Factory;
88
use Nyholm\Psr7Server\ServerRequestCreator;
89
use Psr\Container\ContainerInterface;
90
use Psr\Http\Message\ResponseFactoryInterface;
91
use Psr\Http\Message\ResponseInterface;
92
use Psr\Http\Message\ServerRequestFactoryInterface;
93
use Psr\Http\Message\ServerRequestInterface;
94
use Psr\Http\Message\StreamFactoryInterface;
95
use Psr\Http\Message\UploadedFileFactoryInterface;
96
use Psr\Http\Message\UriFactoryInterface;
97
use Psr\Http\Server\MiddlewareInterface;
98
99
use function date_default_timezone_set;
100
use function error_reporting;
101
use function is_string;
102
use function mb_internal_encoding;
103
use function set_error_handler;
104
use function stream_filter_register;
105
106
use const E_ALL;
107
use const E_DEPRECATED;
108
use const E_USER_DEPRECATED;
109
110
/**
111
 * Definitions for the webtrees database.
112
 */
113
class WebtreesSchema extends Schema
114
{
115
    /**
116
     * @param Schema $schema
117
     * @param string $prefix
118
     *
119
     * @return Table
120
     * @throws SchemaException
121
     */
122
    private static function tableGedcom(Schema $schema, string $prefix): Table
123
    {
124
        $table = $schema->createTable($prefix . 'gedcom');
125
        $table->addColumn('gedcom_id', Types::INTEGER, ['autoincrement' => true]);
126
        $table->addColumn('gedcom_name', Types::STRING, ['length' => 255]);
127
        $table->addColumn('sort_order', Types::INTEGER, ['default' => 0]);
128
        $table->setPrimaryKey(['gedcom_id']);
129
        $table->addUniqueIndex(['gedcom_name']);
130
        $table->addIndex(['sort_order']);
131
132
        return $table;
133
    }
134
135
    /**
136
     * @param Schema $schema
137
     * @param string $prefix
138
     *
139
     * @return Table
140
     * @throws SchemaException
141
     */
142
    private static function tableGedcomSetting(Schema $schema, string $prefix): Table
143
    {
144
        $table = $schema->createTable($prefix . 'gedcom_setting');
145
        $table->addOption('collation', 'utf8mb4_unicode_bin');
146
        $table->addColumn('gedcom_id', Types::INTEGER);
147
        $table->addColumn('setting_name', Types::STRING, ['length' => 32]);
148
        $table->addColumn('setting_value', Types::STRING, ['length' => 255]);
149
        $table->setPrimaryKey(['gedcom_id', 'setting_name']);
150
151
        return $table;
152
    }
153
154
    /**
155
     * @param Schema $schema
156
     * @param string $prefix
157
     *
158
     * @return Table
159
     * @throws SchemaException
160
     */
161
    private static function tableSiteSetting(Schema $schema, string $prefix): Table
162
    {
163
        $table = $schema->createTable($prefix . 'site_setting');
164
        $table->addOption('collation', 'utf8mb4_unicode_bin');
165
        $table->addColumn('setting_name', Types::STRING, ['length' => 32]);
166
        $table->addColumn('setting_value', Types::STRING, ['length' => 2000]);
167
        $table->setPrimaryKey(['setting_name']);
168
169
        return $table;
170
    }
171
172
    /**
173
     * @param Schema $schema
174
     * @param string $prefix
175
     *
176
     * @return Table
177
     * @throws SchemaException
178
     */
179
    private static function tableUser(Schema $schema, string $prefix): Table
180
    {
181
        $table = $schema->createTable($prefix . 'user');
182
        $table->addOption('collation', 'utf8mb4_unicode_bin');
183
        $table->addColumn('user_id', Types::INTEGER, ['autoincrement' => true]);
184
        $table->addColumn('user_name', Types::STRING, ['length' => 32]);
185
        $table->addColumn('real_name', Types::STRING, ['length' => 64]);
186
        $table->addColumn('email', Types::STRING, ['length' => 64]);
187
        $table->addColumn('password', Types::STRING, ['length' => 128]);
188
        $table->setPrimaryKey(['user_id']);
189
        $table->addUniqueIndex(['user_name']);
190
        $table->addUniqueIndex(['email']);
191
192
        return $table;
193
    }
194
195
    /**
196
     * @param Schema $schema
197
     * @param string $prefix
198
     *
199
     * @return Table
200
     * @throws SchemaException
201
     */
202
    private static function tableUserGedcomSetting(Schema $schema, string $prefix): Table
203
    {
204
        $table = $schema->createTable($prefix . 'user_gedcom_setting');
205
        $table->addOption('collation', 'utf8mb4_unicode_bin');
206
        $table->addColumn('user_id', Types::INTEGER);
207
        $table->addColumn('gedcom_id', Types::INTEGER);
208
        $table->addColumn('setting_name', Types::STRING, ['length' => 32]);
209
        $table->addColumn('setting_value', Types::STRING, ['length' => 255]);
210
        $table->setPrimaryKey(['user_id', 'gedcom_id', 'setting_name']);
211
        $table->addIndex(['gedcom_id']);
212
213
        return $table;
214
    }
215
216
    /**
217
     * @param Schema $schema
218
     * @param string $prefix
219
     *
220
     * @return Table
221
     * @throws SchemaException
222
     */
223
    private static function tableUserSetting(Schema $schema, string $prefix): Table
224
    {
225
        $table = $schema->createTable($prefix . 'user_setting');
226
        $table->addOption('collation', 'utf8mb4_unicode_bin');
227
        $table->addColumn('user_id', Types::INTEGER);
228
        $table->addColumn('setting_name', Types::STRING, ['length' => 32]);
229
        $table->addColumn('setting_value', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'utf8mb4_bin']]);
230
        $table->setPrimaryKey(['user_id', 'setting_name']);
231
232
        return $table;
233
    }
234
235
    /**
236
     * @param Connection $connection
237
     * @param string     $prefix
238
     *
239
     * @return void
240
     *
241
     * @throws SchemaException
242
     * @throws \Doctrine\DBAL\Exception
243
     */
244
    public static function migrate(Connection $connection, string $prefix): void
245
    {
246
        $platform = $connection->getDatabasePlatform();
247
248
        // Existing databases may have enum columns.
249
        $platform->registerDoctrineTypeMapping('enum', 'string');
250
251
        $schema_manager = $connection->createSchemaManager();
252
        $current_schema = $schema_manager->createSchema();
253
254
        $schema = new Schema();
255
        $site = $schema->createTable($prefix . 'site');
256
        $site->addOption('charset', 'utf8mb4');
257
        $site->addOption('collation', 'utf8mb4_unicode_bin');
258
        $site->addColumn('uuid', Types::STRING, ['length' => 36, 'platformOptions' => ['charset' => 'ascii', 'collation' => 'ascii_bin']]);
259
        $site->addColumn('db_schema', Types::STRING, ['length' => 20, 'platformOptions' => ['charset' => 'ascii', 'collation' => 'ascii_bin']]);
260
        $site->addColumn('created_at', Types::TIME_MUTABLE);
261
        $site->addColumn('updated_at', Types::TIME_MUTABLE);
262
        $site->setPrimaryKey(['uuid']);
263
264
        $job = $schema->createTable($prefix . 'job');
265
        $job->addOption('charset', 'utf8mb4');
266
        $job->addOption('collation', 'utf8mb4_unicode_bin');
267
        $job->addColumn('uuid', Types::STRING, ['length' => 36, 'platformOptions' => ['charset' => 'ascii', 'collation' => 'ascii_bin']]);
268
        $job->addColumn('failures', Types::INTEGER, ['default' => 0]);
269
        $job->addColumn('job', Types::STRING, ['length' => 255, 'platformOptions' => ['charset' => 'ascii', 'collation' => 'ascii_bin']]);
270
        $job->addColumn('parameters', Types::TEXT, ['platformOptions' => ['charset' => 'utf8mb4', 'collation' => 'utf8mb4_bin']]);
271
        $job->addColumn('error', Types::TEXT, ['notnull' => false, 'platformOptions' => ['charset' => 'utf8mb4', 'collation' => 'utf8mb4_bin']]);
272
        $job->addColumn('created_at', Types::TIME_MUTABLE);
273
        $job->addColumn('updated_at', Types::TIME_MUTABLE);
274
        $job->setPrimaryKey(['uuid']);
275
276
        $job_dependency = $schema->createTable($prefix . 'job_dependency');
277
        $job_dependency->addColumn('uuid1', Types::STRING, ['length' => 36, 'platformOptions' => ['charset' => 'ascii', 'collation' => 'ascii_bin']]);
278
        $job_dependency->addColumn('uuid2', Types::STRING, ['length' => 36, 'platformOptions' => ['charset' => 'ascii', 'collation' => 'ascii_bin']]);
279
        $job_dependency->addUniqueIndex(['uuid1', 'uuid2']);
280
        $job_dependency->addUniqueIndex(['uuid2', 'uuid1']);
281
        $job_dependency->addForeignKeyConstraint($job, ['uuid1'], ['uuid'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
282
        $job_dependency->addForeignKeyConstraint($job, ['uuid2'], ['uuid'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
283
284
        $gedcom              = self::tableGedcom($schema, $prefix);
285
        $gedcom_setting      = self::tableGedcomSetting($schema, $prefix);
286
        $site_setting        = self::tableSiteSetting($schema, $prefix);
0 ignored issues
show
Unused Code introduced by
The assignment to $site_setting is dead and can be removed.
Loading history...
287
        $user                = self::tableUser($schema, $prefix);
288
        $user_gedcom_setting = self::tableUserGedcomSetting($schema, $prefix);
289
        $user_setting        = self::tableUserSetting($schema, $prefix);
290
291
        $gedcom_setting->addForeignKeyConstraint($gedcom, ['gedcom_id'], ['gedcom_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
292
293
        $user_gedcom_setting->addForeignKeyConstraint($gedcom, ['gedcom_id'], ['gedcom_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
294
        $user_gedcom_setting->addForeignKeyConstraint($user, ['user_id'], ['user_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
295
296
        $user_setting->addForeignKeyConstraint($user, ['user_id'], ['user_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
297
298
        $comparator = $schema_manager->createComparator();
299
        $difference = $comparator->compareSchemas($current_schema, $schema);
300
        $queries    = $difference->toSaveSql($platform);
301
302
        foreach ($queries as $query) {
303
            echo '<p>' . $query . '</p>';
304
        }
305
        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...
306
    }
307
}
308