Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions src/Command/DatabaseSchemaCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

declare(strict_types=1);

namespace CodeRhapsodie\DataflowBundle\Command;

use CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory;
use CodeRhapsodie\DataflowBundle\Repository\JobRepository;
use CodeRhapsodie\DataflowBundle\Repository\ScheduledDataflowRepository;
use CodeRhapsodie\DataflowBundle\SchemaProvider\DataflowSchemaProvider;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(name: 'code-rhapsodie:dataflow:database-schema', description: 'Generates schema create / update SQL queries')]
class DatabaseSchemaCommand extends Command
{
public function __construct(private ConnectionFactory $connectionFactory)
{
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setHelp('The <info>%command.name%</info> help you to generate SQL Query to create or update your database schema for this bundle')
->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Dump only the update SQL queries.')
->addOption('update', null, InputOption::VALUE_NONE, 'Dump/execute only the update SQL queries.')
->addOption('connection', null, InputOption::VALUE_REQUIRED, 'Define the DBAL connection to use');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

if (null !== $input->getOption('connection')) {
$this->connectionFactory->setConnectionName($input->getOption('connection'));
}

$connection = $this->connectionFactory->getConnection();

$schemaProvider = new DataflowSchemaProvider();
$schema = $schemaProvider->createSchema();

$sqls = $schema->toSql($connection->getDatabasePlatform());

if ($input->getOption('update')) {
$sm = $connection->createSchemaManager();

$tableArray = [JobRepository::TABLE_NAME, ScheduledDataflowRepository::TABLE_NAME];
$tables = [];
foreach ($sm->listTables() as $table) {
/** @var Table $table */
if (in_array($table->getName(), $tableArray)) {
$tables[] = $table;
}
}

$namespaces = [];

if ($connection->getDatabasePlatform()->supportsSchemas()) {
$namespaces = $sm->listSchemaNames();
}

$sequences = [];

if ($connection->getDatabasePlatform()->supportsSequences()) {
$sequences = $sm->listSequences();
}

$oldSchema = new Schema($tables, $sequences, $sm->createSchemaConfig(), $namespaces);

$sqls = $connection->getDatabasePlatform()->getAlterSchemaSQL((new Comparator($connection->getDatabasePlatform()))->compareSchemas($oldSchema, $schema));

if (empty($sqls)) {
$io->info('There is no update SQL queries.');
}
}

if ($input->getOption('dump-sql')) {
$io->text('Execute these SQL Queries on your database:');
foreach ($sqls as $sql) {
$io->text($sql . ';');
}

return Command::SUCCESS;
}

if (!$io->askQuestion(new ConfirmationQuestion('Are you sure to update database ?', true))) {
$io->text("Execution canceled.");

return Command::SUCCESS;
}

foreach ($sqls as $sql) {
$connection->executeQuery($sql);
}

$io->success(sprintf('%d queries executed.', \count($sqls)));

return parent::SUCCESS;
}
}
62 changes: 16 additions & 46 deletions src/Command/SchemaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@
use Doctrine\DBAL\Schema\Table;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

/**
* @codeCoverageIgnore
* @deprecated This command is deprecated and will be removed in 6.0, use this command "code-rhapsodie:dataflow:database-schema" instead.
*/
#[AsCommand('code-rhapsodie:dataflow:dump-schema', 'Generates schema create / update SQL queries')]
class SchemaCommand extends Command
{
public function __construct(private ConnectionFactory $connectionFactory)
{
parent::__construct();
}

/**
* {@inheritdoc}
*/
Expand All @@ -46,51 +43,24 @@ protected function configure(): void
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (null !== $input->getOption('connection')) {
$this->connectionFactory->setConnectionName($input->getOption('connection'));
}

$connection = $this->connectionFactory->getConnection();

$schemaProvider = new DataflowSchemaProvider();
$schema = $schemaProvider->createSchema();

$sqls = $schema->toSql($connection->getDatabasePlatform());

if ($input->getOption('update')) {
$sm = $connection->createSchemaManager();

$tableArray = [JobRepository::TABLE_NAME, ScheduledDataflowRepository::TABLE_NAME];
$tables = [];
foreach ($sm->listTables() as $table) {
/** @var Table $table */
if (in_array($table->getName(), $tableArray)) {
$tables[] = $table;
}
}

$namespaces = [];

if ($connection->getDatabasePlatform()->supportsSchemas()) {
$namespaces = $sm->listSchemaNames();
}
$io = new SymfonyStyle($input, $output);
$io->warning('This command is deprecated and will be removed in 6.0, use this command "code-rhapsodie:dataflow:database-schema" instead.');

$sequences = [];
$options = array_filter($input->getOptions());

if ($connection->getDatabasePlatform()->supportsSequences()) {
$sequences = $sm->listSequences();
}
//add -- before each keys
$options = array_combine(
array_map(fn($key) => '--' . $key, array_keys($options)),
array_values($options)
);

$oldSchema = new Schema($tables, $sequences, $sm->createSchemaConfig(), $namespaces);
$options['--dump-sql'] = true;

$sqls = $connection->getDatabasePlatform()->getAlterSchemaSQL((new Comparator($connection->getDatabasePlatform()))->compareSchemas($oldSchema, $schema));
}
$io = new SymfonyStyle($input, $output);
$io->text('Execute these SQL Queries on your database:');
foreach ($sqls as $sql) {
$io->text($sql.';');
}
$inputArray = new ArrayInput([
'command' => 'code-rhapsodie:dataflow:database-schema',
...$options
]);

return parent::SUCCESS;
return $this->getApplication()->doRun($inputArray, $output);
}
}
8 changes: 7 additions & 1 deletion src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ services:
tags: ['console.command']

CodeRhapsodie\DataflowBundle\Command\SchemaCommand:
deprecated:
package: 'code-rhapsodie/dataflow-bundle'
version: '5.0'
tags: ['console.command']

CodeRhapsodie\DataflowBundle\Command\DatabaseSchemaCommand:
arguments:
$connectionFactory: '@CodeRhapsodie\DataflowBundle\Factory\ConnectionFactory'
tags: ['console.command']
tags: [ 'console.command' ]

CodeRhapsodie\DataflowBundle\Repository\ScheduledDataflowRepository:
lazy: true
Expand Down