|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RDV\Bundle\MigrationBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Connection; |
|
6
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
8
|
|
|
|
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
13
|
|
|
|
|
14
|
|
|
class DumpMigrationsCommand extends ContainerAwareCommand |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $allowedTables = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $extendedFieldOptions = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $namespace; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $className; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $version; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* {@inheritdoc} |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function configure() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->setName('rdv:migration:dump') |
|
47
|
|
|
->addOption('plain-sql', null, InputOption::VALUE_NONE, 'Out schema as plain sql queries') |
|
48
|
|
|
->addOption( |
|
49
|
|
|
'bundle', |
|
50
|
|
|
null, |
|
51
|
|
|
InputOption::VALUE_OPTIONAL, |
|
52
|
|
|
'Bundle name for which migration wll be generated' |
|
53
|
|
|
) |
|
54
|
|
|
->addOption( |
|
55
|
|
|
'migration-version', |
|
56
|
|
|
null, |
|
57
|
|
|
InputOption::VALUE_OPTIONAL, |
|
58
|
|
|
'Migration version', |
|
59
|
|
|
'v1_0' |
|
60
|
|
|
) |
|
61
|
|
|
->setDescription('Dump existing database structure.'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->version = $input->getOption('migration-version'); |
|
70
|
|
|
$this->initializeBundleRestrictions($input->getOption('bundle')); |
|
71
|
|
|
$this->initializeMetadataInformation(); |
|
72
|
|
|
$doctrine = $this->getContainer()->get('doctrine'); |
|
73
|
|
|
/** @var Schema $schema */ |
|
74
|
|
|
$schema = $doctrine->getConnection()->getSchemaManager()->createSchema(); |
|
75
|
|
|
|
|
76
|
|
|
if ($input->getOption('plain-sql')) { |
|
77
|
|
|
/** @var Connection $connection */ |
|
78
|
|
|
$connection = $this->getContainer()->get('doctrine')->getConnection(); |
|
79
|
|
|
$sqls = $schema->toSql($connection->getDatabasePlatform()); |
|
80
|
|
|
foreach ($sqls as $sql) { |
|
81
|
|
|
$output->writeln($sql . ';'); |
|
82
|
|
|
} |
|
83
|
|
|
} else { |
|
84
|
|
|
$this->dumpPhpSchema($schema, $output); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $bundle |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function initializeBundleRestrictions($bundle) |
|
92
|
|
|
{ |
|
93
|
|
|
if ($bundle) { |
|
94
|
|
|
$bundles = $this->getContainer()->getParameter('kernel.bundles'); |
|
95
|
|
|
if (!array_key_exists($bundle, $bundles)) { |
|
96
|
|
|
throw new \InvalidArgumentException( |
|
97
|
|
|
sprintf('Bundle "%s" is not a known bundle', $bundle) |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
$this->namespace = str_replace($bundle, 'Entity', $bundles[$bundle]); |
|
101
|
|
|
$this->className = $bundle . 'Installer'; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Process metadata information. |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function initializeMetadataInformation() |
|
109
|
|
|
{ |
|
110
|
|
|
$doctrine = $this->getContainer()->get('doctrine'); |
|
111
|
|
|
/** @var ClassMetadata[] $allMetadata */ |
|
112
|
|
|
$allMetadata = $doctrine->getManager()->getMetadataFactory()->getAllMetadata(); |
|
113
|
|
|
array_walk( |
|
114
|
|
|
$allMetadata, |
|
115
|
|
|
function (ClassMetadata $entityMetadata) { |
|
116
|
|
|
if ($this->namespace) { |
|
117
|
|
|
if ($entityMetadata->namespace == $this->namespace) { |
|
118
|
|
|
$this->allowedTables[$entityMetadata->getTableName()] = true; |
|
119
|
|
|
foreach ($entityMetadata->getAssociationMappings() as $associationMappingInfo) { |
|
120
|
|
|
if (!empty($associationMappingInfo['joinTable'])) { |
|
121
|
|
|
$joinTableName = $associationMappingInfo['joinTable']['name']; |
|
122
|
|
|
$this->allowedTables[$joinTableName] = true; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param Schema $schema |
|
133
|
|
|
* @param OutputInterface $output |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function dumpPhpSchema(Schema $schema, OutputInterface $output) |
|
136
|
|
|
{ |
|
137
|
|
|
$visitor = $this->getContainer()->get('rdv_migration.tools.schema_dumper'); |
|
138
|
|
|
$schema->visit($visitor); |
|
139
|
|
|
|
|
140
|
|
|
$output->writeln( |
|
141
|
|
|
$visitor->dump( |
|
142
|
|
|
$this->allowedTables, |
|
143
|
|
|
$this->namespace, |
|
144
|
|
|
$this->className, |
|
145
|
|
|
$this->version, |
|
146
|
|
|
$this->extendedFieldOptions |
|
147
|
|
|
) |
|
148
|
|
|
); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|