Documentation
¶
Index ¶
- Constants
- func CanMigrate(obj attrs.Definer) bool
- func EqualDefaultValue(a, b any) bool
- func GetFieldType(driver driver.Driver, f attrs.Field) string
- func NewAppConfig() *migratorAppConfig
- func RegisterColumnKind(driver driver.Driver, typ []reflect.Kind, fn func(f attrs.Field) string)
- func RegisterColumnType(driver driver.Driver, typ interface{}, fn func(f attrs.Field) string)
- func RegisterSchemaEditor(driver driver.Driver, fn func() (SchemaEditor, error))
- type Action
- type ActionType
- type CanModelMigrate
- type CanSQL
- type CantMigrate
- type Changed
- type Column
- type Dependency
- type Index
- type MigrationAction
- type MigrationEngine
- func (m *MigrationEngine) GetLastMigration(appName, modelName string) *MigrationFile
- func (m *MigrationEngine) Log(action ActionType, file *MigrationFile, table *Changed[*ModelTable], ...)
- func (m *MigrationEngine) MakeMigrations() error
- func (m *MigrationEngine) Migrate() error
- func (m *MigrationEngine) NeedsToMigrate() ([]*contenttypes.BaseContentType[attrs.Definer], error)
- func (e *MigrationEngine) NewMigration(appName, modelName string, newTable *ModelTable, ...) (*MigrationFile, error)
- func (e *MigrationEngine) ReadMigrations() ([]*MigrationFile, error)
- func (e *MigrationEngine) WriteMigration(migration *MigrationFile) error
- type MigrationEngineConsoleLog
- type MigrationFile
- type MigrationLog
- type MigrationRelation
- type MigrationRelationThrough
- type ModelTable
- func (t *ModelTable) Columns() []*Column
- func (t *ModelTable) Comment() string
- func (t *ModelTable) Diff(other *ModelTable) (added, removed []Column, diffs []Changed[Column])
- func (t *ModelTable) Indexes() []Index
- func (t *ModelTable) MarshalJSON() ([]byte, error)
- func (t *ModelTable) Model() attrs.Definer
- func (t *ModelTable) ModelName() string
- func (t *ModelTable) TableName() string
- func (t *ModelTable) UnmarshalJSON(data []byte) error
- type SchemaEditor
- type Table
Constants ¶
const ( // Keys for attrs.Field AttrDBTypeKey = "migrator.db_type" AttrUseInDBKey = "migrator.use_in_db" AttrOnDeleteKey = "migrator.on_delete" AttrOnUpdateKey = "migrator.on_update" // Keys for attrs.ModelMeta MetaAllowMigrateKey = "migrator.allow_migrate" // The directory where the migration files are stored APPVAR_MIGRATION_DIR = "migrator.migration_dir" )
const (
DEFAULT_MIGRATION_DIR = "./migrations"
)
const (
MIGRATION_FILE_SUFFIX = ".mig"
)
Variables ¶
This section is empty.
Functions ¶
func CanMigrate ¶
func EqualDefaultValue ¶
func GetFieldType ¶
GetFieldType returns the database type for a field based on the driver and field type.
It first checks if the field has a custom database type defined in its attributes referenced by AttrDBTypeKey, and if not, it uses the registered functions to determine the type.
func NewAppConfig ¶
func NewAppConfig() *migratorAppConfig
func RegisterColumnKind ¶
RegisterColumnKind registers a function to convert a field to a database type for a specific driver and kind.
The function will be called with the field as an argument and should return the database type as a string.
func RegisterColumnType ¶
RegisterColumnType registers a function to convert a field to a database type for a specific driver and type.
The function will be called with the field as an argument and should return the database type as a string.
func RegisterSchemaEditor ¶
func RegisterSchemaEditor(driver driver.Driver, fn func() (SchemaEditor, error))
Types ¶
type Action ¶
type Action int
Action is the action to take when the target model is deleted or updated.
type ActionType ¶
type ActionType int
const ( ActionCreateTable ActionType = iota + 1 ActionDropTable ActionRenameTable ActionAddIndex ActionDropIndex ActionRenameIndex // ActionAlterUniqueTogether // ActionAlterIndexTogether ActionAddField ActionAlterField ActionRemoveField )
func (ActionType) MarshalJSON ¶
func (a ActionType) MarshalJSON() ([]byte, error)
func (ActionType) String ¶
func (a ActionType) String() string
func (*ActionType) UnmarshalJSON ¶
func (a *ActionType) UnmarshalJSON(data []byte) error
type CanModelMigrate ¶
type CanModelMigrate interface {
CanMigrate() bool
}
type CantMigrate ¶
type CantMigrate struct{}
Embed this struct in your model to prevent it from being migrated.
func (*CantMigrate) CanMigrate ¶
func (c *CantMigrate) CanMigrate() bool
type Changed ¶
type Changed[T any] struct { Old T `json:"old,omitempty"` New T `json:"new,omitempty"` }
type Column ¶
type Column struct { Table Table `json:"-"` Field attrs.Field `json:"-"` Name string `json:"name"` Column string `json:"column"` UseInDB bool `json:"use_in_db,omitempty"` MinLength int64 `json:"min_length,omitempty"` MaxLength int64 `json:"max_length,omitempty"` MinValue float64 `json:"min_value,omitempty"` MaxValue float64 `json:"max_value,omitempty"` Unique bool `json:"unique,omitempty"` Nullable bool `json:"nullable,omitempty"` Primary bool `json:"primary,omitempty"` Auto bool `json:"auto,omitempty"` Default interface{} `json:"default,omitempty"` ReverseAlias string `json:"reverse_alias,omitempty"` Rel *MigrationRelation `json:"relation,omitempty"` }
type Dependency ¶
func (*Dependency) MarshalJSON ¶
func (d *Dependency) MarshalJSON() ([]byte, error)
func (*Dependency) UnmarshalJSON ¶
func (d *Dependency) UnmarshalJSON(data []byte) error
type MigrationAction ¶
type MigrationAction struct { ActionType ActionType `json:"action"` Table *Changed[*ModelTable] `json:"table,omitempty"` Field *Changed[*Column] `json:"field,omitempty"` Index *Changed[*Index] `json:"index,omitempty"` }
Actions are kept track of to ensure a proper name can be generated for the migration file.
type MigrationEngine ¶
type MigrationEngine struct { // The path to the directory where the migration files are stored. // // This is used to load the migration files and apply them to the database. Path string // SchemaEditor is the schema editor used to apply the migrations to the database. // // This is used to execute SQL commands for creating, modifying, and deleting tables and columns. SchemaEditor SchemaEditor // Migrations is the list of migration files that have been applied to the database. // // This is used to keep track of the migrations that have been applied and ensure that they are not applied again. Migrations map[string]map[string][]*MigrationFile // MigrationLog is the migration log used to log the actions taken by the migration engine. // // This is used to log the actions taken by the migration engine for debugging and auditing purposes. MigrationLog MigrationLog // contains filtered or unexported fields }
func NewMigrationEngine ¶
func NewMigrationEngine(path string, schemaEditor SchemaEditor, apps ...string) *MigrationEngine
func (*MigrationEngine) GetLastMigration ¶
func (m *MigrationEngine) GetLastMigration(appName, modelName string) *MigrationFile
GetLastMigration returns the last applied migration for the given app and model.
func (*MigrationEngine) Log ¶
func (m *MigrationEngine) Log(action ActionType, file *MigrationFile, table *Changed[*ModelTable], column *Changed[*Column], index *Changed[*Index])
func (*MigrationEngine) MakeMigrations ¶
func (m *MigrationEngine) MakeMigrations() error
func (*MigrationEngine) Migrate ¶
func (m *MigrationEngine) Migrate() error
func (*MigrationEngine) NeedsToMigrate ¶
func (m *MigrationEngine) NeedsToMigrate() ([]*contenttypes.BaseContentType[attrs.Definer], error)
func (*MigrationEngine) NewMigration ¶
func (e *MigrationEngine) NewMigration(appName, modelName string, newTable *ModelTable, def *contenttypes.BaseContentType[attrs.Definer]) (*MigrationFile, error)
func (*MigrationEngine) ReadMigrations ¶
func (e *MigrationEngine) ReadMigrations() ([]*MigrationFile, error)
ReadMigrations reads the migration files from the specified path and returns a list of migration files.
These migration files are used to apply the migrations to the database.
func (*MigrationEngine) WriteMigration ¶
func (e *MigrationEngine) WriteMigration(migration *MigrationFile) error
WriteMigration writes the migration file to the specified path.
The migration file is used to apply the migrations to the database.
type MigrationEngineConsoleLog ¶
type MigrationEngineConsoleLog struct { }
func (*MigrationEngineConsoleLog) Log ¶
func (e *MigrationEngineConsoleLog) Log(action ActionType, file *MigrationFile, table *Changed[*ModelTable], column *Changed[*Column], index *Changed[*Index])
type MigrationFile ¶
type MigrationFile struct { // The name of the application for this migration. // // This is used to identify the application that the migration is for. AppName string `json:"-"` // The name of the model for this migration. // // This is used to identify the model that the migration is for. ModelName string `json:"-"` // The name of the migration file. // // This is used to identify the migration and apply it in the correct order. Name string `json:"-"` // The order of the migration file. // // This is used to ensure that the migrations are applied in the correct order. Order int `json:"-"` // ContentType is the content type for the model of this migration. // // This is used to identify the model that the migration is for. ContentType *contenttypes.BaseContentType[attrs.Definer] `json:"-"` // Dependencies are the migration files that this migration depends on. // // This is used to ensure that the migrations are applied in the correct order. // If a migration file has dependencies, it will not be applied until all of its dependencies have been applied. Dependencies []Dependency `json:"dependencies,omitempty"` // The SQL commands to be executed in the // migration file. // // This is used to apply the migration to the database. Table *ModelTable `json:"table"` // Actions are the actions that have been taken in this migration file. // // This is used to keep track of the actions that have been taken in the migration file. // These actions are used to generate the migration file name, and can be used to // migrate the database to a different state. Actions []MigrationAction `json:"actions"` }
func (*MigrationFile) FileName ¶
func (m *MigrationFile) FileName() string
type MigrationLog ¶
type MigrationLog interface {
Log(action ActionType, file *MigrationFile, table *Changed[*ModelTable], column *Changed[*Column], index *Changed[*Index])
}
type MigrationRelation ¶
type MigrationRelation struct { Type attrs.RelationType `json:"type"` // The type of the relation TargetModel *contenttypes.BaseContentType[attrs.Definer] `json:"model"` // The target model of the relation TargetField attrs.FieldDefinition `json:"field,omitempty"` // The field in the target model Through *MigrationRelationThrough `json:"through,omitempty"` // The through model of the relation OnDelete Action `json:"on_delete,omitempty"` // The on delete action of the relation OnUpdate Action `json:"on_update,omitempty"` // The on update action of the relation }
func (*MigrationRelation) Field ¶
func (m *MigrationRelation) Field() attrs.FieldDefinition
func (*MigrationRelation) MarshalJSON ¶
func (m *MigrationRelation) MarshalJSON() ([]byte, error)
func (*MigrationRelation) Model ¶
func (m *MigrationRelation) Model() attrs.Definer
func (*MigrationRelation) UnmarshalJSON ¶
func (m *MigrationRelation) UnmarshalJSON(data []byte) error
type MigrationRelationThrough ¶
type MigrationRelationThrough struct { Model *contenttypes.BaseContentType[attrs.Definer] `json:"model"` // The through model of the relation SourceField string `json:"source_field"` // The source field of the relation TargetField string `json:"target_field"` // The target field of the relation }
type ModelTable ¶
type ModelTable struct { Object attrs.Definer Table string Desc string Fields *orderedmap.OrderedMap[string, Column] Index []Index }
func NewModelTable ¶
func NewModelTable(object attrs.Definer) *ModelTable
func (*ModelTable) Columns ¶
func (t *ModelTable) Columns() []*Column
func (*ModelTable) Comment ¶
func (t *ModelTable) Comment() string
func (*ModelTable) Diff ¶
func (t *ModelTable) Diff(other *ModelTable) (added, removed []Column, diffs []Changed[Column])
func (*ModelTable) Indexes ¶
func (t *ModelTable) Indexes() []Index
func (*ModelTable) MarshalJSON ¶
func (t *ModelTable) MarshalJSON() ([]byte, error)
func (*ModelTable) Model ¶
func (t *ModelTable) Model() attrs.Definer
func (*ModelTable) ModelName ¶
func (t *ModelTable) ModelName() string
func (*ModelTable) TableName ¶
func (t *ModelTable) TableName() string
func (*ModelTable) UnmarshalJSON ¶
func (t *ModelTable) UnmarshalJSON(data []byte) error
type SchemaEditor ¶
type SchemaEditor interface { Setup() error StoreMigration(appName string, modelName string, migrationName string) error HasMigration(appName string, modelName string, migrationName string) (bool, error) RemoveMigration(appName string, modelName string, migrationName string) error Execute(ctx context.Context, query string, args ...any) (sql.Result, error) CreateTable(table Table) error DropTable(table Table) error RenameTable(table Table, newName string) error AddIndex(table Table, index Index) error DropIndex(table Table, index Index) error RenameIndex(table Table, oldName string, newName string) error AddField(table Table, col Column) error AlterField(table Table, old Column, newCol Column) error RemoveField(table Table, col Column) error }
func GetSchemaEditor ¶
func GetSchemaEditor(driver driver.Driver) (SchemaEditor, error)