Documentation
¶
Overview ¶
Package migrations provides the database migration runner and schema builder for OniWorks.
Index ¶
- func Register(name string, m Migration)
- type Column
- type Migration
- type MigrationStatus
- type Migrator
- func (m *Migrator) Fresh(ctx context.Context) error
- func (mg *Migrator) LoadRegistry()
- func (m *Migrator) Migrate(ctx context.Context) error
- func (m *Migrator) Register(name string, migration Migration) *Migrator
- func (m *Migrator) Rollback(ctx context.Context) error
- func (m *Migrator) Status(ctx context.Context) ([]MigrationStatus, error)
- type Schema
- func (s *Schema) Create(table string, fn func(*Table))
- func (s *Schema) Drop(table string)
- func (s *Schema) DropIfExists(table string)
- func (s *Schema) HasTable(ctx context.Context, table string) (bool, error)
- func (s *Schema) Raw(sql string)
- func (s *Schema) RenameTable(from, to string)
- func (s *Schema) Statements() []string
- func (s *Schema) Table(table string, fn func(*TableModifier))
- type Table
- func (t *Table) BigInteger(name string) *Column
- func (t *Table) Binary(name string) *Column
- func (t *Table) Boolean(name string) *Column
- func (t *Table) Date(name string) *Column
- func (t *Table) Decimal(name string) *Column
- func (t *Table) Float(name string) *Column
- func (t *Table) ForeignKey(name, refTable, refCol string) *Column
- func (t *Table) ID() *Column
- func (t *Table) Index(cols ...string)
- func (t *Table) Integer(name string) *Column
- func (t *Table) JSON(name string) *Column
- func (t *Table) SoftDeletes()
- func (t *Table) String(name string, length ...int) *Column
- func (t *Table) Text(name string) *Column
- func (t *Table) Timestamp(name string) *Column
- func (t *Table) Timestamps()
- func (t *Table) UUID() *Column
- func (t *Table) UniqueIndex(cols ...string)
- type TableModifier
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Column ¶
type Column struct {
// contains filtered or unexported fields
}
Column represents one table column in a migration.
func (*Column) Default ¶
Default sets a default value. String values are rendered as single-quoted SQL literals with embedded single quotes doubled.
func (*Column) NotNullable ¶
NotNullable disallows NULL values (default).
type Migration ¶
Migration is the interface every migration struct must implement. Up and Down receive a Schema builder — they queue DDL statements by calling schema.Create, schema.Drop, schema.Raw, etc. The actual SQL is executed (and errors surfaced) by the Migrator, not the migration.
type MigrationStatus ¶
MigrationStatus represents one migration's current state.
type Migrator ¶
type Migrator struct {
// contains filtered or unexported fields
}
Migrator runs, rolls back, and reports the status of migrations against a database.
func (*Migrator) LoadRegistry ¶
func (mg *Migrator) LoadRegistry()
LoadRegistry copies all globally registered migrations into this Migrator. Call this in main.go after importing migration packages as side-effects.
func (*Migrator) Register ¶
Register adds a migration to the Migrator's list.
m.Register("2024_01_01_000000_create_users_table", &CreateUsersTable{})
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
Schema is the migration schema builder. It collects DDL statements and executes them atomically when execute() is called.
func (*Schema) Create ¶
Create creates a new table using the fluent Table builder.
s.Create("users", func(t *Table) {
t.ID()
t.String("email", 255).Unique()
t.Timestamps()
})
func (*Schema) DropIfExists ¶
DropIfExists drops a table only if it exists.
func (*Schema) RenameTable ¶
RenameTable renames a table.
func (*Schema) Statements ¶
Statements returns the SQL statements this schema would execute. The Migrator uses it to run a whole batch inside one transaction instead of committing each migration independently.
func (*Schema) Table ¶
func (s *Schema) Table(table string, fn func(*TableModifier))
Table modifies an existing table.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table is the fluent builder for CREATE TABLE statements.
func (*Table) BigInteger ¶
BigInteger adds a BIGINT column.
func (*Table) ForeignKey ¶
ForeignKey adds a BIGINT column with an optional FK constraint.
t.ForeignKey("user_id", "users", "id").OnDelete("CASCADE")
func (*Table) ID ¶
ID adds a "id" BIGINT primary key (auto-increment for MySQL, BIGSERIAL for Postgres).
func (*Table) SoftDeletes ¶
func (t *Table) SoftDeletes()
SoftDeletes adds a nullable "deleted_at" column for soft-delete support.
func (*Table) Timestamps ¶
func (t *Table) Timestamps()
Timestamps adds "created_at" and "updated_at" columns (NOT NULL, set automatically).
func (*Table) UniqueIndex ¶
UniqueIndex creates a unique composite index.
type TableModifier ¶
type TableModifier struct {
// contains filtered or unexported fields
}
TableModifier adds/drops columns and indexes on an existing table.
func (*TableModifier) AddColumn ¶
func (tm *TableModifier) AddColumn(col *Column)
func (*TableModifier) AddIndex ¶
func (tm *TableModifier) AddIndex(cols ...string)
func (*TableModifier) AddUniqueIndex ¶
func (tm *TableModifier) AddUniqueIndex(cols ...string)
func (*TableModifier) DropColumn ¶
func (tm *TableModifier) DropColumn(name string)