-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from willemschots/4-sqlite-settings
4: Seperate read and write instances for sqlite
- Loading branch information
Showing
34 changed files
with
112 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package db | ||
|
||
import ( | ||
"database/sql" | ||
) | ||
|
||
const ( | ||
// both options use wal mode, foreign keys, and a busy timeout of 5 seconds. | ||
// the writeOptions also use immediate transactions to prevent locking issues. | ||
// To run SQLite so that it works well with out app, we need a few options | ||
// We need to configure a few options to make sure SQLite works well with our app: | ||
// - WAL Mode so that reads and writes don't block eachother. | ||
// - A busy timeout, specifying the duration a connection will wait for a lock. | ||
// - Foreign keys are enforced. | ||
writeOptions = "?mode=rw_&_foreign_keys=on&_journal_mode=wal&_busy_timeout=5000&_txlock=immediate" | ||
readOptions = "?mode=ro_&_foreign_keys=on&_journal_mode=wal&_busy_timeout=5000" | ||
) | ||
|
||
// OpenSQLite3 opens a pool of SQLite3 connections. Different settings | ||
// are appropriate for reading and writing, so this function needs to know | ||
// what the sql.DB will be used for. | ||
// | ||
// See this comment for more information: | ||
// https://github.com/mattn/go-sqlite3/issues/1179#issuecomment-1638083995 | ||
func OpenSQLite(dbFile string, write bool) (*sql.DB, error) { | ||
optsPostfix := readOptions | ||
if write { | ||
optsPostfix = writeOptions | ||
} | ||
|
||
// Open the database file with the correct options. | ||
db, err := sql.Open("sqlite3", dbFile+optsPostfix) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if write { | ||
// use only a single connection for writing. | ||
db.SetMaxOpenConns(1) | ||
db.SetMaxIdleConns(1) | ||
|
||
// don't close this connection. | ||
db.SetConnMaxLifetime(0) | ||
db.SetConnMaxIdleTime(0) | ||
} | ||
|
||
return db, nil | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package testdb | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"testing" | ||
"time" | ||
|
||
_ "github.com/mattn/go-sqlite3" | ||
"github.com/willemschots/househunt/internal/db" | ||
"github.com/willemschots/househunt/internal/db/migrate" | ||
"github.com/willemschots/househunt/migrations" | ||
) | ||
|
||
// RunWhile runs a database while the provided test is executing. | ||
// It returns an empty database with all migrations applied. | ||
func RunWhile(t *testing.T, write bool) *sql.DB { | ||
t.Helper() | ||
|
||
db := RunUnmigratedWhile(t, write) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
_, err := migrate.RunFS(ctx, db, migrations.FS, migrate.Metadata{}) | ||
if err != nil { | ||
t.Fatalf("failed to run migrations: %v", err) | ||
} | ||
|
||
return db | ||
} | ||
|
||
// RunUnmigratedWhile runs a database while the provided test is executing. | ||
// It returns an empty database without any migrations applied. | ||
func RunUnmigratedWhile(t *testing.T, write bool) *sql.DB { | ||
t.Helper() | ||
|
||
db, err := db.OpenSQLite(":memory:", write) | ||
if err != nil { | ||
t.Fatalf("failed to open database: %v", err) | ||
} | ||
|
||
t.Cleanup(func() { | ||
err := db.Close() | ||
if err != nil { | ||
t.Errorf("failed to close database: %v", err) | ||
} | ||
}) | ||
|
||
return db | ||
} |
This file was deleted.
Oops, something went wrong.