Browse Source

Add support for composite FK to cubrid

tags/2.0.0-beta
Carsten Brandt 11 years ago
parent
commit
164c70eebc
  1. 8
      framework/yii/db/cubrid/Schema.php
  2. 9
      tests/unit/data/cubrid.sql
  3. 9
      tests/unit/data/mysql.sql
  4. 9
      tests/unit/data/sqlite.sql
  5. 19
      tests/unit/framework/db/SchemaTest.php
  6. 5
      tests/unit/framework/db/sqlite/SqliteSchemaTest.php

8
framework/yii/db/cubrid/Schema.php

@ -148,12 +148,16 @@ class Schema extends \yii\db\Schema
$foreignKeys = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $table->name); $foreignKeys = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $table->name);
foreach($foreignKeys as $key) { foreach($foreignKeys as $key) {
$table->foreignKeys[] = array( 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['PKTABLE_NAME'],
$key['FKCOLUMN_NAME'] => $key['PKCOLUMN_NAME'] $key['FKCOLUMN_NAME'] => $key['PKCOLUMN_NAME']
// TODO support composite foreign keys
); );
} }
}
$table->foreignKeys = array_values($table->foreignKeys);
return $table; return $table;
} else { } else {

9
tests/unit/data/cubrid.sql

@ -3,6 +3,7 @@
* The database setup in config.php is required to perform then relevant tests: * 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_order_item;
DROP TABLE IF EXISTS tbl_item; DROP TABLE IF EXISTS tbl_item;
DROP TABLE IF EXISTS tbl_order; DROP TABLE IF EXISTS tbl_order;
@ -76,6 +77,14 @@ CREATE TABLE `tbl_type` (
`bool_col2` smallint DEFAULT 1 `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 ('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 ('user2@example.com', 'user2', 'address2', 1);
INSERT INTO tbl_customer (email, name, address, status) VALUES ('user3@example.com', 'user3', 'address3', 2); INSERT INTO tbl_customer (email, name, address, status) VALUES ('user3@example.com', 'user3', 'address3', 2);

9
tests/unit/data/mysql.sql

@ -3,6 +3,7 @@
* The database setup in config.php is required to perform then relevant tests: * 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_order_item CASCADE;
DROP TABLE IF EXISTS tbl_item CASCADE; DROP TABLE IF EXISTS tbl_item CASCADE;
DROP TABLE IF EXISTS tbl_order 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 CONSTRAINT `FK_order_item_item_id` FOREIGN KEY (`item_id`) REFERENCES `tbl_item` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) 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` ( CREATE TABLE `tbl_type` (
`int_col` int(11) NOT NULL, `int_col` int(11) NOT NULL,
`int_col2` int(11) DEFAULT '1', `int_col2` int(11) DEFAULT '1',

9
tests/unit/data/sqlite.sql

@ -3,6 +3,7 @@
* The database setup in config.php is required to perform then relevant tests: * 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_order_item;
DROP TABLE IF EXISTS tbl_item; DROP TABLE IF EXISTS tbl_item;
DROP TABLE IF EXISTS tbl_order; DROP TABLE IF EXISTS tbl_order;
@ -48,6 +49,14 @@ CREATE TABLE tbl_order_item (
PRIMARY KEY (order_id, item_id) 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 ( CREATE TABLE tbl_type (
int_col INTEGER NOT NULL, int_col INTEGER NOT NULL,
int_col2 INTEGER DEFAULT '1', int_col2 INTEGER DEFAULT '1',

19
tests/unit/framework/db/SchemaTest.php

@ -56,6 +56,11 @@ class SchemaTest extends DatabaseTestCase
} }
} }
public function testGetNonExistingTableSchema()
{
$this->assertNull($this->getConnection()->schema->getTableSchema('nonexisting_table'));
}
public function testSchemaCache() public function testSchemaCache()
{ {
/** @var Schema $schema */ /** @var Schema $schema */
@ -67,4 +72,18 @@ class SchemaTest extends DatabaseTestCase
$cachedTable = $schema->getTableSchema('tbl_type', true); $cachedTable = $schema->getTableSchema('tbl_type', true);
$this->assertEquals($noCacheTable, $cachedTable); $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']);
}
} }

5
tests/unit/framework/db/sqlite/SqliteSchemaTest.php

@ -6,4 +6,9 @@ use yiiunit\framework\db\SchemaTest;
class SqliteSchemaTest extends SchemaTest class SqliteSchemaTest extends SchemaTest
{ {
protected $driverName = 'sqlite'; protected $driverName = 'sqlite';
public function testCompositeFk()
{
$this->markTestSkipped('sqlite does not allow getting enough information about composite FK.');
}
} }

Loading…
Cancel
Save