Skip to content

Commit

Permalink
fix: insert for mysql 8
Browse files Browse the repository at this point in the history
  • Loading branch information
WatheqAlshowaiter committed Aug 20, 2024
1 parent 9a93f55 commit 0c2dbea
Showing 1 changed file with 9 additions and 25 deletions.
34 changes: 9 additions & 25 deletions src/BackupTablesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,31 +118,10 @@ protected function backupTablesForSqlite($newTableName, $table)

protected function backupTablesForForMysqlAndMariaDb($newTableName, $table): array
{
// Step 1: Create the new table structure, excluding generated columns
// DB::statement(/**@lang MySQL**/ "CREATE TABLE $newTableName AS SELECT * FROM $table WHERE 1=0;");
DB::statement(/**@lang MySQL**/ "CREATE TABLE $newTableName AS SELECT * FROM $table");

DB::statement(/**@lang MySQL**/ "INSERT INTO $newTableName SELECT * FROM $table");

// Step 2: Fetch all non-generated columns
// $nonGeneratedColumns = DB::table('INFORMATION_SCHEMA.COLUMNS')
// ->select('COLUMN_NAME')
// ->where('TABLE_SCHEMA', config('database.connections.mysql.database'))
// ->where('TABLE_NAME', $table)
// ->where('EXTRA', 'NOT LIKE', '%VIRTUAL GENERATED%')
// ->pluck('COLUMN_NAME')
// ->toArray();

// Step 3: Escape reserved keywords and construct the column list
// $escapedColumns = array_map(function ($column) {
// return '`'.$column.'`'; // Escape column names with backticks
// }, $nonGeneratedColumns);

// Convert array to comma-separated string
// $columnList = implode(', ', $escapedColumns);

// Step 4: Insert data excluding generated columns
// DB::statement(/**@lang MySQL* */ "INSERT INTO $newTableName ($columnList) SELECT $columnList FROM $table");
if($this->getMysqlVersion() >= Constants::VERSION_AFTER_STORED_AS_VIRTUAL_AS_SUPPORT){
DB::statement(/**@lang MySQL**/ "CREATE TABLE $newTableName AS SELECT * FROM $table");
DB::statement(/**@lang MySQL**/ "INSERT INTO $newTableName SELECT * FROM $table");
}

$newCreatedTables[] = $newTableName;
$response[] = " Table '$table' cloned successfully.";
Expand All @@ -163,4 +142,9 @@ protected function backupTablesForForSqlServer($newTableName, $table)
{
dd('sql server');
}

private function getMysqlVersion()
{
return (float) DB::select('select version()')[0]->{'version()'};
}
}

0 comments on commit 0c2dbea

Please sign in to comment.