migrations

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package migrations provides the database migration runner and schema builder for OniWorks.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(name string, m Migration)

Register adds a migration to the global application registry. Call this from init() in each migration file so it is auto-discovered.

func init() {
    migrations.Register("20240101000000_create_users_table", &CreateUsersTable{})
}

Types

type Column

type Column struct {
	// contains filtered or unexported fields
}

Column represents one table column in a migration.

func (*Column) AutoInc

func (c *Column) AutoInc() *Column

AutoInc marks this column as auto-increment.

func (*Column) Default

func (c *Column) Default(v any) *Column

Default sets a default value. String values are rendered as single-quoted SQL literals with embedded single quotes doubled.

func (*Column) Len

func (c *Column) Len(n int) *Column

Len sets the column length (for VARCHAR).

func (*Column) NotNullable

func (c *Column) NotNullable() *Column

NotNullable disallows NULL values (default).

func (*Column) Nullable

func (c *Column) Nullable() *Column

Nullable allows NULL values.

func (*Column) OnDelete

func (c *Column) OnDelete(action string) *Column

OnDelete sets the FK ON DELETE action ("CASCADE", "SET NULL", "RESTRICT").

func (*Column) PK

func (c *Column) PK() *Column

PK marks this column as the primary key.

func (*Column) Unique

func (c *Column) Unique() *Column

Unique adds a unique constraint.

type Migration

type Migration interface {
	Up(s *Schema)
	Down(s *Schema)
}

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

type MigrationStatus struct {
	Name  string
	Ran   bool
	Batch int
	RanAt time.Time
}

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 New

func New(db *sql.DB, driver string) *Migrator

New creates a Migrator.

func (*Migrator) Fresh

func (m *Migrator) Fresh(ctx context.Context) error

Fresh drops all tables and re-runs all migrations from scratch.

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) Migrate

func (m *Migrator) Migrate(ctx context.Context) error

Migrate runs all pending migrations in ascending timestamp order.

func (*Migrator) Register

func (m *Migrator) Register(name string, migration Migration) *Migrator

Register adds a migration to the Migrator's list.

m.Register("2024_01_01_000000_create_users_table", &CreateUsersTable{})

func (*Migrator) Rollback

func (m *Migrator) Rollback(ctx context.Context) error

Rollback rolls back the last batch of migrations.

func (*Migrator) Status

func (m *Migrator) Status(ctx context.Context) ([]MigrationStatus, error)

Status prints the status of all registered migrations.

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

func (s *Schema) Create(table string, fn func(*Table))

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) Drop

func (s *Schema) Drop(table string)

Drop drops a table (no IF EXISTS — use DropIfExists for safety).

func (*Schema) DropIfExists

func (s *Schema) DropIfExists(table string)

DropIfExists drops a table only if it exists.

func (*Schema) HasTable

func (s *Schema) HasTable(ctx context.Context, table string) (bool, error)

HasTable reports whether a table exists (runs immediately, not queued).

func (*Schema) Raw

func (s *Schema) Raw(sql string)

Raw adds a raw SQL statement to the execution queue.

func (*Schema) RenameTable

func (s *Schema) RenameTable(from, to string)

RenameTable renames a table.

func (*Schema) Statements

func (s *Schema) Statements() []string

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

func (t *Table) BigInteger(name string) *Column

BigInteger adds a BIGINT column.

func (*Table) Binary

func (t *Table) Binary(name string) *Column

Binary adds a BYTEA/BLOB column.

func (*Table) Boolean

func (t *Table) Boolean(name string) *Column

Boolean adds a BOOL/TINYINT(1) column.

func (*Table) Date

func (t *Table) Date(name string) *Column

Date adds a DATE column.

func (*Table) Decimal

func (t *Table) Decimal(name string) *Column

Decimal adds a DECIMAL(10,2) column.

func (*Table) Float

func (t *Table) Float(name string) *Column

Float adds a DOUBLE/FLOAT column.

func (*Table) ForeignKey

func (t *Table) ForeignKey(name, refTable, refCol string) *Column

ForeignKey adds a BIGINT column with an optional FK constraint.

t.ForeignKey("user_id", "users", "id").OnDelete("CASCADE")

func (*Table) ID

func (t *Table) ID() *Column

ID adds a "id" BIGINT primary key (auto-increment for MySQL, BIGSERIAL for Postgres).

func (*Table) Index

func (t *Table) Index(cols ...string)

Index creates a composite index on the given columns.

func (*Table) Integer

func (t *Table) Integer(name string) *Column

Integer adds an INT column.

func (*Table) JSON

func (t *Table) JSON(name string) *Column

JSON adds a JSON/JSONB column.

func (*Table) SoftDeletes

func (t *Table) SoftDeletes()

SoftDeletes adds a nullable "deleted_at" column for soft-delete support.

func (*Table) String

func (t *Table) String(name string, length ...int) *Column

String adds a VARCHAR column.

func (*Table) Text

func (t *Table) Text(name string) *Column

Text adds a TEXT column.

func (*Table) Timestamp

func (t *Table) Timestamp(name string) *Column

Timestamp adds a TIMESTAMP/TIMESTAMPTZ column.

func (*Table) Timestamps

func (t *Table) Timestamps()

Timestamps adds "created_at" and "updated_at" columns (NOT NULL, set automatically).

func (*Table) UUID

func (t *Table) UUID() *Column

UUID adds a UUID primary key column named "id".

func (*Table) UniqueIndex

func (t *Table) UniqueIndex(cols ...string)

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)

Jump to

Keyboard shortcuts

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