Skip to content

Commit

Permalink
Merge branch 'master' into check_alter_column
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Nov 20, 2023
2 parents 490cbeb + a9bfbf0 commit 02e1633
Show file tree
Hide file tree
Showing 26 changed files with 469 additions and 84 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ jobs:
- ubuntu-latest

php:
- 8.0
- 8.1
- 8.2
- '8.0'
- '8.1'
- '8.2'

steps:
- name: Checkout.
Expand All @@ -61,7 +61,7 @@ jobs:
run: composer require yiisoft/db-sqlite:^1.0 --prefer-dist --no-interaction --no-progress --optimize-autoloader --ansi

- name: Run tests with phpunit.
run: vendor/bin/phpunit --testsuite=YiiDbMigration --coverage-clover=coverage.xml --colors=always
run: vendor/bin/phpunit --testsuite=Migration --coverage-clover=coverage.xml --colors=always

- name: Upload coverage to Codecov.
if: matrix.php == '8.1'
Expand Down
8 changes: 4 additions & 4 deletions bin/MigrationContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public static function definitions(): array
],
MigrationService::class => [
'class' => MigrationService::class,
'createNamespace()' => [''],
'createPath()' => [''],
'updateNamespaces()' => [[]],
'updatePaths()' => [[]],
'setCreateNamespace()' => [''],
'setCreatePath()' => [''],
'setUpdateNamespaces()' => [[]],
'setUpdatePaths()' => [[]],
],
Migrator::class => [
'__constructor()' => [
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@
},
"autoload-dev": {
"psr-4": {
"Yiisoft\\Db\\Migration\\Tests\\": "tests"
"Yiisoft\\Db\\Migration\\Tests\\": "tests",
"Yiisoft\\Db\\Migration\\Tests\\Support\\": "tests/Support",
"Yiisoft\\Db\\Migration\\Tests\\ForTest\\": "tests/Support",
"Yiisoft\\Db\\Migration\\Tests\\Support\\MigrationsExtra\\": ["tests/Support/MigrationsExtra", "tests/Support/MigrationsExtra2"]
}
},
"extra": {
Expand Down
8 changes: 4 additions & 4 deletions config/di-console.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
return [
MigrationService::class => [
'class' => MigrationService::class,
'createNamespace()' => [$params['yiisoft/db-migration']['createNamespace']],
'updateNamespaces()' => [$params['yiisoft/db-migration']['updateNamespaces']],
'createPath()' => [$params['yiisoft/db-migration']['createPath']],
'updatePaths()' => [$params['yiisoft/db-migration']['updatePaths']],
'setCreateNamespace()' => [$params['yiisoft/db-migration']['createNamespace']],
'setUpdateNamespaces()' => [$params['yiisoft/db-migration']['updateNamespaces']],
'setCreatePath()' => [$params['yiisoft/db-migration']['createPath']],
'setUpdatePaths()' => [$params['yiisoft/db-migration']['updatePaths']],
],

MigrationInformerInterface::class => ConsoleMigrationInformer::class,
Expand Down
8 changes: 4 additions & 4 deletions docs/en/usage-with-symfony-console.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ final class MigrationContainer
],
MigrationService::class => [
'class' => MigrationService::class,
'createNamespace()' => [''],
'createPath()' => [''],
'updateNamespaces()' => [['Yii\\User\\Framework\\Migration']],
'updatePaths()' => [[]],
'serCreateNamespace()' => [''],
'setCreatePath()' => [''],
'setUpdateNamespaces()' => [['Yii\\User\\Framework\\Migration']],
'setUpdatePaths()' => [[]],
],
MigrationInformerInterface::class => ConsoleMigrationInformer::class,
];
Expand Down
6 changes: 3 additions & 3 deletions src/Command/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
* ./yii migrate:create post --command=table --path=@root/migrations/blog
* ```
*
* In case {@see createPath} is not set and no namespace is provided, {@see createNamespace} will be used.
* In case {@see $createPath} is not set and no namespace is provided, {@see $createNamespace} will be used.
*/
#[AsCommand('migrate:create', 'Creates a new migration.')]
final class CreateCommand extends Command
Expand Down Expand Up @@ -101,8 +101,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$namespace = $input->getOption('namespace');

if ($path !== null || $namespace !== null) {
$this->migrationService->createPath((string) $path);
$this->migrationService->createNamespace((string) $namespace);
$this->migrationService->setCreatePath((string) $path);
$this->migrationService->setCreateNamespace((string) $namespace);
} else {
$namespace = $this->migrationService->getCreateNamespace();
}
Expand Down
52 changes: 41 additions & 11 deletions src/Command/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Yiisoft\Db\Migration\Service\MigrationService;

use function array_keys;
use function array_slice;
use function count;

/**
Expand All @@ -26,9 +27,15 @@
* For example:
*
* ```shell
* ./yii migrate:down # revert the last migration
* ./yii migrate:down --limit=3 # revert the last 3 migrations
* ./yii migrate:down --all # revert all migrations
* ./yii migrate:down # revert the last migration
* ./yii migrate:down --limit=3 # revert last 3 migrations
* ./yii migrate:down --all # revert all migrations
* ./yii migrate:down --path=@vendor/yiisoft/rbac-db/migrations # revert the last migration from the directory
* ./yii migrate:down --namespace=Yiisoft\\Rbac\\Db\\Migrations # revert the last migration from the namespace
*
* # revert migrations from multiple directories and namespaces
* ./yii migrate:down --path=@vendor/yiisoft/rbac-db/migrations --path=@vendor/yiisoft/cache-db/migrations
* ./yii migrate:down --namespace=Yiisoft\\Rbac\\Db\\Migrations --namespace=Yiisoft\\Cache\\Db\\Migrations
* ```
*/
#[AsCommand('migrate:down', 'Reverts the specified number of latest migrations.')]
Expand All @@ -46,7 +53,9 @@ protected function configure(): void
{
$this
->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of migrations to revert.', 1)
->addOption('all', 'a', InputOption::VALUE_NONE, 'Revert all migrations.');
->addOption('all', 'a', InputOption::VALUE_NONE, 'Revert all migrations.')
->addOption('path', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Path to migrations to revert.')
->addOption('namespace', 'ns', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Namespace of migrations to revert.');
}

protected function execute(InputInterface $input, OutputInterface $output): int
Expand All @@ -69,16 +78,37 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::INVALID;
}

$migrations = $this->migrator->getHistory($limit);
/** @psalm-var string[] $paths */
$paths = $input->getOption('path');
/** @psalm-var string[] $namespaces */
$namespaces = $input->getOption('namespace');

if (empty($migrations)) {
$output->writeln("<fg=yellow> >>> Apply at least one migration first.</>\n");
$io->warning('No migration has been done before.');
if (!empty($paths) || !empty($namespaces)) {
$migrations = $this->migrator->getHistory();
$migrations = array_keys($migrations);
$migrations = $this->migrationService->filterMigrations($migrations, $namespaces, $paths);

return Command::FAILURE;
}
if (empty($migrations)) {
$io->warning('No applied migrations found.');

return Command::FAILURE;
}

if ($limit !== null) {
$migrations = array_slice($migrations, 0, $limit);
}
} else {
$migrations = $this->migrator->getHistory($limit);

if (empty($migrations)) {
$output->writeln("<fg=yellow> >>> Apply at least one migration first.</>\n");
$io->warning('No migration has been done before.');

$migrations = array_keys($migrations);
return Command::FAILURE;
}

$migrations = array_keys($migrations);
}

$n = count($migrations);
$migrationWord = $n === 1 ? 'migration' : 'migrations';
Expand Down
4 changes: 2 additions & 2 deletions src/Command/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$namespaces = $input->getOption('namespace');

if (!empty($paths) || !empty($namespaces)) {
$this->migrationService->updatePaths($paths);
$this->migrationService->updateNamespaces($namespaces);
$this->migrationService->setUpdatePaths($paths);
$this->migrationService->setUpdateNamespaces($namespaces);
}

$this->migrationService->before(self::getDefaultName() ?? '');
Expand Down
4 changes: 2 additions & 2 deletions src/Command/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$namespaces = $input->getOption('namespace');

if (!empty($paths) || !empty($namespaces)) {
$this->migrationService->updatePaths($paths);
$this->migrationService->updateNamespaces($namespaces);
$this->migrationService->setUpdatePaths($paths);
$this->migrationService->setUpdateNamespaces($namespaces);
}

if ($this->migrationService->before(self::getDefaultName() ?? '') === Command::INVALID) {
Expand Down
11 changes: 7 additions & 4 deletions src/MigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use function rtrim;
use function sprintf;
use function substr;
use function trim;

final class MigrationBuilder extends AbstractMigrationBuilder
{
Expand Down Expand Up @@ -52,13 +53,15 @@ public function getDb(): ConnectionInterface
*/
public function execute(string $sql, array $params = []): void
{
$sqlOutput = $sql;
if ($this->maxSqlOutputLength !== null && $this->maxSqlOutputLength < strlen($sql)) {
$sqlOutput = ltrim(rtrim(substr($sql, 0, $this->maxSqlOutputLength)) . ' [... hidden]');
$command = $this->db->createCommand($sql)->bindValues($params);
$sqlOutput = trim($command->getRawSql());

if ($this->maxSqlOutputLength !== null && $this->maxSqlOutputLength < strlen($sqlOutput)) {
$sqlOutput = ltrim(rtrim(substr($sqlOutput, 0, $this->maxSqlOutputLength)) . ' [... hidden]');
}

$time = $this->beginCommand("Execute SQL: $sqlOutput");
$this->db->createCommand($sql)->bindValues($params)->execute();
$command->execute();
$this->endCommand($time);
}

Expand Down
Loading

0 comments on commit 02e1633

Please sign in to comment.