Skip to content

Commit 86c8fdc

Browse files
HorlogeSkynettgragnato
authored andcommitted
chore(persistence): prefers using SEQUENCE to retrieve table max. id
See 0e995d4 and #543. PostgreSQL and CockroachDB support `last_value` column from `SEQUENCE` virtual tables.
1 parent b6bcc68 commit 86c8fdc

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

persistence/postgres.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,14 @@ func (db *postgresDatabase) getExactCount() (uint, error) {
155155
ctx, cancel := context.WithTimeout(context.Background(), time.Second/2)
156156
defer cancel()
157157

158-
rows, err := db.conn.QueryContext(ctx, "SELECT id::BIGINT AS exact_count FROM torrents ORDER BY id DESC LIMIT 1;")
158+
rows, err := db.conn.QueryContext(ctx, "SELECT last_value::BIGINT AS exact_count FROM seq_torrents_id;")
159159
if err != nil {
160160
return 0, err
161161
}
162162
defer rows.Close()
163163

164164
if !rows.Next() {
165-
return 0, errors.New("no rows returned from `SELECT id::BIGINT AS exact_count FROM torrents ORDER BY id DESC LIMIT 1;`")
165+
return 0, errors.New("no rows returned from `SELECT last_value::BIGINT AS exact_count FROM seq_torrents_id;`")
166166
}
167167

168168
// Returns int64: https://godoc.org/github.com/lib/pq#hdr-Data_Types

persistence/postgres_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func TestPostgresDatabase_GetExactCount(t *testing.T) {
116116

117117
// Test case 1: Valid row returned
118118
rows := sqlmock.NewRows([]string{"exact_count"}).AddRow(int64(10))
119-
mock.ExpectQuery("SELECT id::BIGINT AS exact_count FROM torrents ORDER BY id DESC LIMIT 1;").
119+
mock.ExpectQuery("SELECT last_value::BIGINT AS exact_count FROM seq_torrents_id;").
120120
WillReturnRows(rows)
121121

122122
count, err := db.getExactCount()
@@ -129,7 +129,7 @@ func TestPostgresDatabase_GetExactCount(t *testing.T) {
129129

130130
// Test case 2: No rows returned
131131
rows = sqlmock.NewRows([]string{"exact_count"})
132-
mock.ExpectQuery("SELECT id::BIGINT AS exact_count FROM torrents ORDER BY id DESC LIMIT 1;").
132+
mock.ExpectQuery("SELECT last_value::BIGINT AS exact_count FROM seq_torrents_id;").
133133
WillReturnRows(rows)
134134

135135
count, err = db.getExactCount()
@@ -142,7 +142,7 @@ func TestPostgresDatabase_GetExactCount(t *testing.T) {
142142

143143
// Test case 3: Null value returned
144144
rows = sqlmock.NewRows([]string{"exact_count"}).AddRow(nil)
145-
mock.ExpectQuery("SELECT id::BIGINT AS exact_count FROM torrents ORDER BY id DESC LIMIT 1;").
145+
mock.ExpectQuery("SELECT last_value::BIGINT AS exact_count FROM seq_torrents_id;").
146146
WillReturnRows(rows)
147147

148148
count, err = db.getExactCount()
@@ -226,7 +226,7 @@ func TestPostgresDatabase_GetNumberOfTorrents(t *testing.T) {
226226

227227
// Test case 1: getExactCount succeeds
228228
rows := sqlmock.NewRows([]string{"exact_count"}).AddRow(int64(10))
229-
mock.ExpectQuery("SELECT id::BIGINT AS exact_count FROM torrents ORDER BY id DESC LIMIT 1;").
229+
mock.ExpectQuery("SELECT last_value::BIGINT AS exact_count FROM seq_torrents_id;").
230230
WillReturnRows(rows)
231231

232232
count, err := db.GetNumberOfTorrents()
@@ -238,7 +238,7 @@ func TestPostgresDatabase_GetNumberOfTorrents(t *testing.T) {
238238
}
239239

240240
// Test case 2: getExactCount fails, getFuzzyCount succeeds
241-
mock.ExpectQuery("SELECT id::BIGINT AS exact_count FROM torrents ORDER BY id DESC LIMIT 1;").
241+
mock.ExpectQuery("SELECT last_value::BIGINT AS exact_count FROM seq_torrents_id;").
242242
WillReturnError(fmt.Errorf("exact count failed"))
243243
rows = sqlmock.NewRows([]string{"estimate_count"}).AddRow(int64(20))
244244
mock.ExpectQuery("SELECT reltuples::BIGINT AS estimate_count FROM pg_class WHERE relname='torrents';").
@@ -253,7 +253,7 @@ func TestPostgresDatabase_GetNumberOfTorrents(t *testing.T) {
253253
}
254254

255255
// Test case 3: both getExactCount and getFuzzyCount fail
256-
mock.ExpectQuery("SELECT id::BIGINT AS exact_count FROM torrents ORDER BY id DESC LIMIT 1;").
256+
mock.ExpectQuery("SELECT last_value::BIGINT AS exact_count FROM seq_torrents_id;").
257257
WillReturnError(fmt.Errorf("exact count failed"))
258258
mock.ExpectQuery("SELECT reltuples::BIGINT AS estimate_count FROM pg_class WHERE relname='torrents';").
259259
WillReturnError(fmt.Errorf("fuzzy count failed"))

0 commit comments

Comments
 (0)