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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user