database

package
v0.0.0-...-3e91960 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Code generated by forge. DO NOT EDIT. forge-owned: regenerated every run — do not edit (forge disown to take ownership) Source: contract.go in this package.

To customize: edit contract.go (the interface IS the public surface) and re-run "forge generate". This file is regenerated unconditionally.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnectDB

func ConnectDB(ctx context.Context, dsn string) (*sql.DB, error)

ConnectDB opens a database connection using the pgx driver.

func CreateMigration

func CreateMigration(ctx context.Context, name, dir string, opts *MigrationOptions) error

CreateMigration creates a new SQL migration pair using golang-migrate's timestamped filename convention. When opts is non-nil, it gathers schema context and writes a rich comment block into the .up.sql file.

func FormatSchemaJSON

func FormatSchemaJSON(tables []Table) (string, error)

FormatSchemaJSON formats schema as JSON.

func FormatSchemaText

func FormatSchemaText(tables []Table) string

FormatSchemaText formats schema as a human-readable text table.

func GenerateContextComment

func GenerateContextComment(ctx *MigrationContext) string

GenerateContextComment builds the full SQL comment block for a new migration.

func GetMigrationHistory

func GetMigrationHistory(dir string) ([]string, error)

GetMigrationHistory returns a sorted list of all migration filenames (just .up.sql).

Types

type Column

type Column struct {
	Name      string `json:"name"`
	Type      string `json:"type"`
	Nullable  bool   `json:"nullable"`
	Default   string `json:"default,omitempty"`
	IsPrimary bool   `json:"is_primary"`
	MaxLength *int   `json:"max_length,omitempty"`
	UDTName   string `json:"udt_name,omitempty"`
}

Column represents a database column.

type ForeignKey

type ForeignKey struct {
	Name             string `json:"name"`
	Column           string `json:"column"`
	ReferencedTable  string `json:"referenced_table"`
	ReferencedColumn string `json:"referenced_column"`
}

ForeignKey represents a foreign key constraint.

type Index

type Index struct {
	Name    string   `json:"name"`
	Columns []string `json:"columns"`
	Unique  bool     `json:"unique"`
}

Index represents a database index.

type MigrationContext

type MigrationContext struct {
	MigrationName     string
	CreatedAt         time.Time
	ParsedTables      []ParsedTable // Schema reconstructed from existing migrations
	LiveTables        []Table       // Schema from live DB introspection (if --dsn)
	PreviousMigration *PreviousMigrationInfo
	MigrationHistory  []string // List of all previous migration filenames
}

MigrationContext holds all gathered context for generating rich migration comments.

func GatherMigrationContext

func GatherMigrationContext(ctx context.Context, name, migDir string, opts MigrationOptions) (*MigrationContext, error)

GatherMigrationContext collects all available context for a new migration.

type MigrationOptions

type MigrationOptions struct {
	DSN string // If set, introspect the live DB for schema
}

MigrationOptions controls what context is included when creating a new migration.

type MigrationService

type MigrationService interface {
	CreateMigration(ctx context.Context, name string, dir string, opts *MigrationOptions) error
	GatherMigrationContext(ctx context.Context, name string, migDir string, opts MigrationOptions) (*MigrationContext, error)
	GetPreviousMigration(ctx context.Context, dir string) (*PreviousMigrationInfo, error)
	GetMigrationHistory(ctx context.Context, dir string) ([]string, error)
}

MigrationService creates and manages database migrations.

type MockMigrationService

type MockMigrationService struct {
	contractkit.Recorder
	CreateMigrationFunc        func(context.Context, string, string, *MigrationOptions) error
	GatherMigrationContextFunc func(context.Context, string, string, MigrationOptions) (*MigrationContext, error)
	GetPreviousMigrationFunc   func(context.Context, string) (*PreviousMigrationInfo, error)
	GetMigrationHistoryFunc    func(context.Context, string) ([]string, error)
}

MockMigrationService is a test mock for the MigrationService interface.

The embedded contractkit.Recorder records every call so tests can assert call counts and captured arguments. Set XxxFunc fields to override per-method behaviour; unset methods return the canonical "MockMigrationService.<Method>Func not set" error.

func (*MockMigrationService) CreateMigration

func (m *MockMigrationService) CreateMigration(ctx context.Context, name string, dir string, opts *MigrationOptions) error

func (*MockMigrationService) GatherMigrationContext

func (m *MockMigrationService) GatherMigrationContext(ctx context.Context, name string, migDir string, opts MigrationOptions) (*MigrationContext, error)

func (*MockMigrationService) GetMigrationHistory

func (m *MockMigrationService) GetMigrationHistory(ctx context.Context, dir string) ([]string, error)

func (*MockMigrationService) GetPreviousMigration

func (m *MockMigrationService) GetPreviousMigration(ctx context.Context, dir string) (*PreviousMigrationInfo, error)

type MockSchemaIntrospector

type MockSchemaIntrospector struct {
	contractkit.Recorder
	ConnectDBFunc        func(context.Context, string) (*sql.DB, error)
	IntrospectSchemaFunc func(context.Context, *sql.DB, string) ([]Table, error)
	IntrospectTableFunc  func(context.Context, *sql.DB, string) (*Table, error)
}

MockSchemaIntrospector is a test mock for the SchemaIntrospector interface.

The embedded contractkit.Recorder records every call so tests can assert call counts and captured arguments. Set XxxFunc fields to override per-method behaviour; unset methods return the canonical "MockSchemaIntrospector.<Method>Func not set" error.

func (*MockSchemaIntrospector) ConnectDB

func (m *MockSchemaIntrospector) ConnectDB(ctx context.Context, dsn string) (*sql.DB, error)

func (*MockSchemaIntrospector) IntrospectSchema

func (m *MockSchemaIntrospector) IntrospectSchema(ctx context.Context, db *sql.DB, tableFilter string) ([]Table, error)

func (*MockSchemaIntrospector) IntrospectTable

func (m *MockSchemaIntrospector) IntrospectTable(ctx context.Context, db *sql.DB, tableName string) (*Table, error)

type ParsedColumn

type ParsedColumn struct {
	Name       string
	Type       string
	Constraint string // e.g. "PRIMARY KEY", "NOT NULL", "UNIQUE", "REFERENCES users(id)"
}

ParsedColumn represents a column extracted from a CREATE TABLE or ALTER TABLE statement.

type ParsedTable

type ParsedTable struct {
	Name    string
	Columns []ParsedColumn
}

ParsedTable represents a table reconstructed from parsing migration SQL files.

func ParseMigrationsForSchema

func ParseMigrationsForSchema(dir string) ([]ParsedTable, error)

ParseMigrationsForSchema reads all .up.sql files in dir (sorted by name) and reconstructs a best-effort schema by parsing CREATE TABLE and ALTER TABLE statements.

type PreviousMigrationInfo

type PreviousMigrationInfo struct {
	Filename string
	Content  string
}

PreviousMigrationInfo holds information about the most recent migration.

func GetPreviousMigration

func GetPreviousMigration(dir string) (*PreviousMigrationInfo, error)

GetPreviousMigration reads the most recent .up.sql file from the migrations dir.

type SchemaIntrospector

type SchemaIntrospector interface {
	ConnectDB(ctx context.Context, dsn string) (*sql.DB, error)
	IntrospectSchema(ctx context.Context, db *sql.DB, tableFilter string) ([]Table, error)
	IntrospectTable(ctx context.Context, db *sql.DB, tableName string) (*Table, error)
}

SchemaIntrospector connects to databases and introspects their schema.

type Table

type Table struct {
	Name        string       `json:"name"`
	Schema      string       `json:"schema"`
	Columns     []Column     `json:"columns"`
	PrimaryKey  []string     `json:"primary_key"`
	Indexes     []Index      `json:"indexes"`
	ForeignKeys []ForeignKey `json:"foreign_keys"`
}

Table represents a database table with its full schema metadata.

func IntrospectSchema

func IntrospectSchema(ctx context.Context, db *sql.DB, tableFilter string) ([]Table, error)

IntrospectSchema connects to the database and returns the full schema. If tableFilter is non-empty, only that table is returned.

func IntrospectTable

func IntrospectTable(ctx context.Context, db *sql.DB, tableName string) (*Table, error)

IntrospectTable returns full metadata for a single table.

Jump to

Keyboard shortcuts

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