6
6
use Illuminate \Support \Facades \DB ;
7
7
use Illuminate \Support \Facades \Schema ;
8
8
use Symfony \Component \Console \Output \ConsoleOutput ;
9
+
9
10
use function Exception ;
10
11
11
12
class BackupTablesService
12
13
{
13
-
14
14
public array $ response = [];
15
15
16
16
public function backupTables ($ tablesToBackup )
@@ -19,19 +19,19 @@ public function backupTables($tablesToBackup)
19
19
20
20
if (empty ($ tablesToBackup )) {
21
21
$ this ->response [] = 'No tables specified to clone. ' ;
22
+
22
23
return false ;
23
24
}
24
25
25
-
26
26
$ result = $ this ->processBackup ($ tablesToBackup );
27
27
28
- $ output = new ConsoleOutput () ;
28
+ $ output = new ConsoleOutput ;
29
29
30
30
foreach ($ result ['response ' ] as $ message ) {
31
31
$ output ->writeln ($ message );
32
32
}
33
33
34
- if (!empty ($ result ['newCreatedTables ' ])) {
34
+ if (! empty ($ result ['newCreatedTables ' ])) {
35
35
$ output ->writeln ('All tables cloned successfully .. ' );
36
36
$ output ->writeln ('Newly created tables: ' );
37
37
foreach ($ result ['newCreatedTables ' ] as $ tableName ) {
@@ -52,7 +52,7 @@ protected function processBackup(array $tablesToBackup = [])
52
52
$ currentDateTime = now ()->format ('Y_m_d_H_i_s ' );
53
53
54
54
foreach ($ tablesToBackup as $ table ) {
55
- $ newTableName = $ table . '_backup_ ' . $ currentDateTime ;
55
+ $ newTableName = $ table. '_backup_ ' . $ currentDateTime ;
56
56
$ newTableName = str_replace (['- ' , ': ' ], '_ ' , $ newTableName );
57
57
58
58
if (Schema::hasTable ($ newTableName )) {
@@ -61,12 +61,12 @@ protected function processBackup(array $tablesToBackup = [])
61
61
continue ;
62
62
}
63
63
64
- if (!Schema::hasTable ($ table )) {
64
+ if (! Schema::hasTable ($ table )) {
65
65
$ this ->response [] = "Table ` $ table` is not exists. check the table name again " ;
66
+
66
67
continue ;
67
68
}
68
69
69
-
70
70
$ databaseDriver = DB ::connection ()->getDriverName ();
71
71
72
72
switch ($ databaseDriver ) {
@@ -98,7 +98,6 @@ function restoreTable($tableName, $backupName)
98
98
// todo
99
99
}
100
100
101
-
102
101
}
103
102
104
103
protected function backupTablesForSqlite ($ newTableName , $ table )
@@ -110,11 +109,9 @@ protected function backupTablesForSqlite($newTableName, $table)
110
109
111
110
DB ::statement (/**@lang SQLite* */ "INSERT INTO $ newTableName SELECT * FROM $ table " );
112
111
113
-
114
112
$ newCreatedTables [] = $ newTableName ;
115
113
$ response [] = " Table ' $ table' cloned successfully. " ;
116
114
117
-
118
115
return [
119
116
'response ' => $ response ,
120
117
'newCreatedTables ' => $ newCreatedTables ,
@@ -137,7 +134,7 @@ protected function backupTablesForForMysqlAndMariaDb($newTableName, $table): arr
137
134
138
135
// Step 3: Escape reserved keywords and construct the column list
139
136
$ escapedColumns = array_map (function ($ column ) {
140
- return '` ' . $ column . '` ' ; // Escape column names with backticks
137
+ return '` ' . $ column. '` ' ; // Escape column names with backticks
141
138
}, $ nonGeneratedColumns );
142
139
143
140
// Convert array to comma-separated string
0 commit comments