Skip to content

Commit 9e67b84

Browse files
Handles generated columns correctly in Db\APIs\MySQL::backup_table()
Signed-off-by: Jon Stovell <jonstovell@gmail.com>
1 parent 1fa8051 commit 9e67b84

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

Sources/Db/APIs/MySQL.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -938,19 +938,29 @@ public function backup_table(string $table, string $backup_table): object|bool
938938
],
939939
);
940940

941-
// If this failed, we go old school.
942941
if ($result) {
942+
$columns = [];
943+
944+
// Do we have any generated columns to deal with?
945+
foreach ($this->list_columns($table, true) as $column) {
946+
// Skip generated columns in the insert statement.
947+
if (!empty($column['generation_expression'])) {
948+
$columns[] = $column['name'];
949+
}
950+
}
951+
943952
$request = $this->query(
944953
'INSERT INTO {raw:backup_table}
945-
SELECT *
954+
({raw:columns})
955+
SELECT {raw:columns}
946956
FROM {raw:table}',
947957
[
948958
'backup_table' => $backup_table,
949959
'table' => $table,
960+
'columns' => implode(', ', $columns),
950961
],
951962
);
952963

953-
// Old school or no school?
954964
if ($request) {
955965
return $request;
956966
}
@@ -1047,6 +1057,13 @@ public function backup_table(string $table, string $backup_table): object|bool
10471057
);
10481058
}
10491059

1060+
// Restore the generation expressions on any generated columns.
1061+
foreach ($this->list_columns($table, true) as $column) {
1062+
if (!empty($column['generation_expression'])) {
1063+
$this->change_column($backup_table, $column['name'], $column);
1064+
}
1065+
}
1066+
10501067
return $request;
10511068
}
10521069

@@ -2136,7 +2153,7 @@ public function list_columns(string $table_name, bool $detail = false, array $pa
21362153
}
21372154

21382155
if (str_contains($row['Extra'], 'GENERATED')) {
2139-
$columns[$row['Field']]['generation_expression'] = $row['generation_expression'];
2156+
$columns[$row['Field']]['generation_expression'] = $this->unescape_string($row['generation_expression']);
21402157
$columns[$row['Field']]['stored'] = str_contains($row['Extra'], 'STORED');
21412158
}
21422159
}

0 commit comments

Comments
 (0)