diff --git a/database.go b/database.go index 0a6c604..8fce9fd 100644 --- a/database.go +++ b/database.go @@ -45,7 +45,25 @@ func (ndb newsDatabase) upvotesDBWithDataset(ctx context.Context) (*sql.Conn, er // is writing to the frontpage database. s := fmt.Sprintf("attach database 'file:%s?mode=ro' as frontpage", frontpageDatabaseFilename) _, err = conn.ExecContext(ctx, s) - return conn, errors.Wrap(err, "attach frontpage database") + if err != nil && err.Error() != "database frontpage is already in use" { + return conn, errors.Wrap(err, "attach frontpage database") + } + + return conn, nil +} + +func (ndb newsDatabase) attachFrontpageDB() error { + frontpageDatabaseFilename := fmt.Sprintf("%s/%s", ndb.sqliteDataDir, sqliteDataFilename) + + s := fmt.Sprintf("attach database 'file:%s?mode=ro' as frontpage", frontpageDatabaseFilename) + + _, err := ndb.upvotesDB.Exec(s) + + if err != nil && err.Error() != "database frontpage is already in use" { + return errors.Wrap(err, "attach frontpage database") + } + + return nil } func (ndb newsDatabase) close() { @@ -173,7 +191,12 @@ func (ndb newsDatabase) initUpvotesDB() error { _, _ = ndb.upvotesDB.Exec(s) } - return nil + frontpageDatabaseFilename := fmt.Sprintf("%s/%s", ndb.sqliteDataDir, sqliteDataFilename) + + // attach the dataset table + s := fmt.Sprintf("attach database 'file:%s?mode=ro' as frontpage", frontpageDatabaseFilename) + _, err := ndb.upvotesDB.Exec(s) + return errors.Wrap(err, "attach frontpage database") } func openNewsDatabase(sqliteDataDir string) (newsDatabase, error) { diff --git a/position.go b/position.go index 622814a..c9c3de5 100644 --- a/position.go +++ b/position.go @@ -228,7 +228,10 @@ func (app app) getDetailedPositions(ctx context.Context, userID int) ([]Position func (app app) getPositions(ctx context.Context, userID int64, storyIDs []int) ([]Position, error) { positions := make([]Position, 0) - db := app.ndb.upvotesDB + db, err := app.ndb.upvotesDBWithDataset(ctx) + if err != nil { + return positions, errors.Wrap(err, "upvotesDBWithDataset") + } // TODO: only select votes relevant to the stories on the page getPositionsStatement, err := db.PrepareContext(ctx, ` diff --git a/vote.go b/vote.go index be6fbb8..d722e4c 100644 --- a/vote.go +++ b/vote.go @@ -27,6 +27,11 @@ var ( ) func (app app) prepareVoteStatements() error { + err := app.ndb.attachFrontpageDB() + if err != nil { + return errors.Wrap(err, "attachFrontpageDB") + } + if insertVoteStmt == nil { var e error @@ -109,7 +114,11 @@ func (app app) vote(ctx context.Context, userID int64, storyID int, direction in return 0, 0, err } - tx, e := app.ndb.upvotesDB.BeginTx(ctx, nil) + db, err := app.ndb.upvotesDBWithDataset(ctx) + if err != nil { + return 0, 0, errors.Wrap(err, "upvotesDBWithDataset") + } + tx, e := db.BeginTx(ctx, nil) if e != nil { err = errors.Wrap(e, "BeginTX") return @@ -163,7 +172,6 @@ func (app app) vote(ctx context.Context, userID int64, storyID int, direction in } func (app app) voteHandler() func(http.ResponseWriter, *http.Request, voteParams) error { - return func(w http.ResponseWriter, r *http.Request, p voteParams) error { userID := app.getUserID(r)