|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace LaravelDoctrine\Migrations\Console; |
| 4 | + |
| 5 | +use Doctrine\Common\Persistence\ManagerRegistry; |
| 6 | +use Doctrine\DBAL\Migrations\Provider\OrmSchemaProvider; |
| 7 | +use Doctrine\ORM\EntityManagerInterface; |
| 8 | +use Illuminate\Console\Command; |
| 9 | +use LaravelDoctrine\Migrations\Configuration\ConfigurationProvider; |
| 10 | +use LaravelDoctrine\Migrations\Output\MigrationFileGenerator; |
| 11 | +use LaravelDoctrine\Migrations\Output\SqlBuilder; |
| 12 | + |
| 13 | +class DiffCommand extends Command |
| 14 | +{ |
| 15 | + /** |
| 16 | + * The name and signature of the console command. |
| 17 | + * @var string |
| 18 | + */ |
| 19 | + protected $signature = 'doctrine:migrations:diff |
| 20 | + {--connection= : For a specific connection } |
| 21 | + {--filter-expression= : Tables which are filtered by Regular Expression.}'; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var string |
| 25 | + */ |
| 26 | + protected $description = 'Generate a migration by comparing your current database to your mapping information.'; |
| 27 | + |
| 28 | + /** |
| 29 | + * Execute the console command. |
| 30 | + * |
| 31 | + * @param ConfigurationProvider $provider |
| 32 | + * @param ManagerRegistry $registry |
| 33 | + * @param SqlBuilder $builder |
| 34 | + * @param MigrationFileGenerator $generator |
| 35 | + */ |
| 36 | + public function fire( |
| 37 | + ConfigurationProvider $provider, |
| 38 | + ManagerRegistry $registry, |
| 39 | + SqlBuilder $builder, |
| 40 | + MigrationFileGenerator $generator |
| 41 | + ) { |
| 42 | + $configuration = $provider->getForConnection($this->option('connection')); |
| 43 | + $em = $registry->getManager($this->option('connection')); |
| 44 | + $connection = $configuration->getConnection(); |
| 45 | + |
| 46 | + // Overrule the filter |
| 47 | + if ($filterExpr = $this->option('filter-expression')) { |
| 48 | + $connection->getConfiguration()->setFilterSchemaAssetsExpression($filterExpr); |
| 49 | + } |
| 50 | + |
| 51 | + $fromSchema = $connection->getSchemaManager()->createSchema(); |
| 52 | + $toSchema = $this->getSchemaProvider($em)->createSchema(); |
| 53 | + |
| 54 | + // Drop tables which don't suffice to the filter regex |
| 55 | + if ($filterExpr = $connection->getConfiguration()->getFilterSchemaAssetsExpression()) { |
| 56 | + foreach ($toSchema->getTables() as $table) { |
| 57 | + $tableName = $table->getName(); |
| 58 | + if (!preg_match($filterExpr, $this->resolveTableName($tableName))) { |
| 59 | + $toSchema->dropTable($tableName); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + $up = $builder->up($configuration, $fromSchema, $toSchema); |
| 65 | + $down = $builder->down($configuration, $fromSchema, $toSchema); |
| 66 | + |
| 67 | + if (!$up && !$down) { |
| 68 | + return $this->error('No changes detected in your mapping information.'); |
| 69 | + } |
| 70 | + |
| 71 | + $path = $generator->generate( |
| 72 | + $configuration, |
| 73 | + false, |
| 74 | + false, |
| 75 | + $up, |
| 76 | + $down |
| 77 | + ); |
| 78 | + |
| 79 | + $this->line(sprintf('Generated new migration class to "<info>%s</info>" from schema differences.', $path)); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param EntityManagerInterface $em |
| 84 | + * |
| 85 | + * @return OrmSchemaProvider |
| 86 | + */ |
| 87 | + protected function getSchemaProvider(EntityManagerInterface $em) |
| 88 | + { |
| 89 | + return new OrmSchemaProvider($em); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Resolve a table name from its fully qualified name. The `$name` argument |
| 94 | + * comes from Doctrine\DBAL\Schema\Table#getName which can sometimes return |
| 95 | + * a namespaced name with the form `{namespace}.{tableName}`. This extracts |
| 96 | + * the table name from that. |
| 97 | + * |
| 98 | + * @param string $name |
| 99 | + * |
| 100 | + * @return string |
| 101 | + */ |
| 102 | + protected function resolveTableName($name) |
| 103 | + { |
| 104 | + $pos = strpos($name, '.'); |
| 105 | + |
| 106 | + return false === $pos ? $name : substr($name, $pos + 1); |
| 107 | + } |
| 108 | +} |
0 commit comments