0

Final Class Yiisoft\Db\Migration\Migrator

InheritanceYiisoft\Db\Migration\Migrator

Constants

Hide inherited constants

Constant Value Description Defined By
DEFAULT_HISTORY_TABLE '{{%migration}}' Default name of the migration history table. Yiisoft\Db\Migration\Migrator
DEFAULT_MIGRATION_NAME_LIMIT 180 Default maximum length of a migration name. Yiisoft\Db\Migration\Migrator

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\Db\Connection\ConnectionInterface $db, Yiisoft\Db\Migration\Informer\MigrationInformerInterface $informer, string $historyTable = self::DEFAULT_HISTORY_TABLE, ?int $migrationNameLimit = self::DEFAULT_MIGRATION_NAME_LIMIT, ?int $maxSqlOutputLength = null )
$db \Yiisoft\Db\Connection\ConnectionInterface
$informer Yiisoft\Db\Migration\Informer\MigrationInformerInterface
$historyTable string
$migrationNameLimit ?int
$maxSqlOutputLength ?int

                public function __construct(
    private readonly ConnectionInterface $db,
    private readonly MigrationInformerInterface $informer,
    private readonly string $historyTable = self::DEFAULT_HISTORY_TABLE,
    private ?int $migrationNameLimit = self::DEFAULT_MIGRATION_NAME_LIMIT,
    private readonly ?int $maxSqlOutputLength = null,
) {}

            
down() public method

public void down ( Yiisoft\Db\Migration\RevertibleMigrationInterface $migration )
$migration Yiisoft\Db\Migration\RevertibleMigrationInterface

                public function down(RevertibleMigrationInterface $migration): void
{
    $this->checkMigrationHistoryTable();
    match ($migration instanceof TransactionalMigrationInterface) {
        true => $this->db->transaction(fn() => $this->migrateDown($migration)),
        false => $this->migrateDown($migration),
    };
}

            
getHistory() public method

public array getHistory ( ?int $limit = null )
$limit ?int

                public function getHistory(?int $limit = null): array
{
    $this->checkMigrationHistoryTable();
    $query = (new Query($this->db))
        ->select(['apply_time', 'name'])
        ->from($this->historyTable)
        ->orderBy(['apply_time' => SORT_DESC, 'id' => SORT_DESC])
        ->indexBy('name');
    if ($limit > 0) {
        $query->limit($limit);
    }
    /** @psalm-var array<class-string, int|string> */
    return $query->column();
}

            
getHistoryTable() public method

public string getHistoryTable ( )

                public function getHistoryTable(): string
{
    return $this->historyTable;
}

            
getMigrationNameLimit() public method

public ?int getMigrationNameLimit ( )

                public function getMigrationNameLimit(): ?int
{
    if ($this->migrationNameLimit !== null) {
        return $this->migrationNameLimit;
    }
    $tableSchema = $this->db->getSchema()->getTableSchema($this->historyTable);
    if ($tableSchema === null) {
        return null;
    }
    $limit = $tableSchema->getColumns()['name']->getSize();
    if ($limit === null) {
        return null;
    }
    return $this->migrationNameLimit = $limit;
}

            
setIo() public method

public void setIo ( ?\Symfony\Component\Console\Style\SymfonyStyle $io )
$io ?\Symfony\Component\Console\Style\SymfonyStyle

                public function setIo(?SymfonyStyle $io): void
{
    $this->informer->setIo($io);
}

            
up() public method

public void up ( Yiisoft\Db\Migration\MigrationInterface $migration )
$migration Yiisoft\Db\Migration\MigrationInterface

                public function up(MigrationInterface $migration): void
{
    $this->checkMigrationHistoryTable();
    match ($migration instanceof TransactionalMigrationInterface) {
        true => $this->db->transaction(fn() => $this->migrateUp($migration)),
        false => $this->migrateUp($migration),
    };
}

            
updateHistory() public method

Adds and removes migration records atomically without executing migrations.

public void updateHistory ( list $add, list $remove )
$add list

Migration names to record as applied.

$remove list

Migration names to remove from history.

                public function updateHistory(array $add, array $remove): void
{
    // Create the table before starting the transaction: DDL may implicitly commit it.
    $this->checkMigrationHistoryTable();
    $this->db->transaction(function () use ($add, $remove): void {
        foreach ($add as $name) {
            $this->addMigrationToHistory($name);
        }
        foreach ($remove as $name) {
            $this->removeMigrationFromHistory($name);
        }
    });
}