databases

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package databases manages Postgres and Redis database provisioning.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateName

func ValidateName(name string) error

ValidateName checks that a database name is safe for use as a Docker container/volume suffix: 1-63 characters, starts with a letter, only alphanumeric plus hyphens/underscores, cannot end with a hyphen.

Types

type CreateRequest

type CreateRequest struct {
	Name string `json:"name"`
	Type string `json:"type"` // "postgres" or "redis"
}

CreateRequest holds parameters for creating a database.

type Database

type Database struct {
	ID               string `json:"id"`
	TenantID         string `json:"tenant_id"`
	Name             string `json:"name"`
	Type             string `json:"type"`
	Status           string `json:"status"`
	ContainerID      string `json:"-"`
	Host             string `json:"host,omitempty"`
	Port             int    `json:"port,omitempty"`
	DBName           string `json:"db_name,omitempty"`
	Username         string `json:"username,omitempty"`
	ConnectionString string `json:"connection_string,omitempty"` // only populated on create + explicit get
	VolumeName       string `json:"-"`
	CreatedAt        int64  `json:"created_at"`
	UpdatedAt        int64  `json:"updated_at"`
}

Database represents a provisioned database.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages database lifecycle.

func NewManager

func NewManager(db *sql.DB, dockerClient docker.Client, masterKey []byte) *Manager

NewManager creates a database manager.

func (*Manager) Create

func (m *Manager) Create(ctx context.Context, tenantID string, req CreateRequest) (*Database, error)

Create provisions a new database.

func (*Manager) Delete

func (m *Manager) Delete(ctx context.Context, tenantID, dbID string) error

Delete destroys a database: stops container, removes volume, deletes record. Only deletes the DB record after Docker cleanup succeeds to avoid orphaning resources.

func (*Manager) Get

func (m *Manager) Get(ctx context.Context, tenantID, dbID string) (*Database, error)

Get returns a database by ID, scoped to tenant. Connection string NOT included.

func (*Manager) GetConnectionString

func (m *Manager) GetConnectionString(ctx context.Context, tenantID, dbID string) (string, error)

GetConnectionString returns the decrypted connection string.

func (*Manager) List

func (m *Manager) List(ctx context.Context, tenantID string) ([]*Database, error)

List returns all databases for a tenant. Connection strings NOT included.

func (*Manager) ListPaginated

func (m *Manager) ListPaginated(ctx context.Context, tenantID string, limit, offset int) ([]*Database, error)

ListPaginated returns databases for a tenant with limit and offset.

func (*Manager) ReconcileStale

func (m *Manager) ReconcileStale()

ReconcileStale marks databases stuck in "provisioning" as "failed" and attempts to clean up their Docker resources. Called on startup.

func (*Manager) StartAllForTenant

func (m *Manager) StartAllForTenant(ctx context.Context, tenantID string)

StartAllForTenant recreates the containers of a tenant's stopped databases (see StopAllForTenant) from the stored config, reusing each database's volume, port and password. Failures mark that database "failed" and are logged; the remaining databases are still processed.

func (*Manager) StopAllForTenant

func (m *Manager) StopAllForTenant(ctx context.Context, tenantID string)

StopAllForTenant stops and removes all database containers for the tenant, updating each record's status to "stopped". The database records are kept so they can be restarted if the tenant is ever reactivated. Errors are logged but do not abort processing of remaining databases.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL