Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
2.56% |
1 / 39 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
MessageRestoreFromCloud | |
2.56% |
1 / 39 |
|
50.00% |
1 / 2 |
39.30 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
handle | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | |
3 | namespace App\Console\Commands\Messages\Backups; |
4 | |
5 | use Google\Cloud\Storage\StorageClient; |
6 | use Illuminate\Console\Command; |
7 | use Illuminate\Support\Facades\DB; |
8 | use Illuminate\Support\Facades\Log; |
9 | use Illuminate\Support\Facades\Schema; |
10 | |
11 | class MessageRestoreFromCloud extends Command |
12 | { |
13 | /** |
14 | * The name and signature of the console command. |
15 | * |
16 | * @var string |
17 | */ |
18 | protected $signature = 'db:messages-restore-from-cloud'; |
19 | |
20 | /** |
21 | * The console command description. |
22 | * |
23 | * @var string |
24 | */ |
25 | protected $description = 'Command to get the latest backup from the bucket and restore the database'; |
26 | |
27 | /** |
28 | * Create a new command instance. |
29 | * |
30 | * @return void |
31 | */ |
32 | public function __construct() |
33 | { |
34 | parent::__construct(); |
35 | } |
36 | |
37 | /** |
38 | * Execute the console command. |
39 | * |
40 | * @return int |
41 | */ |
42 | public function handle(): int |
43 | { |
44 | // Create cloud connection: |
45 | $storage = new StorageClient([ |
46 | 'keyFilePath' => base_path() . "/gc-" . env('GC_APP_ENV') . ".json" |
47 | ]); |
48 | |
49 | // Create bucket instance |
50 | $bucket = $storage->bucket(env('APP_ENV') . "-backups-bd"); |
51 | |
52 | // Get backup from cloud |
53 | $object = $bucket->object(env('GC_CLOUD_PATH') . env('GC_CLOUD_FILE')); |
54 | |
55 | // Check if exists in the cloud |
56 | if (!$object->exists()) |
57 | { |
58 | // Log |
59 | Log::channel('messages-backups') |
60 | ->error(env('GC_CLOUD_FILE') . ' file does not exist in the cloud!'); |
61 | |
62 | // I/O |
63 | $this->error(env('GC_CLOUD_FILE') . ' file does not exist in the cloud..'); |
64 | |
65 | // Abort |
66 | return 0; |
67 | } |
68 | |
69 | $object->downloadToFile($backupFilePath = base_path() . env('GC_HOST_PATH') . "cloud-backup.json"); |
70 | |
71 | // Load the content of the SQL file |
72 | $jsonContent = file_get_contents($backupFilePath); |
73 | |
74 | // Delete tmp backup |
75 | unlink($backupFilePath); |
76 | |
77 | // Decode the JSON into an associative array |
78 | $dataJson = json_decode($jsonContent, true); |
79 | |
80 | // Check if we have data |
81 | if (empty($dataJson)) |
82 | { |
83 | Log::channel('messages-backups') |
84 | ->error('No data to rollback! About!'); |
85 | |
86 | // I/O |
87 | $this->info("No data to rollback! About.."); |
88 | return 0; |
89 | } |
90 | |
91 | // Check if the 'messages' table exists before truncating |
92 | if (!Schema::hasTable('messages')) |
93 | { |
94 | Log::channel('messages-backups') |
95 | ->error('Table messages not exist! About!'); |
96 | |
97 | // I/O |
98 | $this->info("Table messages not exist! About.."); |
99 | return 0; |
100 | } |
101 | |
102 | // Clean table |
103 | DB::table('messages')->truncate(); |
104 | |
105 | // Start transaction |
106 | DB::beginTransaction(); |
107 | |
108 | try { |
109 | |
110 | // Insert in bulk |
111 | DB::table('messages') |
112 | ->insert($dataJson); |
113 | |
114 | // Commit if everything is successful |
115 | DB::commit(); |
116 | |
117 | // Log success |
118 | Log::channel('messages-backups') |
119 | ->info('Backup restored successfully!'); |
120 | |
121 | // I/O |
122 | $this->info("Backup restored successfully.."); |
123 | |
124 | } catch (\Exception $e) { |
125 | |
126 | // Rollback only if something wrong |
127 | DB::rollback(); |
128 | |
129 | // Log |
130 | Log::channel('messages-backups') |
131 | ->error($e->getMessage()); |
132 | |
133 | // I/O |
134 | $this->error($e->getMessage()); |
135 | } |
136 | |
137 | // Exit |
138 | return 0; |
139 | } |
140 | } |