From f046a8277754f0d2c7d1a9e112eba23201e41250 Mon Sep 17 00:00:00 2001 From: EAMoiseenko Date: Thu, 11 Jul 2024 15:32:47 +0300 Subject: [PATCH] (#8298) Restart sequence for Postgresql when loading fixtures --- framework/test/ActiveFixture.php | 6 ++- tests/framework/test/ActiveFixtureTest.php | 44 +++++++++++++++++++ .../test/data/customer_offset_sequence.php | 25 +++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tests/framework/test/data/customer_offset_sequence.php diff --git a/framework/test/ActiveFixture.php b/framework/test/ActiveFixture.php index 208debbd9ff..bdee3a44279 100644 --- a/framework/test/ActiveFixture.php +++ b/framework/test/ActiveFixture.php @@ -130,7 +130,11 @@ protected function resetTable() $table = $this->getTableSchema(); $this->db->createCommand()->delete($table->fullName)->execute(); if ($table->sequenceName !== null) { - $this->db->createCommand()->executeResetSequence($table->fullName, 1); + if ('pgsql' === $this->db->driverName) { + $this->db->createCommand("ALTER SEQUENCE {$table->sequenceName} RESTART;")->execute(); + } else { + $this->db->createCommand()->executeResetSequence($table->fullName, 1); + } } } diff --git a/tests/framework/test/ActiveFixtureTest.php b/tests/framework/test/ActiveFixtureTest.php index c659e6958cf..7250b04dce5 100644 --- a/tests/framework/test/ActiveFixtureTest.php +++ b/tests/framework/test/ActiveFixtureTest.php @@ -9,6 +9,7 @@ use Yii; use yii\db\Connection; +use yii\di\Instance; use yii\test\ActiveFixture; use yii\test\FixtureTrait; use yiiunit\data\ar\ActiveRecord; @@ -55,6 +56,36 @@ public function testGetData() $test->tearDown(); } + /** + * Testing the use of offset fixtures + * @return void + * @throws \yii\base\InvalidConfigException + * @throws \yii\db\Exception + */ + public function testOffsetGetData() + { + $test = new CustomerOffsetDbTestCase(); + /** @var Connection $db */ + $db = Instance::ensure('db', Connection::class); + if ('pgsql' === $db->driverName) { + $db->createCommand('alter sequence customer_id_seq start 2 minvalue 2 restart 2;')->execute(); + } + $test->setUp(); + $fixture = $test->getFixture('customers'); + + $this->assertEquals(CustomerFixture::className(), get_class($fixture)); + $this->assertCount(2, $fixture); + $this->assertEquals(2, $fixture['customer1']['id']); + $this->assertEquals('customer1@example.com', $fixture['customer1']['email']); + $this->assertEquals(1, $fixture['customer1']['profile_id']); + + $this->assertEquals(3, $fixture['customer2']['id']); + $this->assertEquals('customer2@example.com', $fixture['customer2']['email']); + $this->assertEquals(2, $fixture['customer2']['profile_id']); + + $test->tearDown(); + } + public function testGetModel() { $test = new CustomerDbTestCase(); @@ -225,6 +256,19 @@ public function fixtures() } } +class CustomerOffsetDbTestCase extends BaseDbTestCase +{ + public function fixtures() + { + return [ + 'customers' => [ + 'class' => CustomerFixture::className(), + 'dataFile' => '@app/framework/test/data/customer_offset_sequence.php', + ], + ]; + } +} + class CustomDirectoryDbTestCase extends BaseDbTestCase { public function fixtures() diff --git a/tests/framework/test/data/customer_offset_sequence.php b/tests/framework/test/data/customer_offset_sequence.php new file mode 100644 index 00000000000..d9a25637055 --- /dev/null +++ b/tests/framework/test/data/customer_offset_sequence.php @@ -0,0 +1,25 @@ + [ + 'id' => 2, + 'email' => 'customer1@example.com', + 'name' => 'customer1', + 'address' => 'address1', + 'status' => 1, + 'profile_id' => 1, + ], + 'customer2' => [ + 'id' => 3, + 'email' => 'customer2@example.com', + 'name' => 'customer2', + 'address' => 'address2', + 'status' => 2, + 'profile_id' => 2, + ], +];