Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions refid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ Reserved placeholders:

## Database Setup

`refid.SequenceStore` is a pluggable interface (`Next(ctx, scopeKey, max) (int64, error)`); the package ships two raw-SQL backends, each in its own subpackage. A program that imports only `refid` never compiles a database driver into its binary — Go's per-package import graph links `pgx` or `modernc.org/sqlite` only if you actually import `refid/store/postgres` or `refid/store/sqlite`, respectively. `go.mod` still lists both as requirements of the module as a whole, since every subpackage shares one `go.mod` for simplicity; that's a module-level dependency-graph entry, not something your binary picks up unless you import the subpackage that uses it.
`refid.SequenceStore` is a pluggable interface (`Next(ctx, scopeKey, max) (int64, error)`); the package ships two raw-SQL backends, each in its own subpackage.

Neither backend registers a `database/sql` driver — they only issue SQL against the `*sql.DB` you hand them, so you import the driver, open the connection, and pass the result in. That keeps the driver choice yours, and avoids an `init` panic in a binary that already registers the same driver name.

### PostgreSQL (`refid/store/postgres`)

Expand All @@ -127,14 +129,16 @@ store, err := postgres.New(db, postgres.WithTableName("custom_sequences"))
err = postgres.Migrate(ctx, db, postgres.WithTableName("custom_sequences"))
```

`db` is a `*sql.DB` opened against the `pgx` driver (`sql.Open("pgx", dsn)`) — see the Quickstart above.
`db` is a `*sql.DB` you opened yourself — the queries use PostgreSQL's native `$1` placeholders, so any PostgreSQL driver works. The Quickstart above uses pgx.

### SQLite (`refid/store/sqlite`)

Same schema shape and API, using the pure-Go `modernc.org/sqlite` driver (no CGO):
Same schema shape and API. Import a SQLite driver — `modernc.org/sqlite` is pure Go, no CGO:

```go
db, err := sql.Open("sqlite", "refid.db") // registered by importing github.com/OpenNSW/core/refid/store/sqlite
import _ "modernc.org/sqlite" // registers the "sqlite" driver

db, err := sql.Open("sqlite", "refid.db")
if err := sqlite.Migrate(ctx, db); err != nil { ... }
store, err := sqlite.New(db)
```
Expand Down
9 changes: 3 additions & 6 deletions refid/store/postgres/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// Copyright (c) 2026 Lanka Software Foundation

// Package postgres provides a PostgreSQL-backed implementation of
// refid.SequenceStore using database/sql and the pgx stdlib driver directly
// — no ORM. Open a connection with sql.Open("pgx", dsn) (this package
// blank-imports github.com/jackc/pgx/v5/stdlib to register that driver name)
// and pass the resulting *sql.DB to New.
// refid.SequenceStore using database/sql and raw SQL — no ORM. Import a
// PostgreSQL driver (e.g. github.com/jackc/pgx/v5/stdlib), open a connection
// with sql.Open, and pass the resulting *sql.DB to New.
package postgres

import (
Expand All @@ -16,8 +15,6 @@ import (

"github.com/OpenNSW/core/refid"
"github.com/OpenNSW/core/refid/store/internal/sqlident"

_ "github.com/jackc/pgx/v5/stdlib" // registers the "pgx" database/sql driver
)

// DefaultTableName is the default table name used for sequence counters.
Expand Down
2 changes: 2 additions & 0 deletions refid/store/postgres/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

"github.com/OpenNSW/core/refid"
"github.com/OpenNSW/core/refid/store/postgres"

_ "github.com/jackc/pgx/v5/stdlib" // registers the "pgx" database/sql driver
)

// TestStore_Integration tests Migrate and the SequenceStore against a live
Expand Down
8 changes: 3 additions & 5 deletions refid/store/sqlite/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Copyright (c) 2026 Lanka Software Foundation

// Package sqlite provides a SQLite-backed implementation of
// refid.SequenceStore using database/sql and the pure-Go
// modernc.org/sqlite driver — no CGO, no ORM. Open a connection with
// sql.Open("sqlite", path) and pass the resulting *sql.DB to New.
// refid.SequenceStore using database/sql and raw SQL — no ORM. Import a
// SQLite driver (e.g. modernc.org/sqlite, which is pure Go — no CGO), open a
// connection with sql.Open, and pass the resulting *sql.DB to New.
//
// SQLite allows only one writer at a time via a whole-database file lock,
// and nothing sets a busy_timeout by default, so concurrent access can fail
Expand All @@ -21,8 +21,6 @@ import (

"github.com/OpenNSW/core/refid"
"github.com/OpenNSW/core/refid/store/internal/sqlident"

_ "modernc.org/sqlite" // registers the "sqlite" database/sql driver
)

// DefaultTableName is the default table name used for sequence counters.
Expand Down
2 changes: 2 additions & 0 deletions refid/store/sqlite/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

"github.com/OpenNSW/core/refid"
"github.com/OpenNSW/core/refid/store/sqlite"

_ "modernc.org/sqlite" // registers the "sqlite" database/sql driver
)

// TestStore_Integration tests Migrate and the SequenceStore against a fresh
Expand Down
Loading