diff --git a/plugi-core/config/tables.php b/plugi-core/config/tables.php index 017fcf1..63d326f 100644 --- a/plugi-core/config/tables.php +++ b/plugi-core/config/tables.php @@ -45,6 +45,8 @@ return [ 'updated_at' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP', 'created_at' => 'timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP', ], + 'primaryKey' => 'id', + 'autoIncrement' => 'id', 'uniqueKeys' => [ 'page_id' => ['page_id','locale'] ], diff --git a/plugi-core/src/Core/DB.php b/plugi-core/src/Core/DB.php index 93b924e..a18d8b4 100644 --- a/plugi-core/src/Core/DB.php +++ b/plugi-core/src/Core/DB.php @@ -201,6 +201,35 @@ class DB } } + public function primaryKeyExists(string $table, string $column): bool + { + $tableName = $_ENV['DATABASE_PREFIX'] . phpb_remove_non_alpha_numeric($table); + $columnName = phpb_remove_non_alpha_numeric($column); + try { + return ! empty($this->select( + "SELECT 1 FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND COLUMN_NAME = ? AND CONSTRAINT_NAME = 'PRIMARY' LIMIT 1", + [$this->databaseName, $tableName, $columnName] + )); + } catch (Exception $e) { + return false; + } + } + + public function autoIncrementExists(string $table, string $column): bool + { + $tableName = $_ENV['DATABASE_PREFIX'] . phpb_remove_non_alpha_numeric($table); + $columnName = phpb_remove_non_alpha_numeric($column); + try { + $columns = $this->select( + 'SELECT EXTRA FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND COLUMN_NAME = ?', + [$this->databaseName, $tableName, $columnName] + ); + return isset($columns[0]['EXTRA']) && stripos($columns[0]['EXTRA'], 'auto_increment') !== false; + } catch (Exception $e) { + return false; + } + } + public function getDBDefinition() { $tables = include __DIR__.'/../../config/tables.php'; foreach (Extensions::getConfigs() as $extkey => $config){ @@ -233,6 +262,16 @@ class DB foreach ($tableDefinition['uniqueKeys'] as $key => $keyDefinition) { if(!$this->uniqueKeyExists($table, $key)) $diff[$table]['uniqueKeys'][$key] = '`'.implode('`,`', $keyDefinition).'`'; } + if (isset($tableDefinition['primaryKey'])) { + $diff[$table]['primaryKey'] = $this->primaryKeyExists($table, $tableDefinition['primaryKey']) + ? null + : $tableDefinition['primaryKey']; + } + if (isset($tableDefinition['autoIncrement'])) { + $diff[$table]['autoIncrement'] = $this->autoIncrementExists($table, $tableDefinition['autoIncrement']) + ? null + : $tableDefinition['autoIncrement']; + } } } @@ -264,6 +303,18 @@ class DB if (!preg_match('/^[A-Z,`\s_]+$/', $keyDefinition)) continue; $this->query("ALTER TABLE {$tableName} ADD UNIQUE KEY {$sanitizedKey} ($keyDefinition)"); } + if (! empty($tableDiff['primaryKey'])) { + $column = phpb_remove_non_alpha_numeric($tableDiff['primaryKey']); + if (! empty($column)) { + $this->query("ALTER TABLE {$tableName} ADD PRIMARY KEY (`{$column}`)"); + } + } + if (! empty($tableDiff['autoIncrement'])) { + $column = phpb_remove_non_alpha_numeric($tableDiff['autoIncrement']); + if (! empty($column)) { + $this->query("ALTER TABLE {$tableName} MODIFY `{$column}` int NOT NULL AUTO_INCREMENT"); + } + } } } $settingRepository = new SettingRepository; diff --git a/plugi-core/tests/page-creation-persistence.php b/plugi-core/tests/page-creation-persistence.php index 4b106b2..d46d160 100644 --- a/plugi-core/tests/page-creation-persistence.php +++ b/plugi-core/tests/page-creation-persistence.php @@ -430,6 +430,24 @@ $cases = [ assertPageCreation(str_contains($html, 'Each language shows its own saved values; missing translations are left blank instead of using fallback text.'), 'Locale-specific editing guidance was not shown.'); $_POST = []; }, + 'homepage routes persist for the submitted English and German values' => function (): void { + resetPageCreationDatabase(); + $payload = pageCreationPayload([ + 'name' => 'Hom', + 'layout' => 'master', + 'title' => ['en' => 'Home', 'de' => 'Start'], + 'route' => ['en' => '/', 'de' => '/de/'], + ]); + + $page = (new PageRepository())->create($payload); + + global $phpb_db; + $translations = $phpb_db->rows('page_translations'); + assertPageCreation($page === true, 'The submitted homepage payload did not create a page.'); + assertPageCreation(count($translations) === 2, 'The homepage did not persist both localized translations.'); + assertPageCreation($translations[0]['route'] === '/', 'The English homepage route was not preserved.'); + assertPageCreation($translations[1]['route'] === '/de/', 'The German homepage route was not preserved.'); + }, 'generated routes persist for every active locale' => function (): void { resetPageCreationDatabase();