Migration
in package
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
$db read-only
private
PDO
$db
$dir read-only
private
string
$dir
$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
applyNext()
Apply the next migration the database lacks, and record it.
public
applyNext() : string|null
Tags
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
.sqlfile is ignored.
Tags
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
Return values
boolstatements()
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
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.