Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When load fixture with postgres, sequence numbers updated to wrong values/did not upgraded #8298

Open
tino415 opened this issue May 6, 2015 · 6 comments

Comments

@tino415
Copy link

tino415 commented May 6, 2015

When I load fixtures, sequence number of tables are not updated or updated to wrong values,
I'm using this fix:

<?php

namespace tests\codeception\fixtures;

use yii\test\ActiveFixture;
class DocumentTypeFixture extends ActiveFixture {

    public $modelClass = 'app\models\DocumentType';

    public function load() {
        parent::load();

        if($this->db->drivername === 'pgsql') {
            $this->db->createCommand("SELECT pg_catalog.setval(".
                "pg_get_serial_sequence('document_type', 'id'),".
                "(SELECT MAX(id)+1 FROM document_type)".
            ")")->execute();
        }
    }
}

Error message:

1) tests\codeception\unit\crawlers\SuperMusicDocumentCrawlerTest::testCrawling
yii\db\IntegrityException: SQLSTATE[23505]: 
Unique violation: 7 ERROR:  duplicate key value violates unique constraint "document_type_pkey"
DETAIL:  Key (id)=(3) already exists.
The SQL being executed was: INSERT INTO "document_type" ("name") VALUES ('taby')

Fixture data:

<?php

return [
    'type' => [
        'id' => 1,
        'name' => 'type',
    ],
    'type' => [
        'id' => 2,
        'name' => 'type2',
    ],
    'type' => [
        'id' => 3,
        'name' => 'type333',
    ],
];
@dynasource
Copy link
Member

why would this occur with id=3 and not already with id=2?

INSERT INTO "document_type" ("name") VALUES ('taby')

this does also not match with the fixture data you supplied. Hard to verify.

Please reopen with correct information

@tino415
Copy link
Author

tino415 commented Nov 9, 2016

It is long time closed project, I'm unable to verify.

@roslov
Copy link

roslov commented Jun 26, 2017

I have the same problem.

Fixture for table push_notification:

<?php

return [
    'push1' => [
        'id' => 1,
        'customer_id' => 1,
        'type_id' => 1,
        'data' => '{"likedById":2}',
        'status' => 'new',
        'errors' => 0,
        'updated_at' => '2017-06-23 00:00:04',
    ],
    'push2' => [
        'id' => 2,
        'customer_id' => 2,
        'type_id' => 2,
        'data' => '{"likedById":1}',
        'status' => 'processing',
        'errors' => 0,
        'updated_at' => '2017-06-23 00:00:05',
    ],
    'push3' => [
        'id' => 3,
        'customer_id' => 1,
        'type_id' => 1,
        'data' => '{"likedById":3}',
        'status' => 'done',
        'errors' => 0,
        'updated_at' => '2017-06-23 00:00:02',
    ],
    'push4' => [
        'id' => 4,
        'customer_id' => 5,
        'type_id' => 1,
        'data' => '{"likedById":4}',
        'status' => 'not found',
        'errors' => 1,
        'updated_at' => '2017-06-23 00:00:07',
    ],
    'push5' => [
        'id' => 5,
        'customer_id' => 4,
        'type_id' => 2,
        'data' => '{"likedById":2}',
        'status' => 'failed',
        'errors' => 1,
        'updated_at' => '2017-06-23 00:00:06',
    ],
    'push6' => [
        'id' => 6,
        'customer_id' => 4,
        'type_id' => 2,
        'data' => '{"likedById":1}',
        'status' => 'new',
        'errors' => 0,
        'updated_at' => '2017-06-23 00:00:01',
    ],
    'push7' => [
        'id' => 7,
        'customer_id' => 2,
        'type_id' => 1,
        'data' => '{"likedById":5}',
        'status' => 'done',
        'errors' => 0,
        'updated_at' => '2017-06-23 00:00:00',
    ],
    'push8' => [
        'id' => 8,
        'customer_id' => 1,
        'type_id' => 2,
        'data' => '{"likedById":4}',
        'status' => 'failed',
        'errors' => 20,
        'updated_at' => '2017-06-23 00:00:03',
    ],
    'push9' => [
        'id' => 9,
        'customer_id' => 2,
        'type_id' => 2,
        'data' => '{"likedById":1}',
        'status' => 'processing',
        'errors' => 15,
        'updated_at' => '2017-06-23 00:00:09',
    ],
];

Then try to do

INSERT INTO "push_notification" ("customer_id", "type_id", "data", "status") VALUES (1, 1, '[]', 'new') RETURNING "id";

The error will be:

ERROR:  duplicate key value violates unique constraint "push_notification_pkey"
DETAIL:  Key (id)=(1) already exists.

After sending the query 10 times the error disappears (when id becames 10).

@samdark samdark reopened this Jun 26, 2017
@roslov
Copy link

roslov commented Jun 27, 2017

I’m fixing it this way:

protected function _before()
{
    $this->tester->haveFixtures([
        'notifications' => PushNotificationFixture::className(),
    ]);
    // Postgres fix
    Yii::$app->db
        ->createCommand("SELECT setval('push_notification_id_seq', max(id)) FROM push_notification;")
        ->execute();
}

But it’s a kludge.

@samdark samdark added this to the 2.0.14 milestone Jun 28, 2017
@StalkAlex
Copy link
Contributor

StalkAlex commented Jan 25, 2018

What about something like this? Related method.

Rough but working implementation.

    public function load()
    {
        $this->data = [];
        $table = $this->getTableSchema();
        foreach ($this->getData() as $alias => $row) {
            $primaryKeys = $this->db->schema->insert($table->fullName, $row);
            $this->data[$alias] = array_merge($row, $primaryKeys);
        }
        if ($table->sequenceName !== null && count($primaryKeys) > 0) {
            //recalculate sequence based on last inserted id
            $primaryKey = key($primaryKeys);
            $lastId = (new Query())->select('max('. $primaryKey .')')->from($table->fullName)->scalar();
            $this->db->createCommand()->resetSequence($table->fullName, $lastId)->execute();
        }
    }

@samdark samdark added the type:bug Bug label Feb 6, 2018
@samdark samdark modified the milestones: 2.0.14, 2.0.15 Feb 6, 2018
@samdark samdark added the status:ready for adoption Feel free to implement this issue. label Feb 6, 2018
@samdark
Copy link
Member

samdark commented Feb 6, 2018

@StalkAlex if you have time please do a pull request.

rija pushed a commit to rija/gigadb-website that referenced this issue Apr 1, 2018
… unMerge method

the unMerge method will remove an author from the graph by removing all the outward edges from itself.

I also stumbled into the following errors when writing tests that create or delete an AuthorRel object in database:

CDbException: CDbCommand failed to execute the SQL statement: SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates unique constraint "author_rel_pkey"
DETAIL:  Key (id)=(1) already exists.

This is because the data fixtures for author_rel are loaded before the tests are run and for some reason either CDbTestCase or CdbFixtureManager set db connection are unable to auto-increment from the last id used in the fixture.

I found a workaround from this issue (which is for a different context/framework version but same problem):
yiisoft/yii2#8298

Didn't put it in a setUp function as it makes the other tests fail.
rija pushed a commit to rija/gigadb-website that referenced this issue Apr 1, 2018
… unMerge method

the unMerge method will remove an author from the graph by removing all the outward edges from itself.

I also stumbled into the following errors when writing tests that create or delete an AuthorRel object in database:

CDbException: CDbCommand failed to execute the SQL statement: SQLSTATE[23505]: Unique violation: 7 ERROR:  duplicate key value violates unique constraint "author_rel_pkey"
DETAIL:  Key (id)=(1) already exists.

This is because the data fixtures for author_rel are loaded before the tests are run and for some reason either CDbTestCase or CdbFixtureManager set db connection are unable to auto-increment from the last id used in the fixture.

I found a workaround from this issue (which is for a different context/framework version but same problem):
yiisoft/yii2#8298

Didn't put it in a setUp function as it makes the other tests fail.
@samdark samdark modified the milestones: 2.0.16, 2.0.17 Jan 3, 2019
@samdark samdark removed this from the 2.0.17 milestone Mar 20, 2019
KolesnikovNikolai pushed a commit to KolesnikovNikolai/yii2 that referenced this issue Apr 4, 2019
…updated to wrong values/did not upgraded

update sequence after load data
KolesnikovNikolai pushed a commit to KolesnikovNikolai/yii2 that referenced this issue Apr 4, 2019
…updated to wrong values/did not upgraded

update sequence after load data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants