Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
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 / 43
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 / 8
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\Artisan;
7use Illuminate\Support\Facades\DB;
8use Illuminate\Support\Facades\Log;
9
10class MigrateDatabase extends Command
11{
12    /**
13     * The name and signature of the console command.
14     *
15     * @var string
16     */
17    protected $signature = 'db:migrate {sourceConnection} {targetConnection}';
18
19    /**
20     * The console command description.
21     *
22     * @var string
23     */
24    protected $description = 'Migrate the database from one server to another';
25
26    /**
27     * Create a new command instance.
28     *
29     * @return void
30     */
31    public function __construct()
32    {
33        parent::__construct();
34    }
35
36    /**
37     * Execute the console command.
38     *
39     * @return int
40     */
41    public function handle()
42    {
43        ini_set('memory_limit', -1);
44        set_time_limit(0);
45
46        try {
47            // Retrieve the connection names from the command arguments
48            $sourceConnection = $this->argument('sourceConnection');
49            $targetConnection = $this->argument('targetConnection');
50
51            // Call the method to migrate the database
52            $this->migrateDatabase($sourceConnection, $targetConnection);   // code...
53        } catch (\Exception $e) {
54            Log::channel('migration')->error($e->getMessage());
55        }
56
57        return 0;
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}