add primary key and auto-increment schema support
Configure the page translations table with an `id` primary key and auto-increment column, and detect and apply these constraints in database schema updates. Add persistence coverage for English and German homepage routes.
This commit is contained in:
@@ -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']
|
||||
],
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user