-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from lion-packages/new
EmptyMigrationsCommand added
- Loading branch information
Showing
10 changed files
with
219 additions
and
56 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/LionBundle/Commands/Lion/Migrations/EmptyMigrationsCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Lion\Bundle\Commands\Lion\Migrations; | ||
|
||
use Lion\Command\Command; | ||
use Lion\Database\Drivers\MySQL as DB; | ||
use Lion\Database\Drivers\Schema\MySQL as Schema; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class EmptyMigrationsCommand extends Command | ||
{ | ||
protected function configure(): void | ||
{ | ||
$this | ||
->setName('migrate:empty') | ||
->setDescription('Empties all tables built with the migrations'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$connections = (object) Schema::getConnections(); | ||
|
||
foreach ($connections->connections as $connectionName => $connection) { | ||
$tables = DB::connection($connectionName)->show()->tables()->getAll(); | ||
|
||
if (!is_array($tables) && isSuccess($tables)) { | ||
$output->writeln($this->warningOutput("\t>> MIGRATION: no tables available")); | ||
|
||
continue; | ||
} | ||
|
||
if (!is_array($tables) && isError($tables)) { | ||
$output->writeln($this->errorOutput("\t>> MIGRATION: {$tables->message}")); | ||
|
||
continue; | ||
} | ||
|
||
foreach ($tables as $table) { | ||
$response = Schema::connection($connectionName) | ||
->truncateTable($table->{"Tables_in_{$connection['dbname']}"}) | ||
->execute(); | ||
|
||
$output->writeln( | ||
$this->warningOutput( | ||
"\t>> MIGRATION: {$connection['dbname']}." . $table->{"Tables_in_{$connection['dbname']}"} | ||
) | ||
); | ||
|
||
if (isError($response)) { | ||
$output->writeln($this->errorOutput("\t>> MIGRATION: {$response->message}")); | ||
} else { | ||
$output->writeln($this->successOutput("\t>> MIGRATION: {$response->message}")); | ||
} | ||
} | ||
} | ||
|
||
$output->writeln($this->infoOutput("\n\t>> All tables have been truncated")); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
Oops, something went wrong.