Skip to content

Commit

Permalink
WIP: change code for mysql 5.7
Browse files Browse the repository at this point in the history
  • Loading branch information
WatheqAlshowaiter committed Aug 23, 2024
1 parent 4c1422c commit 612ebdf
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/BackupTablesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,35 @@ protected function backupTablesForSqlite($newTableName, $table): array

protected function backupTablesForForMysql($newTableName, $table): array
{
DB::statement(/**@lang MySQL*/ "CREATE TABLE $newTableName LIKE $table");
DB::statement(/**@lang MySQL*/"INSERT INTO $newTableName SELECT * FROM $table");

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

// for MySQL 5.7

// Step 1: Create the new table with the same structure, including generated columns
//DB::statement(/**@lang MySQL */ "CREATE TABLE $newTableName LIKE $table");

// Step 2: get the definition for generated columns `SHOW CREATE TABLE $table`
// Step 3: extract the virtual columns definition to alter them later
// Step 4: get all non-generated columns, then insert them to the new table
// Step 5: Alter new table to add the generated tables definitions.


$generatedColumns = collect(DB::select(DB::raw('SHOW COLUMNS FROM $table')))
;

// Step 2: Get the list of columns, excluding generated columns
//$columns = collect(DB::select(DB::raw("SHOW COLUMNS FROM $table")))
// ->reject(function ($column) {
// return str_contains($column->Extra, 'VIRTUAL GENERATED') || str_contains($column->Extra, 'STORED GENERATED');
// })->pluck('Field')
// ->implode(', ');

// Step 3: Insert data into the new table, excluding generated columns
//DB::statement(/**@lang MySQL */ "INSERT INTO $newTableName ($columns) SELECT $columns FROM $table");

return $this->returnedBackupResponse($newTableName, $table);
}
Expand Down

0 comments on commit 612ebdf

Please sign in to comment.