Skip to main content

internal/migrate/provision.go

internal/migrate · 117 lines · 6 declarations · source

Declarations

var scopePattern

var scopePattern = regexp.MustCompile(`^[a-z][a-z0-9_]{0,62}$`)

scopePattern is what a project identifier may contain.

A partition's name is built from it and cannot be parameterised, so this is the only thing between a project name and an injection. A strict allowlist rather than an escape routine: an allowlist fails closed on what it did not anticipate, an escape routine fails open.

source

func ProvisionMemorySchema

func ProvisionMemorySchema(ctx context.Context, pool *pgxpool.Pool, name string) error

ProvisionMemorySchema creates and upgrades an instance's memory namespace. Production binds one namespace for its lifetime; parameterization supports existing names and isolated test fixtures. The schema name prevents identifier injection. Database privileges, not naming, protect secrets.

source

var ErrProjectStorageConflict

var ErrProjectStorageConflict = errors.New("project storage conflicts with an existing relation")

ErrProjectStorageConflict refuses an existing relation that cannot serve this project's storage.

source

func ProvisionScope

func ProvisionScope(ctx context.Context, pool *pgxpool.Pool, namespace, scope string) error

ProvisionScope atomically creates a project's row and source-chunk partition. Vector indexes belong to explicit model generations, where their dimension is known. Repeated/concurrent calls validate existing storage instead of trusting its name. A failed statement rolls back all newly created storage. Unprovisioned writes remain in the default partition; moving existing default-partition data is a separate maintenance operation.

source

func projectStorageNames

func projectStorageNames(scope string) string

Short project names remain legible; long names use the full SHA-256 digest so a PostgreSQL identifier truncation cannot merge two projects. Actual partition attachment is still validated.

source

const findProjectPartitionSQL

const findProjectPartitionSQL = `
SELECT child.relname FROM pg_inherits inheritance
JOIN pg_class parent ON parent.oid=inheritance.inhparent
JOIN pg_namespace pn ON pn.oid=parent.relnamespace
JOIN pg_class child ON child.oid=inheritance.inhrelid
JOIN pg_namespace cn ON cn.oid=child.relnamespace
WHERE pn.nspname=$1 AND parent.relname='chunk' AND cn.nspname=$1
AND child.relispartition AND child.relkind='r'
AND pg_get_expr(child.relpartbound,child.oid)=format('FOR VALUES IN (%L)',$2::text)`

source