-
Notifications
You must be signed in to change notification settings - Fork 1
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 #4 from WatheqAlshowaiter/feature/console-command
Feature/console command @coderabbitai
- Loading branch information
Showing
5 changed files
with
199 additions
and
19 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace WatheqAlshowaiter\BackupTables\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use WatheqAlshowaiter\BackupTables\BackupTables; | ||
|
||
class BackupTableCommand extends Command | ||
{ | ||
const SUCCESS = 0; | ||
|
||
const FAILURE = 1; | ||
|
||
protected $signature = 'backup:tables {targets* : The table names or model classes to backup (space-separated)}'; | ||
|
||
protected $description = 'Backup a specific database table/s based on provided table names or model classes'; | ||
|
||
public function handle() | ||
{ | ||
$tables = $this->argument('targets'); | ||
|
||
try { | ||
$result = BackupTables::generateBackup($tables); | ||
|
||
if (! $result) { | ||
$this->error('Failed to backup table.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
return self::SUCCESS; | ||
} catch (\Exception $e) { | ||
$this->error("Error backing up table: {$e->getMessage()}"); | ||
|
||
return self::FAILURE; | ||
} | ||
} | ||
} |
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,115 @@ | ||
<?php | ||
|
||
namespace WatheqAlshowaiter\BackupTables\Tests; | ||
|
||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Support\Facades\Schema; | ||
use WatheqAlshowaiter\BackupTables\Commands\BackupTableCommand; | ||
use WatheqAlshowaiter\BackupTables\Tests\Models\Father; | ||
use WatheqAlshowaiter\BackupTables\Tests\Models\Mother; | ||
|
||
class BackupTableCommandTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
/** @test */ | ||
public function it_can_backup_a_table() | ||
{ | ||
Schema::create('test_table', function ($table) { | ||
$table->bigIncrements('id'); | ||
$table->string('name'); | ||
$table->timestamps(); | ||
}); | ||
|
||
$this->artisan('backup:tables', ['targets' => 'test_table']) | ||
->assertExitCode(BackupTableCommand::SUCCESS); | ||
|
||
$backupTablePattern = 'test_table_backup_'.now()->format('Y_m_d_H_i_s'); | ||
|
||
$this->assertTrue(Schema::hasTable($backupTablePattern)); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_backup_a_table_by_classname() | ||
{ | ||
Schema::create('test_table', function ($table) { | ||
$table->bigIncrements('id'); | ||
$table->string('name'); | ||
$table->timestamps(); | ||
}); | ||
|
||
$this->artisan(BackupTableCommand::class, ['targets' => 'test_table']) | ||
->assertExitCode(BackupTableCommand::SUCCESS); | ||
|
||
$backupTablePattern = 'test_table_backup_'.now()->format('Y_m_d_H_i_s'); | ||
|
||
$this->assertTrue(Schema::hasTable($backupTablePattern)); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_when_table_does_not_exist() | ||
{ | ||
$this->artisan('backup:tables', ['targets' => 'non_existent_table']) | ||
->assertExitCode(BackupTableCommand::FAILURE); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_backup_multiple_tables() | ||
{ | ||
$tables = ['test_table_1', 'test_table_2']; | ||
|
||
foreach ($tables as $table) { | ||
Schema::create($table, function ($table) { | ||
$table->bigIncrements('id'); | ||
$table->string('name'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
$this->artisan('backup:tables', ['targets' => $tables]) | ||
->assertExitCode(BackupTableCommand::SUCCESS); | ||
|
||
foreach ($tables as $table) { | ||
$backupTablePattern = $table.'_backup_'.now()->format('Y_m_d_H_i_s'); | ||
|
||
$this->assertTrue(Schema::hasTable($backupTablePattern)); | ||
} | ||
|
||
} | ||
|
||
/** @test */ | ||
public function it_can_backup_multiple_models() | ||
{ | ||
$models = [Father::class, Mother::class]; | ||
$now = now(); | ||
|
||
$this->artisan('backup:tables', ['targets' => $models]) | ||
->assertExitCode(BackupTableCommand::SUCCESS); | ||
|
||
$backupTablePattern1 = 'fathers_backup_'.$now->format('Y_m_d_H_i_s'); | ||
$backupTablePattern2 = 'mothers_backup_'.$now->format('Y_m_d_H_i_s'); | ||
|
||
$this->assertTrue(Schema::hasTable($backupTablePattern1)); | ||
|
||
$this->assertTrue(Schema::hasTable($backupTablePattern2)); | ||
} | ||
|
||
/** @test */ | ||
public function it_fails_when_any_table_does_not_exist_but_saves_corrected_tables() | ||
{ | ||
Schema::create('existing_table', function ($table) { | ||
$table->bigIncrements('id'); | ||
$table->timestamps(); | ||
}); | ||
|
||
$this->artisan('backup:tables', ['targets' => 'existing_table', 'non_existent_table']) | ||
->assertExitCode(BackupTableCommand::SUCCESS); | ||
|
||
$backupExistingTablePattern = 'existing_table_backup_'.now()->format('Y_m_d_H_i_s'); | ||
$backupNonExistingTablePattern = 'non_existent_table_backup_'.now()->format('Y_m_d_H_i_s'); | ||
|
||
$this->assertTrue(Schema::hasTable($backupExistingTablePattern)); | ||
|
||
$this->assertFalse(Schema::hasTable($backupNonExistingTablePattern)); | ||
} | ||
} |