From ac4f7820c7f9ccfa9906c0063f5c6ab67c91d583 Mon Sep 17 00:00:00 2001 From: Aaron <76254323+huichiaotsou@users.noreply.github.com> Date: Mon, 19 Sep 2022 15:55:19 +0800 Subject: [PATCH] fix: return 0 height inside GetLastBlockHeight if there are no blocks (#76) ## Description When we use the parse blocks command, in case that the database is empty, `return err` here will error out the program and not continuing parsing the desired block height. Replacing it here so it's more handy for dev purpose when we need to just test specific heights. (we don't have to run ` start cmd` before using `parse blocks all`) ## Checklist - [x] Targeted PR against correct branch. - [ ] Linked to Github issue with discussion and accepted design OR link to spec that describes this work. - [ ] Wrote unit tests. - [x] Re-reviewed `Files changed` in the Github PR explorer. --- CHANGELOG.md | 1 + database/postgresql/postgresql.go | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2697470..b1b407a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ### Changes - ([\#74](https://github.com/forbole/juno/pull/74)) Added database block count to prometheus to improve alert monitoring - ([\#75](https://github.com/forbole/juno/pull/75)) Allow modules to handle MsgExec inner messages +- ([\#76](https://github.com/forbole/juno/pull/76)) Return 0 as height for `GetLastBlockHeight()` method while no block is saved ## v3.4.0 ### Changes diff --git a/database/postgresql/postgresql.go b/database/postgresql/postgresql.go index 2dd1113c..47d60acf 100644 --- a/database/postgresql/postgresql.go +++ b/database/postgresql/postgresql.go @@ -101,7 +101,12 @@ func (db *Database) GetLastBlockHeight() (int64, error) { stmt := `SELECT height FROM block ORDER BY height DESC LIMIT 1;` var height int64 - if err := db.Sql.QueryRow(stmt).Scan(&height); err != nil { + err := db.Sql.QueryRow(stmt).Scan(&height) + if err != nil { + if strings.Contains(err.Error(), "no rows in result set") { + // If no rows stored in block table, return 0 as height + return 0, nil + } return 0, fmt.Errorf("error while getting last block height, error: %s", err) }