Pickles Framework Documentation

Migrator

The Migrator class is responsible for handling database migrations.

It provides functionality to run, rollback, and manage migrations within the application.

Table of Contents

Properties

$databaseDriver  : DatabaseDriver
$logProgress  : bool
$migrationsDir  : string
$output  : ConsoleOutput
$templateDir  : string

Methods

__construct()  : mixed
make()  : string
Creates a new migration file based on the provided migration name.
migrate()  : void
Executes the migration process by applying all pending migrations.
rollback()  : void
Rollback the last database migration(s).
createMigrationTable()  : void
Creates the migration table in the database if it does not already exist.
generateMigrationFile()  : array<string|int, string>
Generates a new migration file based on the provided migration name and template.
getAllMigrations()  : array<string|int, mixed>
Retrieves all migration files available in the migrations directory.
getIdForMigrationFile()  : int
Retrieves the unique identifier for a migration file based on the provided date.
getTableName()  : string
Retrieves the table name from a migration name using a regular expression.
log()  : void
Logs a message for debugging or informational purposes.

Properties

$logProgress

private bool $logProgress = true

$migrationsDir

private string $migrationsDir

$templateDir

private string $templateDir

Methods

__construct()

public __construct(string $migrationsDir, string $templateDir, DatabaseDriver $databaseDriver[, bool $logProgress = true ]) : mixed
Parameters
$migrationsDir : string
$templateDir : string
$databaseDriver : DatabaseDriver
$logProgress : bool = true

make()

Creates a new migration file based on the provided migration name.

public make(string $migrationName) : string

This method generates a migration file by transforming the given migration name into a snake_case format and applying a template. Depending on the migration name, it determines the type of migration (e.g., creating a table, altering a table, or a custom migration) and modifies the template accordingly.

Parameters
$migrationName : string

The name of the migration to create.

The method performs the following:

  • Converts the migration name to snake_case.
  • Loads a migration template from the specified template directory.
  • If the migration name matches the pattern for creating a table (create_*_table), it generates SQL statements for creating and dropping the table.
  • If the migration name matches the pattern for altering a table (*_from_*_table or *_to_*_table), it generates SQL statements for altering the table.
  • For other cases, it comments out any DB::statement lines in the template.
  • Generates the migration file and outputs its file path.
Tags
throws
RuntimeException

If the template file cannot be loaded or the migration file cannot be created.

Return values
string

The name of the created migration file.

migrate()

Executes the migration process by applying all pending migrations.

public migrate() : void

This method performs the following steps:

  1. Ensures the migration table exists by calling createMigrationTable().
  2. Retrieves the list of already migrated entries from the migrations table.
  3. Fetches all available migration files.
  4. Compares the migrated entries with the available migrations to determine which migrations are pending.
  5. Iterates over the pending migrations, requiring each migration file, validating its type, and executing its up() method.
  6. Records the successful migration in the migrations table.
  7. Logs the progress and results of the migration process.

If there are no pending migrations, a log message is generated indicating that there is nothing to migrate.

rollback()

Rollback the last database migration(s).

public rollback([int|null $steps = null ]) : void
Parameters
$steps : int|null = null

The number of migrations to rollback. If null, it will rollback the last batch.

createMigrationTable()

Creates the migration table in the database if it does not already exist.

private createMigrationTable() : void

This table is used to track the migrations that have been executed.

generateMigrationFile()

Generates a new migration file based on the provided migration name and template.

private generateMigrationFile(string $migrationName, string $template) : array<string|int, string>
Parameters
$migrationName : string

The name of the migration to be created.

$template : string

The template content to be used for the migration file.

Return values
array<string|int, string>

Returns an array containing the file name and the full path of the created migration file.

getAllMigrations()

Retrieves all migration files available in the migrations directory.

private getAllMigrations() : array<string|int, mixed>
Return values
array<string|int, mixed>

An array of migration file names.

getIdForMigrationFile()

Retrieves the unique identifier for a migration file based on the provided date.

private getIdForMigrationFile(string $date) : int
Parameters
$date : string

The date string used to identify the migration file.

Return values
int

The unique identifier corresponding to the migration file.

getTableName()

Retrieves the table name from a migration name using a regular expression.

private getTableName(string $migrationName, string $regex[, int $group = 1 ]) : string
Parameters
$migrationName : string

The name of the migration.

$regex : string

The regular expression used to extract the table name.

$group : int = 1

The regex capture group to extract the table name. Defaults to 1.

Return values
string

The extracted table name.

log()

Logs a message for debugging or informational purposes.

private log(string $message[, LogType $logType = LogType::INFO ]) : void
Parameters
$message : string

The message to be logged.

$logType : LogType = LogType::INFO

        
On this page

Search results