summaryrefslogtreecommitdiffstats
path: root/lib/Migration/Version150005Date20201009192341.php
blob: 7d739603b41256c61f1b49ac43bcb6aede3eada8 (plain)
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php

declare(strict_types=1);

namespace OCA\News\Migration;

use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;

/**
 * Auto-generated migration step: Please modify to your needs!
 */
class Version150005Date20201009192341 extends SimpleMigrationStep {

    protected $connection;

    public function __construct(IDBConnection $connection)
    {
        $this->connection = $connection;
    }

	/**
	 * @param IOutput $output
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
	 * @param array $options
	 *
	 * @return void
	 */
	public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
        $qb = $this->connection->getQueryBuilder();

        $qb->update('news_feeds')
            ->set('folder_id', $qb->createPositionalParameter(null, IQueryBuilder::PARAM_NULL))
            ->where('folder_id = 0')
            ->executeStatement();

        $feed_name = $this->connection->getQueryBuilder()->getTableName('news_feeds');
        $folder_name = $this->connection->getQueryBuilder()->getTableName('news_folders');

        $items_query = "DELETE FROM ${feed_name} WHERE ${feed_name}.`folder_id` NOT IN (SELECT DISTINCT id FROM ${folder_name}) AND ${feed_name}.`folder_id` IS NOT NULL";
        $this->connection->executeQuery($items_query);

        $item_name = $this->connection->getQueryBuilder()->getTableName('news_items');
        $feed_name = $this->connection->getQueryBuilder()->getTableName('news_feeds');

        $items_query = "DELETE FROM ${item_name} WHERE ${item_name}.`feed_id` NOT IN (SELECT DISTINCT id FROM ${feed_name})";
        $this->connection->executeQuery($items_query);
	}

	/**
	 * @param IOutput $output
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
	 * @param array $options
	 * @return null|ISchemaWrapper
	 */
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
        /** @var ISchemaWrapper $schema */
        $schema = $schemaClosure();

        if ($schema->hasTable('news_items') &&
            !$schema->getTable('news_items')->hasForeignKey('feed')) {

            $schema->getTable('news_items')
                ->addForeignKeyConstraint(
                    $schema->getTable('news_feeds')->getName(),
                    ['feed_id'],
                    ['id'],
                    ['onDelete' => 'CASCADE'],
                    'feed'
                );
        }

        if ($schema->hasTable('news_feeds') &&
            !$schema->getTable('news_feeds')->hasForeignKey('folder')) {

            $schema->getTable('news_feeds')
                ->addForeignKeyConstraint(
                    $schema->getTable('news_folders')->getName(),
                    ['folder_id'],
                    ['id'],
                    ['onDelete' => 'CASCADE'],
                    'folder'
                );
        }

        return $schema;
	}

	/**
	 * @param IOutput $output
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
	 * @param array $options
	 *
	 * @return void
	 */
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
	}
}