codegen

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package codegen generates Go source files for the migration framework. It converts yaml.SchemaDiff changes into compilable Go code that registers migrate.Migration objects via init() functions.

Package codegen generates Go source files for the migration framework.

Package codegen generates Go source files for the migration framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MigrationFileName

func MigrationFileName(name string) string

MigrationFileName returns the .go file name for a migration name.

func NextMigrationNumber

func NextMigrationNumber(count int) string

NextMigrationNumber returns a zero-padded 4-digit number for the next migration.

Types

type BlankGenerator added in v1.0.0

type BlankGenerator struct{}

BlankGenerator generates blank (empty) migration .go files. Blank migrations have no operations and contain a TODO comment as a placeholder for the developer to add custom operations.

func NewBlankGenerator added in v1.0.0

func NewBlankGenerator() *BlankGenerator

NewBlankGenerator creates a new BlankGenerator.

func (*BlankGenerator) GenerateBlank added in v1.0.0

func (g *BlankGenerator) GenerateBlank(name string, deps []string) (string, error)

GenerateBlank generates the source code for a blank migration .go file. name is the migration name (e.g. "0003_add_custom_sql"). deps is the list of migration names this migration depends on (typically the current DAG leaves).

The generated file has an empty Operations slice with a TODO comment so the developer can fill in custom operations such as &m.RunSQL{...}.

type DumpDataGenerator added in v1.1.0

type DumpDataGenerator struct{}

DumpDataGenerator generates Go migration source containing UpsertData operations.

func NewDumpDataGenerator added in v1.1.0

func NewDumpDataGenerator() *DumpDataGenerator

NewDumpDataGenerator creates a new DumpDataGenerator.

func (*DumpDataGenerator) Generate added in v1.1.0

func (g *DumpDataGenerator) Generate(name string, deps []string, tables []TableDump) (string, error)

Generate produces a complete .go migration file source containing UpsertData operations for each table dump. name is the migration name (e.g. "0003_dump_countries"), deps is the list of dependency migration names, and tables must contain at least one entry.

type GoGenerator

type GoGenerator struct{}

GoGenerator produces Go source code for migration files, main.go, and go.mod.

func NewGoGenerator

func NewGoGenerator() *GoGenerator

NewGoGenerator creates a new GoGenerator instance.

func (*GoGenerator) GenerateGoMod

func (g *GoGenerator) GenerateGoMod(moduleName, version, goVersion string) string

GenerateGoMod returns a go.mod file content string for the generated migrations module. moduleName is the module path (e.g. "myproject/migrations"), version is the makemigrations version to require (e.g. "v0.3.0" or "main"), and goVersion is the Go version to declare (e.g. "1.25", defaults to "1.24").

func (*GoGenerator) GenerateMainGo

func (g *GoGenerator) GenerateMainGo() string

GenerateMainGo returns the source for a migrations/main.go file that serves as the entry point for running migrations. It references m.NewApp and m.Config which are provided by the migrate package (implemented in Task 6).

func (*GoGenerator) GenerateMigration

func (g *GoGenerator) GenerateMigration(
	name string,
	deps []string,
	diff *yaml.SchemaDiff,
	currentSchema, previousSchema *yaml.Schema,
	decisions map[int]yaml.PromptResponse,
) (string, error)

GenerateMigration generates a complete .go file that registers a single Migration with the global registry via an init() function. The file is in package main and imports the migrate package aliased as "m". currentSchema and previousSchema are optional and used for AlterField operations when available.

decisions is an optional map keyed by change index that controls how destructive operations are emitted:

  • yaml.PromptOmit → operation is emitted with SchemaOnly: true (advances schema state without executing SQL in the database)
  • yaml.PromptReview → operation is preceded by a // REVIEW comment
  • yaml.PromptGenerate / yaml.PromptGenerateAll / nil entry → emitted normally

type MergeGenerator

type MergeGenerator struct{}

MergeGenerator generates merge migration .go files. Merge migrations have no operations — they exist only to establish a common ancestor for two divergent branches of the migration DAG.

func NewMergeGenerator

func NewMergeGenerator() *MergeGenerator

NewMergeGenerator creates a new MergeGenerator.

func (*MergeGenerator) GenerateMerge

func (g *MergeGenerator) GenerateMerge(name string, deps []string) (string, error)

GenerateMerge generates the source code for a merge migration .go file. name is the migration name (e.g. "0004_merge_feature_a_and_b"). deps is the list of branch leaf names to merge.

type SquashGenerator

type SquashGenerator struct{}

SquashGenerator generates squashed migration .go files. A squashed migration combines multiple migrations into one, listing the originals in its Replaces field so the runner can skip them if already applied.

func NewSquashGenerator

func NewSquashGenerator() *SquashGenerator

NewSquashGenerator creates a new SquashGenerator.

func (*SquashGenerator) GenerateSquash

func (g *SquashGenerator) GenerateSquash(
	name string,
	replaces []string,
	migrations []*migrate.Migration,
) (string, error)

GenerateSquash generates the source code for a squashed migration .go file. name is the new squashed migration name. replaces is the ordered list of migration names being replaced. migrations is the ordered list of Migration objects to squash.

type TableDump added in v1.1.0

type TableDump struct {
	Table        string           // target table name
	ConflictKeys []string         // PK or unique columns for ON CONFLICT
	Rows         []map[string]any // row data (all rows same keys)
}

TableDump holds the data for a single table to be upserted in a dump-data migration.

Jump to

Keyboard shortcuts

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