Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
MigrateDatabase
0.00% covered (danger)
0.00%
0 / 42
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 migrateDatabase
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
12
 dropAllTables
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace App\Console\Commands;
4
5use Illuminate\Console\Command;
6use Illuminate\Support\Facades\DB;
7use Illuminate\Support\Facades\Storage;
8use Illuminate\Support\Facades\Log;
9use Illuminate\Support\Facades\Artisan;
10
11class MigrateDatabase extends Command
12{
13    /**
14     * The name and signature of the console command.
15     *
16     * @var string
17     */
18    protected $signature = 'db:migrate {sourceConnection} {targetConnection}';
19    
20
21    /**
22     * The console command description.
23     *
24     * @var string
25     */
26    protected $description = 'Migrate the database from one server to another';
27
28    /**
29     * Create a new command instance.
30     *
31     * @return void
32     */
33    public function __construct()
34    {
35        parent::__construct();
36    }
37
38    /**
39     * Execute the console command.
40     *
41     * @return int
42     */
43    public function handle()
44    {
45        ini_set('memory_limit', -1);
46        set_time_limit(0);
47        
48        try {
49            // Retrieve the connection names from the command arguments
50            $sourceConnection = $this->argument('sourceConnection');
51            $targetConnection = $this->argument('targetConnection');
52    
53            // Call the method to migrate the database
54            $this->migrateDatabase($sourceConnection, $targetConnection);   //code...
55        } catch (\Exception $e) {
56            Log::channel('migration')->error($e->getMessage());
57        }
58         
59    }
60
61    private function migrateDatabase($sourceConnection, $targetConnection)
62    {
63        Log::channel('migration')->info("Get a list of tables from the source connection");
64        $tables = DB::connection($sourceConnection)->select('SHOW TABLES');
65        $databaseName = DB::connection($sourceConnection)->getDatabaseName();
66        $tables = array_map('current', $tables);
67
68        Log::channel('migration')->info("Drop all tables in the target connection");
69        $this->dropAllTables($targetConnection);
70
71        Log::channel('migration')->info("Recreate each table and insert data");
72        foreach ($tables as $table) {            
73            
74            Log::channel('migration')->info("Retrieve the create table statement from the source connection: {$table}");
75            $createTableStmt = DB::connection($sourceConnection)->selectOne("SHOW CREATE TABLE $table")->{'Create Table'};
76
77            Log::channel('migration')->info("Execute the create table statement on the target connection");
78            DB::connection($targetConnection)->statement($createTableStmt);
79
80            Log::channel('migration')->info("Fetch all data from the source table");
81            $data = DB::connection($sourceConnection)->table($table)->get();
82
83            Log::channel('migration')->info("Disable foreign key checks on the target connection");
84            DB::connection($targetConnection)->statement('SET FOREIGN_KEY_CHECKS=0;');
85
86            Log::channel('migration')->info("Insert data into the target table");
87            foreach ($data as $row) {
88                $row = (array) $row;
89                DB::connection($targetConnection)->table($table)->insert($row);
90            }
91
92            Log::channel('migration')->info("Re-enable foreign key checks on the target connection");
93            DB::connection($targetConnection)->statement('SET FOREIGN_KEY_CHECKS=1;');
94        }
95
96        Artisan::call('migrate');
97        Log::channel('migration')->info("Database migration from '$sourceConnection' to '$targetConnection' completed successfully.");
98    }
99
100    private function dropAllTables($connection)
101    {
102        Log::channel('migration')->info("Retrieve the list of all tables in the target connection");
103        $tables = DB::connection($connection)->select('SHOW TABLES');
104        $tables = array_map('current', $tables);
105
106        Log::channel('migration')->info("Disable foreign key checks");
107        DB::connection($connection)->statement('SET FOREIGN_KEY_CHECKS=0;');
108
109        Log::channel('migration')->info("Drop each table");
110        foreach ($tables as $table) {
111            DB::connection($connection)->statement("DROP TABLE IF EXISTS $table");
112        }
113
114        Log::channel('migration')->info("Re-enable foreign key checks");
115        DB::connection($connection)->statement('SET FOREIGN_KEY_CHECKS=1;');
116    }
117}