Commit 164c70ee by Carsten Brandt

Add support for composite FK to cubrid

parent 8abeed03
......@@ -148,12 +148,16 @@ class Schema extends \yii\db\Schema
$foreignKeys = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $table->name);
foreach($foreignKeys as $key) {
$table->foreignKeys[] = array(
$key['PKTABLE_NAME'],
$key['FKCOLUMN_NAME'] => $key['PKCOLUMN_NAME']
// TODO support composite foreign keys
);
if (isset($table->foreignKeys[$key['FK_NAME']])) {
$table->foreignKeys[$key['FK_NAME']][$key['FKCOLUMN_NAME']] = $key['PKCOLUMN_NAME'];
} else {
$table->foreignKeys[$key['FK_NAME']] = array(
$key['PKTABLE_NAME'],
$key['FKCOLUMN_NAME'] => $key['PKCOLUMN_NAME']
);
}
}
$table->foreignKeys = array_values($table->foreignKeys);
return $table;
} else {
......
......@@ -3,6 +3,7 @@
* The database setup in config.php is required to perform then relevant tests:
*/
DROP TABLE IF EXISTS tbl_composite_fk;
DROP TABLE IF EXISTS tbl_order_item;
DROP TABLE IF EXISTS tbl_item;
DROP TABLE IF EXISTS tbl_order;
......@@ -76,6 +77,14 @@ CREATE TABLE `tbl_type` (
`bool_col2` smallint DEFAULT 1
);
CREATE TABLE `tbl_composite_fk` (
`id` int(11) NOT NULL,
`order_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_composite_fk_order_item` FOREIGN KEY (`order_id`,`item_id`) REFERENCES `tbl_order_item` (`order_id`,`item_id`) ON DELETE CASCADE
);
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user1@example.com', 'user1', 'address1', 1);
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user2@example.com', 'user2', 'address2', 1);
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user3@example.com', 'user3', 'address3', 2);
......
......@@ -3,6 +3,7 @@
* The database setup in config.php is required to perform then relevant tests:
*/
DROP TABLE IF EXISTS tbl_composite_fk CASCADE;
DROP TABLE IF EXISTS tbl_order_item CASCADE;
DROP TABLE IF EXISTS tbl_item CASCADE;
DROP TABLE IF EXISTS tbl_order CASCADE;
......@@ -62,6 +63,14 @@ CREATE TABLE `tbl_order_item` (
CONSTRAINT `FK_order_item_item_id` FOREIGN KEY (`item_id`) REFERENCES `tbl_item` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `tbl_composite_fk` (
`id` int(11) NOT NULL,
`order_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_composite_fk_order_item` FOREIGN KEY (`order_id`,`item_id`) REFERENCES `tbl_order_item` (`order_id`,`item_id`) ON DELETE CASCADE
);
CREATE TABLE `tbl_type` (
`int_col` int(11) NOT NULL,
`int_col2` int(11) DEFAULT '1',
......
......@@ -3,6 +3,7 @@
* The database setup in config.php is required to perform then relevant tests:
*/
DROP TABLE IF EXISTS tbl_composite_fk;
DROP TABLE IF EXISTS tbl_order_item;
DROP TABLE IF EXISTS tbl_item;
DROP TABLE IF EXISTS tbl_order;
......@@ -48,6 +49,14 @@ CREATE TABLE tbl_order_item (
PRIMARY KEY (order_id, item_id)
);
CREATE TABLE `tbl_composite_fk` (
`id` int(11) NOT NULL,
`order_id` int(11) NOT NULL,
`item_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `FK_composite_fk_order_item` FOREIGN KEY (`order_id`,`item_id`) REFERENCES `tbl_order_item` (`order_id`,`item_id`) ON DELETE CASCADE
);
CREATE TABLE tbl_type (
int_col INTEGER NOT NULL,
int_col2 INTEGER DEFAULT '1',
......
......@@ -56,6 +56,11 @@ class SchemaTest extends DatabaseTestCase
}
}
public function testGetNonExistingTableSchema()
{
$this->assertNull($this->getConnection()->schema->getTableSchema('nonexisting_table'));
}
public function testSchemaCache()
{
/** @var Schema $schema */
......@@ -67,4 +72,18 @@ class SchemaTest extends DatabaseTestCase
$cachedTable = $schema->getTableSchema('tbl_type', true);
$this->assertEquals($noCacheTable, $cachedTable);
}
public function testCompositeFk()
{
/** @var Schema $schema */
$schema = $this->getConnection()->schema;
$table = $schema->getTableSchema('tbl_composite_fk');
$this->assertCount(1, $table->foreignKeys);
$this->assertTrue(isset($table->foreignKeys[0]));
$this->assertEquals('tbl_order_item', $table->foreignKeys[0][0]);
$this->assertEquals('order_id', $table->foreignKeys[0]['order_id']);
$this->assertEquals('item_id', $table->foreignKeys[0]['item_id']);
}
}
......@@ -6,4 +6,9 @@ use yiiunit\framework\db\SchemaTest;
class SqliteSchemaTest extends SchemaTest
{
protected $driverName = 'sqlite';
public function testCompositeFk()
{
$this->markTestSkipped('sqlite does not allow getting enough information about composite FK.');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment