Skip to content

Commit

Permalink
Merge pull request #32 from lion-packages/new
Browse files Browse the repository at this point in the history
EmptyMigrationsCommand added
  • Loading branch information
Sleon4 authored Mar 2, 2024
2 parents 7451874 + 22f1fa2 commit dcb197b
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 56 deletions.
77 changes: 39 additions & 38 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 49 additions & 9 deletions src/LionBundle/Commands/Lion/DB/DBSeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,17 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
if (isError($this->store->exist('./database/Seed/'))) {
$output->writeln($this->errorOutput("\t>> SEED: there are no defined seeds"));

return Command::FAILURE;
}

$end = (int) $input->getOption('run');

/** @var array<SeedInterface> $files */
$files = [];

foreach ($this->container->getFiles($this->container->normalizePath('./database/Seed/')) as $seed) {
if (isSuccess($this->store->validate([$seed], ['php']))) {
$class = $this->container->getNamespace(
Expand All @@ -60,22 +69,53 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/** @var SeedInterface $seedInterface */
$seedInterface = new $class();

for ($i = 0; $i < $end; $i++) {
$response = $seedInterface->run();
$files[] = $seedInterface;
}
}

foreach ($this->orderList($files) as $seedInterface) {
$output->writeln($this->warningOutput("\t>> SEED: " . $seedInterface::class));

$output->writeln($this->warningOutput("\t>> SEED: " . $seedInterface::class));
for ($i = 0; $i < $end; $i++) {
$response = $seedInterface->run();

if (isError($response)) {
$output->writeln($this->errorOutput("\t>> SEED: {$response->message}"));
} else {
$output->writeln($this->successOutput("\t>> SEED: {$response->message}"));
}
if (isError($response)) {
$output->writeln($this->errorOutput("\t>> SEED: {$response->message}"));
} else {
$output->writeln($this->successOutput("\t>> SEED: {$response->message}"));
}
}
}

$output->writeln($this->purpleOutput("\n\t>> SEED: seeds executed"));
$output->writeln($this->infoOutput("\n\t>> SEED: seeds executed"));

return Command::SUCCESS;
}

/**
* Sorts the list of elements by the value defined in the INDEX constant
*
* @param array $files [Class List]
*
* @return array<SeedInterface>
*/
private function orderList(array $files): array
{
uasort($files, function($classA, $classB) {
$namespaceA = $classA::class;
$namespaceB = $classB::class;

if (!defined($namespaceA . "::INDEX")) {
return -1;
}

if (!defined($namespaceB . "::INDEX")) {
return -1;
}

return $classA::INDEX <=> $classB::INDEX;
});

return $files;
}
}
2 changes: 1 addition & 1 deletion src/LionBundle/Commands/Lion/DB/ShowDatabasesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
->setHeaders(['DATABASE CONNECTION', 'DATABASE HOST', 'DATABASE PORT', 'DATABASE NAME', 'DATABASE USER'])
->setFooterTitle(
$size > 1
? "<info> Showing [" . $size . "] connections </info>"
? "<info> Showing [{$size}] connections </info>"
: ($size === 1
? '<info> showing a single connection </info>'
: '<info> No connections available </info>'
Expand Down
64 changes: 64 additions & 0 deletions src/LionBundle/Commands/Lion/Migrations/EmptyMigrationsCommand.php
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;
}
}
Loading

0 comments on commit dcb197b

Please sign in to comment.