Skip to content

Commit

Permalink
[Task]: create-dump script improvements (#598)
Browse files Browse the repository at this point in the history
  • Loading branch information
kingjia90 authored Sep 16, 2024
1 parent 9661451 commit b0b0381
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions dump/create-dump.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,22 @@
$tableBlacklist = [
'application_logs',
'cache',
'cache_items',
'classes',
'cache_tags',
'edit_lock',
'email_log',
'http_error_log',
'locks',
'lock_keys',
'tmp_store',
'targeting_storage',
'tracking_events',
'versions',
'users'
'versions'
];

$insertIgnoreTables = [
'migration_versions'
];

// dump data
Expand All @@ -86,16 +91,43 @@
}

$tableColumns = [];
// skip columns to dump, for exmple when they are virtually generated
$skipColumns = [];
$data = $db->fetchAllAssociative('SHOW COLUMNS FROM ' . $name);

foreach ($data as $dataRow) {
if ($dataRow['Extra'] == "VIRTUAL GENERATED"){
$skipColumns[] = $dataRow['Field'];
continue;
}
$tableColumns[] = $db->quoteIdentifier($dataRow['Field']);
}

$tableData = $db->fetchAllAssociative('SELECT * FROM ' . $name);
$condition = '';
if ($name === 'users') {
$condition = ' WHERE id != 0';
}
if (
str_starts_with($name, 'translations_')
&& in_array($db->quoteIdentifier('key'), $tableColumns)
&& in_array($db->quoteIdentifier('text'), $tableColumns)
) {
$condition = ' WHERE text != ""';
}

$tableData = $db->fetchAllAssociative('SELECT * FROM ' . $name . $condition);

$insertStatement = 'INSERT INTO';
if(in_array($name, $insertIgnoreTables)) {
$insertStatement = 'INSERT IGNORE INTO';
}

foreach ($tableData as $row) {
$cells = [];
foreach ($row as $cell) {
foreach ($row as $columnKey => $cell) {
if (in_array($columnKey, $skipColumns)){
continue;
}
if (is_string($cell)) {
$cell = $db->quote($cell);
} elseif ($cell === null) {
Expand All @@ -106,7 +138,7 @@
}

$dumpData .= sprintf(
"INSERT INTO %s (%s) VALUES (%s);\n",
$insertStatement . " %s (%s) VALUES (%s);\n",
$name,
implode(',', $tableColumns),
implode(',', $cells)
Expand Down

0 comments on commit b0b0381

Please sign in to comment.