Documentation
¶
Overview ¶
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.
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 GenerationHeader(prefix, command string) string
- func MigrationFileNameForFormat(name string, _ MigrationFormat) string
- func NextMigrationNumber(count int) string
- func WriteTableJSONL(path string, rows []map[string]any) error
- type DumpDataGenerator
- 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 GenerationHeader ¶ added in v1.10.1
GenerationHeader returns a comment block for the top of generated files. The command parameter identifies which morphic command created the file (e.g. "dump-data", "morphic", "merge", "squash", "convert"). The prefix is the comment character(s) to use (e.g. "#" for .star and .jsonl).
func MigrationFileNameForFormat ¶ added in v1.9.0
func MigrationFileNameForFormat(name string, _ MigrationFormat) string
MigrationFileNameForFormat returns the .star file name for a migration.
func NextMigrationNumber ¶
NextMigrationNumber returns a zero-padded 4-digit number for the next migration.
Types ¶
type DumpDataGenerator ¶
type DumpDataGenerator struct{}
DumpDataGenerator generates Starlark migration source containing UpsertData operations.
func NewDumpDataGenerator ¶
func NewDumpDataGenerator() *DumpDataGenerator
NewDumpDataGenerator creates a new DumpDataGenerator.
func (*DumpDataGenerator) GenerateStarlark ¶ added in v1.10.0
func (g *DumpDataGenerator) GenerateStarlark(name string, deps []string, tables []TableDump) (string, error)
GenerateStarlark produces a .star migration file source containing upsert_data operations for each table dump.
func (*DumpDataGenerator) GenerateStarlarkWithRowsFile ¶ added in v1.10.0
func (g *DumpDataGenerator) GenerateStarlarkWithRowsFile(name string, deps []string, tables []TableDump) (string, error)
GenerateStarlarkWithRowsFile produces a .star migration file where each upsert_data operation references an external JSONL file via rows_file instead of inlining the row data.
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) GenerateStarlarkMerge ¶ added in v1.10.0
func (g *MergeGenerator) GenerateStarlarkMerge(name string, deps []string) (string, error)
GenerateStarlarkMerge generates a .star merge migration file.
type MigrationFormat ¶ added in v1.9.0
type MigrationFormat string
MigrationFormat identifies the output language for generated migration files.
const (
FormatStarlark MigrationFormat = "starlark"
)
func FormatFromExtension ¶ added in v1.9.0
func FormatFromExtension(_ string) MigrationFormat
FormatFromExtension returns the migration format (always Starlark).
func ParseMigrationFormat ¶ added in v1.9.0
func ParseMigrationFormat(_ string) MigrationFormat
ParseMigrationFormat parses a format string from config (always returns Starlark).
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 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) GenerateStarlarkSquash ¶ added in v1.10.0
func (g *SquashGenerator) GenerateStarlarkSquash( name string, replaces []string, migrations []*migrate.Migration, ) (string, error)
GenerateStarlarkSquash generates a .star squashed migration file.
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.