protected function execute(InputInterface $input, OutputInterface $output) { $schema_file = Schema::getFile($input->getArgument('schema')); $format = $input->getOption('format'); if (empty($schema_file)) { throw new RuntimeException("Schema file not found."); } $config = new Config($schema_file); $db = $this->getDb($config); $config->loadSchema(); $output->write('Dumping database schema...' . PHP_EOL); $config->save($format); $output->write('<info>Database schema dumped successfully!</info>' . PHP_EOL); }
protected function execute(InputInterface $input, OutputInterface $output) { $schema_file = Schema::getFile($input->getArgument('schema')); if (empty($schema_file)) { throw new RuntimeException("Schema file not found."); } $config = new Config($schema_file); $db = $this->getDb($config); $schema = $config->getSchema(); $sqls = $schema->toSql($db->getDatabasePlatform()); if ($input->getOption('dump-sql') === true) { $output->write(implode(';' . PHP_EOL, $sqls) . ';' . PHP_EOL); } else { $output->write('ATTENTION: This operation should not be executed in a production environment.' . PHP_EOL . PHP_EOL); $output->write('Creating database schema...' . PHP_EOL); foreach ($sqls as $sql) { $db->exec($sql); } $output->write('<info>Database schema created successfully!</info>' . PHP_EOL); } }
protected function execute(InputInterface $input, OutputInterface $output) { $schema_file = Schema::getFile($input->getArgument('schema')); $dumpSql = true === $input->getOption('dump-sql'); $force = true === $input->getOption('force'); if (empty($schema_file)) { throw new RuntimeException("Schema file not found."); } $config = new Config($schema_file); $db = $this->getDb($config); $toSchema = $config->getSchema($db); $comparator = new \Doctrine\DBAL\Schema\Comparator(); $schemaDiff = $comparator->compare($db->getSchemaManager($db)->createSchema(), $toSchema); $sqls = $schemaDiff->toSql($db->getDatabasePlatform()); if (0 == count($sqls)) { $output->writeln('Nothing to update - your database is already in sync with the current entity metadata.'); return; } if ($dumpSql && $force) { throw new \InvalidArgumentException('You can pass either the --dump-sql or the --force option (but not both simultaneously).'); } if ($dumpSql) { $output->writeln(implode(';' . PHP_EOL, $sqls)); } elseif ($force) { $output->writeln('Updating database schema...'); foreach ($schemaDiff->toSql($db->getDatabasePlatform()) as $sql) { $db->exec($sql); } $output->writeln(sprintf('Database schema updated successfully! "<info>%s</info>" queries were executed', count($sqls))); } else { $output->writeln('<comment>ATTENTION</comment>: This operation should not be executed in a production environment.'); $output->writeln(' Use the incremental update to detect changes during development and use'); $output->writeln(' the SQL provided to manually update your database in production.'); $output->writeln(''); $output->writeln(sprintf('The Schema-Tool would execute <info>"%s"</info> queries to update the database.', count($sqls))); $output->writeln('Please run the operation by passing one of the following options:'); $output->writeln(sprintf(' <info>%s --force</info> to execute the command', $this->getName())); $output->writeln(sprintf(' <info>%s --dump-sql</info> to dump the SQL statements to the screen', $this->getName())); } }