Completed
Push — master ( 8e32ae...77800d )
by Yangsin
100:37 queued 95:24
created

Version20161219143135::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace DoctrineMigrations;
4
5
use Doctrine\DBAL\Migrations\AbstractMigration;
6
use Doctrine\DBAL\Schema\Schema;
7
8
/**
9
 * Auto-generated Migration: Please modify to your needs!
10
 */
11
class Version20161219143135 extends AbstractMigration
12
{
13
    /**
14
     * @param Schema $schema
15
     */
16
    public function up(Schema $schema)
17
    {
18
        $this->createIndex($schema, 'dtb_customer', array('email'), 'dtb_customer_email_idx', array('email' => 191));
19
        $this->createIndex($schema, 'dtb_order', array('order_email'), 'dtb_order_order_email_idx', array('order_email' => 191));
20
    }
21
22
    /**
23
     * @param Schema $schema
24
     */
25
    public function down(Schema $schema)
26
    {
27
        // XXX 前回のインデックスサイズを記憶しておくのは難しいため削除のみ
28
        $this->dropIndex($schema, 'dtb_customer', 'dtb_customer_email_idx');
29
        $this->dropIndex($schema, 'dtb_order', 'dtb_order_order_email_idx');
30
    }
31
32
    /**
33
     * @param Schema $schema
34
     * @param string $tableName
35
     * @param array $columns
36
     * @param string $indexName
37
     * @param array $length
38
     */
39
    protected function createIndex(Schema $schema, $tableName, array $columns, $indexName, array $length = array())
40
    {
41
        if (!$schema->hasTable($tableName)) {
42
            return false;
43
        }
44
45
        $table = $schema->getTable($tableName);
46
        if ($table->hasIndex($indexName)) {
47
            $this->dropIndex($schema, $tableName, $indexName);
48
        }
49 View Code Duplication
        if (!$table->hasIndex($indexName)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            if ($this->connection->getDatabasePlatform()->getName() == "mysql" && !empty($length)) {
51
                $cols = array();
52
                foreach ($length as $column => $len) {
53
                    $cols[] = sprintf('%s(%d)', $column, $len);
54
                }
55
                $this->addSql('CREATE INDEX '.$indexName.' ON '.$tableName.'('.implode(', ', $cols).');');
56
            } else {
57
                $table->addIndex($columns, $indexName);
58
            }
59
        }
60
        return true;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
61
    }
62
63
    /**
64
     * @param Schema $schema
65
     * @param string $tableName
66
     * @param string $indexName
67
     */
68 View Code Duplication
    protected function dropIndex(Schema $schema, $tableName, $indexName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        if (!$schema->hasTable($tableName)) {
71
            return false;
72
        }
73
        $table = $schema->getTable($tableName);
74
        if ($table->hasIndex($indexName)) {
75
            $table->dropIndex($indexName);
76
            return true;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
77
        }
78
        return false;
0 ignored issues
show
introduced by
Missing blank line before return statement
Loading history...
79
    }
80
}
81