Skip to content

Commit a1f0fab

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

1 file changed

Lines changed: 23 additions & 4 deletions

File tree

Sources/Db/APIs/MySQL.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,9 @@ public function backup_table(string $table, string $backup_table): object|bool
929929
],
930930
);
931931

932+
// Do we have any generated columns to deal with?
933+
$existing_columns = $this->list_columns($table, true);
934+
932935
// Can we do this the quick way?
933936
$result = $this->query(
934937
'CREATE TABLE {raw:backup_table} LIKE {raw:table}',
@@ -938,19 +941,28 @@ public function backup_table(string $table, string $backup_table): object|bool
938941
],
939942
);
940943

941-
// If this failed, we go old school.
942944
if ($result) {
945+
$columns = [];
946+
947+
foreach ($existing_columns as $column) {
948+
// Skip generated columns in the insert statement.
949+
if (!empty($column['generation_expression'])) {
950+
$columns[] = $column['name'];
951+
}
952+
}
953+
943954
$request = $this->query(
944955
'INSERT INTO {raw:backup_table}
945-
SELECT *
956+
({raw:columns})
957+
SELECT {raw:columns}
946958
FROM {raw:table}',
947959
[
948960
'backup_table' => $backup_table,
949961
'table' => $table,
962+
'columns' => implode(', ', $columns),
950963
],
951964
);
952965

953-
// Old school or no school?
954966
if ($request) {
955967
return $request;
956968
}
@@ -1047,6 +1059,13 @@ public function backup_table(string $table, string $backup_table): object|bool
10471059
);
10481060
}
10491061

1062+
// Restore the generation expressions on any generated columns.
1063+
foreach ($existing_columns as $column) {
1064+
if (!empty($column['generation_expression'])) {
1065+
$this->change_column($backup_table, $column['name'], $column);
1066+
}
1067+
}
1068+
10501069
return $request;
10511070
}
10521071

@@ -2136,7 +2155,7 @@ public function list_columns(string $table_name, bool $detail = false, array $pa
21362155
}
21372156

21382157
if (str_contains($row['Extra'], 'GENERATED')) {
2139-
$columns[$row['Field']]['generation_expression'] = $row['generation_expression'];
2158+
$columns[$row['Field']]['generation_expression'] = $this->unescape_string($row['generation_expression']);
21402159
$columns[$row['Field']]['stored'] = str_contains($row['Extra'], 'STORED');
21412160
}
21422161
}

0 commit comments

Comments
 (0)