Skip to content

Commit f86ed0d

Browse files
Merge branch 'main' of github.com:WatheqAlshowaiter/backup-tables
2 parents 13d3e3d + c905860 commit f86ed0d

10 files changed

+18
-35
lines changed

database/migrations/2024_07_13_090447_create_fathers_table.php

-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
5-
use Illuminate\Support\Facades\App;
65
use Illuminate\Support\Facades\Schema;
7-
use WatheqAlshowaiter\BackupTablesServiceProvider\Constants;
86

97
class CreateFathersTable extends Migration
108
{

database/migrations/2024_07_13_093048_create_mothers_table.php

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313
class CreateMothersTable extends Migration
1414
{
15-
1615
public function up(): void
1716
{
1817
Schema::create('mothers', function (Blueprint $table) {

database/migrations/2024_07_13_100247_create_sons_table.php

-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
use Illuminate\Database\Migrations\Migration;
44
use Illuminate\Database\Schema\Blueprint;
5-
use Illuminate\Support\Facades\App;
6-
use Illuminate\Support\Facades\DB;
75
use Illuminate\Support\Facades\Schema;
8-
use WatheqAlshowaiter\BackupTablesServiceProvider\Constants;
96

107
class CreateSonsTable extends Migration
118
{

src/BackupTables.php

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use Illuminate\Support\Facades\Facade;
66

7-
8-
97
/**
108
* public function backupTables($tablesToBackup = []): bool
119
* protected function processBackup(array $tablesToBackup = []): array

src/BackupTablesService.php

+8-11
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
use Illuminate\Support\Facades\DB;
77
use Illuminate\Support\Facades\Schema;
88
use Symfony\Component\Console\Output\ConsoleOutput;
9+
910
use function Exception;
1011

1112
class BackupTablesService
1213
{
13-
1414
public array $response = [];
1515

1616
public function backupTables($tablesToBackup)
@@ -19,19 +19,19 @@ public function backupTables($tablesToBackup)
1919

2020
if (empty($tablesToBackup)) {
2121
$this->response[] = 'No tables specified to clone.';
22+
2223
return false;
2324
}
2425

25-
2626
$result = $this->processBackup($tablesToBackup);
2727

28-
$output = new ConsoleOutput();
28+
$output = new ConsoleOutput;
2929

3030
foreach ($result['response'] as $message) {
3131
$output->writeln($message);
3232
}
3333

34-
if (!empty($result['newCreatedTables'])) {
34+
if (! empty($result['newCreatedTables'])) {
3535
$output->writeln('All tables cloned successfully ..');
3636
$output->writeln('Newly created tables:');
3737
foreach ($result['newCreatedTables'] as $tableName) {
@@ -52,7 +52,7 @@ protected function processBackup(array $tablesToBackup = [])
5252
$currentDateTime = now()->format('Y_m_d_H_i_s');
5353

5454
foreach ($tablesToBackup as $table) {
55-
$newTableName = $table . '_backup_' . $currentDateTime;
55+
$newTableName = $table.'_backup_'.$currentDateTime;
5656
$newTableName = str_replace(['-', ':'], '_', $newTableName);
5757

5858
if (Schema::hasTable($newTableName)) {
@@ -61,12 +61,12 @@ protected function processBackup(array $tablesToBackup = [])
6161
continue;
6262
}
6363

64-
if (!Schema::hasTable($table)) {
64+
if (! Schema::hasTable($table)) {
6565
$this->response[] = "Table `$table` is not exists. check the table name again";
66+
6667
continue;
6768
}
6869

69-
7070
$databaseDriver = DB::connection()->getDriverName();
7171

7272
switch ($databaseDriver) {
@@ -98,7 +98,6 @@ function restoreTable($tableName, $backupName)
9898
// todo
9999
}
100100

101-
102101
}
103102

104103
protected function backupTablesForSqlite($newTableName, $table)
@@ -110,11 +109,9 @@ protected function backupTablesForSqlite($newTableName, $table)
110109

111110
DB::statement(/**@lang SQLite* */ "INSERT INTO $newTableName SELECT * FROM $table");
112111

113-
114112
$newCreatedTables[] = $newTableName;
115113
$response[] = " Table '$table' cloned successfully.";
116114

117-
118115
return [
119116
'response' => $response,
120117
'newCreatedTables' => $newCreatedTables,
@@ -137,7 +134,7 @@ protected function backupTablesForForMysqlAndMariaDb($newTableName, $table): arr
137134

138135
// Step 3: Escape reserved keywords and construct the column list
139136
$escapedColumns = array_map(function ($column) {
140-
return '`' . $column . '`'; // Escape column names with backticks
137+
return '`'.$column.'`'; // Escape column names with backticks
141138
}, $nonGeneratedColumns);
142139

143140
// Convert array to comma-separated string

src/Constants.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Constants
66
{
77
public const VERSION_AFTER_ULID_SUPPORT = 9.40;
88

9-
public const VERSION_AFTER_FOREIGN_ID_SUPPORT = 7.0;
9+
public const VERSION_AFTER_FOREIGN_ID_SUPPORT = 7.0;
1010

1111
public const VERSION_AFTER_UUID_SUPPORT = 7.0;
1212

src/Models/Father.php

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace WatheqAlshowaiter\BackupTablesServiceProvider\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6-
7-
86
class Father extends Model
97
{
108
protected $guarded = ['id'];

src/Models/Mother.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Model;
66

7-
87
/**
98
* @deprecated
109
*/
11-
class Mother extends Model
12-
{
13-
}
10+
class Mother extends Model {}

src/Models/Son.php

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
class Son extends Model
88
{
99
protected $guarded = ['id'];
10+
1011
public $timestamps = false;
1112
}

tests/BackupTablesTest.php

+7-9
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@ public function test_return_when_table_string_empty()
3030
$this->assertFalse(BackupTables::backupTables($emptyArray));
3131
}
3232

33-
34-
3533
public function test_generate_single_table_backup_with_proper_name()
3634
{
3735
Carbon::setTestNow();
3836

3937
$tableName = 'fathers';
4038
BackupTables::backupTables($tableName);
4139

42-
$newTableName = $tableName . '_backup_' . now()->format('Y_m_d_H_i_s');
40+
$newTableName = $tableName.'_backup_'.now()->format('Y_m_d_H_i_s');
4341

4442
$this->assertTrue(Schema::hasTable($newTableName));
4543
}
@@ -53,12 +51,12 @@ public function test_generate_single_table_backup_all_table_data()
5351
'id' => 1,
5452
'first_name' => 'Ahmed',
5553
'last_name' => 'Saleh',
56-
'email' => 'father@email.com'
54+
'email' => 'father@email.com',
5755
]);
5856

5957
BackupTables::backupTables($tableName);
6058

61-
$newTableName = $tableName . '_backup_' . now()->format('Y_m_d_H_i_s');
59+
$newTableName = $tableName.'_backup_'.now()->format('Y_m_d_H_i_s');
6260

6361
$this->assertTrue(Schema::hasTable($newTableName));
6462

@@ -77,17 +75,17 @@ public function test_generate_multiple_table_backup()
7775
'id' => 1,
7876
'first_name' => 'Ahmed',
7977
'last_name' => 'Saleh',
80-
'email' => 'father@email.com'
78+
'email' => 'father@email.com',
8179
]);
8280

8381
Son::create([
8482
'father_id' => 1,
8583
]);
8684

87-
BackupTables::backupTables([ $tableName2, $tableName]);
85+
BackupTables::backupTables([$tableName2, $tableName]);
8886

89-
$newTableName = $tableName . '_backup_' . now()->format('Y_m_d_H_i_s');
90-
$newTableName2 = $tableName2 . '_backup_' . now()->format('Y_m_d_H_i_s');
87+
$newTableName = $tableName.'_backup_'.now()->format('Y_m_d_H_i_s');
88+
$newTableName2 = $tableName2.'_backup_'.now()->format('Y_m_d_H_i_s');
9189

9290
$this->assertTrue(Schema::hasTable($newTableName));
9391
$this->assertTrue(Schema::hasTable($newTableName2));

0 commit comments

Comments
 (0)