From a5e4ac6ced88804ea57a79f54b45aa6b43e97027 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Sat, 28 Feb 2026 23:09:34 +0530 Subject: [PATCH] Harden schema-sync for large databases with limits and safer preview --- app/Controllers/Setup.php | 22 ++++++++++++++++++++++ app/Views/setup/db_setup.php | 13 ++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/app/Controllers/Setup.php b/app/Controllers/Setup.php index 88f0284..0eec6e7 100644 --- a/app/Controllers/Setup.php +++ b/app/Controllers/Setup.php @@ -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, ]; } diff --git a/app/Views/setup/db_setup.php b/app/Views/setup/db_setup.php index 7158c46..9e7cda4 100644 --- a/app/Views/setup/db_setup.php +++ b/app/Views/setup/db_setup.php @@ -135,6 +135,7 @@
Master tables: , + processed tables: /, create statements: , alter statements: . @@ -143,6 +144,13 @@
+ +
+ Schema sync run was truncated to configured max tables per run. + Increase setup.sync.max_tables_per_run in .env if needed. +
+ +
Apply Errors: @@ -156,7 +164,10 @@