codegen

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2026 License: MIT Imports: 6 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 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 string) string

GenerateGoMod returns a go.mod file content string for the generated migrations module. moduleName is the module path (e.g. "myproject/migrations") and version is the makemigrations version to require (e.g. "v0.3.0").

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.

Jump to

Keyboard shortcuts

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