Harden schema-sync for large databases with limits and safer preview
This commit is contained in:
parent
9f63cc5501
commit
a5e4ac6ced
|
|
@ -86,6 +86,11 @@ class Setup extends BaseController
|
||||||
return $deny;
|
return $deny;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ini_set('memory_limit', (string) env('setup.sync.memory_limit', '1024M'));
|
||||||
|
@ini_set('max_execution_time', '0');
|
||||||
|
@set_time_limit(0);
|
||||||
|
@ignore_user_abort(true);
|
||||||
|
|
||||||
$action = strtolower(trim((string) $this->request->getPost('sync_action')));
|
$action = strtolower(trim((string) $this->request->getPost('sync_action')));
|
||||||
if (!in_array($action, ['analyze', 'apply'], true)) {
|
if (!in_array($action, ['analyze', 'apply'], true)) {
|
||||||
$action = 'analyze';
|
$action = 'analyze';
|
||||||
|
|
@ -302,10 +307,22 @@ class Setup extends BaseController
|
||||||
$masterTables = $masterSchema['tables'] ?? [];
|
$masterTables = $masterSchema['tables'] ?? [];
|
||||||
$clientTables = array_flip($client->listTables());
|
$clientTables = array_flip($client->listTables());
|
||||||
|
|
||||||
|
$maxTables = (int) env('setup.sync.max_tables_per_run', 150);
|
||||||
|
if ($maxTables <= 0) {
|
||||||
|
$maxTables = 150;
|
||||||
|
}
|
||||||
|
$processedTables = 0;
|
||||||
|
$truncated = false;
|
||||||
|
|
||||||
$createCount = 0;
|
$createCount = 0;
|
||||||
$alterCount = 0;
|
$alterCount = 0;
|
||||||
|
|
||||||
foreach ($masterTables as $table => $masterTableDef) {
|
foreach ($masterTables as $table => $masterTableDef) {
|
||||||
|
if ($processedTables >= $maxTables) {
|
||||||
|
$truncated = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
$table = (string) $table;
|
$table = (string) $table;
|
||||||
if (!isset($clientTables[$table])) {
|
if (!isset($clientTables[$table])) {
|
||||||
$createSql = (string) ($masterTableDef['create_sql'] ?? '');
|
$createSql = (string) ($masterTableDef['create_sql'] ?? '');
|
||||||
|
|
@ -329,6 +346,8 @@ class Setup extends BaseController
|
||||||
}
|
}
|
||||||
$alterCount += count($tableAlterSql);
|
$alterCount += count($tableAlterSql);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$processedTables++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
@ -336,10 +355,13 @@ class Setup extends BaseController
|
||||||
'sql' => $sql,
|
'sql' => $sql,
|
||||||
'summary' => [
|
'summary' => [
|
||||||
'master_tables' => count((array) $masterTables),
|
'master_tables' => count((array) $masterTables),
|
||||||
|
'processed_tables' => $processedTables,
|
||||||
|
'max_tables_per_run' => $maxTables,
|
||||||
'create_tables' => $createCount,
|
'create_tables' => $createCount,
|
||||||
'alter_statements' => $alterCount,
|
'alter_statements' => $alterCount,
|
||||||
],
|
],
|
||||||
'source' => (string) ($masterSchema['source'] ?? 'master-schema-file'),
|
'source' => (string) ($masterSchema['source'] ?? 'master-schema-file'),
|
||||||
|
'truncated' => $truncated,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -135,6 +135,7 @@
|
||||||
<?php else : ?>
|
<?php else : ?>
|
||||||
<div class="alert alert-success">
|
<div class="alert alert-success">
|
||||||
Master tables: <?= esc((string) ($sync_result['summary']['master_tables'] ?? 0)) ?>,
|
Master tables: <?= esc((string) ($sync_result['summary']['master_tables'] ?? 0)) ?>,
|
||||||
|
processed tables: <?= esc((string) ($sync_result['summary']['processed_tables'] ?? 0)) ?>/<?= esc((string) ($sync_result['summary']['max_tables_per_run'] ?? 0)) ?>,
|
||||||
create statements: <?= esc((string) ($sync_result['summary']['create_tables'] ?? 0)) ?>,
|
create statements: <?= esc((string) ($sync_result['summary']['create_tables'] ?? 0)) ?>,
|
||||||
alter statements: <?= esc((string) ($sync_result['summary']['alter_statements'] ?? 0)) ?>.
|
alter statements: <?= esc((string) ($sync_result['summary']['alter_statements'] ?? 0)) ?>.
|
||||||
<?php if (isset($sync_result['applied'])) : ?>
|
<?php if (isset($sync_result['applied'])) : ?>
|
||||||
|
|
@ -143,6 +144,13 @@
|
||||||
</div>
|
</div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<?php if (!empty($sync_result['truncated'] ?? false)) : ?>
|
||||||
|
<div class="alert alert-warning">
|
||||||
|
Schema sync run was truncated to configured max tables per run.
|
||||||
|
Increase <code>setup.sync.max_tables_per_run</code> in .env if needed.
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
<?php if (!empty($sync_result['apply_errors'] ?? [])) : ?>
|
<?php if (!empty($sync_result['apply_errors'] ?? [])) : ?>
|
||||||
<div class="alert alert-danger">
|
<div class="alert alert-danger">
|
||||||
<strong>Apply Errors:</strong>
|
<strong>Apply Errors:</strong>
|
||||||
|
|
@ -156,7 +164,10 @@
|
||||||
<label>Generated SQL</label>
|
<label>Generated SQL</label>
|
||||||
<textarea class="form-control" rows="14" readonly><?php
|
<textarea class="form-control" rows="14" readonly><?php
|
||||||
$sqlLines = $sync_result['sql'] ?? [];
|
$sqlLines = $sync_result['sql'] ?? [];
|
||||||
echo esc(is_array($sqlLines) ? implode("\n", $sqlLines) : '');
|
if (is_array($sqlLines)) {
|
||||||
|
$sqlLines = array_slice($sqlLines, 0, 300);
|
||||||
|
echo esc(implode("\n", $sqlLines));
|
||||||
|
}
|
||||||
?></textarea>
|
?></textarea>
|
||||||
</div>
|
</div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user