Failed Conditions
Push — master ( 5770fd...0536ca )
by Adrien
05:42
created

Version20221011092853::up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 19
ccs 0
cts 5
cp 0
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Migration;
6
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\Migrations\AbstractMigration;
9
10
class Version20221011092853 extends AbstractMigration
11
{
12
    public function up(Schema $schema): void
13
    {
14
        $this->addSql('ALTER TABLE user ADD first_login DATETIME DEFAULT NULL, ADD last_login DATETIME DEFAULT NULL');
15
16
        // Migrate first/last login from log to user
17
        $this->addSql(
18
            <<<STRING
19
                UPDATE user INNER JOIN (
20
                    SELECT creator_id, MIN(creation_date) AS min, MAX(creation_date) AS max
21
                    FROM log
22
                    WHERE creator_id IS NOT NULL AND message = 'login'
23
                    GROUP BY creator_id
24
                ) AS tmp ON tmp.creator_id = user.id
25
                SET user.first_login = IFNULL(user.first_login, tmp.min), user.last_login = tmp.max;
26
                STRING
27
        );
28
29
        // Delete old login logs
30
        $this->addSql("DELETE FROM log WHERE message = 'login' AND creation_date < DATE_SUB(NOW(), INTERVAL 2 MONTH)");
31
    }
32
}
33