42d9a5d9232ea4c83bf511260936e9979c60d416 — Victor Freire 4 months ago ca81fac
article: sqlite-production-configuration
1 files changed, 55 insertions(+), 0 deletions(-)

M content-org/content.org
M content-org/content.org => content-org/content.org +55 -0
@@ 5438,6 5438,61 @@ w := io.MultiWriter(os.Stdout, f)
logger := log.New(w, "MultiWriter loggger", log.LstdFlags)

** SQLite Production Configuration                             :database:go:
:EXPORT_DATE: 2023-07-19
:EXPORT_FILE_NAME: sqlite-production-configuration
:EXPORT_HUGO_SLUG: sqlite-production-configuration

/Notes taken from [[https://github.com/benbjohnson][Ben Johnson’s]] talk [[https://www.youtube.com/watch?v=XcAYkriuQ1o][Building Production Applications
Using Go & SQLite]]./

The complete list of pragmas can be found on [[https://www.sqlite.org/pragma.html#pragma_foreign_keys][SQLite’s official

- Journal Mode :: Journal mode configures how SQLite writes
  transactions. You almost always want it as *WAL* (write ahead log).

  #+begin_src sql
  PRAGMA journal_mode = WAL;

- Busy timeout :: Busy timeout sets how long write transactions will
  wait to start. If unset, writes will fail immediately if another
  write is running. Five seconds might be enough... most of the times.

  #+begin_src sql
  PRAGMA busy_timeout = 5000;

- Foreign Keys :: Believe it or not, for historical reasons foreign
  keys are not enforced by default

  #+begin_src sql
  PRAGMA foreign_keys = ON;

*** Usage in Go

This is a [[https://go.dev][Go]] example showing how to use the options above with the
[[https://github.com/mattn/go-sqlite3][github.com/mattn/go-sqlite3]] driver.

#+begin_src go
db, err := sql.Open(
if err != nil {
    log.Fatalln("could not open database: %w", err)
defer func() {
    if err := db.Close(); err != nil {
        log.Fatalln("could not close database: %w", err)

* Footnotes

[fn:48] [[https://www.polyml.org/][Poly/ML]] exposes a function that does just that: [[https://www.polyml.org/documentation/Reference/PolyMLStructure.html#print][PolyML.print]].