Repository navigation
Expand file tree
/
Copy pathconfig.go
More file actions
369 lines (331 loc) · 12.3 KB
/
Copy pathconfig.go
File metadata and controls
369 lines (331 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package testdb
import (
"context"
"crypto/rand"
"encoding/hex"
"errors"
"fmt"
"os"
"strings"
"time"
)
// Config holds the configuration for test database creation and management.
type Config struct {
// AdminDSNOverride is an optional connection string override for creating/dropping test databases.
// The user specified here must have privileges to create and drop databases.
//
// For PostgreSQL, this is typically the 'postgres' database.
//
// If not specified, the library automatically discovers the admin DSN from:
// 1. A database-specific environment variable (e.g., TEST_POSTGRES_URL)
// 2. TEST_DATABASE_URL environment variable
// 3. DATABASE_URL environment variable
// 4. Database-specific defaults (e.g., postgres://postgres:postgres@localhost:5432/postgres)
//
// Most users don't need to set this - automatic discovery works in most cases.
// Use this when you need custom admin credentials or connection settings.
AdminDSNOverride string
// Migrator prepares each new test database, typically by running the
// project's migrations. nil (the default) leaves the database empty.
// See WithMigrator.
Migrator Migrator
// DBPrefix is prepended to test database names.
// Useful for identifying test databases in a shared environment.
//
// Default: "test"
// Example database name: "test_1699564231_a1b2c3d4"
DBPrefix string
// TemplateName specifies a template database to clone instead of creating a fresh database.
// Templates are significantly faster than fresh database creation, especially for large schemas
// or databases with pre-seeded data.
//
// If empty (default), a fresh database is created.
// If specified, the template database must already exist (see postgres.EnsureTemplate).
//
// Example: "us_region" or "seeded_schema"
//
// Performance impact:
// - Fresh database: ~12-15ms (PostgreSQL CREATE DATABASE)
// - Template clone: ~3-5ms (PostgreSQL CREATE DATABASE WITH TEMPLATE)
TemplateName string
// Verbose enables logging of database operations.
// When false (default), testdb operates silently.
// When true, logs database creation, cleanup, and migration completion.
//
// Default: false
Verbose bool
// KeepOnFailure leaves a test's database in place when the test fails,
// and logs a connection string for it, so the data can be inspected.
// See WithKeepOnFailure.
//
// Default: false, or true when the TESTDB_KEEP_ON_FAILURE environment
// variable is set to 1, true, yes or on.
KeepOnFailure bool
}
// Migrator prepares a freshly created, empty database: it runs migrations,
// loads fixtures, or does whatever the tests need to find in place. It
// receives the DSN of the database to prepare.
//
// Adapters for the common migration tools live in their own modules, so a
// project only depends on the tool it uses:
// - github.com/bashhack/testdb/migrate/goose
// - github.com/bashhack/testdb/migrate/tern
// - github.com/bashhack/testdb/migrate/golangmigrate
//
// For plain SQL without a tool, see postgres.SQLFiles and postgres.LoadSQLFile.
// Template setup functions have the same shape (postgres.SetupFunc is a
// MigratorFunc), so one Migrator serves both per-test databases and templates.
type Migrator interface {
Migrate(ctx context.Context, dsn string) error
}
// MigratorFunc adapts a function to the Migrator interface.
type MigratorFunc func(ctx context.Context, dsn string) error
// Migrate calls f.
func (f MigratorFunc) Migrate(ctx context.Context, dsn string) error {
return f(ctx, dsn)
}
// Option is a functional option for configuring test databases.
type Option func(*Config)
// WithMigrator sets how each new test database is prepared before the test
// gets it. Setup and New run the migrator right after creating the database.
//
// Example:
//
// pool := postgres.Setup(t, testdb.WithMigrator(goose.Dir("./migrations")))
// pool := postgres.Setup(t, testdb.WithMigrator(postgres.SQLFiles("./schema")))
func WithMigrator(m Migrator) Option {
return func(c *Config) {
c.Migrator = m
}
}
// WithAdminDSN overrides the admin connection string.
// Use this when your database is not on localhost or uses non-default credentials.
//
// Most users don't need this - the library automatically discovers the admin DSN from
// environment variables (TEST_DATABASE_URL, DATABASE_URL) or uses sensible defaults.
//
// Example:
//
// testdb.WithAdminDSN("postgres://user:pass@db.example.com:5432/postgres")
func WithAdminDSN(dsn string) Option {
return func(c *Config) {
c.AdminDSNOverride = dsn
}
}
// WithDBPrefix sets the database name prefix.
// Useful for identifying test databases in a shared environment.
//
// Example:
//
// testdb.WithDBPrefix("myapp_test")
// // Results in database names like: myapp_test_1699564231_a1b2c3d4
func WithDBPrefix(prefix string) Option {
return func(c *Config) {
c.DBPrefix = prefix
}
}
// WithTemplate configures the test database to be created from a template database.
// This is significantly faster than creating a fresh database, especially for large
// schemas or databases with pre-seeded data.
//
// The template database must already exist (see postgres.EnsureTemplate).
// If the template doesn't exist, database creation will fail.
//
// Performance impact:
// - Fresh database: ~12-15ms
// - Template clone: ~3-5ms (3-5x faster)
//
// Common use cases:
// - Large SQL dumps (fixtures) loaded once, cloned per test
// - Different fixture sets for different test suites (us_region, eu_region)
// - Testing migrations from specific schema versions
//
// Example:
//
// db := postgres.New(t, &postgres.PoolInitializer{},
// testdb.WithTemplate("us_region"))
func WithTemplate(name string) Option {
return func(c *Config) {
c.TemplateName = name
}
}
// WithVerbose enables verbose logging of database operations.
// By default, testdb operates silently. Enable this for debugging.
//
// Example:
//
// testdb.WithVerbose()
func WithVerbose() Option {
return func(c *Config) {
c.Verbose = true
}
}
// WithKeepOnFailure keeps a test's database when the test fails, instead of
// dropping it, and logs a psql command for it. Passing tests still have their
// databases dropped. Use it to look at the data a failing test left behind.
//
// The TESTDB_KEEP_ON_FAILURE environment variable (1, true, yes or on) turns
// this on for a whole run without changing code:
//
// TESTDB_KEEP_ON_FAILURE=1 go test ./... -run TestCheckout
//
// Kept databases are yours to drop afterwards; WithDBPrefix makes them easy
// to spot. Leave this off in CI, where nobody is going to look and the
// databases would only pile up.
func WithKeepOnFailure() Option {
return func(c *Config) {
c.KeepOnFailure = true
}
}
// KeepOnFailureEnv is the environment variable that turns on KeepOnFailure
// for a run.
const KeepOnFailureEnv = "TESTDB_KEEP_ON_FAILURE"
// DefaultConfig returns a Config with reasonable defaults.
func DefaultConfig() Config {
return Config{
DBPrefix: "test",
KeepOnFailure: envBool(KeepOnFailureEnv),
}
}
// envBool reports whether an environment variable is set to a true-ish value.
func envBool(name string) bool {
switch strings.ToLower(strings.TrimSpace(os.Getenv(name))) {
case "1", "true", "yes", "on":
return true
default:
return false
}
}
// discoverAdminDSN attempts to discover the admin DSN from environment variables.
// It checks in order:
// 1. TEST_DATABASE_URL
// 2. DATABASE_URL
// 3. Returns empty string if neither is set
//
// This is an internal helper called by ResolveAdminDSN.
func discoverAdminDSN() string {
if dsn := os.Getenv("TEST_DATABASE_URL"); dsn != "" {
return dsn
}
if dsn := os.Getenv("DATABASE_URL"); dsn != "" {
return dsn
}
return ""
}
// ResolveAdminDSN resolves the admin DSN using a consistent priority order.
// This helper consolidates the DSN resolution logic to avoid duplication across
// database-specific providers (PostgreSQL, MySQL, SQLite, etc.).
//
// The library automatically discovers the admin DSN so users don't need to
// provide it unless they have custom connection requirements.
//
// Resolution order:
// 1. cfg.AdminDSNOverride (explicit user override via WithAdminDSN)
// 2. The provider's own environment variables, in the order given
// (e.g. TEST_POSTGRES_URL), so a project that tests against two
// databases can point each provider at its own server
// 3. TEST_DATABASE_URL environment variable
// 4. DATABASE_URL environment variable
// 5. defaultDSN (database-specific default)
//
// Example usage in provider initialization:
//
// adminDSN := testdb.ResolveAdminDSN(cfg,
// "postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable",
// "TEST_POSTGRES_URL")
func ResolveAdminDSN(cfg Config, defaultDSN string, providerEnv ...string) string {
if cfg.AdminDSNOverride != "" {
return cfg.AdminDSNOverride
}
for _, name := range providerEnv {
if dsn := os.Getenv(name); dsn != "" {
return dsn
}
}
if discovered := discoverAdminDSN(); discovered != "" {
return discovered
}
return defaultDSN
}
// NewDatabaseName returns a unique database name with the given prefix, in
// the form every test database gets: {prefix}_{timestamp}_{random}, for
// example test_1699564231_a1b2c3d4. Providers that prepare databases ahead
// of a test use it so those look like any other test database.
func NewDatabaseName(prefix string) (string, error) {
return generateDatabaseName(prefix)
}
// generateDatabaseName creates a unique database name with the given prefix.
// Format: {prefix}_{timestamp}_{random}
//
// Example: test_1699564231_a1b2c3d4
func generateDatabaseName(prefix string) (string, error) {
if prefix == "" {
prefix = "test"
}
// Use nanosecond timestamp for uniqueness
timestamp := time.Now().UnixNano()
randBytes := make([]byte, 4)
if _, err := rand.Read(randBytes); err != nil {
return "", fmt.Errorf("failed to generate random suffix: %w", err)
}
randSuffix := hex.EncodeToString(randBytes)
return fmt.Sprintf("%s_%d_%s", prefix, timestamp, randSuffix), nil
}
const (
// MaxDBPrefixLength is the maximum recommended length for database name prefixes.
//
// This limit is intentionally based on the most restrictive database to ensure
// consistent behavior across all supported databases:
// - PostgreSQL: 63 bytes (most restrictive)
// - MySQL: 64 characters
// - SQLite: effectively unlimited
//
// Database name format: prefix_timestamp_random (prefix + 29 chars)
// To avoid truncation: prefix + 29 <= 63, therefore prefix <= 34
//
// Design decision: I'm using the most restrictive limit (PostgreSQL's 63-byte limit)
// for all databases rather than implementing database-specific validation. This
// provides a consistent, safe experience and simplifies the API. A 34-character
// prefix is sufficient for all practical use cases.
MaxDBPrefixLength = 34
)
var (
// ErrNilProvider is returned when a nil provider is passed to New().
ErrNilProvider = errors.New("provider cannot be nil")
// ErrNoMigrator is returned when RunMigrations is called without a Migrator configured.
ErrNoMigrator = errors.New("no migrator configured")
// ErrTemplatesNotSupported is returned by New when WithTemplate is used
// with a provider that does not implement TemplateProvider.
ErrTemplatesNotSupported = errors.New("templates are not supported")
// ErrPrefixTooLong is returned when the database prefix would cause identifier truncation.
ErrPrefixTooLong = errors.New("database prefix too long: would exceed database identifier limit")
)
// Error represents a testdb error with operation context.
type Error struct {
// Op is the operation that failed (e.g., "provider.Initialize").
Op string
// Err is the underlying error.
Err error
}
func (e *Error) Error() string {
if e.Op != "" {
return fmt.Sprintf("testdb: %s: %v", e.Op, e.Err)
}
return fmt.Sprintf("testdb: %v", e.Err)
}
func (e *Error) Unwrap() error {
return e.Err
}
// validateConfig validates the configuration for consistency.
func validateConfig(cfg Config) error {
// Validate prefix length to prevent database identifier truncation.
// Database name format: prefix_timestamp_random (prefix + 29 chars)
// Limit based on most restrictive database (PostgreSQL: 63 bytes, MySQL: 64 chars).
// This intentionally applies to all databases (including SQLite which has no limit)
// to provide consistent behavior and a simple API.
if len(cfg.DBPrefix) > MaxDBPrefixLength {
return fmt.Errorf("%w (max %d characters, got %d)",
ErrPrefixTooLong, MaxDBPrefixLength, len(cfg.DBPrefix))
}
return nil
}