Fix database lock when trying to write already existing redirect

This commit is contained in:
Jan-Lukas Else 2020-09-20 22:04:42 +02:00
parent 4393d28186
commit fb6b4f65ac
5 changed files with 18 additions and 6 deletions

View File

@ -81,6 +81,7 @@ func getCache(context context.Context, path string) (creationTime int64, header
func saveCache(path string, now time.Time, header map[string][]string, body []byte) {
headerBytes, _ := json.Marshal(header)
startWritingToDb()
defer finishWritingToDb()
tx, err := appDb.Begin()
if err != nil {
return
@ -88,16 +89,15 @@ func saveCache(path string, now time.Time, header map[string][]string, body []by
_, _ = tx.Exec("delete from cache where time<?;", now.Unix()-appConfig.Cache.Expiration)
_, _ = tx.Exec("insert or replace into cache (path, time, header, body) values (?, ?, ?, ?);", path, now.Unix(), headerBytes, body)
_ = tx.Commit()
finishWritingToDb()
}
func purgeCache(path string) {
startWritingToDb()
defer finishWritingToDb()
tx, err := appDb.Begin()
if err != nil {
return
}
_, _ = tx.Exec("delete from cache where path=?", path)
_ = tx.Commit()
finishWritingToDb()
}

View File

@ -32,6 +32,6 @@ func closeDb() error {
func vacuumDb() {
startWritingToDb()
defer finishWritingToDb()
_, _ = appDb.Exec("VACUUM;")
finishWritingToDb()
}

View File

@ -77,7 +77,7 @@ func parseHugoFile(fileContent string, path string) (*Post, error) {
splittedPostPath := strings.Split(post.Path, "/")
alias = strings.TrimSuffix(post.Path, splittedPostPath[len(splittedPostPath)-1]) + alias
}
_ = createRedirect(alias, post.Path)
_ = createOrReplaceRedirect(alias, post.Path)
}
// Return post
return post, nil

View File

@ -44,16 +44,19 @@ func (p *Post) createOrReplace() error {
startWritingToDb()
tx, err := appDb.Begin()
if err != nil {
finishWritingToDb()
return err
}
_, err = tx.Exec("insert or replace into posts (path, content, published, updated) values (?, ?, ?, ?)", p.Path, p.Content, p.Published, p.Updated)
if err != nil {
_ = tx.Rollback()
finishWritingToDb()
return err
}
_, err = tx.Exec("delete from post_parameters where path=?", p.Path)
if err != nil {
_ = tx.Rollback()
finishWritingToDb()
return err
}
for param, value := range p.Parameters {
@ -62,6 +65,7 @@ func (p *Post) createOrReplace() error {
_, err = tx.Exec("insert into post_parameters (path, parameter, value) values (?, ?, ?)", p.Path, param, value)
if err != nil {
_ = tx.Rollback()
finishWritingToDb()
return err
}
}
@ -69,6 +73,7 @@ func (p *Post) createOrReplace() error {
}
err = tx.Commit()
if err != nil {
finishWritingToDb()
return err
}
finishWritingToDb()
@ -84,20 +89,24 @@ func (p *Post) delete() error {
startWritingToDb()
tx, err := appDb.Begin()
if err != nil {
finishWritingToDb()
return err
}
_, err = tx.Exec("delete from posts where path=?", p.Path)
if err != nil {
_ = tx.Rollback()
finishWritingToDb()
return err
}
_, err = tx.Exec("delete from post_parameters where path=?", p.Path)
if err != nil {
_ = tx.Rollback()
finishWritingToDb()
return err
}
err = tx.Commit()
if err != nil {
finishWritingToDb()
return err
}
finishWritingToDb()

View File

@ -55,7 +55,7 @@ func allRedirectPaths() ([]string, error) {
return redirectPaths, nil
}
func createRedirect(from, to string) error {
func createOrReplaceRedirect(from, to string) error {
if from == "" || to == "" {
return errors.New("empty path")
}
@ -63,15 +63,18 @@ func createRedirect(from, to string) error {
startWritingToDb()
tx, err := appDb.Begin()
if err != nil {
finishWritingToDb()
return err
}
_, err = tx.Exec("insert into redirects (fromPath, toPath) values (?, ?)", from, to)
_, err = tx.Exec("insert or replace into redirects (fromPath, toPath) values (?, ?)", from, to)
if err != nil {
_ = tx.Rollback()
finishWritingToDb()
return err
}
err = tx.Commit()
if err != nil {
finishWritingToDb()
return err
}
finishWritingToDb()