phpscript

manual

ClassGo

Database\Migrate

class Database\Migrate {

}

Description

Database\Migrate loads a set of SQL migrations from the script filesystem and runs them against the named connection.

The first name is also the project name mig records under and two schemas sharing one database keep separate records when they open under separate names. A script that names no connection migrates "default": the connection an unnamed one resolves to anyway.

The constructor tries each name as "<name>:migrate" before "<name>" and a deployment can point migrations at a user allowed to alter tables while the script queries through one that is not, without the script naming either. The project stays "<name>" whichever credential answered: what a migration connects with is not what it records under. A "<name>:migrate" credential cannot come from the PLATFORM_DB_<NAME>=<dsn> environment form, because an environment variable name holds no colon; it arrives through Database::register and a virtual host owning its own connections already supplies them.

A name longer than 16 characters fails the run with mig's ErrNoProject, because 16 is the width of the project column.

Database\Migrate is a class phpscript declares.

$object = new Database\Migrate($names);

Methods

public function load(string $pattern): void

load selects the migrations to run, as a glob against the runtime source filesystem. Load itself opens no file: mig reads the files it applies, and a pattern matching no file shows up as wrong only once Run looks.

A script names its migrations relative to itself ("./schema/*.up.sql") and Load joins the work directory in. What the pattern matched is what mig records and a database migrated by the older binding - which recorded the base name under no project - applies every file again.

The go method Load.

public function run(): void

run applies the loaded migrations. It opens one span and not one per file: migrations run at startup, where what matters is how long the schema took and whether it failed, with no row per statement.

The go method Run.

Binding

See also