xref: /webtrees/app/Schema/Migration0.php (revision 56a34df1984fbc88561415294f7408501262a1ab)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Schema;
19
20use Illuminate\Database\Capsule\Manager as DB;
21use Illuminate\Database\Schema\Blueprint;
22
23/**
24 * Upgrade the database schema from version 0 (empty database) to version 1.
25 */
26class Migration0 implements MigrationInterface
27{
28    /**
29     * Upgrade to to the next version.
30     *
31     * @return void
32     */
33    public function upgrade(): void
34    {
35        DB::schema()->create('gedcom', static function (Blueprint $table): void {
36            $table->integer('gedcom_id', true);
37            $table->string('gedcom_name', 255);
38            $table->integer('sort_order')->default(0);
39
40            $table->unique('gedcom_name');
41            $table->index('sort_order');
42        });
43
44        DB::schema()->create('site_setting', static function (Blueprint $table): void {
45            $table->string('setting_name', 32);
46            $table->string('setting_value', 2000);
47
48            $table->primary('setting_name');
49        });
50
51        DB::schema()->create('gedcom_setting', static function (Blueprint $table): void {
52            $table->integer('gedcom_id');
53            $table->string('setting_name', 32);
54            $table->string('setting_value', 255);
55
56            $table->primary(['gedcom_id', 'setting_name']);
57
58            $table->foreign('gedcom_id')->references('gedcom_id')->on('gedcom');
59        });
60
61        DB::schema()->create('user', static function (Blueprint $table): void {
62            $table->integer('user_id', true);
63            $table->string('user_name', 32);
64            $table->string('real_name', 64);
65            $table->string('email', 64);
66            $table->string('password', 128);
67
68            $table->unique('user_name');
69            $table->unique('email');
70        });
71
72        DB::schema()->create('user_setting', static function (Blueprint $table): void {
73            $table->integer('user_id');
74            $table->string('setting_name', 32);
75            $table->string('setting_value', 255);
76
77            $table->primary(['user_id', 'setting_name']);
78
79            $table->foreign('user_id')->references('user_id')->on('user');
80        });
81
82        DB::schema()->create('user_gedcom_setting', static function (Blueprint $table): void {
83            $table->integer('user_id');
84            $table->integer('gedcom_id');
85            $table->string('setting_name', 32);
86            $table->string('setting_value', 255);
87
88            // Default constraint names are too long for MySQL.
89            $key = DB::connection()->getTablePrefix() . $table->getTable() . '_primary';
90
91            $table->primary(['user_id', 'gedcom_id', 'setting_name'], $key);
92            $table->index('gedcom_id');
93
94            $table->foreign('user_id')->references('user_id')->on('user');
95            $table->foreign('gedcom_id')->references('gedcom_id')->on('gedcom');
96        });
97
98        DB::schema()->create('log', static function (Blueprint $table): void {
99            $table->integer('log_id', true);
100            $table->timestamp('log_time')->useCurrent();
101            $table->enum('log_type', ['auth', 'config', 'debug', 'edit', 'error', 'media', 'search']);
102            $table->longText('log_message');
103            $table->ipAddress('ip_address');
104            $table->integer('user_id')->nullable();
105            $table->integer('gedcom_id')->nullable();
106
107            $table->index('log_time');
108            $table->index('log_type');
109            $table->index('ip_address');
110            $table->index('user_id');
111            $table->index('gedcom_id');
112
113            $table->foreign('user_id')->references('user_id')->on('user');
114            $table->foreign('gedcom_id')->references('gedcom_id')->on('gedcom');
115        });
116
117        DB::schema()->create('change', static function (Blueprint $table): void {
118            $table->integer('change_id', true);
119            $table->timestamp('change_time')->useCurrent();
120            $table->enum('status', ['accepted', 'pending', 'rejected'])->default('pending');
121            $table->integer('gedcom_id');
122            $table->string('xref', 20);
123            $table->longText('old_gedcom');
124            $table->longText('new_gedcom');
125            $table->integer('user_id');
126
127            $table->index(['gedcom_id', 'status', 'xref']);
128            $table->index('user_id');
129
130            $table->foreign('user_id')->references('user_id')->on('user');
131            $table->foreign('gedcom_id')->references('gedcom_id')->on('gedcom');
132        });
133
134        DB::schema()->create('message', static function (Blueprint $table): void {
135            $table->integer('message_id', true);
136            $table->string('sender', 64);
137            $table->ipAddress('ip_address');
138            $table->integer('user_id');
139            $table->string('subject', 255);
140            $table->longText('body');
141            $table->timestamp('created')->useCurrent();
142
143            $table->index('user_id');
144
145            $table->foreign('user_id')->references('user_id')->on('user');
146        });
147
148        DB::schema()->create('default_resn', static function (Blueprint $table): void {
149            $table->integer('default_resn_id', true);
150            $table->integer('gedcom_id');
151            $table->string('xref', 20)->nullable();
152            $table->string('tag_type', 15)->nullable();
153            $table->enum('resn', ['none', 'privacy', 'confidential', 'hidden']);
154            $table->string('comment', 255)->nullable();
155            $table->timestamp('updated')->useCurrent();
156
157            $table->unique(['gedcom_id', 'xref', 'tag_type']);
158
159            $table->foreign('gedcom_id')->references('gedcom_id')->on('gedcom');
160        });
161
162        DB::schema()->create('individuals', static function (Blueprint $table): void {
163            $table->string('i_id', 20);
164            $table->integer('i_file');
165            $table->string('i_rin', 20);
166            $table->enum('i_sex', ['U', 'M', 'F']);
167            $table->longText('i_gedcom');
168
169            $table->primary(['i_id', 'i_file']);
170            $table->unique(['i_file', 'i_id']);
171        });
172
173        DB::schema()->create('families', static function (Blueprint $table): void {
174            $table->string('f_id', 20);
175            $table->integer('f_file');
176            $table->string('f_husb', 20)->nullable();
177            $table->string('f_wife', 20)->nullable();
178            $table->longText('f_gedcom');
179            $table->integer('f_numchil');
180
181            $table->primary(['f_id', 'f_file']);
182            $table->unique(['f_file', 'f_id']);
183            $table->index('f_husb');
184            $table->index('f_wife');
185        });
186
187        DB::schema()->create('places', static function (Blueprint $table): void {
188            $table->integer('p_id', true);
189            $table->string('p_place', 150);
190            $table->integer('p_parent_id')->nullable();
191            $table->integer('p_file');
192            $table->longText('p_std_soundex')->nullable();
193            $table->longText('p_dm_soundex')->nullable();
194
195            $table->index(['p_file', 'p_place']);
196            $table->unique(['p_parent_id', 'p_file', 'p_place']);
197        });
198
199        DB::schema()->create('placelinks', static function (Blueprint $table): void {
200            $table->integer('pl_p_id');
201            $table->string('pl_gid', 20);
202            $table->integer('pl_file');
203
204            $table->primary(['pl_p_id', 'pl_gid', 'pl_file']);
205            $table->index('pl_p_id');
206            $table->index('pl_gid');
207            $table->index('pl_file');
208        });
209
210        DB::schema()->create('dates', static function (Blueprint $table): void {
211            $table->tinyInteger('d_day');
212            $table->char('d_month', 5)->nullable();
213            $table->tinyInteger('d_mon');
214            $table->smallInteger('d_year');
215            $table->mediumInteger('d_julianday1');
216            $table->mediumInteger('d_julianday2');
217            $table->string('d_fact', 15);
218            $table->string('d_gid', 20);
219            $table->integer('d_file');
220            $table->enum('d_type', ['@#DGREGORIAN@', '@#DJULIAN@', '@#DHEBREW@', '@#DFRENCH R@', '@#DHIJRI@', '@#DROMAN@', '@#DJALALI@']);
221
222            $table->index('d_day');
223            $table->index('d_month');
224            $table->index('d_mon');
225            $table->index('d_year');
226            $table->index('d_julianday1');
227            $table->index('d_julianday2');
228            $table->index('d_gid');
229            $table->index('d_file');
230            $table->index('d_type');
231            $table->index(['d_fact', 'd_gid']);
232        });
233
234        DB::schema()->create('media', static function (Blueprint $table): void {
235            $table->string('m_id', 20);
236            $table->string('m_ext', 6)->nullable();
237            $table->string('m_type', 20)->nullable();
238            $table->string('m_titl', 248)->nullable();
239            $table->string('m_filename', 248)->nullable();
240            $table->integer('m_file');
241            $table->longText('m_gedcom')->nullable();
242
243            $table->primary(['m_file', 'm_id']);
244            $table->unique(['m_id', 'm_file']);
245            $table->index(['m_ext', 'm_type']);
246        });
247
248        DB::schema()->create('next_id', static function (Blueprint $table): void {
249            $table->integer('gedcom_id');
250            $table->string('record_type', 15);
251            $table->decimal('next_id', 20, 0);
252
253            $table->primary(['gedcom_id', 'record_type']);
254
255            $table->foreign('gedcom_id')->references('gedcom_id')->on('gedcom');
256        });
257
258        DB::schema()->create('other', static function (Blueprint $table): void {
259            $table->string('o_id', 20);
260            $table->integer('o_file');
261            $table->string('o_type', 15);
262            $table->longText('o_gedcom');
263
264            $table->primary(['o_id', 'o_file']);
265            $table->unique(['o_file', 'o_id']);
266        });
267
268        DB::schema()->create('sources', static function (Blueprint $table): void {
269            $table->string('s_id', 20);
270            $table->integer('s_file');
271            $table->string('s_name', 255);
272            $table->longText('s_gedcom');
273
274            $table->primary(['s_id', 's_file']);
275            $table->unique(['s_file', 's_id']);
276            $table->index('s_name');
277        });
278
279        DB::schema()->create('link', static function (Blueprint $table): void {
280            $table->integer('l_file');
281            $table->string('l_from', 20);
282            $table->string('l_type', 15);
283            $table->string('l_to', 20);
284
285            $table->primary(['l_from', 'l_file', 'l_type', 'l_to']);
286            $table->unique(['l_to', 'l_file', 'l_type', 'l_from']);
287        });
288
289        DB::schema()->create('name', static function (Blueprint $table): void {
290            $table->integer('n_file');
291            $table->string('n_id', 20);
292            $table->integer('n_num');
293            $table->string('n_type', 15);
294            $table->string('n_sort', 255);
295            $table->string('n_full', 255);
296            $table->string('n_surname', 255)->nullable();
297            $table->string('n_surn', 255)->nullable();
298            $table->string('n_givn', 255)->nullable();
299            $table->string('n_soundex_givn_std', 255)->nullable();
300            $table->string('n_soundex_surn_std', 255)->nullable();
301            $table->string('n_soundex_givn_dm', 255)->nullable();
302            $table->string('n_soundex_surn_dm', 255)->nullable();
303
304            $table->primary(['n_id', 'n_file', 'n_num']);
305            $table->index(['n_full', 'n_id', 'n_file']);
306            $table->index(['n_surn', 'n_file', 'n_type', 'n_id']);
307            $table->index(['n_givn', 'n_file', 'n_type', 'n_id']);
308        });
309
310        DB::schema()->create('module', static function (Blueprint $table): void {
311            $table->string('module_name', 32);
312            $table->enum('status', ['enabled', 'disabled'])->default('enabled');
313            $table->integer('tab_order')->nullable();
314            $table->integer('menu_order')->nullable();
315            $table->integer('sidebar_order')->nullable();
316
317            $table->primary('module_name');
318        });
319
320        DB::schema()->create('module_setting', static function (Blueprint $table): void {
321            $table->string('module_name', 32);
322            $table->string('setting_name', 32);
323            $table->longText('setting_value');
324
325            $table->primary(['module_name', 'setting_name']);
326
327            $table->foreign('module_name')->references('module_name')->on('module');
328        });
329
330        DB::schema()->create('module_privacy', static function (Blueprint $table): void {
331            $table->string('module_name', 32);
332            $table->integer('gedcom_id');
333            $table->enum('component', ['block', 'chart', 'menu', 'report', 'sidebar', 'tab', 'theme']);
334            $table->tinyInteger('access_level');
335
336            // Default constraint names are too long for MySQL.
337            $key0 = DB::connection()->getTablePrefix() . $table->getTable() . '_primary';
338            $key1 = DB::connection()->getTablePrefix() . $table->getTable() . '_ix1';
339
340            $table->primary(['module_name', 'gedcom_id', 'component'], $key0);
341            $table->unique(['gedcom_id', 'module_name', 'component'], $key1);
342
343            $table->foreign('module_name')->references('module_name')->on('module');
344            $table->foreign('gedcom_id')->references('gedcom_id')->on('gedcom');
345        });
346
347        DB::schema()->create('block', static function (Blueprint $table): void {
348            $table->integer('block_id', true);
349            $table->integer('gedcom_id')->nullable();
350            $table->integer('user_id')->nullable();
351            $table->string('xref', 20)->nullable();
352            $table->enum('location', ['main', 'side'])->nullable();
353            $table->integer('block_order');
354            $table->string('module_name', 32);
355
356            $table->index('module_name');
357            $table->index('gedcom_id');
358            $table->index('user_id');
359
360            $table->foreign('module_name')->references('module_name')->on('module');
361            $table->foreign('gedcom_id')->references('gedcom_id')->on('gedcom');
362            $table->foreign('user_id')->references('user_id')->on('user');
363        });
364
365        DB::schema()->create('block_setting', static function (Blueprint $table): void {
366            $table->integer('block_id');
367            $table->string('setting_name', 32);
368            $table->longText('setting_value');
369
370            $table->primary(['block_id', 'setting_name']);
371
372            $table->foreign('block_id')->references('block_id')->on('block');
373        });
374
375        DB::schema()->create('hit_counter', static function (Blueprint $table): void {
376            $table->integer('gedcom_id');
377            $table->string('page_name', 32);
378            $table->string('page_parameter', 32);
379            $table->integer('page_count');
380
381            // Default constraint names are too long for MySQL.
382            $key = DB::connection()->getTablePrefix() . $table->getTable() . '_primary';
383
384            $table->primary(['gedcom_id', 'page_name', 'page_parameter'], $key);
385
386            $table->foreign('gedcom_id')->references('gedcom_id')->on('gedcom');
387        });
388
389        DB::schema()->create('session', static function (Blueprint $table): void {
390            $table->string('session_id', 32);
391            $table->timestamp('session_time')->useCurrent();
392            $table->integer('user_id');
393            $table->ipAddress('ip_address');
394            $table->binary('session_data');
395
396            $table->primary('session_id');
397            $table->index('session_time');
398            $table->index(['user_id', 'ip_address']);
399        });
400
401        // See https://github.com/laravel/framework/issues/3544
402        if (DB::connection()->getDriverName() === 'mysql') {
403            $table = DB::connection()->getSchemaGrammar()->wrapTable('session');
404            $sql   = 'ALTER TABLE ' . $table . ' MODIFY session_data LONGBLOB';
405            DB::connection()->statement($sql);
406        }
407
408        DB::schema()->create('gedcom_chunk', static function (Blueprint $table): void {
409            $table->integer('gedcom_chunk_id', true);
410            $table->integer('gedcom_id');
411            $table->binary('chunk_data');
412            $table->boolean('imported')->default(0);
413
414            $table->index(['gedcom_id', 'imported']);
415
416            $table->foreign('gedcom_id')->references('gedcom_id')->on('gedcom');
417        });
418
419        // See https://github.com/laravel/framework/issues/3544
420        if (DB::connection()->getDriverName() === 'mysql') {
421            $table = DB::connection()->getSchemaGrammar()->wrapTable('gedcom_chunk');
422            $sql   = 'ALTER TABLE ' . $table . ' MODIFY chunk_data LONGBLOB';
423            DB::connection()->statement($sql);
424        }
425    }
426}
427