Commit 97841221 authored by Ian Chechin's avatar Ian Chechin
Browse files

sqlite: document mattn-compat DSN pragma keys and test them together

Follow-up to the DSN shorthand keys carried over from !73: document
_busy_timeout/_fk/_journal/_sync/_vacuum/_query_only in the driver DSN
reference, note the fixed apply order (busy_timeout first, query_only last)
at the query_only call site, and add a combined-DSN test asserting the keys
coexist in a single DSN regardless of the order they appear in it.
parent 5831f4be
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -53,6 +53,19 @@ func newDriver() *Driver { return d }
// keyword added for you). May be specified more than once, '&'-separated. For more
// information on supported PRAGMAs see: https://www.sqlite.org/pragma.html
//
// The following shorthand keys set common PRAGMAs for easier DSN compatibility
// when migrating from github.com/mattn/go-sqlite3. Each value is passed as-is to
// the corresponding PRAGMA and is not validated. They are applied in a fixed
// order (busy_timeout first, query_only last) so an order-sensitive combination
// works in a single DSN:
//
//	_busy_timeout, _timeout   -> PRAGMA busy_timeout
//	_foreign_keys, _fk        -> PRAGMA foreign_keys
//	_journal_mode, _journal   -> PRAGMA journal_mode
//	_synchronous, _sync       -> PRAGMA synchronous
//	_auto_vacuum, _vacuum     -> PRAGMA auto_vacuum
//	_query_only               -> PRAGMA query_only
//
// _time_format: The name of a format to use when writing time values to the database.
// The currently supported values are (1) "sqlite" for YYYY-MM-DD HH:MM:SS.SSS[+-]HH:MM
// (format 4 from https://www.sqlite.org/lang_datefunc.html#time_values with sub-second
+15 −0
Original line number Diff line number Diff line
@@ -145,4 +145,19 @@ func TestDSNPragmas(t *testing.T) {
		// invalid keeps it disabled
		verifyPragma(t, "_query_only=x", "query_only", "0")
	})

	t.Run("Test combined DSN", func(t *testing.T) {
		// The mattn-compat keys must coexist in a single DSN. busy_timeout is
		// applied first and query_only last (see applyQueryParams); the DSN lists
		// them in a different order on purpose, so this also confirms the apply
		// order is fixed by the driver, not by the order the keys appear in the
		// DSN.
		verifyPragma(t, "_query_only=1&_synchronous=NORMAL&_journal_mode=wal&_foreign_keys=on&_busy_timeout=5000",
			"busy_timeout", "5000",
			"foreign_keys", "1",
			"journal_mode", "wal",
			"synchronous", "1",
			"query_only", "1",
		)
	})
}
+2 −0
Original line number Diff line number Diff line
@@ -367,6 +367,8 @@ func applyQueryParams(c *conn, query string) error {
		}
	}

	// query_only is applied last: it makes the connection read-only, so it must
	// not precede the write-capable pragmas above (notably auto_vacuum).
	if v := q.Get("_query_only"); v != "" {
		cmd := "pragma query_only = " + v
		_, err := c.exec(context.Background(), cmd, nil)