diff --git a/database/migrations/2024_07_13_090447_create_fathers_table.php b/database/migrations/2024_07_13_090447_create_fathers_table.php index 783a6fb..b8fe529 100644 --- a/database/migrations/2024_07_13_090447_create_fathers_table.php +++ b/database/migrations/2024_07_13_090447_create_fathers_table.php @@ -2,9 +2,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; -use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Schema; -use WatheqAlshowaiter\BackupTablesServiceProvider\Constants; class CreateFathersTable extends Migration { diff --git a/database/migrations/2024_07_13_093048_create_mothers_table.php b/database/migrations/2024_07_13_093048_create_mothers_table.php index 7a15c54..338505e 100644 --- a/database/migrations/2024_07_13_093048_create_mothers_table.php +++ b/database/migrations/2024_07_13_093048_create_mothers_table.php @@ -12,7 +12,6 @@ */ class CreateMothersTable extends Migration { - public function up(): void { Schema::create('mothers', function (Blueprint $table) { diff --git a/database/migrations/2024_07_13_100247_create_sons_table.php b/database/migrations/2024_07_13_100247_create_sons_table.php index 1779f47..222d4ab 100644 --- a/database/migrations/2024_07_13_100247_create_sons_table.php +++ b/database/migrations/2024_07_13_100247_create_sons_table.php @@ -2,10 +2,7 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; -use Illuminate\Support\Facades\App; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; -use WatheqAlshowaiter\BackupTablesServiceProvider\Constants; class CreateSonsTable extends Migration { diff --git a/src/BackupTables.php b/src/BackupTables.php index ad63a2e..55e9cd6 100644 --- a/src/BackupTables.php +++ b/src/BackupTables.php @@ -4,8 +4,6 @@ use Illuminate\Support\Facades\Facade; - - /** * public function backupTables($tablesToBackup = []): bool * protected function processBackup(array $tablesToBackup = []): array diff --git a/src/BackupTablesService.php b/src/BackupTablesService.php index d70f755..d67c0b8 100644 --- a/src/BackupTablesService.php +++ b/src/BackupTablesService.php @@ -6,11 +6,11 @@ use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Schema; use Symfony\Component\Console\Output\ConsoleOutput; + use function Exception; class BackupTablesService { - public array $response = []; public function backupTables($tablesToBackup) @@ -19,19 +19,19 @@ public function backupTables($tablesToBackup) if (empty($tablesToBackup)) { $this->response[] = 'No tables specified to clone.'; + return false; } - $result = $this->processBackup($tablesToBackup); - $output = new ConsoleOutput(); + $output = new ConsoleOutput; foreach ($result['response'] as $message) { $output->writeln($message); } - if (!empty($result['newCreatedTables'])) { + if (! empty($result['newCreatedTables'])) { $output->writeln('All tables cloned successfully ..'); $output->writeln('Newly created tables:'); foreach ($result['newCreatedTables'] as $tableName) { @@ -52,7 +52,7 @@ protected function processBackup(array $tablesToBackup = []) $currentDateTime = now()->format('Y_m_d_H_i_s'); foreach ($tablesToBackup as $table) { - $newTableName = $table . '_backup_' . $currentDateTime; + $newTableName = $table.'_backup_'.$currentDateTime; $newTableName = str_replace(['-', ':'], '_', $newTableName); if (Schema::hasTable($newTableName)) { @@ -61,12 +61,12 @@ protected function processBackup(array $tablesToBackup = []) continue; } - if (!Schema::hasTable($table)) { + if (! Schema::hasTable($table)) { $this->response[] = "Table `$table` is not exists. check the table name again"; + continue; } - $databaseDriver = DB::connection()->getDriverName(); switch ($databaseDriver) { @@ -98,7 +98,6 @@ function restoreTable($tableName, $backupName) // todo } - } protected function backupTablesForSqlite($newTableName, $table) @@ -110,11 +109,9 @@ protected function backupTablesForSqlite($newTableName, $table) DB::statement(/**@lang SQLite* */ "INSERT INTO $newTableName SELECT * FROM $table"); - $newCreatedTables[] = $newTableName; $response[] = " Table '$table' cloned successfully."; - return [ 'response' => $response, 'newCreatedTables' => $newCreatedTables, @@ -137,7 +134,7 @@ protected function backupTablesForForMysqlAndMariaDb($newTableName, $table): arr // Step 3: Escape reserved keywords and construct the column list $escapedColumns = array_map(function ($column) { - return '`' . $column . '`'; // Escape column names with backticks + return '`'.$column.'`'; // Escape column names with backticks }, $nonGeneratedColumns); // Convert array to comma-separated string diff --git a/src/Constants.php b/src/Constants.php index f87b824..7f4a456 100644 --- a/src/Constants.php +++ b/src/Constants.php @@ -6,7 +6,7 @@ class Constants { public const VERSION_AFTER_ULID_SUPPORT = 9.40; - public const VERSION_AFTER_FOREIGN_ID_SUPPORT = 7.0; + public const VERSION_AFTER_FOREIGN_ID_SUPPORT = 7.0; public const VERSION_AFTER_UUID_SUPPORT = 7.0; diff --git a/src/Models/Father.php b/src/Models/Father.php index d3dae5e..6fba0a1 100644 --- a/src/Models/Father.php +++ b/src/Models/Father.php @@ -3,8 +3,6 @@ namespace WatheqAlshowaiter\BackupTablesServiceProvider\Models; use Illuminate\Database\Eloquent\Model; - - class Father extends Model { protected $guarded = ['id']; diff --git a/src/Models/Mother.php b/src/Models/Mother.php index 324afaf..e4308b6 100644 --- a/src/Models/Mother.php +++ b/src/Models/Mother.php @@ -4,10 +4,7 @@ use Illuminate\Database\Eloquent\Model; - /** * @deprecated */ -class Mother extends Model -{ -} +class Mother extends Model {} diff --git a/src/Models/Son.php b/src/Models/Son.php index 151d484..7b5904b 100644 --- a/src/Models/Son.php +++ b/src/Models/Son.php @@ -7,5 +7,6 @@ class Son extends Model { protected $guarded = ['id']; + public $timestamps = false; } diff --git a/tests/BackupTablesTest.php b/tests/BackupTablesTest.php index f03b363..a189ad8 100644 --- a/tests/BackupTablesTest.php +++ b/tests/BackupTablesTest.php @@ -30,8 +30,6 @@ public function test_return_when_table_string_empty() $this->assertFalse(BackupTables::backupTables($emptyArray)); } - - public function test_generate_single_table_backup_with_proper_name() { Carbon::setTestNow(); @@ -39,7 +37,7 @@ public function test_generate_single_table_backup_with_proper_name() $tableName = 'fathers'; BackupTables::backupTables($tableName); - $newTableName = $tableName . '_backup_' . now()->format('Y_m_d_H_i_s'); + $newTableName = $tableName.'_backup_'.now()->format('Y_m_d_H_i_s'); $this->assertTrue(Schema::hasTable($newTableName)); } @@ -53,12 +51,12 @@ public function test_generate_single_table_backup_all_table_data() 'id' => 1, 'first_name' => 'Ahmed', 'last_name' => 'Saleh', - 'email' => 'father@email.com' + 'email' => 'father@email.com', ]); BackupTables::backupTables($tableName); - $newTableName = $tableName . '_backup_' . now()->format('Y_m_d_H_i_s'); + $newTableName = $tableName.'_backup_'.now()->format('Y_m_d_H_i_s'); $this->assertTrue(Schema::hasTable($newTableName)); @@ -77,17 +75,17 @@ public function test_generate_multiple_table_backup() 'id' => 1, 'first_name' => 'Ahmed', 'last_name' => 'Saleh', - 'email' => 'father@email.com' + 'email' => 'father@email.com', ]); Son::create([ 'father_id' => 1, ]); - BackupTables::backupTables([ $tableName2, $tableName]); + BackupTables::backupTables([$tableName2, $tableName]); - $newTableName = $tableName . '_backup_' . now()->format('Y_m_d_H_i_s'); - $newTableName2 = $tableName2 . '_backup_' . now()->format('Y_m_d_H_i_s'); + $newTableName = $tableName.'_backup_'.now()->format('Y_m_d_H_i_s'); + $newTableName2 = $tableName2.'_backup_'.now()->format('Y_m_d_H_i_s'); $this->assertTrue(Schema::hasTable($newTableName)); $this->assertTrue(Schema::hasTable($newTableName2));