diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 8d3aea4f8..10e7b20fa 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -8,6 +8,7 @@ use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; +use Yiisoft\Db\Schema\SchemaInterface; use Yiisoft\Db\Schema\TableSchemaInterface; use function array_diff; @@ -84,6 +85,11 @@ public function attributes(): array return $this->getTableSchema()->getColumnNames(); } + public function columnType(string $columnName): string + { + return $this->getTableSchema()->getColumn($columnName)?->getType() ?? SchemaInterface::TYPE_STRING; + } + public function filterCondition(array $condition, array $aliases = []): array { $result = []; diff --git a/src/ActiveRecordInterface.php b/src/ActiveRecordInterface.php index a54f65ef8..6ae6d0bfc 100644 --- a/src/ActiveRecordInterface.php +++ b/src/ActiveRecordInterface.php @@ -11,6 +11,7 @@ use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; use Yiisoft\Db\Exception\StaleObjectException; +use Yiisoft\Db\Schema\SchemaInterface; interface ActiveRecordInterface { @@ -25,6 +26,12 @@ interface ActiveRecordInterface */ public function attributes(): array; + /** + * Returns the abstract type of the column. See {@see SchemaInterface} constants started with prefix `TYPE_` for + * possible abstract types. + */ + public function columnType(string $columnName): string; + /** * Returns the database connection used by the Active Record instance. */ diff --git a/src/ActiveRelationTrait.php b/src/ActiveRelationTrait.php index 5fa26c548..270a5e0ff 100644 --- a/src/ActiveRelationTrait.php +++ b/src/ActiveRelationTrait.php @@ -11,6 +11,10 @@ use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidConfigException; use Yiisoft\Db\Exception\NotSupportedException; +use Yiisoft\Db\QueryBuilder\Condition\ArrayOverlapsCondition; +use Yiisoft\Db\QueryBuilder\Condition\InCondition; +use Yiisoft\Db\QueryBuilder\Condition\JsonOverlapsCondition; +use Yiisoft\Db\Schema\SchemaInterface; use function array_column; use function array_combine; @@ -19,6 +23,7 @@ use function array_filter; use function array_flip; use function array_intersect_key; +use function array_key_first; use function array_keys; use function array_merge; use function array_unique; @@ -505,11 +510,11 @@ protected function filterByModels(array $models): void if (count($attributes) === 1) { /** single key */ - $attribute = reset($this->link); + $linkedAttribute = reset($this->link); if ($model instanceof ActiveRecordInterface) { foreach ($models as $model) { - $value = $model->getAttribute($attribute); + $value = $model->getAttribute($linkedAttribute); if ($value !== null) { if (is_array($value)) { @@ -521,8 +526,8 @@ protected function filterByModels(array $models): void } } else { foreach ($models as $model) { - if (isset($model[$attribute])) { - $value = $model[$attribute]; + if (isset($model[$linkedAttribute])) { + $value = $model[$linkedAttribute]; if (is_array($value)) { $values = [...$values, ...$value]; @@ -533,31 +538,47 @@ protected function filterByModels(array $models): void } } - if (!empty($values)) { - $scalarValues = array_filter($values, 'is_scalar'); - $nonScalarValues = array_diff_key($values, $scalarValues); - - $scalarValues = array_unique($scalarValues); - $values = [...$scalarValues, ...$nonScalarValues]; + if (empty($values)) { + $this->emulateExecution(); + $this->andWhere('1=0'); + return; } - } else { - $nulls = array_fill_keys($this->link, null); - if ($model instanceof ActiveRecordInterface) { - foreach ($models as $model) { - $value = $model->getAttributes($this->link); + $scalarValues = array_filter($values, 'is_scalar'); + $nonScalarValues = array_diff_key($values, $scalarValues); - if (!empty($value)) { - $values[] = array_combine($attributes, array_merge($nulls, $value)); - } + $scalarValues = array_unique($scalarValues); + $values = [...$scalarValues, ...$nonScalarValues]; + + $attribute = reset($attributes); + /** @var string $columnName */ + $columnName = array_key_first($this->link); + + match ($this->getARInstance()->columnType($columnName)) { + 'array' => $this->andWhere(new ArrayOverlapsCondition($attribute, $values)), + SchemaInterface::TYPE_JSON => $this->andWhere(new JsonOverlapsCondition($attribute, $values)), + default => $this->andWhere(new InCondition($attribute, 'IN', $values)), + }; + + return; + } + + $nulls = array_fill_keys($this->link, null); + + if ($model instanceof ActiveRecordInterface) { + foreach ($models as $model) { + $value = $model->getAttributes($this->link); + + if (!empty($value)) { + $values[] = array_combine($attributes, array_merge($nulls, $value)); } - } else { - foreach ($models as $model) { - $value = array_intersect_key($model, $nulls); + } + } else { + foreach ($models as $model) { + $value = array_intersect_key($model, $nulls); - if (!empty($value)) { - $values[] = array_combine($attributes, array_merge($nulls, $value)); - } + if (!empty($value)) { + $values[] = array_combine($attributes, array_merge($nulls, $value)); } } } @@ -568,7 +589,7 @@ protected function filterByModels(array $models): void return; } - $this->andWhere(['in', $attributes, $values]); + $this->andWhere(new InCondition($attributes, 'IN', $values)); } private function getModelKeys(ActiveRecordInterface|array $activeRecord, array $attributes): array diff --git a/tests/ActiveRecordTest.php b/tests/ActiveRecordTest.php index e52c4edab..2f893c567 100644 --- a/tests/ActiveRecordTest.php +++ b/tests/ActiveRecordTest.php @@ -8,6 +8,7 @@ use DivisionByZeroError; use ReflectionException; use Yiisoft\ActiveRecord\ActiveQuery; +use Yiisoft\ActiveRecord\ArArrayHelper; use Yiisoft\ActiveRecord\ConnectionProvider; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Animal; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Cat; @@ -25,6 +26,7 @@ use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\OrderItem; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\OrderItemWithNullFK; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\OrderWithFactory; +use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Promotion; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Profile; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Type; use Yiisoft\ActiveRecord\Tests\Support\Assert; @@ -1110,4 +1112,62 @@ public function testSerialization(): void serialize($profile) ); } + + public function testRelationViaJson() + { + if (in_array($this->db()->getDriverName(), ['oci', 'sqlsrv'], true)) { + $this->markTestSkipped('Oracle and MSSQL drivers do not support JSON columns.'); + } + + $this->checkFixture($this->db(), 'promotion'); + + $promotionQuery = new ActiveQuery(Promotion::class); + /** @var Promotion[] $promotions */ + $promotions = $promotionQuery->with('itemsViaJson')->all(); + + $this->assertSame([1, 2], ArArrayHelper::getColumn($promotions[0]->getItemsViaJson(), 'id')); + $this->assertSame([3, 4, 5], ArArrayHelper::getColumn($promotions[1]->getItemsViaJson(), 'id')); + $this->assertSame([1, 3], ArArrayHelper::getColumn($promotions[2]->getItemsViaJson(), 'id')); + $this->assertCount(0, $promotions[3]->getItemsViaJson()); + + /** Test inverse relation */ + foreach ($promotions as $promotion) { + foreach ($promotion->getItemsViaJson() as $item) { + $this->assertTrue($item->isRelationPopulated('promotionsViaJson')); + } + } + + $this->assertSame([1, 3], ArArrayHelper::getColumn($promotions[0]->getItemsViaJson()[0]->getPromotionsViaJson(), 'id')); + $this->assertSame([1], ArArrayHelper::getColumn($promotions[0]->getItemsViaJson()[1]->getPromotionsViaJson(), 'id')); + $this->assertSame([2, 3], ArArrayHelper::getColumn($promotions[1]->getItemsViaJson()[0]->getPromotionsViaJson(), 'id')); + $this->assertSame([2], ArArrayHelper::getColumn($promotions[1]->getItemsViaJson()[1]->getPromotionsViaJson(), 'id')); + $this->assertSame([2], ArArrayHelper::getColumn($promotions[1]->getItemsViaJson()[2]->getPromotionsViaJson(), 'id')); + $this->assertSame([1, 3], ArArrayHelper::getColumn($promotions[2]->getItemsViaJson()[0]->getPromotionsViaJson(), 'id')); + $this->assertSame([2, 3], ArArrayHelper::getColumn($promotions[2]->getItemsViaJson()[1]->getPromotionsViaJson(), 'id')); + } + + public function testLazzyRelationViaJson() + { + if (in_array($this->db()->getDriverName(), ['oci', 'sqlsrv'], true)) { + $this->markTestSkipped('Oracle and MSSQL drivers do not support JSON columns.'); + } + + $this->checkFixture($this->db(), 'item'); + + $itemQuery = new ActiveQuery(Item::class); + /** @var Item[] $items */ + $items = $itemQuery->all(); + + $this->assertFalse($items[0]->isRelationPopulated('promotionsViaJson')); + $this->assertFalse($items[1]->isRelationPopulated('promotionsViaJson')); + $this->assertFalse($items[2]->isRelationPopulated('promotionsViaJson')); + $this->assertFalse($items[3]->isRelationPopulated('promotionsViaJson')); + $this->assertFalse($items[4]->isRelationPopulated('promotionsViaJson')); + + $this->assertSame([1, 3], ArArrayHelper::getColumn($items[0]->getPromotionsViaJson(), 'id')); + $this->assertSame([1], ArArrayHelper::getColumn($items[1]->getPromotionsViaJson(), 'id')); + $this->assertSame([2, 3], ArArrayHelper::getColumn($items[2]->getPromotionsViaJson(), 'id')); + $this->assertSame([2], ArArrayHelper::getColumn($items[3]->getPromotionsViaJson(), 'id')); + $this->assertSame([2], ArArrayHelper::getColumn($items[4]->getPromotionsViaJson(), 'id')); + } } diff --git a/tests/Driver/Pgsql/ActiveRecordTest.php b/tests/Driver/Pgsql/ActiveRecordTest.php index 2ac1a5e95..177e4fcd9 100644 --- a/tests/Driver/Pgsql/ActiveRecordTest.php +++ b/tests/Driver/Pgsql/ActiveRecordTest.php @@ -8,6 +8,7 @@ use Traversable; use Yiisoft\ActiveRecord\ActiveQuery; use Yiisoft\ActiveRecord\ArArrayHelper; +use Yiisoft\ActiveRecord\Tests\Driver\Pgsql\Stubs\Item; use Yiisoft\ActiveRecord\Tests\Driver\Pgsql\Stubs\Promotion; use Yiisoft\ActiveRecord\Tests\Driver\Pgsql\Stubs\Type; use Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\ArrayAndJsonTypes; @@ -445,26 +446,47 @@ public function testRelationViaArray() $promotionQuery = new ActiveQuery(Promotion::class); /** @var Promotion[] $promotions */ - $promotions = $promotionQuery->with('items')->all(); + $promotions = $promotionQuery->with('itemsViaArray')->all(); - $this->assertSame([1, 2], ArArrayHelper::getColumn($promotions[0]->getItems(), 'id')); - $this->assertSame([3, 4, 5], ArArrayHelper::getColumn($promotions[1]->getItems(), 'id')); - $this->assertSame([1, 3], ArArrayHelper::getColumn($promotions[2]->getItems(), 'id')); - $this->assertCount(0, $promotions[3]->getItems()); + $this->assertSame([1, 2], ArArrayHelper::getColumn($promotions[0]->getItemsViaArray(), 'id')); + $this->assertSame([3, 4, 5], ArArrayHelper::getColumn($promotions[1]->getItemsViaArray(), 'id')); + $this->assertSame([1, 3], ArArrayHelper::getColumn($promotions[2]->getItemsViaArray(), 'id')); + $this->assertCount(0, $promotions[3]->getItemsViaArray()); /** Test inverse relation */ foreach ($promotions as $promotion) { - foreach ($promotion->getItems() as $item) { - $this->assertTrue($item->isRelationPopulated('promotions')); + foreach ($promotion->getItemsViaArray() as $item) { + $this->assertTrue($item->isRelationPopulated('promotionsViaArray')); } } - $this->assertSame([1, 3], ArArrayHelper::getColumn($promotions[0]->getItems()[0]->getPromotions(), 'id')); - $this->assertSame([1], ArArrayHelper::getColumn($promotions[0]->getItems()[1]->getPromotions(), 'id')); - $this->assertSame([2, 3], ArArrayHelper::getColumn($promotions[1]->getItems()[0]->getPromotions(), 'id')); - $this->assertSame([2], ArArrayHelper::getColumn($promotions[1]->getItems()[1]->getPromotions(), 'id')); - $this->assertSame([2], ArArrayHelper::getColumn($promotions[1]->getItems()[2]->getPromotions(), 'id')); - $this->assertSame([1, 3], ArArrayHelper::getColumn($promotions[2]->getItems()[0]->getPromotions(), 'id')); - $this->assertSame([2, 3], ArArrayHelper::getColumn($promotions[2]->getItems()[1]->getPromotions(), 'id')); + $this->assertSame([1, 3], ArArrayHelper::getColumn($promotions[0]->getItemsViaArray()[0]->getPromotionsViaArray(), 'id')); + $this->assertSame([1], ArArrayHelper::getColumn($promotions[0]->getItemsViaArray()[1]->getPromotionsViaArray(), 'id')); + $this->assertSame([2, 3], ArArrayHelper::getColumn($promotions[1]->getItemsViaArray()[0]->getPromotionsViaArray(), 'id')); + $this->assertSame([2], ArArrayHelper::getColumn($promotions[1]->getItemsViaArray()[1]->getPromotionsViaArray(), 'id')); + $this->assertSame([2], ArArrayHelper::getColumn($promotions[1]->getItemsViaArray()[2]->getPromotionsViaArray(), 'id')); + $this->assertSame([1, 3], ArArrayHelper::getColumn($promotions[2]->getItemsViaArray()[0]->getPromotionsViaArray(), 'id')); + $this->assertSame([2, 3], ArArrayHelper::getColumn($promotions[2]->getItemsViaArray()[1]->getPromotionsViaArray(), 'id')); + } + + public function testLazzyRelationViaArray() + { + $this->checkFixture($this->db(), 'item'); + + $itemQuery = new ActiveQuery(Item::class); + /** @var Item[] $items */ + $items = $itemQuery->all(); + + $this->assertFalse($items[0]->isRelationPopulated('promotionsViaArray')); + $this->assertFalse($items[1]->isRelationPopulated('promotionsViaArray')); + $this->assertFalse($items[2]->isRelationPopulated('promotionsViaArray')); + $this->assertFalse($items[3]->isRelationPopulated('promotionsViaArray')); + $this->assertFalse($items[4]->isRelationPopulated('promotionsViaArray')); + + $this->assertSame([1, 3], ArArrayHelper::getColumn($items[0]->getPromotionsViaArray(), 'id')); + $this->assertSame([1], ArArrayHelper::getColumn($items[1]->getPromotionsViaArray(), 'id')); + $this->assertSame([2, 3], ArArrayHelper::getColumn($items[2]->getPromotionsViaArray(), 'id')); + $this->assertSame([2], ArArrayHelper::getColumn($items[3]->getPromotionsViaArray(), 'id')); + $this->assertSame([2], ArArrayHelper::getColumn($items[4]->getPromotionsViaArray(), 'id')); } } diff --git a/tests/Driver/Pgsql/Stubs/Item.php b/tests/Driver/Pgsql/Stubs/Item.php index fcb19dc49..07be93d07 100644 --- a/tests/Driver/Pgsql/Stubs/Item.php +++ b/tests/Driver/Pgsql/Stubs/Item.php @@ -14,14 +14,14 @@ final class Item extends \Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Item public function relationQuery(string $name): ActiveQueryInterface { return match ($name) { - 'promotions' => $this->hasMany(Promotion::class, ['item_ids' => 'id']), + 'promotionsViaArray' => $this->hasMany(Promotion::class, ['array_item_ids' => 'id']), default => parent::relationQuery($name), }; } /** @return Promotion[] */ - public function getPromotions(): array + public function getPromotionsViaArray(): array { - return $this->relation('promotions'); + return $this->relation('promotionsViaArray'); } } diff --git a/tests/Driver/Pgsql/Stubs/Promotion.php b/tests/Driver/Pgsql/Stubs/Promotion.php index 730728bd5..2b447babb 100644 --- a/tests/Driver/Pgsql/Stubs/Promotion.php +++ b/tests/Driver/Pgsql/Stubs/Promotion.php @@ -5,15 +5,9 @@ namespace Yiisoft\ActiveRecord\Tests\Driver\Pgsql\Stubs; use Yiisoft\ActiveRecord\ActiveQueryInterface; -use Yiisoft\ActiveRecord\ActiveRecord; -final class Promotion extends ActiveRecord +final class Promotion extends \Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Promotion { - public int $id; - /** @var int[] $item_ids */ - public array $item_ids; - public string $title; - public function getTableName(): string { return '{{%promotion}}'; @@ -22,14 +16,15 @@ public function getTableName(): string public function relationQuery(string $name): ActiveQueryInterface { return match ($name) { - 'items' => $this->hasMany(Item::class, ['id' => 'item_ids'])->inverseOf('promotions'), + 'itemsViaArray' => $this->hasMany(Item::class, ['id' => 'array_item_ids']) + ->inverseOf('promotionsViaArray'), default => parent::relationQuery($name), }; } /** @return Item[] */ - public function getItems(): array + public function getItemsViaArray(): array { - return $this->relation('items'); + return $this->relation('itemsViaArray'); } } diff --git a/tests/Stubs/ActiveRecord/Item.php b/tests/Stubs/ActiveRecord/Item.php index 40d149586..ee4ed4658 100644 --- a/tests/Stubs/ActiveRecord/Item.php +++ b/tests/Stubs/ActiveRecord/Item.php @@ -26,6 +26,7 @@ public function relationQuery(string $name): ActiveQueryInterface { return match ($name) { 'category' => $this->getCategoryQuery(), + 'promotionsViaJson' => $this->hasMany(Promotion::class, ['json_item_ids' => 'id']), default => parent::relationQuery($name), }; } @@ -54,4 +55,10 @@ public function getCategoryQuery(): ActiveQuery { return $this->hasOne(Category::class, ['id' => 'category_id']); } + + /** @return Promotion[] */ + public function getPromotionsViaJson(): array + { + return $this->relation('promotionsViaJson'); + } } diff --git a/tests/Stubs/ActiveRecord/Promotion.php b/tests/Stubs/ActiveRecord/Promotion.php new file mode 100644 index 000000000..86a5654e5 --- /dev/null +++ b/tests/Stubs/ActiveRecord/Promotion.php @@ -0,0 +1,38 @@ + $this->hasMany(Item::class, ['id' => 'json_item_ids']) + ->inverseOf('promotionsViaJson'), + default => parent::relationQuery($name), + }; + } + + /** @return Item[] */ + public function getItemsViaJson(): array + { + return $this->relation('itemsViaJson'); + } +} diff --git a/tests/data/mysql.sql b/tests/data/mysql.sql index f4a4b3848..308c18199 100644 --- a/tests/data/mysql.sql +++ b/tests/data/mysql.sql @@ -7,6 +7,7 @@ DROP TABLE IF EXISTS `composite_fk` CASCADE; DROP TABLE IF EXISTS `order_item` CASCADE; DROP TABLE IF EXISTS `order_item_with_null_fk` CASCADE; DROP TABLE IF EXISTS `item` CASCADE; +DROP TABLE IF EXISTS `promotion` CASCADE; DROP TABLE IF EXISTS `order` CASCADE; DROP TABLE IF EXISTS `order_with_null_fk` CASCADE; DROP TABLE IF EXISTS `category` CASCADE; @@ -74,6 +75,12 @@ CREATE TABLE `item` ( CONSTRAINT `FK_item_category_id` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8; +CREATE TABLE `promotion` ( + `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, + `json_item_ids` json NOT NULL, + `title` varchar(126) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + CREATE TABLE `order` ( `id` int(11) NOT NULL AUTO_INCREMENT, `customer_id` int(11) NOT NULL, @@ -246,6 +253,11 @@ INSERT INTO `item` (name, category_id) VALUES ('Ice Age', 2); INSERT INTO `item` (name, category_id) VALUES ('Toy Story', 2); INSERT INTO `item` (name, category_id) VALUES ('Cars', 2); +INSERT INTO `promotion` (json_item_ids, title) VALUES ('[1,2]', 'Discounted items'); +INSERT INTO `promotion` (json_item_ids, title) VALUES ('[3,4,5]', 'New arrivals'); +INSERT INTO `promotion` (json_item_ids, title) VALUES ('[1,3]', 'Free shipping'); +INSERT INTO `promotion` (json_item_ids, title) VALUES ('[]', 'Free!'); + INSERT INTO `order` (customer_id, created_at, total) VALUES (1, 1325282384, 110.0); INSERT INTO `order` (customer_id, created_at, total) VALUES (2, 1325334482, 33.0); INSERT INTO `order` (customer_id, created_at, total) VALUES (2, 1325502201, 40.0); diff --git a/tests/data/pgsql.sql b/tests/data/pgsql.sql index 8083b5b16..d53a408f3 100644 --- a/tests/data/pgsql.sql +++ b/tests/data/pgsql.sql @@ -8,6 +8,7 @@ DROP TABLE IF EXISTS "composite_fk" CASCADE; DROP TABLE IF EXISTS "order_item" CASCADE; DROP TABLE IF EXISTS "item" CASCADE; DROP SEQUENCE IF EXISTS "item_id_seq_2" CASCADE; +DROP TABLE IF EXISTS "promotion" CASCADE; DROP TABLE IF EXISTS "order_item_with_null_fk" CASCADE; DROP TABLE IF EXISTS "order" CASCADE; DROP TABLE IF EXISTS "order_with_null_fk" CASCADE; @@ -28,7 +29,6 @@ DROP TABLE IF EXISTS "employee" CASCADE; DROP TABLE IF EXISTS "department" CASCADE; DROP TABLE IF EXISTS "alpha" CASCADE; DROP TABLE IF EXISTS "beta" CASCADE; -DROP TABLE IF EXISTS "promotion" CASCADE; DROP VIEW IF EXISTS "animal_view" CASCADE; DROP TABLE IF EXISTS "T_constraints_4" CASCADE; DROP TABLE IF EXISTS "T_constraints_3" CASCADE; @@ -83,6 +83,13 @@ CREATE TABLE "item" ( ); CREATE SEQUENCE "item_id_seq_2"; +CREATE TABLE "promotion" ( + id serial primary key, + array_item_ids integer[] NOT NULL, + json_item_ids jsonb NOT NULL, + title varchar(126) NOT NULL +); + CREATE TABLE "order" ( id serial not null primary key, customer_id integer NOT NULL references "customer"(id) on UPDATE CASCADE on DELETE CASCADE, @@ -225,12 +232,6 @@ CREATE TABLE "beta" ( PRIMARY KEY (id) ); -CREATE TABLE "promotion" ( - id serial primary key, - item_ids integer[] not null, - title varchar(126) not null -); - CREATE VIEW "animal_view" AS SELECT * FROM "animal"; INSERT INTO "animal" (type) VALUES ('Yiisoft\ActiveRecord\Tests\Stubs\ActiveRecord\Cat'); @@ -256,6 +257,11 @@ INSERT INTO "item" (name, category_id) VALUES ('Ice Age', 2); INSERT INTO "item" (name, category_id) VALUES ('Toy Story', 2); INSERT INTO "item" (name, category_id) VALUES ('Cars', 2); +INSERT INTO "promotion" (array_item_ids, json_item_ids, title) VALUES ('{1,2}', '[1,2]', 'Discounted items'); +INSERT INTO "promotion" (array_item_ids, json_item_ids, title) VALUES ('{3,4,5}', '[3,4,5]', 'New arrivals'); +INSERT INTO "promotion" (array_item_ids, json_item_ids, title) VALUES ('{1,3}', '[1,3]', 'Free shipping'); +INSERT INTO "promotion" (array_item_ids, json_item_ids, title) VALUES ('{}', '[]', 'Free!'); + INSERT INTO "order" (customer_id, created_at, total) VALUES (1, 1325282384, 110.0); INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325334482, 33.0); INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325502201, 40.0); @@ -309,11 +315,6 @@ INSERT INTO "beta" (id, alpha_string_identifier) VALUES (6, '2b'); INSERT INTO "beta" (id, alpha_string_identifier) VALUES (7, '2b'); INSERT INTO "beta" (id, alpha_string_identifier) VALUES (8, '02'); -INSERT INTO "promotion" (item_ids, title) VALUES ('{1,2}', 'Discounted items'); -INSERT INTO "promotion" (item_ids, title) VALUES ('{3,4,5}', 'New arrivals'); -INSERT INTO "promotion" (item_ids, title) VALUES ('{1,3}', 'Free shipping'); -INSERT INTO "promotion" (item_ids, title) VALUES ('{}', 'Free!'); - /** * (Postgres-)Database Schema for validator tests */ diff --git a/tests/data/sqlite.sql b/tests/data/sqlite.sql index e10b97fa9..8678bd494 100644 --- a/tests/data/sqlite.sql +++ b/tests/data/sqlite.sql @@ -7,6 +7,7 @@ DROP TABLE IF EXISTS "composite_fk"; DROP TABLE IF EXISTS "order_item"; DROP TABLE IF EXISTS "order_item_with_null_fk"; DROP TABLE IF EXISTS "item"; +DROP TABLE IF EXISTS "promotion"; DROP TABLE IF EXISTS "order"; DROP TABLE IF EXISTS "order_with_null_fk"; DROP TABLE IF EXISTS "category"; @@ -61,6 +62,13 @@ CREATE TABLE "item" ( PRIMARY KEY (id) ); +CREATE TABLE "promotion" ( + id INTEGER NOT NULL, + json_item_ids JSON NOT NULL, + title varchar(126) NOT NULL, + PRIMARY KEY (id) +); + CREATE TABLE "order" ( id INTEGER NOT NULL, customer_id INTEGER NOT NULL, @@ -210,6 +218,11 @@ INSERT INTO "item" (name, category_id) VALUES ('Ice Age', 2); INSERT INTO "item" (name, category_id) VALUES ('Toy Story', 2); INSERT INTO "item" (name, category_id) VALUES ('Cars', 2); +INSERT INTO "promotion" (json_item_ids, title) VALUES ('[1,2]', 'Discounted items'); +INSERT INTO "promotion" (json_item_ids, title) VALUES ('[3,4,5]', 'New arrivals'); +INSERT INTO "promotion" (json_item_ids, title) VALUES ('[1,3]', 'Free shipping'); +INSERT INTO "promotion" (json_item_ids, title) VALUES ('[]', 'Free!'); + INSERT INTO "order" (customer_id, created_at, total) VALUES (1, 1325282384, 110.0); INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325334482, 33.0); INSERT INTO "order" (customer_id, created_at, total) VALUES (2, 1325502201, 40.0);