Migration2   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A upgrade() 0 31 3
1
<?php
2
3
/**
4
 * webtrees-lib: MyArtJaub library for webtrees
5
 *
6
 * @package MyArtJaub\Webtrees
7
 * @subpackage Sosa
8
 * @author Jonathan Jaubart <[email protected]>
9
 * @copyright Copyright (c) 2009-2022, Jonathan Jaubart
10
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License, version 3
11
 */
12
13
declare(strict_types=1);
14
15
namespace MyArtJaub\Webtrees\Module\Sosa\Schema;
16
17
use Fisharebest\Webtrees\Schema\MigrationInterface;
18
use Illuminate\Database\Capsule\Manager as DB;
19
use Illuminate\Database\Schema\Blueprint;
20
21
/**
22
 * Upgrade the database schema from version 2 to version 3.
23
 */
24
class Migration2 implements MigrationInterface
25
{
26
    /**
27
     * {@inheritDoc}
28
     * @see \Fisharebest\Webtrees\Schema\MigrationInterface::upgrade()
29
     */
30
    public function upgrade(): void
31
    {
32
        $in_transaction = DB::connection()->getPdo()->inTransaction();
33
34
        // Clean up previous sosa table if it exists
35
        DB::schema()->dropIfExists('maj_sosa');
36
37
        DB::schema()->create('maj_sosa', static function (Blueprint $table): void {
38
39
            $table->integer('majs_gedcom_id');
40
            $table->integer('majs_user_id')->default(-1);
41
            $table->bigInteger('majs_sosa')->unsigned(); // Allow to calculate sosa on 64 generations
42
            $table->string('majs_i_id', 20);
43
            $table->tinyInteger('majs_gen')->nullable();
44
            $table->smallInteger('majs_birth_year')->nullable();
45
            $table->smallInteger('majs_birth_year_est')->nullable();
46
            $table->smallInteger('majs_death_year')->nullable();
47
            $table->smallInteger('majs_death_year_est')->nullable();
48
49
            $table->primary(['majs_gedcom_id', 'majs_user_id', 'majs_sosa']);
50
51
            $table->index(['majs_gedcom_id', 'majs_user_id']);
52
            $table->index(['majs_gedcom_id', 'majs_user_id', 'majs_i_id']);
53
            $table->index(['majs_gedcom_id', 'majs_user_id', 'majs_gen']);
54
55
            $table->foreign('majs_gedcom_id')->references('gedcom_id')->on('gedcom')->onDelete('cascade');
56
            $table->foreign('majs_user_id')->references('user_id')->on('user')->onDelete('cascade');
57
        });
58
59
        if ($in_transaction && !DB::connection()->getPdo()->inTransaction()) {
60
            DB::connection()->beginTransaction();
61
        }
62
    }
63
}
64