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.
schema_dsl_generator.go converts a *types.Schema into a Starlark schema DSL (.star) file. It reuses the field/index/fk formatting helpers from starlark_generator.go.
Package codegen generates Go source files for the migration framework.
starlark_converter.go converts in-memory migrate.Migration objects (loaded via Yaegi) into Starlark .star source files. This enables format conversion from Go migrations to Starlark without re-parsing Go source text.
starlark_generator.go produces .star migration scripts from yaml.SchemaDiff, mirroring GoGenerator but emitting Starlark syntax with named arguments.
Index ¶
- func ConvertMigrationToStarlark(m *migrate.Migration) (string, error)
- func GenerateSchemaDSL(schema *types.Schema) (string, error)
- func MigrationFileName(name string) string
- func MigrationFileNameForFormat(name string, format MigrationFormat) string
- func NextMigrationNumber(count int) string
- type BlankGenerator
- type DumpDataGenerator
- type GoGenerator
- func (g *GoGenerator) FileExtension() string
- func (g *GoGenerator) GenerateBlank(name string, deps []string) (string, error)
- func (g *GoGenerator) GenerateGoMod(moduleName, version, goVersion string) string
- func (g *GoGenerator) GenerateMainGo() string
- func (g *GoGenerator) GenerateMigration(name string, deps []string, diff *yaml.SchemaDiff, ...) (string, error)
- type MergeGenerator
- type MigrationFormat
- type MigrationGenerator
- type SquashGenerator
- type StarlarkGenerator
- type TableDump
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConvertMigrationToStarlark ¶ added in v1.9.0
ConvertMigrationToStarlark takes a migrate.Migration (typically loaded via Yaegi from a .go file) and returns the equivalent .star source code.
func GenerateSchemaDSL ¶ added in v1.9.0
GenerateSchemaDSL converts a *types.Schema into the Starlark schema DSL source text. The output is a complete .star file that can be evaluated by SchemaDSLBuiltins to reconstruct the same schema.
func MigrationFileName ¶
MigrationFileName returns the .go file name for a migration name.
func MigrationFileNameForFormat ¶ added in v1.9.0
func MigrationFileNameForFormat(name string, format MigrationFormat) string
MigrationFileNameForFormat returns the file name for a migration in the given format.
func NextMigrationNumber ¶
NextMigrationNumber returns a zero-padded 4-digit number for the next migration.
Types ¶
type BlankGenerator ¶
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 ¶
func NewBlankGenerator() *BlankGenerator
NewBlankGenerator creates a new BlankGenerator.
func (*BlankGenerator) GenerateBlank ¶
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 ¶
type DumpDataGenerator struct{}
DumpDataGenerator generates Go migration source containing UpsertData operations.
func NewDumpDataGenerator ¶
func NewDumpDataGenerator() *DumpDataGenerator
NewDumpDataGenerator creates a new DumpDataGenerator.
func (*DumpDataGenerator) Generate ¶
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) FileExtension ¶ added in v1.9.0
func (g *GoGenerator) FileExtension() string
FileExtension returns ".go".
func (*GoGenerator) GenerateBlank ¶ added in v1.9.0
func (g *GoGenerator) GenerateBlank(name string, deps []string) (string, error)
GenerateBlank generates a blank migration .go file with a TODO placeholder.
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 morphic 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. The file is **optional at runtime** — `morphic migrate` interprets the migration .go files in-process via yaegi and never invokes main(). It is generated so users can still `go build` the migrations directory into a standalone binary if they want one.
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 MigrationFormat ¶ added in v1.9.0
type MigrationFormat string
MigrationFormat identifies the output language for generated migration files.
const ( FormatGo MigrationFormat = "go" FormatStarlark MigrationFormat = "starlark" )
func FormatFromExtension ¶ added in v1.9.0
func FormatFromExtension(ext string) MigrationFormat
FormatFromExtension returns the format based on a file extension.
func ParseMigrationFormat ¶ added in v1.9.0
func ParseMigrationFormat(s string) MigrationFormat
ParseMigrationFormat parses a format string from config, defaulting to Go.
type MigrationGenerator ¶ added in v1.9.0
type MigrationGenerator interface {
// GenerateMigration generates a migration file from a schema diff.
GenerateMigration(
name string,
deps []string,
diff *yaml.SchemaDiff,
currentSchema, previousSchema *yaml.Schema,
decisions map[int]yaml.PromptResponse,
) (string, error)
// GenerateBlank generates an empty migration with a TODO placeholder.
GenerateBlank(name string, deps []string) (string, error)
// FileExtension returns the file extension for this format (e.g. ".go", ".star").
FileExtension() string
}
MigrationGenerator generates migration source code in a specific language.
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 StarlarkGenerator ¶ added in v1.9.0
type StarlarkGenerator struct{}
StarlarkGenerator produces .star source code for migration files.
func NewStarlarkGenerator ¶ added in v1.9.0
func NewStarlarkGenerator() *StarlarkGenerator
NewStarlarkGenerator creates a new StarlarkGenerator instance.
func (*StarlarkGenerator) FileExtension ¶ added in v1.9.0
func (g *StarlarkGenerator) FileExtension() string
FileExtension returns ".star".
func (*StarlarkGenerator) GenerateBlank ¶ added in v1.9.0
func (g *StarlarkGenerator) GenerateBlank(name string, deps []string) (string, error)
GenerateBlank generates a blank Starlark migration with a TODO comment.
func (*StarlarkGenerator) GenerateMigration ¶ added in v1.9.0
func (g *StarlarkGenerator) GenerateMigration( name string, deps []string, diff *yaml.SchemaDiff, currentSchema, previousSchema *yaml.Schema, decisions map[int]yaml.PromptResponse, ) (string, error)
GenerateMigration generates a .star file from a schema diff.