1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the [code] |
5
|
|
|
* |
6
|
|
|
* Copyright (C) [year] [author] |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DoctrineMigrations; |
13
|
|
|
|
14
|
|
|
use Doctrine\DBAL\Migrations\AbstractMigration; |
15
|
|
|
use Doctrine\DBAL\Schema\Schema; |
16
|
|
|
use Doctrine\ORM\EntityManager; |
17
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
18
|
|
|
use Eccube\Application; |
19
|
|
|
use Eccube\Common\Constant; |
20
|
|
|
|
21
|
|
|
class Version[datetime] extends AbstractMigration |
22
|
|
|
{ |
23
|
|
|
protected $entities = array( |
24
|
|
|
[entityList] |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Schema $schema |
29
|
|
|
*/ |
30
|
|
|
public function up(Schema $schema) |
31
|
|
|
{ |
32
|
|
|
// this up() migration is auto-generated, please modify it to your needs |
33
|
|
|
|
34
|
|
|
if (version_compare(Constant::VERSION, '3.0.9', '>=')) { |
35
|
|
|
// 3.0.9 以降の場合、dcm.ymlの定義からテーブル生成を行う. |
36
|
|
|
$app = Application::getInstance(); |
37
|
|
|
$meta = $this->getMetadata($app['orm.em']); |
38
|
|
|
$tool = new SchemaTool($app['orm.em']); |
39
|
|
|
$tool->createSchema($meta); |
40
|
|
|
} else { |
41
|
|
|
// 3.0.0 - 3.0.8 |
|
|
|
|
42
|
|
|
[createTable] |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Schema $schema |
48
|
|
|
*/ |
49
|
|
|
public function down(Schema $schema) |
50
|
|
|
{ |
51
|
|
|
// this down() migration is auto-generated, please modify it to your needs |
52
|
|
|
|
53
|
|
|
if (version_compare(Constant::VERSION, '3.0.9', '>=')) { |
54
|
|
|
// 3.0.9 以降の場合、dcm.ymlの定義からテーブル/シーケンスの削除を行う |
55
|
|
|
$app = Application::getInstance(); |
56
|
|
|
$meta = $this->getMetadata($app['orm.em']); |
57
|
|
|
|
58
|
|
|
$tool = new SchemaTool($app['orm.em']); |
59
|
|
|
$schemaFromMetadata = $tool->getSchemaFromMetadata($meta); |
60
|
|
|
|
61
|
|
|
// テーブル削除 |
62
|
|
|
foreach ($schemaFromMetadata->getTables() as $table) { |
63
|
|
|
if ($schema->hasTable($table->getName())) { |
64
|
|
|
$schema->dropTable($table->getName()); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// シーケンス削除 |
69
|
|
|
foreach ($schemaFromMetadata->getSequences() as $sequence) { |
70
|
|
|
if ($schema->hasSequence($sequence->getName())) { |
71
|
|
|
$schema->dropSequence($sequence->getName()); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} else { |
75
|
|
|
// 3.0.0 - 3.0.8 |
|
|
|
|
76
|
|
|
[dropTable] |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param EntityManager $em |
82
|
|
|
* @return array |
83
|
|
|
* @throws \Doctrine\Common\Persistence\Mapping\MappingException |
84
|
|
|
*/ |
85
|
|
|
protected function getMetadata(EntityManager $em) |
86
|
|
|
{ |
87
|
|
|
$meta = array(); |
88
|
|
|
foreach ($this->entities as $entity) { |
89
|
|
|
$meta[] = $em->getMetadataFactory()->getMetadataFor($entity); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $meta; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
[createFunction] |
96
|
|
|
} |
97
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.