From 3c38ec62dc94d3f018722d7e7aeba46b83d92df7 Mon Sep 17 00:00:00 2001 From: Jan-Lukas Else Date: Sun, 14 Nov 2021 07:58:49 +0100 Subject: [PATCH] Reuse skipped shortpath ids --- postsDb_test.go | 10 ++++++++++ shortPath.go | 11 +++++++++-- shortPath_test.go | 7 +++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/postsDb_test.go b/postsDb_test.go index a1d2c90..3ead607 100644 --- a/postsDb_test.go +++ b/postsDb_test.go @@ -95,6 +95,16 @@ func Test_postsDb(t *testing.T) { must.NoError(err) is.Equal(1, count) + // Check by taxonomy + count, err = app.db.countPosts(&postsRequestConfig{taxonomy: &configTaxonomy{Name: "tags"}}) + must.NoError(err) + is.Equal(1, count) + + // Check by taxonomy value + count, err = app.db.countPosts(&postsRequestConfig{taxonomy: &configTaxonomy{Name: "tags"}, taxonomyValue: "A"}) + must.NoError(err) + is.Equal(1, count) + // Delete post _, err = app.deletePostFromDb("/test/abc") must.NoError(err) diff --git a/shortPath.go b/shortPath.go index 4513beb..202adfe 100644 --- a/shortPath.go +++ b/shortPath.go @@ -17,9 +17,16 @@ func (db *database) shortenPath(p string) (string, error) { return spi.(string), nil } // Insert in case it isn't shortened yet - _, err := db.exec("insert or rollback into shortpath (path) values (@path)", sql.Named("path", p)) + _, err := db.exec(` + insert or rollback into shortpath (id, path) + values ( + -- next available id (reuse skipped ids due to bug) + (select min(id) + 1 from (select id from shortpath union all select 0) where id + 1 not in (select id from shortpath)), + @path + )`, sql.Named("path", p)) if err != nil { - if no, ok := err.(sqlite3.Error); !ok || no.Code != sqlite3.ErrConstraint { + if no, ok := err.(sqlite3.Error); !ok || sqlite3.ErrNo(no.ExtendedCode) != sqlite3.ErrNo(sqlite3.ErrConstraintUnique) { + // Some other error than unique constraint violation because path is already shortened return nil, err } } diff --git a/shortPath_test.go b/shortPath_test.go index 82365bb..b470a52 100644 --- a/shortPath_test.go +++ b/shortPath_test.go @@ -35,4 +35,11 @@ func Test_shortenPath(t *testing.T) { assert.NotEqual(t, res1, res3) assert.Equal(t, "/s/2", res3) + + db.spc.Delete("/a") + db.exec("delete from shortpath where id = 1") + + res4, err := db.shortenPath("/c") + require.NoError(t, err) + assert.Equal(t, "/s/1", res4) }