Skip to content

Commit

Permalink
Normalize column names.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed May 18, 2024
1 parent 132b06c commit 14e2631
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
15 changes: 11 additions & 4 deletions framework/db/mysql/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,26 +223,33 @@ protected function loadTableChecks($tableName)
return $checks;
}

$tableRows = $this->normalizePdoRowKeyCase($tableRows, true);

foreach ($tableRows as $tableRow) {
$sql = <<<SQL
SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE CONSTRAINT_NAME = :constraintName
SQL;

$checkRows = $this->db->createCommand($sql, [':constraintName' => $tableRow['CONSTRAINT_NAME']])->queryAll();
$checkRows = $this->db->createCommand(
$sql,
[':constraintName' => $tableRow['constraint_name']],
)->queryAll();

$checkRows = $this->normalizePdoRowKeyCase($checkRows, true);

foreach ($checkRows as $checkRow) {
$matches = [];
$columnName = null;

if (preg_match('/\(`?([a-zA-Z0-9_]+)`?\s*[><=]/', $checkRow['CHECK_CLAUSE'], $matches)) {
if (preg_match('/\(`?([a-zA-Z0-9_]+)`?\s*[><=]/', $checkRow['check_clause'], $matches)) {
$columnName = $matches[1];
}

$check = new CheckConstraint(
[
'name' => $checkRow['CONSTRAINT_NAME'],
'name' => $checkRow['constraint_name'],
'columnNames' => $columnName,
'expression' => $checkRow['CHECK_CLAUSE'],
'expression' => $checkRow['check_clause'],
]
);
$checks[] = $check;
Expand Down
4 changes: 0 additions & 4 deletions tests/framework/db/mysql/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,12 @@ public function testGetSchemaNames()
public function constraintsProvider()
{
$result = parent::constraintsProvider();
$result['1: check'][2] = false;

$result['2: primary key'][2]->name = null;
$result['2: check'][2] = false;

// Work aroung bug in MySQL 5.1 - it creates only this table in lowercase. O_o
$result['3: foreign key'][2][0]->foreignTableName = new AnyCaseValue('T_constraints_2');
$result['3: check'][2] = false;

$result['4: check'][2] = false;
return $result;
}

Expand Down

0 comments on commit 14e2631

Please sign in to comment.