izartu API

Migration
in package

FinalYes

The database schema's migrations: SQL files applied in order, each recorded in the `migration` table.

A file is named NNNN-name.sql, where NNNN is its version. Each statement ends with ; at the end of a line, and lines starting with -- are comments.

The first migration builds the whole schema, the migration table included. Every later one is either one DDL statement or DML only: MySQL commits DDL implicitly, so no transaction can cover it, but each DDL statement is atomic; DML runs in one transaction with the migration's record.

Table of Contents

Properties

$db  : PDO
$dir  : string
$migrations  : array<int, string>

Methods

__construct()  : mixed
Read a directory's migrations.
applyNext()  : string|null
Apply the next migration the database lacks, and record it.
parse()  : array<int, string>
The migrations among a directory's entries, by version.
pending()  : bool
Whether the database lacks a migration.
statements()  : array<int, string>
Split a migration file into its statements.
transactional()  : bool
Whether a migration is DML only, and so can run inside a transaction.
version()  : int
The database's current version, after checking these migrations can continue from it.
apply()  : void
Run one migration's statements and record it.

Properties

$migrations

private array<int, string> $migrations

Migration names by version, in ascending order.

Methods

__construct()

Read a directory's migrations.

public __construct(PDO $db, string $dir) : mixed
Parameters
$db : PDO

A connection to the database to migrate, in exception error mode.

$dir : string

The directory of migration files.

Tags
throws
RuntimeException

When the directory cannot be read or holds a misnamed or repeated migration.

applyNext()

Apply the next migration the database lacks, and record it.

public applyNext() : string|null
Tags
throws
RuntimeException

When version() refuses the database, the migration is empty or mixes DDL with other statements, or a statement fails. A failed migration is not recorded.

Return values
string|null —

The migration applied, as NNNN-name, or null when the database is up to date.

parse()

The migrations among a directory's entries, by version.

public static parse(array<int, string> $names) : array<int, string>
Parameters
$names : array<int, string>

The directory's entries; anything that is not a .sql file is ignored.

Tags
throws
RuntimeException

When a .sql file is not named NNNN-name.sql with a version of 0001 or higher, or two files share a version.

Return values
array<int, string> —

Each migration's name (the file name without its number and extension) by version, in ascending order.

pending()

Whether the database lacks a migration.

public pending() : bool
Tags
throws
RuntimeException

When version() refuses the database.

Return values
bool

statements()

Split a migration file into its statements.

public static statements(string $sql) : array<int, string>
Parameters
$sql : string

The file's contents.

Return values
array<int, string> —

The statements, trimmed and without their final ;; comment lines and blank statements are dropped.

transactional()

Whether a migration is DML only, and so can run inside a transaction.

public static transactional(array<int, string> $statements) : bool
Parameters
$statements : array<int, string>

The migration's statements.

Return values
bool —

True when there is at least one statement and every one is an INSERT, UPDATE, DELETE or REPLACE.

version()

The database's current version, after checking these migrations can continue from it.

public version() : int
Tags
throws
RuntimeException

When the database has tables but no migration table, or is at a version newer than the latest migration.

Return values
int —

The highest recorded version, or 0 when none is recorded (as in an empty database).

apply()

Run one migration's statements and record it.

private apply(int $version, string $name) : void
Parameters
$version : int

The migration's version.

$name : string

The migration's name.

Tags
throws
RuntimeException

When the file is empty or unreadable, mixes DDL with other statements (allowed only in the first migration), or a statement fails.

On this page

Search results