Harden schema-sync for large databases with limits and safer preview

This commit is contained in:
DevSoftTech Server 2026-02-28 23:09:34 +05:30
parent 9f63cc5501
commit a5e4ac6ced
2 changed files with 34 additions and 1 deletions

View File

@ -86,6 +86,11 @@ class Setup extends BaseController
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')));
if (!in_array($action, ['analyze', 'apply'], true)) {
$action = 'analyze';
@ -302,10 +307,22 @@ class Setup extends BaseController
$masterTables = $masterSchema['tables'] ?? [];
$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;
$alterCount = 0;
foreach ($masterTables as $table => $masterTableDef) {
if ($processedTables >= $maxTables) {
$truncated = true;
break;
}
$table = (string) $table;
if (!isset($clientTables[$table])) {
$createSql = (string) ($masterTableDef['create_sql'] ?? '');
@ -329,6 +346,8 @@ class Setup extends BaseController
}
$alterCount += count($tableAlterSql);
}
$processedTables++;
}
return [
@ -336,10 +355,13 @@ class Setup extends BaseController
'sql' => $sql,
'summary' => [
'master_tables' => count((array) $masterTables),
'processed_tables' => $processedTables,
'max_tables_per_run' => $maxTables,
'create_tables' => $createCount,
'alter_statements' => $alterCount,
],
'source' => (string) ($masterSchema['source'] ?? 'master-schema-file'),
'truncated' => $truncated,
];
}

View File

@ -135,6 +135,7 @@
<?php else : ?>
<div class="alert alert-success">
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)) ?>,
alter statements: <?= esc((string) ($sync_result['summary']['alter_statements'] ?? 0)) ?>.
<?php if (isset($sync_result['applied'])) : ?>
@ -143,6 +144,13 @@
</div>
<?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'] ?? [])) : ?>
<div class="alert alert-danger">
<strong>Apply Errors:</strong>
@ -156,7 +164,10 @@
<label>Generated SQL</label>
<textarea class="form-control" rows="14" readonly><?php
$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>
</div>
<?php endif; ?>