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)) { |
|
|
|
|
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; |
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) |
|
|
|
|
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; |
77
|
|
|
} |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
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.