Documentation
¶
Overview ¶
Package db wires the entity model to a database connection.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var NamingStrategy = schema.NamingStrategy{IdentifierMaxLength: 63}
NamingStrategy is the identifier naming every connection uses.
The only departure from GORM's default is the length cap. GORM truncates a generated index or constraint name to 64 characters and appends a hash; PostgreSQL truncates identifiers at 63 bytes. Left at the default, the longest names this schema produces — the sharing tables' foreign keys — would be silently shortened by the server, and two names that differ only in their final character would collide. Capping at 63 keeps the name GORM generates and the name the server stores identical.
Functions ¶
func AutoMigrate ¶
AutoMigrate creates or updates the schema to match the entity model. It is the counterpart to the Java service's spring.jpa.hibernate.ddl-auto setting.
Like ddl-auto, this is a development convenience: it adds tables, columns and indexes but never drops or narrows anything. Production schema changes belong in versioned migrations.
func Entities ¶
func Entities() []any
Entities lists every persistent model, ordered so that a table is always created after everything its foreign keys point at. AutoMigrate resolves dependencies on its own, but keeping the order explicit makes the reference graph readable and the generated DDL deterministic.
Types ¶
type Config ¶
Config describes the PostgreSQL connection, mirroring the spring.datasource.* and spring.datasource.hikari.* properties of the Java service.
func DefaultConfig ¶
DefaultConfig returns the pool settings the Java service used for its Hikari pool: a maximum of 20 connections with 2 kept idle.
type Migration ¶
Migration is one versioned, embedded schema change.
func Migrations ¶
func Migrations() []Migration
Migrations returns every embedded migration under migrations/, ordered by version ascending.
It panics on a malformed filename or a duplicate version rather than returning an error: both are authoring mistakes caught by any test that calls this, not conditions a production caller could sensibly recover from.
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator applies versioned migrations and tracks which have run, in the schema_migrations table.
Migrations are forward-only, matching the AutoMigrate philosophy this framework exists to replace in production: a mistake is corrected with a new migration, not a down script that would have to reconstruct dropped data anyway.
func NewMigrator ¶
NewMigrator returns a migrator over the given, explicit migration set — Migrations() for the real embedded set in production, or a small hand-built slice in a test, so the runner's mechanics can be exercised without depending on the committed SQL files or a PostgreSQL-specific dialect.
The set is sorted by version here, so every method applies and reports migrations in order regardless of the order the caller happened to list them in.
func (*Migrator) Status ¶
Status reports every known migration's applied state, ascending by version.
func (*Migrator) Up ¶
Up applies every pending migration in order and returns the ones it applied.
Each migration runs in its own transaction, and on PostgreSQL that covers the DDL as well as the bookkeeping insert: a file that fails partway through leaves the schema as it was, so a failed migration can be fixed and re-run rather than needing a repair migration for whatever it managed to apply. (This is one of the things that changed with the move off MariaDB, where InnoDB committed each DDL statement regardless of the surrounding transaction.)