Skip to content

Commit 72d56d1

Browse files
Fix styling
1 parent c852ffb commit 72d56d1

5 files changed

+41
-56
lines changed

database/migrations/2024_07_13_090447_create_fathers_table.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ public function up(): void
3030
$table->string('full_name')->storedAs("first_name || ' ' || last_name");
3131
}
3232

33-
3433
$table->timestamps();
3534
});
3635

37-
if ((float)App::version() >= Constants::VERSION_AFTER_STORED_AS_VIRTUAL_AS_SUPPORT && DB::getDriverName() == 'sqlsrv') {
36+
if ((float) App::version() >= Constants::VERSION_AFTER_STORED_AS_VIRTUAL_AS_SUPPORT && DB::getDriverName() == 'sqlsrv') {
3837
DB::statement(/**@lang TSQL */ "
3938
ALTER TABLE fathers
4039
ADD full_name AS CONCAT(first_name, ' ', last_name) PERSISTED;

database/migrations/2024_07_13_100247_create_sons_table.php

+8-10
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@
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\BackupTables\Constants;
86

97
class CreateSonsTable extends Migration
8+
{
9+
public function up(): void
1010
{
11-
public function up(): void
12-
{
13-
Schema::create('sons', function (Blueprint $table) {
14-
$table->bigIncrements('id');
11+
Schema::create('sons', function (Blueprint $table) {
12+
$table->bigIncrements('id');
1513

16-
$table->unsignedBigInteger('father_id');
17-
$table->foreign('father_id')->references('id')->on('fathers');
18-
});
19-
}
14+
$table->unsignedBigInteger('father_id');
15+
$table->foreign('father_id')->references('id')->on('fathers');
16+
});
17+
}
2018

2119
public function down(): void
2220
{

src/BackupTablesService.php

+11-18
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ class BackupTablesService
1515
/**
1616
* Generate backup for the given table or tables
1717
*
18-
* @param string|array $tablesToBackup
19-
* @param string $dataTimeText
20-
* @return bool
18+
* @param string|array $tablesToBackup
19+
*
2120
* @throws Exception
2221
*/
2322
public function generateBackup($tablesToBackup, string $dataTimeText = 'Y_m_d_H_i_s'): bool
@@ -38,7 +37,7 @@ public function generateBackup($tablesToBackup, string $dataTimeText = 'Y_m_d_H_
3837
$output->writeln($message);
3938
}
4039

41-
if(! empty(data_get($result, 'response.0.newCreatedTables'))){
40+
if (! empty(data_get($result, 'response.0.newCreatedTables'))) {
4241
return true;
4342
}
4443

@@ -60,7 +59,7 @@ protected function processBackup(array $tablesToBackup = [], $dateTimeFormat = '
6059
continue;
6160
}
6261

63-
if (!Schema::hasTable($table)) {
62+
if (! Schema::hasTable($table)) {
6463
$this->response[] = "Table `$table` is not exists. check the table name again";
6564

6665
continue;
@@ -110,6 +109,7 @@ protected function backupTablesForForMysql($newTableName, $table): array
110109

111110
if ($this->getMysqlVersion() >= Constants::VERSION_AFTER_STORED_AS_VIRTUAL_AS_SUPPORT) {
112111
DB::statement(/**@lang PostgreSQL */ "CREATE TABLE $newTableName AS SELECT * FROM $table");
112+
113113
return $this->returnedBackupResponse($newTableName, $table);
114114
}
115115

@@ -125,7 +125,6 @@ protected function backupTablesForForMysql($newTableName, $table): array
125125

126126
DB::statement(/**@lang MySQL */ "INSERT INTO $newTableName ($columns) SELECT $columns FROM $table");
127127

128-
129128
return $this->returnedBackupResponse($newTableName, $table);
130129
}
131130

@@ -145,15 +144,11 @@ protected function backupTablesForForPostgres($newTableName, $table): array
145144

146145
protected function backupTablesForForSqlServer($newTableName, $table): array
147146
{
148-
DB::statement(/**@lang TSQL*/"SELECT * INTO $newTableName FROM $table");
147+
DB::statement(/**@lang TSQL*/ "SELECT * INTO $newTableName FROM $table");
149148

150149
return $this->returnedBackupResponse($newTableName, $table);
151150
}
152151

153-
/**
154-
* @param $table
155-
* @return string
156-
*/
157152
public function convertModelToTableName($table): string
158153
{
159154
$modelParent = "Illuminate\Database\Eloquent\Model";
@@ -162,17 +157,16 @@ public function convertModelToTableName($table): string
162157
$table = (new $table)->getTable();
163158
}
164159
}
160+
165161
return $table;
166162
}
167163

168164
/**
169-
* @param $newTableName
170-
* @param $table
171165
* @return array[]
172166
*/
173167
public function returnedBackupResponse($newTableName, $table): array
174168
{
175-
$result = [
169+
$result = [
176170
'response' => "Table '$table' completed backup successfully.",
177171
'newCreatedTables' => "Newly created table: $newTableName",
178172
];
@@ -184,18 +178,17 @@ public function returnedBackupResponse($newTableName, $table): array
184178
}
185179

186180
/**
187-
* @param string $table
188-
* @param string $currentDateTime
189181
* @return array|string|string[]
190182
*/
191183
private function buildBackupFilename(string $table, string $currentDateTime)
192184
{
193-
$newTableName = $table . '_backup_' . $currentDateTime;
185+
$newTableName = $table.'_backup_'.$currentDateTime;
186+
194187
return str_replace(['-', ':'], '_', $newTableName);
195188
}
196189

197190
private function getMysqlVersion(): float
198191
{
199-
return (float)DB::select('select version()')[0]->{'version()'};
192+
return (float) DB::select('select version()')[0]->{'version()'};
200193
}
201194
}

src/Models/Mother.php

-1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ class Mother extends Model
1010

1111
public $timestamps = false;
1212
}
13-

tests/BackupTablesTest.php

+21-25
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function test_return_when_table_string_empty()
3636

3737
public function test_generate_single_table_backup()
3838
{
39-
$dateTime = Carbon::parse("2024-01-01 12:12:08");
39+
$dateTime = Carbon::parse('2024-01-01 12:12:08');
4040
Carbon::setTestNow($dateTime);
4141

4242
$tableName = 'fathers';
@@ -56,10 +56,9 @@ public function test_generate_single_table_backup()
5656
Carbon::setTestNow();
5757
}
5858

59-
6059
public function test_generate_single_table_backup_with_different_data()
6160
{
62-
$dateTime = Carbon::parse("2024-01-07 12:12:08");
61+
$dateTime = Carbon::parse('2024-01-07 12:12:08');
6362
Carbon::setTestNow($dateTime);
6463

6564
$tableName = 'mothers';
@@ -68,14 +67,13 @@ public function test_generate_single_table_backup_with_different_data()
6867
'types' => 'one',
6968
'uuid' => Str::uuid(),
7069
'ulid' => '01J5Y93TVJRVFCSRQFHHF2NRC4',
71-
'description' => "{ar: 'some description'}"
70+
'description' => "{ar: 'some description'}",
7271
]);
7372

7473
BackupTables::generateBackup($tableName);
7574

7675
$newTableName = $tableName.'_backup_'.now()->format('Y_m_d_H_i_s');
7776

78-
7977
$this->assertTrue(Schema::hasTable($newTableName));
8078

8179
$this->assertEquals(DB::table($tableName)->value('types'), DB::table($newTableName)->value('types'));
@@ -88,7 +86,7 @@ public function test_generate_single_table_backup_with_different_data()
8886

8987
public function test_generate_single_table_backup_then_another_table_backup_later()
9088
{
91-
$dateTime = Carbon::parse("2024-01-02 12:12:08");
89+
$dateTime = Carbon::parse('2024-01-02 12:12:08');
9290
Carbon::setTestNow($dateTime);
9391

9492
$fatherTable = 'fathers';
@@ -107,8 +105,8 @@ public function test_generate_single_table_backup_then_another_table_backup_late
107105
BackupTables::generateBackup($fatherTable);
108106

109107
$currentDateTime = now()->format('Y_m_d_H_i_s');
110-
$newFatherTable = $fatherTable . '_backup_' . $currentDateTime;
111-
$newSonTable = $sonTable . '_backup_' . $currentDateTime;
108+
$newFatherTable = $fatherTable.'_backup_'.$currentDateTime;
109+
$newSonTable = $sonTable.'_backup_'.$currentDateTime;
112110

113111
$this->assertTrue(Schema::hasTable($newFatherTable));
114112

@@ -124,7 +122,7 @@ public function test_generate_single_table_backup_then_another_table_backup_late
124122

125123
public function test_generate_multiple_table_backup()
126124
{
127-
$dateTime = Carbon::parse("2024-01-03 12:12:08");
125+
$dateTime = Carbon::parse('2024-01-03 12:12:08');
128126
Carbon::setTestNow($dateTime);
129127

130128
$tableName = 'fathers';
@@ -138,7 +136,7 @@ public function test_generate_multiple_table_backup()
138136
]);
139137

140138
Son::create([
141-
'father_id' => Father::value('id')
139+
'father_id' => Father::value('id'),
142140
]);
143141

144142
BackupTables::generateBackup([$tableName, $tableName2]);
@@ -163,7 +161,7 @@ public function test_generate_multiple_table_backup()
163161

164162
public function test_generate_single_table_backup_with_with_custom_format()
165163
{
166-
$dateTime = Carbon::parse("2024-01-01 12:12:08");
164+
$dateTime = Carbon::parse('2024-01-01 12:12:08');
167165
Carbon::setTestNow($dateTime);
168166

169167
$tableName = 'fathers';
@@ -179,7 +177,7 @@ public function test_generate_single_table_backup_with_with_custom_format()
179177

180178
public function test_generate_multiple_models_backup()
181179
{
182-
$dateTime = Carbon::parse("2024-01-04 12:12:08");
180+
$dateTime = Carbon::parse('2024-01-04 12:12:08');
183181
Carbon::setTestNow($dateTime);
184182
$tableName = Father::class;
185183
$tableName2 = Son::class;
@@ -192,24 +190,24 @@ public function test_generate_multiple_models_backup()
192190
]);
193191

194192
Son::create([
195-
'father_id' => Father::value('id')
193+
'father_id' => Father::value('id'),
196194
]);
197195

198196
BackupTables::generateBackup([$tableName, $tableName2]);
199197

200198
$tableName = BackupTables::convertModelToTableName($tableName);
201199
$tableName2 = BackupTables::convertModelToTableName($tableName2);
202200

203-
$newTableName = $tableName . '_backup_' . now()->format('Y_m_d_H_i_s');
204-
$newTableName2 = $tableName2 . '_backup_' . now()->format('Y_m_d_H_i_s');
201+
$newTableName = $tableName.'_backup_'.now()->format('Y_m_d_H_i_s');
202+
$newTableName2 = $tableName2.'_backup_'.now()->format('Y_m_d_H_i_s');
205203

206204
$this->assertTrue(Schema::hasTable($newTableName));
207205
$this->assertTrue(Schema::hasTable($newTableName2));
208206

209207
$this->assertEquals(DB::table($tableName)->value('first_name'), DB::table($newTableName)->value('first_name'));
210208
$this->assertEquals(DB::table($tableName)->value('email'), DB::table($newTableName)->value('email'));
211209

212-
if (DB::getDriverName() == 'mysql' || DB::getDriverName() == 'mariadb' || (float)App::version() >= Constants::VERSION_AFTER_STORED_AS_VIRTUAL_AS_SUPPORT) {
210+
if (DB::getDriverName() == 'mysql' || DB::getDriverName() == 'mariadb' || (float) App::version() >= Constants::VERSION_AFTER_STORED_AS_VIRTUAL_AS_SUPPORT) {
213211
$this->assertEquals(DB::table($tableName)->value('full_name'), DB::table($newTableName)->value('full_name')); // StoredAs/VirtualAs column
214212
}
215213

@@ -218,14 +216,14 @@ public function test_generate_multiple_models_backup()
218216

219217
public function test_skip_duplicated_backups()
220218
{
221-
$dateTime = Carbon::parse("2024-01-05 12:12:08");
219+
$dateTime = Carbon::parse('2024-01-05 12:12:08');
222220
Carbon::setTestNow($dateTime);
223221

224222
$tableName = 'fathers';
225223
BackupTables::generateBackup($tableName);
226224
BackupTables::generateBackup($tableName); // another backup up will be skipped
227225

228-
$newTableName = $tableName . '_backup_' . now()->format('Y_m_d_H_i_s');
226+
$newTableName = $tableName.'_backup_'.now()->format('Y_m_d_H_i_s');
229227

230228
$this->assertTrue(Schema::hasTable($newTableName));
231229

@@ -236,16 +234,16 @@ public function test_skip_duplicated_backups()
236234
switch ($databaseDriver) {
237235
case 'mysql':
238236
case 'mariadb':
239-
$result = DB::select(/**@lang MySQL*/"
237+
$result = DB::select(/**@lang MySQL*/ '
240238
SELECT COUNT(*) as count
241239
FROM information_schema.tables
242240
WHERE table_schema = DATABASE()
243-
AND table_name LIKE ?", [$pattern]);
241+
AND table_name LIKE ?', [$pattern]);
244242
$count = $result[0]->count;
245243
break;
246244

247245
case 'pgsql':
248-
$result = DB::select(/**@lang PostgreSQL*/"
246+
$result = DB::select(/**@lang PostgreSQL*/ "
249247
SELECT COUNT(*) as count
250248
FROM information_schema.tables
251249
WHERE table_schema = 'public'
@@ -261,17 +259,15 @@ public function test_skip_duplicated_backups()
261259
break;
262260

263261
case 'sqlsrv':
264-
$result = DB::select(/**@lang TSQL*/"
262+
$result = DB::select(/**@lang TSQL*/ '
265263
SELECT COUNT(*) as count
266264
FROM sys.tables
267-
WHERE name LIKE ?", [$pattern]);
265+
WHERE name LIKE ?', [$pattern]);
268266
$count = $result[0]->count;
269267
break;
270268
}
271269
$this->assertEquals(1, $count);
272270

273-
274271
Carbon::setTestNow();
275272
}
276-
277273
}

0 commit comments

Comments
 (0)