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

testutils/db: use sequences provided by create data during import #441

Merged
merged 1 commit into from
Oct 2, 2023

Conversation

alessandro-sorint
Copy link
Contributor

@alessandro-sorint alessandro-sorint commented Oct 2, 2023

When importing, use sequences provided by create data and not the db struct sequences since they are the latest db version sequences and could be different.

Copy link
Member

@sgotti sgotti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alessandro-sorint

  • I don't understand why so much converting between types.
  • The commit title/description isn't clear.

Doesn't something like this is the same (with also more detailed commit title/description)?

Author: alessandro.pinna <alessandro.pinna@sorint.it>
Date:   Mon Oct 2 10:00:59 2023 +0200

    testutils/db: use sequences provided by create data during import

    When importing, use sequences provided by create data and not the db
    struct sequences since they are the latest db version sequences and
    could be different.

diff --git a/internal/testutil/db.go b/internal/testutil/db.go
index 8a4eb1a64..217f18019 100644
--- a/internal/testutil/db.go
+++ b/internal/testutil/db.go
@@ -241,7 +241,7 @@ func (c *DBContext) query(tx *sql.Tx, rq sq.Builder) (*stdsql.Rows, error) {
        return r, errors.WithStack(err)
 }

-func (c *DBContext) Import(ctx context.Context, r io.Reader) error {
+func (c *DBContext) Import(ctx context.Context, r io.Reader, createData *CreateData) error {
        br := bufio.NewReader(r)
        dec := json.NewDecoder(br)

@@ -381,7 +381,7 @@ func (c *DBContext) Import(ctx context.Context, r io.Reader) error {
                }

                // Populate sequences
-               for _, seq := range c.D.Sequences() {
+               for _, seq := range createData.Sequences {
                        switch c.D.DBType() {
                        case sql.Postgres:
                                q := fmt.Sprintf("SELECT setval('%s', (SELECT COALESCE(MAX(%s), 1) FROM %s));", seq.Name, seq.Column, seq.Table)
@@ -552,7 +552,7 @@ func TestCreate(t *testing.T, lastVersion uint, dataFixtures DataFixtures, setup
                        err = dc.DBM.Create(ctx, stmts, createVersion)
                        assert.NilError(t, err)

-                       err = dc.Import(ctx, bytes.NewBuffer(dataFixture))
+                       err = dc.Import(ctx, bytes.NewBuffer(dataFixture), createData)
                        assert.NilError(t, err)
                })
        }
@@ -610,7 +610,7 @@ func TestMigrate(t *testing.T, lastVersion uint, dataFixtures DataFixtures, setu
                                err = createDC.DBM.Create(ctx, createStmts, migrateVersion)
                                assert.NilError(t, err)

-                               err = createDC.Import(ctx, bytes.NewBuffer(dataFixtureCreate))
+                               err = createDC.Import(ctx, bytes.NewBuffer(dataFixtureCreate), createDataCreate)
                                assert.NilError(t, err)

                                // create db at create version to be migrated.
@@ -648,7 +648,7 @@ func TestMigrate(t *testing.T, lastVersion uint, dataFixtures DataFixtures, setu
                                err = dc.DBM.Create(ctx, stmts, createVersion)
                                assert.NilError(t, err)

-                               err = dc.Import(ctx, bytes.NewBuffer(dataFixture))
+                               err = dc.Import(ctx, bytes.NewBuffer(dataFixture), createData)
                                assert.NilError(t, err)

                                err = dc.DBM.MigrateToVersion(ctx, migrateVersion)

When importing, use sequences provided by create data and not the db
struct sequences since they are the latest db version sequences and
could be different.
@alessandro-sorint
Copy link
Contributor Author

@alessandro-sorint

  • I don't understand why so much converting between types.
  • The commit title/description isn't clear.

Doesn't something like this is the same (with also more detailed commit title/description)?

Author: alessandro.pinna <alessandro.pinna@sorint.it>
Date:   Mon Oct 2 10:00:59 2023 +0200

    testutils/db: use sequences provided by create data during import

    When importing, use sequences provided by create data and not the db
    struct sequences since they are the latest db version sequences and
    could be different.

diff --git a/internal/testutil/db.go b/internal/testutil/db.go
index 8a4eb1a64..217f18019 100644
--- a/internal/testutil/db.go
+++ b/internal/testutil/db.go
@@ -241,7 +241,7 @@ func (c *DBContext) query(tx *sql.Tx, rq sq.Builder) (*stdsql.Rows, error) {
        return r, errors.WithStack(err)
 }

-func (c *DBContext) Import(ctx context.Context, r io.Reader) error {
+func (c *DBContext) Import(ctx context.Context, r io.Reader, createData *CreateData) error {
        br := bufio.NewReader(r)
        dec := json.NewDecoder(br)

@@ -381,7 +381,7 @@ func (c *DBContext) Import(ctx context.Context, r io.Reader) error {
                }

                // Populate sequences
-               for _, seq := range c.D.Sequences() {
+               for _, seq := range createData.Sequences {
                        switch c.D.DBType() {
                        case sql.Postgres:
                                q := fmt.Sprintf("SELECT setval('%s', (SELECT COALESCE(MAX(%s), 1) FROM %s));", seq.Name, seq.Column, seq.Table)
@@ -552,7 +552,7 @@ func TestCreate(t *testing.T, lastVersion uint, dataFixtures DataFixtures, setup
                        err = dc.DBM.Create(ctx, stmts, createVersion)
                        assert.NilError(t, err)

-                       err = dc.Import(ctx, bytes.NewBuffer(dataFixture))
+                       err = dc.Import(ctx, bytes.NewBuffer(dataFixture), createData)
                        assert.NilError(t, err)
                })
        }
@@ -610,7 +610,7 @@ func TestMigrate(t *testing.T, lastVersion uint, dataFixtures DataFixtures, setu
                                err = createDC.DBM.Create(ctx, createStmts, migrateVersion)
                                assert.NilError(t, err)

-                               err = createDC.Import(ctx, bytes.NewBuffer(dataFixtureCreate))
+                               err = createDC.Import(ctx, bytes.NewBuffer(dataFixtureCreate), createDataCreate)
                                assert.NilError(t, err)

                                // create db at create version to be migrated.
@@ -648,7 +648,7 @@ func TestMigrate(t *testing.T, lastVersion uint, dataFixtures DataFixtures, setu
                                err = dc.DBM.Create(ctx, stmts, createVersion)
                                assert.NilError(t, err)

-                               err = dc.Import(ctx, bytes.NewBuffer(dataFixture))
+                               err = dc.Import(ctx, bytes.NewBuffer(dataFixture), createData)
                                assert.NilError(t, err)

                                err = dc.DBM.MigrateToVersion(ctx, migrateVersion)

I tested with PR #432 it works

@alessandro-sorint alessandro-sorint changed the title testutils/db: fix import testutils/db: use sequences provided by create data during import Oct 2, 2023
@sgotti sgotti merged commit 7c492ff into agola-io:master Oct 2, 2023
1 check passed
@alessandro-sorint alessandro-sorint deleted the fix-dbtest branch October 17, 2023 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants