Documentation
ΒΆ
Overview ΒΆ
Package sqliteproxy provides a multi-tenant SQLite database proxy that routes queries to organization-specific databases based on context.
This package enables seamless multi-tenancy for SQLite applications while maintaining complete compatibility with the standard database/sql interface.
Index ΒΆ
- type Migration
- type MigrationMode
- type MigrationStatus
- type Migrator
- func (m *Migrator) DryRun(ctx context.Context, orgID string) ([]Migration, error)
- func (m *Migrator) MigrateAll(ctx context.Context) error
- func (m *Migrator) MigrateBatch(ctx context.Context, orgIDs []string) error
- func (m *Migrator) MigrateOrg(ctx context.Context, orgID string) error
- func (m *Migrator) Rollback(ctx context.Context, orgID string, targetVersion int) error
- func (m *Migrator) Status(ctx context.Context) ([]MigrationStatus, error)
- func (m *Migrator) StatusForOrg(ctx context.Context, orgID string) (*MigrationStatus, error)
- type OrgIDKey
- type SQLiteProxy
- func (p *SQLiteProxy) Begin() (*sql.Tx, error)
- func (p *SQLiteProxy) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
- func (p *SQLiteProxy) Close() error
- func (p *SQLiteProxy) CloseWithContext(ctx context.Context) error
- func (p *SQLiteProxy) CloseWithTimeout(timeout time.Duration) error
- func (p *SQLiteProxy) Conn(ctx context.Context) (*sql.Conn, error)
- func (p *SQLiteProxy) Exec(query string, args ...interface{}) (sql.Result, error)
- func (p *SQLiteProxy) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (p *SQLiteProxy) Migrator() *Migrator
- func (p *SQLiteProxy) Ping() error
- func (p *SQLiteProxy) PingContext(ctx context.Context) error
- func (p *SQLiteProxy) Prepare(query string) (*sql.Stmt, error)
- func (p *SQLiteProxy) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
- func (p *SQLiteProxy) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (p *SQLiteProxy) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (p *SQLiteProxy) QueryRow(query string, args ...interface{}) *sql.Row
- func (p *SQLiteProxy) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (p *SQLiteProxy) Stats() StatsInfo
- type SQLiteProxyOption
- func WithConnMaxIdleTime(d time.Duration) SQLiteProxyOption
- func WithConnMaxLifetime(d time.Duration) SQLiteProxyOption
- func WithDSNResolver(resolver func(orgID, baseDsnInfo string) string) SQLiteProxyOption
- func WithEviction(maxConnections int, evictionInterval, idleTimeout time.Duration) SQLiteProxyOption
- func WithLogger(logger *slog.Logger) SQLiteProxyOption
- func WithMaxIdleConns(n int) SQLiteProxyOption
- func WithMaxOpenConns(n int) SQLiteProxyOption
- func WithMigrationMode(mode MigrationMode) SQLiteProxyOption
- func WithMigrations(migrations ...Migration) SQLiteProxyOption
- func WithOrgIDKey(key OrgIDKey) SQLiteProxyOption
- func WithQueryTimeout(timeout time.Duration) SQLiteProxyOption
- func WithResourceLimits(maxTotalConnections int64, maxMemoryPerTenant, maxTotalMemory int64) SQLiteProxyOption
- func WithShutdownTimeout(timeout time.Duration) SQLiteProxyOption
- func WithTelemetry(traceProvider trace.TracerProvider, meterProvider metric.MeterProvider) SQLiteProxyOption
- type StatsInfo
Constants ΒΆ
This section is empty.
Variables ΒΆ
This section is empty.
Functions ΒΆ
This section is empty.
Types ΒΆ
type Migration ΒΆ
type Migration struct {
Version int
Name string
Up string // SQL to apply the migration
Down string // SQL to rollback the migration (optional)
Description string // Human-readable description
}
Migration represents a single schema migration
type MigrationMode ΒΆ
type MigrationMode int
MigrationMode defines how migrations are applied
const ( // MigrationModeAuto automatically runs migrations on first connection (default) MigrationModeAuto MigrationMode = iota // MigrationModeManual requires explicit migration calls MigrationModeManual // MigrationModeCheck verifies schema version, fails if outdated MigrationModeCheck )
type MigrationStatus ΒΆ
type MigrationStatus struct {
OrgID string
CurrentVersion int
TargetVersion int
IsUpToDate bool
AppliedAt *time.Time
}
MigrationStatus represents the current migration state
type Migrator ΒΆ
type Migrator struct {
// contains filtered or unexported fields
}
Migrator handles schema migrations for the proxy
func (*Migrator) MigrateAll ΒΆ
MigrateAll applies pending migrations to all organizations
func (*Migrator) MigrateBatch ΒΆ
MigrateBatch applies pending migrations to a specific set of organizations
func (*Migrator) MigrateOrg ΒΆ
MigrateOrg applies pending migrations to a specific organization
func (*Migrator) Rollback ΒΆ
Rollback rolls back to a specific version (if Down migrations are provided)
func (*Migrator) Status ΒΆ
func (m *Migrator) Status(ctx context.Context) ([]MigrationStatus, error)
Status returns the migration status for all organizations
func (*Migrator) StatusForOrg ΒΆ
StatusForOrg returns the migration status for a specific organization
type OrgIDKey ΒΆ
type OrgIDKey string
OrgIDKey is the context key for storing organization ID
const ( // DefaultOrgIDKey is the default context key for the organization ID DefaultOrgIDKey OrgIDKey = "organization_id" )
type SQLiteProxy ΒΆ
type SQLiteProxy struct {
// contains filtered or unexported fields
}
SQLiteProxy implements most of the *sql.DB interface methods but routes queries to organization-specific databases based on context
func NewSQLiteProxy ΒΆ
func NewSQLiteProxy(directoryOrDSN string, options ...SQLiteProxyOption) *SQLiteProxy
NewSQLiteProxy creates a new proxy with the given base directory or DSN template If directoryOrDSN is a directory, each organization will get a file in that directory If it's a DSN template, it will be used with the DSN resolver
func (*SQLiteProxy) Begin ΒΆ
func (p *SQLiteProxy) Begin() (*sql.Tx, error)
Begin starts a transaction with default options However, since we need organization context, we require the user to use BeginTx
func (*SQLiteProxy) Close ΒΆ
func (p *SQLiteProxy) Close() error
Close performs graceful shutdown of the proxy with default timeout
func (*SQLiteProxy) CloseWithContext ΒΆ
func (p *SQLiteProxy) CloseWithContext(ctx context.Context) error
CloseWithContext performs graceful shutdown using the provided context
func (*SQLiteProxy) CloseWithTimeout ΒΆ
func (p *SQLiteProxy) CloseWithTimeout(timeout time.Duration) error
CloseWithTimeout performs graceful shutdown with a custom timeout
func (*SQLiteProxy) Exec ΒΆ
func (p *SQLiteProxy) Exec(query string, args ...interface{}) (sql.Result, error)
Exec is a convenience method that calls ExecContext with a background context However, since we need organization context, we require the user to use ExecContext
func (*SQLiteProxy) ExecContext ΒΆ
func (p *SQLiteProxy) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
ExecContext executes a query with the organization from the context
func (*SQLiteProxy) Migrator ΒΆ
func (p *SQLiteProxy) Migrator() *Migrator
Migrator returns a Migrator instance for managing migrations
func (*SQLiteProxy) Ping ΒΆ
func (p *SQLiteProxy) Ping() error
Ping verifies that the connections are available by creating a connection to a test organization
func (*SQLiteProxy) PingContext ΒΆ
func (p *SQLiteProxy) PingContext(ctx context.Context) error
PingContext verifies that connections can be established
func (*SQLiteProxy) Prepare ΒΆ
func (p *SQLiteProxy) Prepare(query string) (*sql.Stmt, error)
Prepare is a convenience method that calls PrepareContext with a background context However, since we need organization context, we require the user to use PrepareContext
func (*SQLiteProxy) PrepareContext ΒΆ
PrepareContext prepares a statement for later queries or executions
func (*SQLiteProxy) Query ΒΆ
func (p *SQLiteProxy) Query(query string, args ...interface{}) (*sql.Rows, error)
Query is a convenience method that calls QueryContext with a background context However, since we need organization context, we require the user to use QueryContext
func (*SQLiteProxy) QueryContext ΒΆ
func (p *SQLiteProxy) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryContext executes a query with the organization from the context Note: No timeout is applied to Query operations because they return lazy *sql.Rows that may be scanned much later. Apply timeouts at the application level if needed.
func (*SQLiteProxy) QueryRow ΒΆ
func (p *SQLiteProxy) QueryRow(query string, args ...interface{}) *sql.Row
QueryRow is a convenience method that calls QueryRowContext with a background context However, since we need organization context, we require the user to use QueryRowContext
func (*SQLiteProxy) QueryRowContext ΒΆ
func (p *SQLiteProxy) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
QueryRowContext executes a query that returns a single row Note: No timeout is applied to QueryRow operations because they return lazy *sql.Row that may be scanned much later. Apply timeouts at the application level if needed.
func (*SQLiteProxy) Stats ΒΆ
func (p *SQLiteProxy) Stats() StatsInfo
Stats returns current connection statistics
type SQLiteProxyOption ΒΆ
type SQLiteProxyOption func(*SQLiteProxy)
SQLiteProxyOption configures the SQLiteProxy
func WithConnMaxIdleTime ΒΆ
func WithConnMaxIdleTime(d time.Duration) SQLiteProxyOption
WithConnMaxIdleTime sets the maximum idle time for connections
func WithConnMaxLifetime ΒΆ
func WithConnMaxLifetime(d time.Duration) SQLiteProxyOption
WithConnMaxLifetime sets the maximum lifetime of connections
func WithDSNResolver ΒΆ
func WithDSNResolver(resolver func(orgID, baseDsnInfo string) string) SQLiteProxyOption
WithDSNResolver sets a custom DSN resolver function
func WithEviction ΒΆ
func WithEviction(maxConnections int, evictionInterval, idleTimeout time.Duration) SQLiteProxyOption
WithEviction enables connection eviction with the specified parameters
func WithLogger ΒΆ
func WithLogger(logger *slog.Logger) SQLiteProxyOption
WithLogger sets a custom structured logger
func WithMaxIdleConns ΒΆ
func WithMaxIdleConns(n int) SQLiteProxyOption
WithMaxIdleConns sets the maximum number of idle connections per database
func WithMaxOpenConns ΒΆ
func WithMaxOpenConns(n int) SQLiteProxyOption
WithMaxOpenConns sets the maximum number of open connections per database
func WithMigrationMode ΒΆ
func WithMigrationMode(mode MigrationMode) SQLiteProxyOption
WithMigrationMode sets how migrations are applied
func WithMigrations ΒΆ
func WithMigrations(migrations ...Migration) SQLiteProxyOption
WithMigrations configures the proxy with schema migrations
func WithOrgIDKey ΒΆ
func WithOrgIDKey(key OrgIDKey) SQLiteProxyOption
WithOrgIDKey sets the context key used to extract organization ID
func WithQueryTimeout ΒΆ
func WithQueryTimeout(timeout time.Duration) SQLiteProxyOption
WithQueryTimeout sets a timeout that applies to all database operations If timeout is 0, no timeout is applied (database/sql default behavior)
func WithResourceLimits ΒΆ
func WithResourceLimits(maxTotalConnections int64, maxMemoryPerTenant, maxTotalMemory int64) SQLiteProxyOption
WithResourceLimits sets global resource limits
func WithShutdownTimeout ΒΆ
func WithShutdownTimeout(timeout time.Duration) SQLiteProxyOption
WithShutdownTimeout sets the graceful shutdown timeout
func WithTelemetry ΒΆ
func WithTelemetry(traceProvider trace.TracerProvider, meterProvider metric.MeterProvider) SQLiteProxyOption
WithTelemetry enables OpenTelemetry instrumentation
