From 54e758aacd37b166c5a0716fe2cca12261a2fbb9 Mon Sep 17 00:00:00 2001 From: Francisco Moura Date: Thu, 8 Aug 2024 15:54:50 -0300 Subject: [PATCH] fixup! feat(db): Add Epoch and Snapshot --- internal/repository/base.go | 11 ++++++----- internal/repository/base_test.go | 3 ++- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/internal/repository/base.go b/internal/repository/base.go index 27434bd55..8372dc50e 100644 --- a/internal/repository/base.go +++ b/internal/repository/base.go @@ -268,7 +268,7 @@ func (pg *Database) InsertReport( func (pg *Database) InsertSnapshot( ctx context.Context, snapshot *Snapshot, -) error { +) (id uint64, _ error) { query := ` INSERT INTO snapshot (input_id, @@ -277,7 +277,8 @@ func (pg *Database) InsertSnapshot( VALUES (@inputId, @appAddress, - @uri)` + @uri) + RETURNING id` args := pgx.NamedArgs{ "inputId": snapshot.InputId, @@ -285,12 +286,12 @@ func (pg *Database) InsertSnapshot( "uri": snapshot.URI, } - _, err := pg.db.Exec(ctx, query, args) + err := pg.db.QueryRow(ctx, query, args).Scan(&id) if err != nil { - return fmt.Errorf("%w: %w", ErrInsertRow, err) + return 0, fmt.Errorf("%w: %w", ErrInsertRow, err) } - return nil + return id, nil } func (pg *Database) GetNodeConfig( diff --git a/internal/repository/base_test.go b/internal/repository/base_test.go index a5b7c3349..d07a12e92 100644 --- a/internal/repository/base_test.go +++ b/internal/repository/base_test.go @@ -222,8 +222,9 @@ func (s *RepositorySuite) SetupDatabase() { URI: "/some/path", } - err = s.database.InsertSnapshot(s.ctx, &snapshot) + id, err := s.database.InsertSnapshot(s.ctx, &snapshot) s.Require().Nil(err) + s.Require().Equal(uint64(1), id) } func (s *RepositorySuite) TestApplicationExists() {