88 lines
2.4 KiB
PHP
Executable File
88 lines
2.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
class MakeMigrationCommand extends Command
|
|
{
|
|
protected static $defaultName = 'make:migration';
|
|
protected static $defaultDescription = 'Creates a new migration file';
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this
|
|
->addArgument('name', InputArgument::REQUIRED, 'The migration name (e.g. create_users_table)');
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$name = $input->getArgument('name');
|
|
|
|
// Convert name to class format (CreateUsersTable)
|
|
$className = str_replace(' ', '', ucwords(str_replace('_', ' ', $name)));
|
|
|
|
// Generate timestamp for ordering
|
|
$timestamp = date('YmdHis');
|
|
|
|
// Define file name
|
|
$fileName = "{$timestamp}_{$name}.php";
|
|
|
|
// Migrations directory path
|
|
$migrationsPath = dirname(__DIR__, 3) . '/database/migrations';
|
|
|
|
if (!is_dir($migrationsPath)) {
|
|
mkdir($migrationsPath, 0755, true);
|
|
}
|
|
|
|
$filePath = $migrationsPath . '/' . $fileName;
|
|
|
|
// Migration template
|
|
$template = <<<PHP
|
|
<?php
|
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
|
|
class {$className}
|
|
{
|
|
/**
|
|
* Run the migrations.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function up(): void
|
|
{
|
|
if (!Capsule::schema()->hasTable('table_name')) {
|
|
Capsule::schema()->create('table_name', function (Blueprint \$table) {
|
|
\$table->id();
|
|
\$table->timestamps();
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reverse the migrations.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function down(): void
|
|
{
|
|
Capsule::schema()->dropIfExists('table_name');
|
|
}
|
|
}
|
|
PHP;
|
|
|
|
if (file_put_contents($filePath, $template)) {
|
|
$output->writeln("<info>Migração criada com sucesso:</info> {$fileName}");
|
|
return Command::SUCCESS;
|
|
} else {
|
|
$output->writeln("<error>Falha ao criar o arquivo de migração.</error>");
|
|
return Command::FAILURE;
|
|
}
|
|
}
|
|
}
|