Skip to content

Commit

Permalink
Another attempt at fixing the 'no such table: dataset' error by using…
Browse files Browse the repository at this point in the history
… sql.Conn to execute statements that involve upvotesDB, and then doing the attach databsase on that connection.
  • Loading branch information
johnwarden committed Aug 29, 2023
1 parent f598e14 commit f438cff
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
27 changes: 25 additions & 2 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion position.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, `
Expand Down
12 changes: 10 additions & 2 deletions vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit f438cff

Please sign in to comment.