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