Completed
Pull Request — master (#186)
by Vladimir
28:22 queued 13:18
created

NewsCategoryStatusColumnConversion::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
1
<?php
2
3
use Phinx\Migration\AbstractMigration;
4
5
class NewsCategoryStatusColumnConversion extends AbstractMigration
6
{
7
    public function up()
8
    {
9
        $newsCategoryTable = $this->table('news_categories');
10
        $newsCategoryTable
11
            ->addColumn('is_read_only', 'boolean', [
12
                'after' => 'protected',
13
                'null' => false,
14
                'default' => false,
15
                'comment' => 'When set to true, no new articles should be able to use this category'
16
            ])
17
            ->addColumn('is_deleted', 'boolean', [
18
                'after' => 'is_read_only',
19
                'null' => false,
20
                'default' => false,
21
                'comment' => 'Whether or not the news category has been soft deleted',
22
            ])
23
            ->changeColumn('protected', 'boolean', [
24
                'default' => false,
25
                'null' => false,
26
                'comment' => 'When set to true, prevents the category from being deleted from the UI',
27
            ])
28
            ->update()
29
        ;
30
31
        $this->query("UPDATE news_categories SET is_deleted = 1 WHERE status = 'deleted';");
32
33
        $newsCategoryTable
34
            ->removeColumn('status')
35
            ->renameColumn('protected', 'is_protected')
36
            ->update()
37
        ;
38
    }
39
40
    public function down()
41
    {
42
        $newsCategoryTable = $this->table('news_categories');
43
        $newsCategoryTable
44
            ->addColumn('status', 'set', [
45
                'values' => ['enabled', 'disabled', 'deleted'],
46
                'after' => 'is_deleted',
47
                'null' => false,
48
                'default' => 'enabled',
49
                'comment' => 'The status of the news element',
50
            ])
51
            ->renameColumn('is_protected', 'protected')
52
            ->update()
53
        ;
54
55
        $this->query("UPDATE news_categories SET status = 'deleted' WHERE is_deleted = 1;");
56
57
        $newsCategoryTable
58
            ->removeColumn('is_deleted')
59
            ->removeColumn('is_read_only')
60
            ->update()
61
        ;
62
    }
63
}
64