Skip to content

Commit

Permalink
Merge pull request #1837 from MasterOdin/mysql_information
Browse files Browse the repository at this point in the history
Use capitalized references to INFORMATION_SCHEMA for mysql
  • Loading branch information
dereuromark committed Jul 19, 2020
2 parents afaebb7 + 30970a7 commit f3ad118
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
20 changes: 10 additions & 10 deletions src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -726,14 +726,14 @@ public function getPrimaryKey($tableName)
$options = $this->getOptions();
$rows = $this->fetchAll(sprintf(
"SELECT
k.constraint_name,
k.column_name
FROM information_schema.table_constraints t
JOIN information_schema.key_column_usage k
USING(constraint_name,table_name)
WHERE t.constraint_type='PRIMARY KEY'
AND t.table_schema='%s'
AND t.table_name='%s'",
k.CONSTRAINT_NAME,
k.COLUMN_NAME
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS t
JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
USING(CONSTRAINT_NAME,TABLE_NAME)
WHERE t.CONSTRAINT_TYPE='PRIMARY KEY'
AND t.TABLE_SCHEMA='%s'
AND t.TABLE_NAME='%s'",
$options['name'],
$tableName
));
Expand All @@ -742,8 +742,8 @@ public function getPrimaryKey($tableName)
'columns' => [],
];
foreach ($rows as $row) {
$primaryKey['constraint'] = $row['constraint_name'];
$primaryKey['columns'][] = $row['column_name'];
$primaryKey['constraint'] = $row['CONSTRAINT_NAME'];
$primaryKey['columns'][] = $row['COLUMN_NAME'];
}

return $primaryKey;
Expand Down
12 changes: 6 additions & 6 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,17 @@ public function testCreateTableWithForeignKeys()
$this->assertFalse($this->adapter->hasColumn('ntable', 'address'));

$rows = $this->adapter->fetchAll(sprintf(
"SELECT table_name, column_name, referenced_table_name, referenced_column_name
"SELECT TABLE_NAME, COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE table_schema='%s' AND REFERENCED_TABLE_NAME='ntable_tag'",
WHERE TABLE_SCHEMA='%s' AND REFERENCED_TABLE_NAME='ntable_tag'",
MYSQL_DB_CONFIG['name']
));
$foreignKey = $rows[0];

$this->assertEquals($foreignKey['table_name'], 'ntable');
$this->assertEquals($foreignKey['column_name'], 'tag_id');
$this->assertEquals($foreignKey['referenced_table_name'], 'ntable_tag');
$this->assertEquals($foreignKey['referenced_column_name'], 'id');
$this->assertEquals($foreignKey['TABLE_NAME'], 'ntable');
$this->assertEquals($foreignKey['COLUMN_NAME'], 'tag_id');
$this->assertEquals($foreignKey['REFERENCED_TABLE_NAME'], 'ntable_tag');
$this->assertEquals($foreignKey['REFERENCED_COLUMN_NAME'], 'id');
}

public function testCreateTableCustomIdColumn()
Expand Down

0 comments on commit f3ad118

Please sign in to comment.