Repository: trustwallet/blockatlas
Pull request #1160 (2020-06-30 14:02:03)

Add updatedAt to db model

codecov[bot]: # [Codecov](https://codecov.io/gh/trustwallet/blockatlas/pull/1160?src=pr&el=h1) Report
> Merging [#1160](https://codecov.io/gh/trustwallet/blockatlas/pull/1160?src=pr&el=desc) into [master](https://codecov.io/gh/trustwallet/blockatlas/commit/b58fdc08ffbadcba94534f9d40f23956fb4df062&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/trustwallet/blockatlas/pull/1160/graphs/tree.svg?width=650&height=150&src=pr&token=jnrjoEQAqv)](https://codecov.io/gh/trustwallet/blockatlas/pull/1160?src=pr&el=tree)

```diff
@@          Coverage Diff           @@
##           master   #1160   +/-   ##
======================================
  Coverage      38%     38%           
======================================
  Files         169     169           
  Lines        6065    6065           
======================================
  Hits         2324    2324           
  Misses       3587    3587           
  Partials      154     154           
```


| [Impacted Files](https://codecov.io/gh/trustwallet/blockatlas/pull/1160?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [db/tracker.go](https://codecov.io/gh/trustwallet/blockatlas/pull/1160/diff?src=pr&el=tree#diff-ZGIvdHJhY2tlci5nbw==) | `93% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/trustwallet/blockatlas/pull/1160?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/trustwallet/blockatlas/pull/1160?src=pr&el=footer). Last update [b58fdc0...77e353a](https://codecov.io/gh/trustwallet/blockatlas/pull/1160?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/db/models/tracker.go b/db/models/tracker.go
index 723531d..14bbd35 100644
--- a/db/models/tracker.go
+++ b/db/models/tracker.go
@@ -1,6 +1,9 @@
 package models
 
+import "time"
+
 type Tracker struct {
-	Coin   string `gorm:"primary_key:true; type:varchar(64)"`
-	Height int64
+	UpdatedAt time.Time
+	Coin      string `gorm:"primary_key:true; type:varchar(64)"`
+	Height    int64
 }
diff --git a/db/tracker.go b/db/tracker.go
index 86c8ba6..04f54d2 100644
--- a/db/tracker.go
+++ b/db/tracker.go
@@ -52,7 +52,7 @@ func (i *Instance) SetLastParsedBlockNumber(coin string, num int64, ctx context.
 	}
 	g := apmgorm.WithContext(ctx, i.Gorm)
 	return g.
-		Set("gorm:insert_option", "ON CONFLICT (coin) DO UPDATE SET height = excluded.height").
+		Set("gorm:insert_option", "ON CONFLICT (coin) DO UPDATE SET height = excluded.height, updated_at = excluded.updated_at").
 		Where(models.Tracker{Coin: coin}).
 		Create(&tracker).Error
 }


Test patch:
diff --git a/db/tracker_test.go b/db/tracker_test.go
index 502c4a8..fc7984c 100644
--- a/db/tracker_test.go
+++ b/db/tracker_test.go
@@ -15,7 +15,7 @@ func TestHeightBlockMap_SetHeight(t *testing.T) {
 	mock.ExpectBegin()
 	mock.ExpectQuery(
 		regexp.QuoteMeta(
-			`INSERT INTO "trackers" ("coin","height") VALUES ($1,$2) ON CONFLICT (coin) DO UPDATE SET height = excluded.height RETURNING "trackers"."coin"`)).WithArgs("bitcoin", 1).WillReturnRows(sqlmock.NewRows([]string{"id"}).
+			`INSERT INTO "trackers" ("updated_at","coin","height") VALUES ($1,$2,$3) ON CONFLICT (coin) DO UPDATE SET height = excluded.height, updated_at = excluded.updated_at RETURNING "trackers"."coin"`)).WithArgs(sqlmock.AnyArg(), "bitcoin", 1).WillReturnRows(sqlmock.NewRows([]string{"id"}).
 		AddRow("id"))
 	mock.ExpectCommit()
 	i := Instance{Gorm: db}
@@ -29,7 +29,7 @@ func TestHeightBlockMap_GetHeight(t *testing.T) {
 	mock.ExpectBegin()
 	mock.ExpectQuery(
 		regexp.QuoteMeta(
-			`INSERT INTO "trackers" ("coin","height") VALUES ($1,$2) ON CONFLICT (coin) DO UPDATE SET height = excluded.height RETURNING "trackers"."coin"`)).WithArgs("bitcoin", 1).WillReturnRows(sqlmock.NewRows([]string{"id"}).
+			`INSERT INTO "trackers" ("updated_at","coin","height") VALUES ($1,$2,$3) ON CONFLICT (coin) DO UPDATE SET height = excluded.height, updated_at = excluded.updated_at RETURNING "trackers"."coin"`)).WithArgs(sqlmock.AnyArg(), "bitcoin", 1).WillReturnRows(sqlmock.NewRows([]string{"id"}).
 		AddRow("id"))
 	mock.ExpectCommit()
 	i := Instance{Gorm: db}