Skip to content

Commit 2824911

Browse files
committed
Add documentation for *WithDB constructors
Documents the new constructors for using existing database connections with SQLite, MySQL, and PostgreSQL backends, including: - Usage examples - Migration handling differences - MySQL-specific WithMigrationDSN option
1 parent 2d3e47c commit 2824911

1 file changed

Lines changed: 66 additions & 3 deletions

File tree

docs/source/includes/_backends.md

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,30 @@ func NewSqliteBackend(path string, opts ...option)
2121

2222
Create a new SQLite backend instance with `NewSqliteBackend`.
2323

24+
### Using an Existing Connection
25+
26+
```go
27+
func NewSqliteBackendWithDB(db *sql.DB, opts ...option)
28+
```
29+
30+
If you already have a `*sql.DB` connection, you can use `NewSqliteBackendWithDB` to create a backend that uses your existing connection. When using this constructor:
31+
32+
- The backend will **not** close the database connection when `Close()` is called
33+
- Migrations are **disabled by default** - use `WithApplyMigrations(true)` to enable them
34+
- You are responsible for configuring the connection appropriately (e.g., WAL mode, busy timeout, max open connections)
35+
36+
```go
37+
db, _ := sql.Open("sqlite", "file:mydb.sqlite?_txlock=immediate")
38+
db.Exec("PRAGMA journal_mode=WAL;")
39+
db.Exec("PRAGMA busy_timeout = 5000;")
40+
db.SetMaxOpenConns(1)
41+
42+
backend := sqlite.NewSqliteBackendWithDB(db, sqlite.WithApplyMigrations(true))
43+
```
44+
2445
### Options
2546

26-
- `WithApplyMigrations(applyMigrations bool)` - Set whether migrations should be applied on startup. Defaults to `true`
47+
- `WithApplyMigrations(applyMigrations bool)` - Set whether migrations should be applied on startup. Defaults to `true` for `NewSqliteBackend`, `false` for `NewSqliteBackendWithDB`
2748
- `WithBackendOptions(opts ...backend.BackendOption)` - Apply generic backend options
2849

2950
### Schema
@@ -44,10 +65,35 @@ func NewMysqlBackend(host string, port int, user, password, database string, opt
4465

4566
Create a new MySQL backend instance with `NewMysqlBackend`.
4667

68+
### Using an Existing Connection
69+
70+
```go
71+
func NewMysqlBackendWithDB(db *sql.DB, opts ...option)
72+
```
73+
74+
If you already have a `*sql.DB` connection, you can use `NewMysqlBackendWithDB` to create a backend that uses your existing connection. When using this constructor:
75+
76+
- The backend will **not** close the database connection when `Close()` is called
77+
- Migrations are **disabled by default**
78+
- To enable migrations, you must provide a DSN with `WithMigrationDSN()` that supports multi-statement queries
79+
80+
```go
81+
db, _ := sql.Open("mysql", "user:pass@tcp(localhost:3306)/mydb?parseTime=true")
82+
83+
// To enable migrations, provide a DSN with multiStatements=true
84+
migrationDSN := "user:pass@tcp(localhost:3306)/mydb?parseTime=true&multiStatements=true"
85+
86+
backend := mysql.NewMysqlBackendWithDB(db,
87+
mysql.WithApplyMigrations(true),
88+
mysql.WithMigrationDSN(migrationDSN),
89+
)
90+
```
91+
4792
### Options
4893

4994
- `WithMySQLOptions(f func(db *sql.DB))` - Apply custom options to the MySQL database connection
50-
- `WithApplyMigrations(applyMigrations bool)` - Set whether migrations should be applied on startup. Defaults to `true`
95+
- `WithApplyMigrations(applyMigrations bool)` - Set whether migrations should be applied on startup. Defaults to `true` for `NewMysqlBackend`, `false` for `NewMysqlBackendWithDB`
96+
- `WithMigrationDSN(dsn string)` - Set the DSN to use for migrations. Required when using `NewMysqlBackendWithDB` with `ApplyMigrations` enabled. The DSN must support multi-statement queries.
5197
- `WithBackendOptions(opts ...backend.BackendOption)` - Apply generic backend options
5298

5399

@@ -69,10 +115,27 @@ func NewPostgresBackend(host string, port int, user, password, database string,
69115

70116
Create a new PostgreSQL backend instance with `NewPostgresBackend`.
71117

118+
### Using an Existing Connection
119+
120+
```go
121+
func NewPostgresBackendWithDB(db *sql.DB, opts ...option)
122+
```
123+
124+
If you already have a `*sql.DB` connection, you can use `NewPostgresBackendWithDB` to create a backend that uses your existing connection. When using this constructor:
125+
126+
- The backend will **not** close the database connection when `Close()` is called
127+
- Migrations are **disabled by default** - use `WithApplyMigrations(true)` to enable them
128+
129+
```go
130+
db, _ := sql.Open("pgx", "host=localhost port=5432 user=myuser password=mypass dbname=mydb sslmode=disable")
131+
132+
backend := postgres.NewPostgresBackendWithDB(db, postgres.WithApplyMigrations(true))
133+
```
134+
72135
### Options
73136

74137
- `WithPostgresOptions(f func(db *sql.DB))` - Apply custom options to the PostgreSQL database connection
75-
- `WithApplyMigrations(applyMigrations bool)` - Set whether migrations should be applied on startup. Defaults to `true`
138+
- `WithApplyMigrations(applyMigrations bool)` - Set whether migrations should be applied on startup. Defaults to `true` for `NewPostgresBackend`, `false` for `NewPostgresBackendWithDB`
76139
- `WithBackendOptions(opts ...backend.BackendOption)` - Apply generic backend options
77140

78141

0 commit comments

Comments
 (0)