db

package
v1.0.0-alpha1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package db wires the entity model to a database connection.

Index

Constants

This section is empty.

Variables

View Source
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

func AutoMigrate(gdb *gorm.DB) error

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.

func Open

func Open(cfg Config, opts ...gorm.Option) (*gorm.DB, error)

Open connects to PostgreSQL and configures the underlying connection pool.

Types

type Config

type Config struct {
	DSN string

	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
}

Config describes the PostgreSQL connection, mirroring the spring.datasource.* and spring.datasource.hikari.* properties of the Java service.

func DefaultConfig

func DefaultConfig(dsn string) Config

DefaultConfig returns the pool settings the Java service used for its Hikari pool: a maximum of 20 connections with 2 kept idle.

type Migration

type Migration struct {
	Version     int64
	Description string
	Filename    string
	SQL         string
}

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

func NewMigrator(gdb *gorm.DB, migrations []Migration) *Migrator

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) Applied

func (m *Migrator) Applied(ctx context.Context) ([]int64, error)

Applied returns the versions already recorded as applied, ascending.

func (*Migrator) Pending

func (m *Migrator) Pending(ctx context.Context) ([]Migration, error)

Pending returns the embedded migrations not yet applied, ascending.

func (*Migrator) Status

func (m *Migrator) Status(ctx context.Context) ([]Status, error)

Status reports every known migration's applied state, ascending by version.

func (*Migrator) Up

func (m *Migrator) Up(ctx context.Context) ([]Migration, error)

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.)

type Status

type Status struct {
	Version     int64
	Description string
	Applied     bool
	AppliedAt   *time.Time
}

Status is one migration's applied state.

Jump to

Keyboard shortcuts

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