Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop target database schema before next run #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;

class RunCommand extends FinishCommand
Expand Down Expand Up @@ -100,6 +101,17 @@ protected function execute(InputInterface $input, OutputInterface $output)
return -1;
}

if (!$this->schemaManipulator->isTargetSchemaEmpty()) {
$io->warning('Target database schema is not empty');
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('Drop target database schema? (y/n) ', false);
if ($helper->ask($input, $output, $question)) {
$io->text('Dropping database schema...');
$this->schemaManipulator->dropTargetSchema();
$io->success('Target database schema dropped successfully!');
}
}

try {
$this->schemaManipulator->copySchemaDroppingIndexesAndForeignKeys();
$this->recipe = $this->recipeFactory
Expand Down
22 changes: 22 additions & 0 deletions src/Fogger/Schema/SchemaManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,28 @@ public function __construct(Connection $source, Connection $target)
$this->targetSchema = $target->getSchemaManager();
}

/**
* @return bool
*/
public function isTargetSchemaEmpty()
{
return empty($this->targetSchema->listTableNames());
}

public function dropTargetSchema()
{
/** @var DBAL\Table[] $tables */
$tables = $this->targetSchema->listTables();
foreach ($tables as $table) {
foreach ($table->getForeignKeys() as $fk) {
$this->targetSchema->dropForeignKey($fk->getName(), $table->getName());
}
}
foreach ($tables as $table) {
$this->targetSchema->dropTable($table);
}
}

/**
* @throws DBAL\SchemaException
*/
Expand Down