aboutsummaryrefslogtreecommitdiffhomepage
path: root/app/DoctrineMigrations/Version20190826204730.php
blob: ee1ba6bf100b66e43036cb9b9d4734c9b980f466 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php

declare(strict_types=1);

namespace Application\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Wallabag\CoreBundle\Doctrine\WallabagMigration;

/**
 * Add tables for the ignore origin rules.
 */
final class Version20190826204730 extends WallabagMigration
{
    public function up(Schema $schema): void
    {
        if (false === $schema->hasTable($this->getTable('ignore_origin_user_rule'))) {
            $userTable = $schema->createTable($this->getTable('ignore_origin_user_rule', true));
            $userTable->addColumn('id', 'integer', ['autoincrement' => true]);
            $userTable->addColumn('config_id', 'integer');
            $userTable->addColumn('rule', 'string', ['length' => 255]);
            $userTable->addIndex(['config_id'], 'idx_config');
            $userTable->setPrimaryKey(['id']);
            $userTable->addForeignKeyConstraint($this->getTable('config'), ['config_id'], ['id'], [], 'fk_config');

            if ('postgresql' === $this->connection->getDatabasePlatform()->getName()) {
                $schema->dropSequence('ignore_origin_user_rule_id_seq');
                $schema->createSequence('ignore_origin_user_rule_id_seq');
            }
        }

        if (false === $schema->hasTable($this->getTable('ignore_origin_instance_rule'))) {
            $instanceTable = $schema->createTable($this->getTable('ignore_origin_instance_rule', true));
            $instanceTable->addColumn('id', 'integer', ['autoincrement' => true]);
            $instanceTable->addColumn('rule', 'string', ['length' => 255]);
            $instanceTable->setPrimaryKey(['id']);

            if ('postgresql' === $this->connection->getDatabasePlatform()->getName()) {
                $schema->dropSequence('ignore_origin_instance_rule_id_seq');
                $schema->createSequence('ignore_origin_instance_rule_id_seq');
            }
        }
    }

    public function postUp(Schema $schema): void
    {
        foreach ($this->container->getParameter('wallabag_core.default_ignore_origin_instance_rules') as $entity) {
            $previous_rule = $this->container
                ->get('doctrine.orm.default_entity_manager')
                ->getConnection()
                ->fetchArray('SELECT * FROM ' . $this->getTable('ignore_origin_instance_rule') . " WHERE rule = '" . $entity['rule'] . "'");

            if (false === $previous_rule) {
                $this->addSql('INSERT INTO ' . $this->getTable('ignore_origin_instance_rule') . " (rule) VALUES ('" . $entity['rule'] . "');");
            }
        }
    }

    public function down(Schema $schema): void
    {
        $schema->dropTable($this->getTable('ignore_origin_user_rule'));
        $schema->dropTable($this->getTable('ignore_origin_instance_rule'));
    }
}