Skip to content

Commit f0e4359

Browse files
refactor: improve console command
1 parent 6f4be41 commit f0e4359

File tree

4 files changed

+38
-53
lines changed

4 files changed

+38
-53
lines changed

README.md

+21-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
Backup single or multiple database tables with ease.
1212

13-
> Note: if you want a full database backup with many features go for [Spatie Laravel Backup](https://github.com/spatie/laravel-backup).
13+
> [!NOTE]
14+
> If you want a full database backup with many features, go for [Spatie Laravel Backup](https://github.com/spatie/laravel-backup).
1415
1516
## Installation
1617

@@ -27,7 +28,7 @@ generate `$tableToBackup_backup_2024_08_22_17_40_01` table in the database with
2728
the datetime `2024_08_22_17_40_01` will be varied based on your datetime.
2829

2930
You can also use the `php artisan backup:tables <targets>` command to back up tables,
30-
where `<targets>` is a space-separated list of tables names or models.
31+
where `<targets>` is a space-separated list of table names or models.
3132

3233
```php
3334
use WatheqAlshowaiter\BackupTables\BackupTables; // import the facade
@@ -45,7 +46,7 @@ class ChangeSomeData
4546

4647
And More Customizations
4748

48-
- You can use an array to backup more than one table
49+
- You can use an array to back up more than one table
4950

5051
```php
5152
BackupTables::generateBackup(['users', 'posts']);
@@ -68,9 +69,10 @@ BackupTables::generateBackup([User::class, Post::class]); // users_backup_2024_0
6869
BackupTables::generateBackup('users', 'Y_d_m_H_i'); // users_backup_2024_22_08_17_40
6970
```
7071

71-
> *Note: be aware if you customize the datetime to wide datetime the package will check the backup datetime file and
72+
> [!WARNING]
73+
> Be aware if you customize the datetime to wide datetime the package will check the backup datetime file and
7274
> will be skipped
73-
> the exact same datetime, so most of the time the default will be fine
75+
> the same datetime, so most of the time the default will be fine
7476
> For example: if you use this `Y_d_m_H` you can not generate the same backup in the same hour
7577
7678
```php
@@ -81,19 +83,22 @@ BackupTables::generateBackup('users', 'Y_d_m'); // can not generate the same bac
8183
- Using the artisan command for one or more tables/models
8284
```bash
8385
php artisan backup:tables users posts # users_backup_2024_08_22_17_40_01, posts_backup_2024_08_22_17_40_01
84-
php artisan backup:tables \App\Models\User \App\Models\Post # users_backup_2024_08_22_17_40_01, posts_backup_2024_08_22_17_40_01
86+
php artisan backup:tables \\App\\Models\\User \\App\\Models\\Post # users_backup_2024_08_22_17_40_01, posts_backup_2024_08_22_17_40_01
8587
```
8688

8789
## Why?
8890

89-
Sometimes you want to backup some database tables before changing data for whatever reason, this package serves this
90-
need. I used it personally before adding foreign keys for tables that required the removal of unlinked fields for parent tables.
91-
You may find some situation where you play with table data or you're afraid of missing data so you backup these tables
91+
Sometimes you want to back up some database tables before changing data for whatever reason, this package serves this
92+
need.
93+
94+
I used it personally before adding foreign keys to tables that required removing unlinked fields from parent tables.
95+
96+
You may find some situation where you play with table data, or you're afraid of missing data, so you back up these tables
9297
beforehand.
9398

9499
## Features
95100

96-
✅ Backup tables from the code or from the console command.
101+
✅ Backup tables from the code using (Facade) or from the console command.
97102

98103
✅ Supports Laravel versions: 11, 10, 9, 8, 7, and 6.
99104

@@ -113,11 +118,15 @@ composer test
113118

114119
## Changelog
115120

116-
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
121+
Please see [CHANGELOG](CHANGELOG.md) for more information on recent changes.
117122

118123
## Contributing
119124

120-
If you have any ideas or suggestions to improve it or fix bugs, your contribution is welcome. I encourage you to look at [todos](./todos.md) which are the most important features that need to be added. If you have something different, submit an issue first to discuss or report a bug, then do a pull request.
125+
If you have any ideas or suggestions to improve it or fix bugs, your contribution is welcome.
126+
127+
I encourage you to look at [todos](./todos.md) which are the most important features that need to be added.
128+
129+
If you have something different, submit an issue first to discuss or report a bug, then do a pull request.
121130

122131
## Security Vulnerabilities
123132

src/BackupTablesService.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ protected function returnedBackupResponse($newTableName, $table): array
111111
'newCreatedTables' => "Newly created table: $newTableName",
112112
];
113113

114-
// to prevent duplicating message if you use generateBackup() twice in the same request event for different tables
114+
// to prevent a duplicating message if you use generateBackup() twice in the same request event for different tables
115115
Arr::forget($this->response, '0');
116116

117117
return $result;

src/Commands/BackupTableCommand.php

-16
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public function handle()
1616
{
1717
$tables = $this->argument('targets');
1818

19-
$tables = $this->getMultipleTables($tables);
20-
2119
try {
2220
$result = BackupTables::generateBackup($tables);
2321

@@ -32,18 +30,4 @@ public function handle()
3230
return CommandCodes::FAILURE;
3331
}
3432
}
35-
36-
/**
37-
* @param $table
38-
*
39-
* @return false|string[]
40-
*/
41-
private function getMultipleTables($table)
42-
{
43-
$tablesArr = explode(' ', $table);
44-
45-
return array_filter($tablesArr, function ($item) {
46-
return !empty($item);
47-
});
48-
}
4933
}

tests/BackupTableCommandTest.php

+16-24
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ public function it_can_backup_a_table()
2626

2727
$backupTablePattern = 'test_table_backup_'.now()->format('Y_m_d_H_i_s');
2828

29-
$this->assertTrue(
30-
Schema::hasTable($backupTablePattern),
31-
"Backup table was not created"
32-
);
29+
$this->assertTrue(Schema::hasTable($backupTablePattern));
3330
}
3431

3532
/** @test */
@@ -46,10 +43,7 @@ public function it_can_backup_a_table_by_classname()
4643

4744
$backupTablePattern = 'test_table_backup_'.now()->format('Y_m_d_H_i_s');
4845

49-
$this->assertTrue(
50-
Schema::hasTable($backupTablePattern),
51-
"Backup table was not created"
52-
);
46+
$this->assertTrue(Schema::hasTable($backupTablePattern));
5347
}
5448

5549
/** @test */
@@ -72,16 +66,13 @@ public function it_can_backup_multiple_tables()
7266
});
7367
}
7468

75-
$this->artisan('backup:tables', ['targets' => implode(' ', $tables)])
69+
$this->artisan('backup:tables', ['targets' => $tables])
7670
->assertSuccessful();
7771

7872
foreach ($tables as $table) {
7973
$backupTablePattern = $table.'_backup_'.now()->format('Y_m_d_H_i_s');
8074

81-
$this->assertTrue(
82-
Schema::hasTable($backupTablePattern),
83-
"Backup table was not created"
84-
);
75+
$this->assertTrue(Schema::hasTable($backupTablePattern));
8576
}
8677

8778
}
@@ -91,32 +82,33 @@ public function it_can_backup_multiple_models()
9182
{
9283
$models = [Father::class, Mother::class];
9384

94-
$this->artisan('backup:tables', ['targets' => implode(' ', $models)])
85+
$this->artisan('backup:tables', ['targets' => $models])
9586
->assertSuccessful();
9687

9788
$backupTablePattern1 = 'fathers_backup_'.now()->format('Y_m_d_H_i_s');
9889
$backupTablePattern2 = 'mothers_backup_'.now()->format('Y_m_d_H_i_s');
9990

100-
$this->assertTrue(
101-
Schema::hasTable($backupTablePattern1),
102-
"Backup table was not created"
103-
);
91+
$this->assertTrue(Schema::hasTable($backupTablePattern1));
10492

105-
$this->assertTrue(
106-
Schema::hasTable($backupTablePattern2),
107-
"Backup table was not created"
108-
);
93+
$this->assertTrue(Schema::hasTable($backupTablePattern2));
10994
}
11095

11196
/** @test */
112-
public function it_fails_when_any_table_does_not_exist()
97+
public function it_fails_when_any_table_does_not_exist_but_saved_corrected_tables()
11398
{
11499
Schema::create('existing_table', function ($table) {
115100
$table->id();
116101
$table->timestamps();
117102
});
118103

119-
$this->artisan('backup:tables', ['targets' => 'existing_table non_existent_table'])
104+
$this->artisan('backup:tables', ['targets' => 'existing_table', 'non_existent_table'])
120105
->assertSuccessful();
106+
107+
$backupExistingTablePattern = 'existing_table_backup_'.now()->format('Y_m_d_H_i_s');
108+
$backupNonExistingTablePattern = 'non_existent_table_backup_'.now()->format('Y_m_d_H_i_s');
109+
110+
$this->assertTrue(Schema::hasTable($backupExistingTablePattern));
111+
112+
$this->assertFalse(Schema::hasTable($backupNonExistingTablePattern));
121113
}
122114
}

0 commit comments

Comments
 (0)