Skip to content

Commit 0c2dbea

Browse files
fix: insert for mysql 8
1 parent 9a93f55 commit 0c2dbea

File tree

1 file changed

+9
-25
lines changed

1 file changed

+9
-25
lines changed

src/BackupTablesService.php

+9-25
Original file line numberDiff line numberDiff line change
@@ -118,31 +118,10 @@ protected function backupTablesForSqlite($newTableName, $table)
118118

119119
protected function backupTablesForForMysqlAndMariaDb($newTableName, $table): array
120120
{
121-
// Step 1: Create the new table structure, excluding generated columns
122-
// DB::statement(/**@lang MySQL**/ "CREATE TABLE $newTableName AS SELECT * FROM $table WHERE 1=0;");
123-
DB::statement(/**@lang MySQL**/ "CREATE TABLE $newTableName AS SELECT * FROM $table");
124-
125-
DB::statement(/**@lang MySQL**/ "INSERT INTO $newTableName SELECT * FROM $table");
126-
127-
// Step 2: Fetch all non-generated columns
128-
// $nonGeneratedColumns = DB::table('INFORMATION_SCHEMA.COLUMNS')
129-
// ->select('COLUMN_NAME')
130-
// ->where('TABLE_SCHEMA', config('database.connections.mysql.database'))
131-
// ->where('TABLE_NAME', $table)
132-
// ->where('EXTRA', 'NOT LIKE', '%VIRTUAL GENERATED%')
133-
// ->pluck('COLUMN_NAME')
134-
// ->toArray();
135-
136-
// Step 3: Escape reserved keywords and construct the column list
137-
// $escapedColumns = array_map(function ($column) {
138-
// return '`'.$column.'`'; // Escape column names with backticks
139-
// }, $nonGeneratedColumns);
140-
141-
// Convert array to comma-separated string
142-
// $columnList = implode(', ', $escapedColumns);
143-
144-
// Step 4: Insert data excluding generated columns
145-
// DB::statement(/**@lang MySQL* */ "INSERT INTO $newTableName ($columnList) SELECT $columnList FROM $table");
121+
if($this->getMysqlVersion() >= Constants::VERSION_AFTER_STORED_AS_VIRTUAL_AS_SUPPORT){
122+
DB::statement(/**@lang MySQL**/ "CREATE TABLE $newTableName AS SELECT * FROM $table");
123+
DB::statement(/**@lang MySQL**/ "INSERT INTO $newTableName SELECT * FROM $table");
124+
}
146125

147126
$newCreatedTables[] = $newTableName;
148127
$response[] = " Table '$table' cloned successfully.";
@@ -163,4 +142,9 @@ protected function backupTablesForForSqlServer($newTableName, $table)
163142
{
164143
dd('sql server');
165144
}
145+
146+
private function getMysqlVersion()
147+
{
148+
return (float) DB::select('select version()')[0]->{'version()'};
149+
}
166150
}

0 commit comments

Comments
 (0)