Migrations
in package
Database migrations and initialization utilities.
Provides methods for updating database schema, running migrations, and initializing the database.
Tags
Table of Contents
Methods
- calculateChecksum() : string
- Calculate SHA-256 checksum for a migration file.
- checkAndUpdate() : void
- Check and/or update the database.
- dropAllForeignKeys() : void
- Drop all foreign key constraints from all tables in the database.
- getAppliedMigrations() : array<string|int, string>
- Get list of migrations that have already been applied.
- getMigrationFiles() : array<string|int, string>
- Get list of all migration files from the migrations directory.
- prefixQuery() : string
- Add a prefix to table in a SQL query string.
- recordMigration() : void
- Record a migration as applied with its checksum.
- reparseAllTexts() : void
- Reparse all texts in order.
- update() : void
- Update the database if it is using an outdate version.
- upgradeMigrationsTable() : void
- Upgrade the _migrations table from old schema to new schema.
- validateMigrationIntegrity() : array{valid: bool, errors: string[]}
- Validate that applied migrations haven't been modified.
Methods
calculateChecksum()
Calculate SHA-256 checksum for a migration file.
public
static calculateChecksum(string $filepath) : string
Parameters
- $filepath : string
-
Full path to the migration file
Return values
string —SHA-256 hash or empty string if file not readable
checkAndUpdate()
Check and/or update the database.
public
static checkAndUpdate() : void
dropAllForeignKeys()
Drop all foreign key constraints from all tables in the database.
public
static dropAllForeignKeys() : void
This is needed before running migrations from scratch because SET FOREIGN_KEY_CHECKS = 0 only affects INSERT/UPDATE/DELETE and DROP TABLE, not ALTER TABLE MODIFY on columns referenced by FKs.
getAppliedMigrations()
Get list of migrations that have already been applied.
public
static getAppliedMigrations() : array<string|int, string>
Return values
array<string|int, string> —List of applied migration filenames
getMigrationFiles()
Get list of all migration files from the migrations directory.
public
static getMigrationFiles() : array<string|int, string>
Return values
array<string|int, string> —Sorted list of migration filenames
prefixQuery()
Add a prefix to table in a SQL query string.
public
static prefixQuery(string $sql_line, string $prefix) : string
Parameters
- $sql_line : string
-
SQL string to prefix.
- $prefix : string
-
Prefix to add
Return values
string —Prefixed SQL query
recordMigration()
Record a migration as applied with its checksum.
public
static recordMigration(string $filename[, string $checksum = '' ]) : void
Parameters
- $filename : string
-
The migration filename
- $checksum : string = ''
-
SHA-256 hash of the migration file
reparseAllTexts()
Reparse all texts in order.
public
static reparseAllTexts() : void
update()
Update the database if it is using an outdate version.
public
static update() : void
upgradeMigrationsTable()
Upgrade the _migrations table from old schema to new schema.
public
static upgradeMigrationsTable() : void
Old schema stored migrations to be run; new schema tracks applied migrations. This method adds the applied_at and checksum columns.
validateMigrationIntegrity()
Validate that applied migrations haven't been modified.
public
static validateMigrationIntegrity() : array{valid: bool, errors: string[]}
Checks the checksum of each applied migration against its stored value. This detects tampering or accidental modification of migration files.
Return values
array{valid: bool, errors: string[]} —Validation result