Passed
Push — dbal ( 0f94c0...f50b57 )
by Greg
12:18
created

WebtreesSchema.php$0 ➔ migrate()   C

Complexity

Conditions 9

Size

Total Lines 458

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
dl 0
loc 458
rs 6.4444
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A WebtreesSchema.php$0 ➔ getSQLDeclaration() 0 12 3
A WebtreesSchema.php$0 ➔ getName() 0 3 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Doctrine\DBAL\Connection;
23
use Doctrine\DBAL\Platforms\AbstractPlatform;
24
use Doctrine\DBAL\Platforms\SqlitePlatform;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Platforms\SqlitePlatform was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Doctrine\DBAL\Platforms\SQLServerPlatform;
26
use Doctrine\DBAL\Schema\Schema;
27
use Doctrine\DBAL\Schema\SchemaException;
28
use Doctrine\DBAL\Types\PhpDateTimeMappingType;
29
use Doctrine\DBAL\Types\Type;
30
use Doctrine\DBAL\Types\Types;
31
32
use Exception;
33
use PDOException;
34
35
use Throwable;
36
37
use function str_contains;
38
use function var_dump;
39
40
/**
41
 * Definitions for the webtrees database.
42
 */
43
class WebtreesSchema
44
{
45
    public function foo(): void
46
    {
47
        switch ('webtrees_schema') {
48
            case 1: // webtrees 1.0.0 - 1.0.3
49
            case 2: // webtrees 1.0.4
50
            case 3:
51
            case 4: // webtrees 1.0.5
52
            case 5: // webtrees 1.0.6
53
            case 6:
54
            case 7:
55
            case 8:
56
            case 9: // webtrees 1.1.0 - 1.1.1
57
            case 10: // webtrees 1.1.2
58
            case 11: // webtrees 1.2.0
59
            case 12: // webtrees 1.2.1 - 1.2.3
60
            case 13:
61
            case 14:
62
            case 15: // webtrees 1.2.4 - 1.2.5
63
            case 16: // webtrees 1.2.7
64
            case 17:
65
            case 18: // webtrees 1.3.0
66
            case 19: // webtrees 1.3.1
67
            case 20: // webtrees 1.3.2
68
            case 21:
69
            case 22:
70
            case 23: // webtrees 1.4.0 - 1.4.1
71
            case 24:
72
            case 25: // webtrees 1.4.2 - 1.4.4, 1.5.0
73
            case 26: // webtrees 1.4.5 - 1.4.6
74
            case 27: // webtrees 1.5.1 - 1.6.0
75
            case 28:
76
            case 29: // webtrees 1.6.1 - 1.6.2
77
            case 30:
78
            case 31: // webtrees 1.7.0 - 1.7.1
79
            case 32: // webtrees 1.7.2
80
            case 33:
81
            case 34: // webtrees 1.7.3 - 1.7.4
82
            case 35:
83
            case 36: // webtrees 1.7.5 - 1.7.7
84
            case 37: // webtrees 1.7.8 - 2.0.0
85
            case 38:
86
            case 39:
87
            case 40: // webtrees 2.0.1 - 2.1.7
88
        }
89
    }
90
91
    /**
92
     * @param Connection $connection
93
     * @param string     $prefix
94
     *
95
     * @return void
96
     *
97
     * @throws SchemaException
98
     * @throws \Doctrine\DBAL\Exception
99
     */
100
    public static function migrate(Connection $connection, string $prefix): void
101
    {
102
        $platform = $connection->getDatabasePlatform();
103
104
        // Existing databases may have enum columns.
105
        $platform->registerDoctrineTypeMapping('enum', 'string');
106
107
        // Timestamp
108
        if (!Type::hasType('timestamp')) {
109
            Type::addType(
110
                'timestamp',
111
                new class extends Type implements PhpDateTimeMappingType {
112
                    public function getName(): string
113
                    {
114
                        return 'timestamp';
115
                    }
116
117
                    public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
118
                    {
119
                        if ($platform instanceof SqlitePlatform) {
120
                            return 'DATETIME(' . $column['precision'] . ')';
121
                        }
122
123
                        if ($platform instanceof SQLServerPlatform) {
124
                            return 'DATETIME2(' . $column['precision'] . ')';
125
                        }
126
127
                        // Standard SQL
128
                        return 'TIMESTAMP(' . $column['precision'] . ')';
129
                    }
130
                }
131
            );
132
        }
133
134
        $schema = new Schema();
135
136
        $table_gedcom = $schema->createTable($prefix . 'gedcom');
137
        $table_gedcom->addColumn('gedcom_id', Types::INTEGER, ['autoincrement' => true]);
138
        $table_gedcom->addColumn('gedcom_name', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
139
        $table_gedcom->addColumn('sort_order', Types::INTEGER, ['default' => 0]);
140
        $table_gedcom->setPrimaryKey(['gedcom_id']);
141
        $table_gedcom->addUniqueIndex(['gedcom_name']);
142
        $table_gedcom->addIndex(['sort_order']);
143
144
        $table_user = $schema->createTable($prefix . 'user');
145
        $table_user->addColumn('user_id', Types::INTEGER, ['autoincrement' => true]);
146
        $table_user->addColumn('user_name', Types::STRING, ['length' => 32, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
147
        $table_user->addColumn('real_name', Types::STRING, ['length' => 64, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
148
        $table_user->addColumn('email', Types::STRING, ['length' => 64, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
149
        $table_user->addColumn('password', Types::STRING, ['length' => 128, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
150
        $table_user->setPrimaryKey(['user_id']);
151
        $table_user->addUniqueIndex(['user_name']);
152
        $table_user->addUniqueIndex(['email']);
153
154
        $table_module = $schema->createTable($prefix . 'module');
155
        $table_module->addColumn('module_name', Types::STRING, ['length' => 32, 'platformOptions' => ['collation' => 'utf8mb4_bin']]);
156
        $table_module->addColumn('status', Types::STRING, ['length' => 8, 'platformOptions' => ['collation' => 'ascii_bin']]);
157
        $table_module->addColumn('tab_order', Types::INTEGER, ['notnull' => false]);
158
        $table_module->addColumn('menu_order', Types::INTEGER, ['notnull' => false]);
159
        $table_module->addColumn('sidebar_order', Types::INTEGER, ['notnull' => false]);
160
        $table_module->addColumn('footer_order', Types::INTEGER, ['notnull' => false]);
161
        $table_module->setPrimaryKey(['module_name']);
162
163
        $table_block = $schema->createTable($prefix . 'block');
164
        $table_block->addColumn('block_id', Types::INTEGER, ['autoincrement' => true]);
165
        $table_block->addColumn('gedcom_id', Types::INTEGER, ['notnull' => false]);
166
        $table_block->addColumn('user_id', Types::INTEGER, ['notnull' => false]);
167
        $table_block->addColumn('xref', Types::STRING, ['length' => 20, 'notnull' => false, 'platformOptions' => ['collation' => 'ascii_bin']]);
168
        $table_block->addColumn('location', Types::STRING, ['length' => 4, 'notnull' => false, 'platformOptions' => ['collation' => 'ascii_bin']]);
169
        $table_block->addColumn('block_order', Types::INTEGER);
170
        $table_block->addColumn('module_name', Types::STRING, ['length' => 32, 'platformOptions' => ['collation' => 'utf8mb4_bin']]);
171
        $table_block->setPrimaryKey(['block_id']);
172
        $table_block->addForeignKeyConstraint($table_gedcom, ['gedcom_id'], ['gedcom_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
173
        $table_block->addForeignKeyConstraint($table_user, ['user_id'], ['user_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
174
        $table_block->addForeignKeyConstraint($table_module, ['module_name'], ['module_name'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
175
176
        $table_block_setting = $schema->createTable($prefix . 'block_setting');
177
        $table_block_setting->addColumn('block_id', Types::INTEGER);
178
        $table_block_setting->addColumn('setting_name', Types::STRING, ['length' => 32, 'platformOptions' => ['collation' => 'ascii_bin']]);
179
        $table_block_setting->addColumn('setting_value', Types::TEXT, ['platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
180
        $table_block_setting->setPrimaryKey(['block_id', 'setting_name']);
181
        $table_block_setting->addForeignKeyConstraint($table_block, ['block_id'], ['block_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
182
183
        $table_change = $schema->createTable($prefix . 'change');
184
        $table_change->addColumn('change_id', Types::INTEGER, ['autoincrement' => true]);
185
        $table_change->addColumn('change_time', Types::DATETIME_MUTABLE, ['default' => 'CURRENT_TIMESTAMP']);
186
        $table_change->addColumn('status', Types::STRING, ['length' => 8, 'default' => 'pending', 'platformOptions' => ['collation' => 'ascii_bin']]);
187
        $table_change->addColumn('gedcom_id', Types::INTEGER);
188
        $table_change->addColumn('xref', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
189
        $table_change->addColumn('old_gedcom', Types::TEXT, ['platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
190
        $table_change->addColumn('new_gedcom', Types::TEXT, ['platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
191
        $table_change->addColumn('user_id', Types::INTEGER);
192
        $table_change->setPrimaryKey(['change_id']);
193
        $table_change->addIndex(['gedcom_id', 'status', 'xref']);
194
        $table_change->addIndex(['user_id']);
195
        $table_change->addForeignKeyConstraint($table_gedcom, ['gedcom_id'], ['gedcom_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
196
        $table_change->addForeignKeyConstraint($table_user, ['user_id'], ['user_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
197
198
        $table_dates = $schema->createTable($prefix . 'dates');
199
        $table_dates->addColumn('d_day', Types::SMALLINT);
200
        $table_dates->addColumn('d_month', Types::STRING, ['length' => 5, 'fixed' => true, 'platformOptions' => ['collation' => 'ascii_bin']]);
201
        $table_dates->addColumn('d_mon', Types::SMALLINT);
202
        $table_dates->addColumn('d_year', Types::SMALLINT);
203
        $table_dates->addColumn('d_julianday1', Types::INTEGER);
204
        $table_dates->addColumn('d_julianday2', Types::INTEGER);
205
        $table_dates->addColumn('d_fact', Types::STRING, ['length' => 15, 'platformOptions' => ['collation' => 'ascii_bin']]);
206
        $table_dates->addColumn('d_gid', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
207
        $table_dates->addColumn('d_file', Types::INTEGER);
208
        $table_dates->addColumn('d_type', Types::STRING, ['length' => 13, 'platformOptions' => ['collation' => 'ascii_bin']]);
209
        $table_dates->addIndex(['d_day']);
210
        $table_dates->addIndex(['d_month']);
211
        $table_dates->addIndex(['d_mon']);
212
        $table_dates->addIndex(['d_year']);
213
        $table_dates->addIndex(['d_julianday1']);
214
        $table_dates->addIndex(['d_julianday2']);
215
        $table_dates->addIndex(['d_gid']);
216
        $table_dates->addIndex(['d_file']);
217
        $table_dates->addIndex(['d_type']);
218
        $table_dates->addIndex(['d_fact', 'd_gid']);
219
220
        $table_default_resn = $schema->createTable($prefix . 'default_resn');
221
        $table_default_resn->addColumn('default_resn_id', Types::INTEGER, ['autoincrement' => true]);
222
        $table_default_resn->addColumn('gedcom_id', Types::INTEGER);
223
        $table_default_resn->addColumn('xref', Types::STRING, ['length' => 20, 'notNull' => false, 'platformOptions' => ['collation' => 'ascii_bin']]);
224
        $table_default_resn->addColumn('tag_type', Types::STRING, ['length' => 15, 'notNull' => false, 'platformOptions' => ['collation' => 'ascii_bin']]);
225
        $table_default_resn->addColumn('resn', Types::STRING, ['length' => 12, 'platformOptions' => ['collation' => 'ascii_bin']]);
226
        $table_default_resn->addColumn('comment', Types::STRING, ['length' => 255, 'notNull' => false, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
227
        $table_default_resn->addColumn('updated', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'precision' => 0]);
228
        $table_default_resn->setPrimaryKey(['default_resn_id']);
229
        $table_default_resn->addIndex(['gedcom_id', 'xref', 'tag_type']);
230
        $table_default_resn->addForeignKeyConstraint($table_gedcom, ['gedcom_id'], ['gedcom_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
231
232
        $table_families = $schema->createTable($prefix . 'families');
233
        $table_families->addColumn('f_id', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
234
        $table_families->addColumn('f_file', Types::INTEGER);
235
        $table_families->addColumn('f_husb', Types::STRING, ['length' => 20, 'notNull' => false, 'platformOptions' => ['collation' => 'ascii_bin']]);
236
        $table_families->addColumn('f_wife', Types::STRING, ['length' => 20, 'notNull' => false, 'platformOptions' => ['collation' => 'ascii_bin']]);
237
        $table_families->addColumn('f_gedcom', Types::TEXT, ['platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
238
        $table_families->addColumn('f_numchil', Types::INTEGER);
239
        $table_families->setPrimaryKey(['f_id', 'f_file']);
240
        $table_families->addUniqueIndex(['f_file', 'f_id']);
241
        $table_families->addIndex(['f_husb']);
242
        $table_families->addIndex(['f_wife']);
243
244
        $table_favorite = $schema->createTable($prefix . 'favorite');
245
        $table_favorite->addColumn('favorite_id', Types::INTEGER, ['autoincrement' => true]);
246
        $table_favorite->addColumn('user_id', Types::INTEGER, ['notNull' => false]);
247
        $table_favorite->addColumn('xref', Types::STRING, ['length' => 20, 'notNull' => false, 'platformOptions' => ['collation' => 'ascii_bin']]);
248
        $table_favorite->addColumn('favorite_type', Types::STRING, ['length' => 4, 'platformOptions' => ['collation' => 'ascii_bin']]);
249
        $table_favorite->addColumn('url', Types::STRING, ['length' => 255, 'notNull' => false, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
250
        $table_favorite->addColumn('title', Types::STRING, ['length' => 255, 'notNull' => false, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
251
        $table_favorite->addColumn('note', Types::STRING, ['length' => 1000, 'notNull' => false, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
252
        $table_favorite->addColumn('gedcom_id', Types::INTEGER);
253
        $table_favorite->setPrimaryKey(['favorite_id']);
254
        $table_favorite->addIndex(['user_id']);
255
        $table_favorite->addIndex(['gedcom_id', 'user_id']);
256
        $table_favorite->addForeignKeyConstraint($table_gedcom, ['gedcom_id'], ['gedcom_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
257
        $table_favorite->addForeignKeyConstraint($table_user, ['user_id'], ['user_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
258
259
        $table_gedcom_chunk = $schema->createTable($prefix . 'gedcom_chunk');
260
        $table_gedcom_chunk->addColumn('gedcom_chunk_id', Types::INTEGER, ['autoincrement' => true]);
261
        $table_gedcom_chunk->addColumn('gedcom_id', Types::INTEGER);
262
        $table_gedcom_chunk->addColumn('chunk_data', Types::BLOB);
263
        $table_gedcom_chunk->addColumn('imported', Types::INTEGER, ['default' => 0]);
264
        $table_gedcom_chunk->setPrimaryKey(['gedcom_chunk_id']);
265
        $table_gedcom_chunk->addIndex(['gedcom_id', 'imported']);
266
        $table_gedcom_chunk->addForeignKeyConstraint($table_gedcom, ['gedcom_id'], ['gedcom_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
267
268
        $table_gedcom_setting = $schema->createTable($prefix . 'gedcom_setting');
269
        $table_gedcom_setting->addColumn('gedcom_id', Types::INTEGER);
270
        $table_gedcom_setting->addColumn('setting_name', Types::STRING, ['length' => 32, 'platformOptions' => ['collation' => 'ascii_bin']]);
271
        $table_gedcom_setting->addColumn('setting_value', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
272
        $table_gedcom_setting->setPrimaryKey(['gedcom_id', 'setting_name']);
273
        $table_gedcom_setting->addForeignKeyConstraint($table_gedcom, ['gedcom_id'], ['gedcom_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
274
275
        $table_hit_counter = $schema->createTable($prefix . 'hit_counter');
276
        $table_hit_counter->addColumn('gedcom_id', Types::INTEGER);
277
        $table_hit_counter->addColumn('page_name', Types::STRING, ['length' => 32, 'platformOptions' => ['collation' => 'ascii_bin']]);
278
        $table_hit_counter->addColumn('page_parameter', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
279
        $table_hit_counter->addColumn('page_count', Types::INTEGER);
280
        $table_hit_counter->setPrimaryKey(['gedcom_id', 'page_name', 'page_parameter']);
281
        $table_hit_counter->addForeignKeyConstraint($table_gedcom, ['gedcom_id'], ['gedcom_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
282
283
        $table_individuals = $schema->createTable($prefix . 'individuals');
284
        $table_individuals->addColumn('i_id', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
285
        $table_individuals->addColumn('i_file', Types::INTEGER);
286
        $table_individuals->addColumn('i_rin', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
287
        $table_individuals->addColumn('i_sex', Types::STRING, ['length' => 1, 'platformOptions' => ['collation' => 'ascii_bin']]);
288
        $table_individuals->addColumn('i_gedcom', Types::TEXT, ['platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
289
        $table_individuals->setPrimaryKey(['i_id', 'i_file']);
290
        $table_individuals->addUniqueIndex(['i_file', 'i_id']);
291
292
        $table_link = $schema->createTable($prefix . 'link');
293
        $table_link->addColumn('l_file', Types::INTEGER);
294
        $table_link->addColumn('l_from', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
295
        $table_link->addColumn('l_type', Types::STRING, ['length' => 15, 'platformOptions' => ['collation' => 'ascii_bin']]);
296
        $table_link->addColumn('l_to', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
297
        $table_link->setPrimaryKey(['l_from', 'l_file', 'l_type', 'l_to']);
298
        $table_link->addUniqueIndex(['l_to', 'l_file', 'l_type', 'l_from']);
299
300
        $table_log = $schema->createTable($prefix . 'log');
301
        $table_log->addColumn('log_id', Types::INTEGER, ['autoincrement' => true]);
302
        $table_log->addColumn('log_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'precision' => 0]);
303
        $table_log->addColumn('log_type', Types::STRING, ['length' => 6, 'platformOptions' => ['collation' => 'ascii_bin']]);
304
        $table_log->addColumn('log_message', Types::TEXT, ['platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
305
        $table_log->addColumn('ip_address', Types::STRING, ['length' => 45]);
306
        $table_log->addColumn('user_id', Types::INTEGER, ['notNull' => false]);
307
        $table_log->addColumn('gedcom_id', Types::INTEGER, ['notNull' => false]);
308
        $table_log->setPrimaryKey(['log_id']);
309
        $table_log->addIndex(['log_time']);
310
        $table_log->addIndex(['log_type']);
311
        $table_log->addIndex(['ip_address']);
312
        $table_log->addIndex(['user_id']);
313
        $table_log->addIndex(['gedcom_id']);
314
        $table_log->addForeignKeyConstraint($table_gedcom, ['gedcom_id'], ['gedcom_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
315
        $table_log->addForeignKeyConstraint($table_user, ['user_id'], ['user_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
316
317
        $table_media = $schema->createTable($prefix . 'media');
318
        $table_media->addColumn('m_id', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
319
        $table_media->addColumn('m_file', Types::INTEGER);
320
        $table_media->addColumn('m_gedcom', Types::TEXT, ['platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
321
        $table_media->setPrimaryKey(['m_file', 'm_id']);
322
        $table_media->addUniqueIndex(['m_id', 'm_file']);
323
324
        $table_media_file = $schema->createTable($prefix . 'media_file');
325
        $table_media_file->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
326
        $table_media_file->addColumn('m_id', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
327
        $table_media_file->addColumn('m_file', Types::INTEGER);
328
        $table_media_file->addColumn('multimedia_file_refn', Types::STRING, ['length' => 246, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
329
        $table_media_file->addColumn('multimedia_format', Types::STRING, ['length' => 4, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
330
        $table_media_file->addColumn('source_media_type', Types::STRING, ['length' => 15, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
331
        $table_media_file->addColumn('descriptive_title', Types::STRING, ['length' => 248, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
332
        $table_media_file->setPrimaryKey(['id']);
333
        $table_media_file->addIndex(['m_id', 'm_file']);
334
        $table_media_file->addIndex(['m_file', 'm_id']);
335
        $table_media_file->addIndex(['m_file', 'multimedia_file_refn']);
336
        $table_media_file->addIndex(['m_file', 'multimedia_format']);
337
        $table_media_file->addIndex(['m_file', 'source_media_type']);
338
        $table_media_file->addIndex(['m_file', 'descriptive_title']);
339
340
        $table_message = $schema->createTable($prefix . 'message');
341
        $table_message->addColumn('message_id', Types::INTEGER, ['autoincrement' => true]);
342
        $table_message->addColumn('sender', Types::STRING, ['length' => 64, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
343
        $table_message->addColumn('ip_address', Types::STRING, ['length' => 45, 'platformOptions' => ['collation' => 'ascii_bin']]);
344
        $table_message->addColumn('user_id', Types::INTEGER);
345
        $table_message->addColumn('subject', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
346
        $table_message->addColumn('body', Types::TEXT, ['platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
347
        $table_message->addColumn('created', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'precision' => 0]);
348
        $table_message->addIndex(['user_id']);
349
        $table_message->setPrimaryKey(['message_id']);
350
        $table_message->addForeignKeyConstraint($table_user, ['user_id'], ['user_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
351
352
        $table_module_privacy = $schema->createTable($prefix . 'module_privacy');
353
        $table_module_privacy->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
354
        $table_module_privacy->addColumn('module_name', Types::STRING, ['length' => 32, 'platformOptions' => ['collation' => 'utf8mb4_bin']]);
355
        $table_module_privacy->addColumn('gedcom_id', Types::INTEGER);
356
        $table_module_privacy->addColumn('interface', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'ascii_bin']]);
357
        $table_module_privacy->addColumn('access_level', Types::SMALLINT);
358
        $table_module_privacy->addUniqueIndex(['gedcom_id', 'module_name', 'interface']);
359
        $table_module_privacy->addUniqueIndex(['module_name', 'gedcom_id', 'interface']);
360
        $table_module_privacy->setPrimaryKey(['id']);
361
        $table_module_privacy->addForeignKeyConstraint($table_gedcom, ['gedcom_id'], ['gedcom_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
362
        $table_module_privacy->addForeignKeyConstraint($table_module, ['module_name'], ['module_name'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
363
364
        $table_module_setting = $schema->createTable($prefix . 'module_setting');
365
        $table_module_setting->addColumn('module_name', Types::STRING, ['length' => 32, 'platformOptions' => ['collation' => 'utf8mb4_bin']]);
366
        $table_module_setting->addColumn('setting_name', Types::STRING, ['length' => 32, 'platformOptions' => ['collation' => 'ascii_bin']]);
367
        $table_module_setting->addColumn('setting_value', Types::TEXT, ['platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
368
        $table_module_setting->setPrimaryKey(['module_name', 'setting_name']);
369
        $table_module_setting->addForeignKeyConstraint($table_module, ['module_name'], ['module_name'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
370
371
        $table_name = $schema->createTable($prefix . 'name');
372
        $table_name->addColumn('n_file', Types::INTEGER);
373
        $table_name->addColumn('n_id', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
374
        $table_name->addColumn('n_num', Types::INTEGER);
375
        $table_name->addColumn('n_type', Types::STRING, ['length' => 15, 'platformOptions' => ['collation' => 'ascii_bin']]);
376
        $table_name->addColumn('n_sort', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
377
        $table_name->addColumn('n_full', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
378
        $table_name->addColumn('n_surname', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
379
        $table_name->addColumn('n_surn', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
380
        $table_name->addColumn('n_givn', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
381
        $table_name->addColumn('n_soundex_givn_std', Types::STRING, ['length' => 255, 'notNull' => false, 'platformOptions' => ['collation' => 'ascii_bin']]);
382
        $table_name->addColumn('n_soundex_surn_std', Types::STRING, ['length' => 255, 'notNull' => false, 'platformOptions' => ['collation' => 'ascii_bin']]);
383
        $table_name->addColumn('n_soundex_givn_dm', Types::STRING, ['length' => 255, 'notNull' => false, 'platformOptions' => ['collation' => 'ascii_bin']]);
384
        $table_name->addColumn('n_soundex_surn_dm', Types::STRING, ['length' => 255, 'notNull' => false, 'platformOptions' => ['collation' => 'ascii_bin']]);
385
        $table_name->setPrimaryKey(['n_id', 'n_file', 'n_num']);
386
        $table_name->addIndex(['n_full', 'n_id', 'n_file']);
387
        $table_name->addIndex(['n_surn', 'n_file', 'n_type', 'n_id']);
388
        $table_name->addIndex(['n_givn', 'n_file', 'n_type', 'n_id']);
389
390
        $table_news = $schema->createTable($prefix . 'news');
391
        $table_news->addColumn('news_id', Types::INTEGER, ['autoincrement' => true]);
392
        $table_news->addColumn('user_id', Types::INTEGER, ['notNull' => false]);
393
        $table_news->addColumn('gedcom_id', Types::INTEGER, ['notNull' => false]);
394
        $table_news->addColumn('subject', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
395
        $table_news->addColumn('body', Types::TEXT, ['platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
396
        $table_news->addColumn('updated', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'precision' => 0]);
397
        $table_news->setPrimaryKey(['news_id']);
398
        $table_news->addIndex(['user_id', 'updated']);
399
        $table_news->addIndex(['gedcom_id', 'updated']);
400
        $table_news->addForeignKeyConstraint($table_gedcom, ['gedcom_id'], ['gedcom_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
401
        $table_news->addForeignKeyConstraint($table_user, ['user_id'], ['user_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
402
403
        $table_other = $schema->createTable($prefix . 'other');
404
        $table_other->addColumn('o_id', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
405
        $table_other->addColumn('o_file', Types::INTEGER);
406
        $table_other->addColumn('o_type', Types::STRING, ['length' => 15, 'platformOptions' => ['collation' => 'ascii_bin']]);
407
        $table_other->addColumn('o_gedcom', Types::TEXT, ['platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
408
        $table_other->setPrimaryKey(['o_id', 'o_file']);
409
        $table_other->addUniqueIndex(['o_file', 'o_id']);
410
411
        $table_places = $schema->createTable($prefix . 'places');
412
        $table_places->addColumn('p_id', Types::INTEGER, ['autoincrement' => true]);
413
        $table_places->addColumn('p_place', Types::STRING, ['length' => 150, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
414
        $table_places->addColumn('p_parent_id', Types::INTEGER, ['notNull' => false]);
415
        $table_places->addColumn('p_file', Types::INTEGER);
416
        $table_places->addColumn('p_std_soundex', Types::TEXT, ['platformOptions' => ['collation' => 'ascii_bin']]);
417
        $table_places->addColumn('p_dm_soundex', Types::TEXT, ['platformOptions' => ['collation' => 'ascii_bin']]);
418
        $table_places->setPrimaryKey(['p_id']);
419
        $table_places->addUniqueIndex(['p_parent_id', 'p_file', 'p_place']);
420
        $table_places->addIndex(['p_file', 'p_place']);
421
422
        $table_placelinks = $schema->createTable($prefix . 'placelinks');
423
        $table_placelinks->addColumn('pl_p_id', Types::INTEGER);
424
        $table_placelinks->addColumn('pl_gid', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
425
        $table_placelinks->addColumn('pl_file', Types::INTEGER);
426
        $table_placelinks->setPrimaryKey(['pl_p_id', 'pl_gid', 'pl_file']);
427
        $table_placelinks->addIndex(['pl_p_id']);
428
        $table_placelinks->addIndex(['pl_gid']);
429
        $table_placelinks->addIndex(['pl_file']);
430
431
        $table_place_location = $schema->createTable($prefix . 'place_location');
432
        $table_place_location->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
433
        $table_place_location->addColumn('parent_id', Types::INTEGER, ['notNull' => false]);
434
        $table_place_location->addColumn('place', Types::STRING, ['length' => 120, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
435
        $table_place_location->addColumn('latitude', Types::FLOAT, ['notNull' => false]);
436
        $table_place_location->addColumn('longitude', Types::FLOAT, ['notNull' => false]);
437
        $table_place_location->setPrimaryKey(['id']);
438
        $table_place_location->addUniqueIndex(['parent_id', 'place']);
439
        $table_place_location->addUniqueIndex(['place', 'parent_id']);
440
        $table_place_location->addIndex(['latitude']);
441
        $table_place_location->addIndex(['longitude']);
442
        $table_place_location->addForeignKeyConstraint($table_place_location, ['parent_id'], ['id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
443
444
        $table_sources = $schema->createTable($prefix . 'session');
445
        $table_sources->addColumn('session_id', Types::STRING, ['length' => 32, 'platformOptions' => ['collation' => 'ascii_bin']]);
446
        $table_sources->addColumn('session_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'precision' => 0]);
447
        $table_sources->addColumn('user_id', Types::INTEGER);
448
        $table_sources->addColumn('ip_address', Types::STRING, ['length' => 45, 'platformOptions' => ['collation' => 'ascii_bin']]);
449
        $table_sources->addColumn('session_data', Types::BLOB);
450
        $table_sources->setPrimaryKey(['session_id']);
451
        $table_sources->addIndex(['session_time']);
452
        $table_sources->addIndex(['user_id', 'ip_address']);
453
454
        $table_site_setting = $schema->createTable($prefix . 'site_setting');
455
        $table_site_setting->addColumn('setting_name', Types::STRING, ['length' => 32, 'platformOptions' => ['collation' => 'ascii_bin']]);
456
        $table_site_setting->addColumn('setting_value', Types::STRING, ['length' => 2000, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
457
        $table_site_setting->setPrimaryKey(['setting_name']);
458
459
        $table_sources = $schema->createTable($prefix . 'sources');
460
        $table_sources->addColumn('s_id', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
461
        $table_sources->addColumn('s_file', Types::INTEGER);
462
        $table_sources->addColumn('s_name', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
463
        $table_sources->addColumn('s_gedcom', Types::TEXT, ['platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
464
        $table_sources->setPrimaryKey(['s_id', 's_file']);
465
        $table_sources->addUniqueIndex(['s_file', 's_id']);
466
        $table_sources->addIndex(['s_name']);
467
468
        $table_user_gedcom_setting = $schema->createTable($prefix . 'user_gedcom_setting');
469
        $table_user_gedcom_setting->addColumn('user_id', Types::INTEGER);
470
        $table_user_gedcom_setting->addColumn('gedcom_id', Types::INTEGER);
471
        $table_user_gedcom_setting->addColumn('setting_name', Types::STRING, ['length' => 32, 'platformOptions' => ['collation' => 'ascii_bin']]);
472
        $table_user_gedcom_setting->addColumn('setting_value', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
473
        $table_user_gedcom_setting->setPrimaryKey(['user_id', 'gedcom_id', 'setting_name']);
474
        $table_user_gedcom_setting->addIndex(['gedcom_id']);
475
        $table_user_gedcom_setting->addForeignKeyConstraint($table_gedcom, ['gedcom_id'], ['gedcom_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
476
        $table_user_gedcom_setting->addForeignKeyConstraint($table_user, ['user_id'], ['user_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
477
478
        $table_user_setting = $schema->createTable($prefix . 'user_setting');
479
        $table_user_setting->addColumn('user_id', Types::INTEGER);
480
        $table_user_setting->addColumn('setting_name', Types::STRING, ['length' => 32, 'platformOptions' => ['collation' => 'ascii_bin']]);
481
        $table_user_setting->addColumn('setting_value', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
482
        $table_user_setting->setPrimaryKey(['user_id', 'setting_name']);
483
        $table_user_setting->addForeignKeyConstraint($table_user, ['user_id'], ['user_id'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
484
485
        /*
486
        $site = $schema->createTable($prefix . 'site');
487
        $site->addColumn('uuid', Types::STRING, ['length' => 36, 'platformOptions' => ['collation' => 'ascii_bin']]);
488
        $site->addColumn('db_schema', Types::STRING, ['length' => 20, 'platformOptions' => ['collation' => 'ascii_bin']]);
489
        $site->addColumn('created_at', Types::TIME_MUTABLE);
490
        $site->addColumn('updated_at', Types::TIME_MUTABLE);
491
        $site->setPrimaryKey(['uuid']);
492
493
        $job = $schema->createTable($prefix . 'job');
494
        $job->addColumn('uuid', Types::STRING, ['length' => 36, 'platformOptions' => ['collation' => 'ascii_bin']]);
495
        $job->addColumn('failures', Types::INTEGER, ['default' => 0]);
496
        $job->addColumn('job', Types::STRING, ['length' => 255, 'platformOptions' => ['collation' => 'ascii_bin']]);
497
        $job->addColumn('parameters', Types::TEXT, ['platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
498
        $job->addColumn('error', Types::TEXT, ['notnull' => false, 'platformOptions' => ['collation' => 'utf8mb4_unicode_ci']]);
499
        $job->addColumn('created_at', Types::TIME_MUTABLE);
500
        $job->addColumn('updated_at', Types::TIME_MUTABLE);
501
        $job->setPrimaryKey(['uuid']);
502
503
        $job_dependency = $schema->createTable($prefix . 'job_dependency');
504
        $job_dependency->addColumn('uuid1', Types::STRING, ['length' => 36, 'platformOptions' => ['collation' => 'ascii_bin']]);
505
        $job_dependency->addColumn('uuid2', Types::STRING, ['length' => 36, 'platformOptions' => ['collation' => 'ascii_bin']]);
506
        $job_dependency->addUniqueIndex(['uuid1', 'uuid2']);
507
        $job_dependency->addUniqueIndex(['uuid2', 'uuid1']);
508
        $job_dependency->addForeignKeyConstraint($job, ['uuid1'], ['uuid'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
509
        $job_dependency->addForeignKeyConstraint($job, ['uuid2'], ['uuid'], ['onDelete' => 'CASCADE', 'onUpdate' => 'CASCADE']);
510
        */
511
512
        $schema_manager = $connection->createSchemaManager();
513
        $comparator     = $schema_manager->createComparator();
514
515
        do {
516
            $has_errors = false;
517
518
            echo '<h1>TRYING...</h1>';
519
            $current_schema = $schema_manager->createSchema();
520
            $difference = $comparator->compareSchemas($current_schema, $schema);
521
            $queries    = $difference->toSaveSql($platform);
522
523
            foreach ($queries as $query) {
524
                try {
525
                    echo '<p>', $query, '</p>';
526
                    $connection->executeStatement($query);
527
                } catch (Exception $ex) {
528
                    echo '<p style="color:red">', $ex->getMessage(), '</p>';
529
                    $has_errors = true;
530
                }
531
            }
532
        } while ($has_errors);
533
534
        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...
535
/*
536
         function f(string $query): int {
537
            if (str_contains($query, 'DROP FOREIGN KEY')) {
538
                return 1;
539
            }
540
            if (str_contains($query, 'ADD CONSTRAINT FK_')) {
541
                return 3;
542
            }
543
            return 2;
544
        };
545
546
        usort($queries, fn($x, $y) => f($x) <=> f($y));
547
*/
548
549
        foreach ($queries as $query) {
0 ignored issues
show
Unused Code introduced by
ForeachNode 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...
550
            echo '<p>', $query, '</p>';
551
            try {
552
                $connection->executeStatement($query);
553
            } catch (Exception $ex) {
554
                echo '<p>', $ex->getMessage(), '</p>';
555
            }
556
        }
557
        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...
558
    }
559
}
560