Documentation
¶
Overview ¶
Package postgres provides the opt-in PostgreSQL driver adapter for the database module, the standard cloud backend alongside the local sqlite adapter.
Importing this package has no side effects. Call Register with an explicit database.DialectRegistry before selecting it, and use MigrateDriver to wire golang-migrate against the same connection.
Index ¶
- Constants
- func ApplySchema(ctx context.Context, migrationDB *gorm.DB, plan SchemaPlan) (err error)
- func Dialect() database.Dialect
- func Migrate(ctx context.Context, log *logging.Logger, m SchemaMigration) (err error)
- func MigrateDriver() migration.DriverFunc
- func MigrationBackend() migration.SQLBackend
- func Prepare(ctx context.Context, input database.ConnectionInput) (database.Opener, error)
- func Readiness(plan SchemaPlan) database.Check
- func ReadyAccess(ctx context.Context, runtimeDB *gorm.DB, plan SchemaPlan) error
- func Register(reg *database.DialectRegistry) error
- func SetLocal(ctx context.Context, tx *gorm.DB, name, value string) error
- type Privilege
- type RelationAccess
- type RuntimeAccess
- type SchemaMigration
- type SchemaPlan
Constants ¶
const DefaultSetBudget = 2 * time.Minute
DefaultSetBudget bounds the migration of one set when SchemaMigration.SetBudget is zero.
const Name = "postgres"
Name is the registry key for the PostgreSQL backend.
Variables ¶
This section is empty.
Functions ¶
func ApplySchema ¶
ApplySchema provisions plan with the migration role on one pinned connection while holding the migration advisory lock: it audits the existing runtime role, plans every set, creates the schema owned by the current role when it is absent, applies every set to its target, reconciles grants transactionally to the exact manifest, and verifies the runtime role's effective privileges before committing the grants. It never creates or edits roles. It works on a pool limited to one connection. Rerunning is idempotent; a failure leaves runtime readiness unavailable.
func Migrate ¶
Migrate opens a short-lived connection with m.Config, applies m.Plan through ApplySchema, and closes the connection, all within m.SetBudget for each set in the plan.
func MigrateDriver ¶
func MigrateDriver() migration.DriverFunc
MigrateDriver creates a context-bound migration session with a database-scoped advisory lock shared by every migration set, so concurrent sets serialize.
func MigrationBackend ¶
func MigrationBackend() migration.SQLBackend
MigrationBackend returns the PostgreSQL SQL backend for coordinated sessions (see migration.Begin).
func Prepare ¶
Prepare resolves explicit credentials and builds a parser-owned pgx configuration without allocating connections. PostgreSQL opaque input is a credential-free URL; structured parameters carry passwords.
func Readiness ¶
func Readiness(plan SchemaPlan) database.Check
Readiness returns a database.Check for database.Component.WithReadiness that requires every set of plan at its expected clean version, then ReadyAccess for the runtime session. It is read-only: no metadata creation, locks or migrations, so a component with this check fails Start before migration and reports drift from Health.
func ReadyAccess ¶
ReadyAccess verifies, as the runtime role on runtimeDB, that the session user is the plan's runtime role with exactly the manifest's privileges: schema USAGE without CREATE, no database CREATE or TEMP, the declared relation and sequence rights, and SELECT-only version tables. It runs the same role and undeclared-object audit as ApplySchema, so every state ApplySchema rejects also fails readiness. It reads catalogs only; it never locks, migrates or creates metadata.
func Register ¶
func Register(reg *database.DialectRegistry) error
Register registers the PostgreSQL dialect in an explicit database registry.
func SetLocal ¶
SetLocal sets a custom setting, such as one read by row-level security policies through current_setting, for the remainder of tx's transaction only. name must be a two-part lowercase custom setting ("app.partition"); value is bound. It rejects a handle that is not in a transaction, so a pooled session never retains the value.
Optional row-level security still requires USING and WITH CHECK policies and a non-owner runtime role; a setting alone isolates nothing.
Types ¶
type Privilege ¶
type Privilege string
Privilege is a table privilege a runtime role may hold on an application relation.
type RelationAccess ¶
RelationAccess grants the runtime role exactly Privileges on one application table or view of the plan schema.
type RuntimeAccess ¶
type RuntimeAccess struct {
Role string
Relations []RelationAccess
Sequences []string
}
RuntimeAccess is the runtime role's complete allowlist in one schema. Sequences receive USAGE only, for serial inserts. Every version table of the plan receives SELECT only. Anything not listed receives nothing; there are no default or future-table grants, so each migration that adds a relation must update the manifest.
type SchemaMigration ¶
type SchemaMigration struct {
// Config connects as the migration role. One connection is enough; [ApplySchema] pins one.
Config database.Config
Plan SchemaPlan
// SetBudget bounds the whole migration per set in Plan; zero means [DefaultSetBudget].
SetBudget time.Duration
// Options apply when connecting, such as a secret source for a password file.
Options []database.Option
}
SchemaMigration is one schema plan applied with a migration login.
type SchemaPlan ¶
type SchemaPlan struct {
Name string
Sets []migration.Set
RuntimeAccess RuntimeAccess
}
SchemaPlan is one application schema, the migration sets whose metadata it holds, and its runtime access.
func (SchemaPlan) Validate ¶
func (p SchemaPlan) Validate() error
Validate checks every identifier and target before any database work. Identifiers follow migration.IsIdentifier.