Completed
Pull Request — master (#1840)
by Kentaro
33:36
created

Version20161014100031::dropIndex()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 3
1
<?php
2
3
namespace DoctrineMigrations;
4
5
use Doctrine\DBAL\Migrations\AbstractMigration;
6
use Doctrine\DBAL\Schema\Schema;
7
8
class Version20161014100031 extends AbstractMigration
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
9
{
10
    /**
11
     * @param Schema $schema
12
     */
13
    public function up(Schema $schema)
14
    {
15
        $this->createIndex($schema, 'dtb_product_class', array('price02'), 'dtb_product_class_price02_idx');
16
        $this->createIndex($schema, 'dtb_product_class', array('stock', 'stock_unlimited'), 'dtb_product_class_stock_stock_unlimited_idx');
17
        $this->createIndex($schema, 'dtb_customer', array('email'), 'dtb_customer_email_idx', array('email' => 256));
18
        $this->createIndex($schema, 'dtb_customer', array('create_date'), 'dtb_customer_create_date_idx');
19
        $this->createIndex($schema, 'dtb_customer', array('update_date'), 'dtb_customer_update_date_idx');
20
        $this->createIndex($schema, 'dtb_customer', array('last_buy_date'), 'dtb_customer_last_buy_date_idx');
21
        $this->createIndex($schema, 'dtb_customer', array('buy_times'), 'dtb_customer_buy_times_idx');
22
        $this->createIndex($schema, 'dtb_customer', array('buy_total'), 'dtb_customer_buy_total_idx');
23
        $this->createIndex($schema, 'dtb_order', array('pre_order_id'), 'dtb_order_pre_order_id_idx', array('pre_order_id' => 40));
24
        $this->createIndex($schema, 'dtb_order', array('order_email'), 'dtb_order_order_email_idx', array('order_email' => 256));
25
        $this->createIndex($schema, 'dtb_order', array('order_date'), 'dtb_order_order_date_idx');
26
        $this->createIndex($schema, 'dtb_order', array('payment_date'), 'dtb_order_payment_date_idx');
27
        $this->createIndex($schema, 'dtb_order', array('commit_date'), 'dtb_order_commit_date_idx');
28
        $this->createIndex($schema, 'dtb_order', array('update_date'), 'dtb_order_update_date_idx');
29
        $this->createIndex($schema, 'dtb_page_layout', array('url'), 'dtb_page_layout_url_idx', array('url' => 128));
30
    }
31
32
    /**
33
     * @param Schema $schema
34
     */
35
    public function down(Schema $schema)
36
    {
37
        $this->dropIndex($schema, 'dtb_product_class', 'dtb_product_class_price02_idx');
38
        $this->dropIndex($schema, 'dtb_product_class', 'dtb_product_class_stock_stock_unlimited_idx');
39
        $this->dropIndex($schema, 'dtb_customer', 'dtb_customer_email_idx');
40
        $this->dropIndex($schema, 'dtb_customer', 'dtb_customer_create_date_idx');
41
        $this->dropIndex($schema, 'dtb_customer', 'dtb_customer_update_date_idx');
42
        $this->dropIndex($schema, 'dtb_customer', 'dtb_customer_last_buy_date_idx');
43
        $this->dropIndex($schema, 'dtb_customer', 'dtb_customer_buy_times_idx');
44
        $this->dropIndex($schema, 'dtb_customer', 'dtb_customer_buy_total_idx');
45
        $this->dropIndex($schema, 'dtb_order', 'dtb_order_pre_order_id_idx');
46
        $this->dropIndex($schema, 'dtb_order', 'dtb_order_pre_order_email_idx');
47
        $this->dropIndex($schema, 'dtb_order', 'dtb_order_pre_order_date_idx');
48
        $this->dropIndex($schema, 'dtb_order', 'dtb_order_pre_payment_date_idx');
49
        $this->dropIndex($schema, 'dtb_order', 'dtb_order_pre_commit_date_idx');
50
        $this->dropIndex($schema, 'dtb_order', 'dtb_order_pre_update_date_idx');
51
        $this->dropIndex($schema, 'dtb_page_layout', 'dtb_page_layout_url_idx');
52
    }
53
54
    /**
55
     * @param Schema $schema
56
     * @param string $tableName
57
     * @param array $columns
58
     * @param string $indexName
59
     * @param array $length
60
     */
61
    protected function createIndex(Schema $schema, $tableName, array $columns, $indexName, array $length = array())
62
    {
63
        if (!$schema->hasTable($tableName)) {
64
            return false;
65
        }
66
67
        $table = $schema->getTable($tableName);
68
        if (!$table->hasIndex($indexName)) {
69
            if ($this->connection->getDatabasePlatform()->getName() == "mysql" && !empty($length)) {
70
                $cols = array();
71
                foreach ($length as $column => $len) {
72
                    $cols[] = sprintf('%s(%d)', $column, $len);
73
                }
74
                $this->addSql('CREATE INDEX '.$indexName.' ON '.$tableName.'('.implode(', ', $cols).');');
75
            } else {
76
                $table->addIndex($columns, $indexName);
77
            }
78
            return true;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
79
        }
80
        return false;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
81
    }
82
83
    /**
84
     * @param Schema $schema
85
     * @param string $tableName
86
     * @param string $indexName
87
     */
88
    protected function dropIndex(Schema $schema, $tableName, $indexName)
89
    {
90
        if (!$schema->hasTable($tableName)) {
91
            return false;
92
        }
93
        $table = $schema->getTable($tableName);
94
        if ($table->hasIndex($indexName)) {
95
            $table->dropIndex($indexName);
96
            return true;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
97
        }
98
        return false;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
99
    }
100
}
101