mirror of https://github.com/jlelse/GoBlog
Reuse skipped shortpath ids
This commit is contained in:
parent
99980cb461
commit
3c38ec62dc
|
@ -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)
|
||||
|
|
11
shortPath.go
11
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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue