Skip to content

Commit

Permalink
Allow use maxSqlOutputLength (#211)
Browse files Browse the repository at this point in the history
* Allow use `maxSqlOutputLength`

* Apply fixes from StyleCI

* Add test migration file

* Apply fixes from StyleCI

* Fix test

* Remove `Migrator::getMaxSqlOutputLength()`

* Apply @vjik suggestion

Co-authored-by: Sergei Predvoditelev <[email protected]>

* Update according to the last commit

* Revert migration after test

* Update default value in config template

---------

Co-authored-by: StyleCI Bot <[email protected]>
Co-authored-by: Sergei Predvoditelev <[email protected]>
  • Loading branch information
3 people authored Oct 16, 2023
1 parent cca5a2f commit 59f8546
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 5 deletions.
8 changes: 8 additions & 0 deletions bin/MigrationContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Symfony\Component\Console\Application;
use Yiisoft\Db\Migration\Migrator;
use Yiisoft\Definitions\ReferencesArray;
use Yiisoft\Db\Migration\Informer\MigrationInformerInterface;
use Yiisoft\Db\Migration\Informer\NullMigrationInformer;
Expand Down Expand Up @@ -33,6 +34,13 @@ public static function definitions(): array
'updateNamespaces()' => [[]],
'updatePaths()' => [[]],
],
Migrator::class => [
'__constructor()' => [
'historyTable' => '{{%migration}}',
'migrationNameLimit' => 180,
'maxSqlOutputLength' => null,
],
],
MigrationInformerInterface::class => NullMigrationInformer::class,
];
}
Expand Down
8 changes: 5 additions & 3 deletions src/MigrationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
use Yiisoft\Db\Migration\Informer\MigrationInformerInterface;

use function implode;
use function ltrim;
use function microtime;
use function rtrim;
use function sprintf;
use function substr;

Expand All @@ -24,7 +26,7 @@ final class MigrationBuilder extends AbstractMigrationBuilder
public function __construct(
private ConnectionInterface $db,
private MigrationInformerInterface $informer,
private int $maxSqlOutputLength = 0
private ?int $maxSqlOutputLength = null,
) {
parent::__construct($this->db->getSchema());
}
Expand All @@ -51,8 +53,8 @@ public function getDb(): ConnectionInterface
public function execute(string $sql, array $params = []): void
{
$sqlOutput = $sql;
if (0 < $this->maxSqlOutputLength && $this->maxSqlOutputLength < strlen($sql)) {
$sqlOutput = substr($sql, 0, $this->maxSqlOutputLength) . ' [... hidden]';
if ($this->maxSqlOutputLength !== null && $this->maxSqlOutputLength < strlen($sql)) {
$sqlOutput = ltrim(rtrim(substr($sql, 0, $this->maxSqlOutputLength)) . ' [... hidden]');
}

$time = $this->beginCommand("Execute SQL: $sqlOutput");
Expand Down
4 changes: 3 additions & 1 deletion src/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public function __construct(
private ConnectionInterface $db,
private MigrationInformerInterface $informer,
private string $historyTable = '{{%migration}}',
private ?int $migrationNameLimit = 180
private ?int $migrationNameLimit = 180,
private ?int $maxSqlOutputLength = null,
) {
}

Expand Down Expand Up @@ -157,6 +158,7 @@ private function createBuilder(?MigrationInformerInterface $informer = null): Mi
return new MigrationBuilder(
$this->db,
$informer ?? $this->informer,
$this->maxSqlOutputLength,
);
}
}
2 changes: 1 addition & 1 deletion tests/Common/AbstractMigrationBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ protected function assertInformerOutputContains(string $string): void
$this->assertStringContainsString($string, $this->informer->getOutput());
}

private function prepareVariables(int $maxSqlOutputLength = 0): void
private function prepareVariables(int|null $maxSqlOutputLength = null): void
{
$this->db = $this->container->get(ConnectionInterface::class);

Expand Down
42 changes: 42 additions & 0 deletions tests/Common/AbstractMigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Migration\Informer\NullMigrationInformer;
use Yiisoft\Db\Migration\Migrator;
use Yiisoft\Db\Migration\Tests\Support\Migrations\M231015155500ExecuteSql;
use Yiisoft\Db\Migration\Tests\Support\Stub\StubMigration;
use Yiisoft\Db\Migration\Tests\Support\Stub\StubMigrationInformer;

abstract class AbstractMigratorTest extends TestCase
{
Expand Down Expand Up @@ -81,4 +83,44 @@ public function testGetMigrationNameLimitFromSchema(): void

$this->assertGreaterThan(180, $migrator->getMigrationNameLimit());
}

public function testMaxSqlOutputLength(): void
{
$informer = new StubMigrationInformer();

$migrator = new Migrator(
$this->container->get(ConnectionInterface::class),
$informer,
maxSqlOutputLength: 20,
);

$migrator->up(new M231015155500ExecuteSql());

$this->assertStringContainsString(
'Execute SQL: CREATE TABLE person [... hidden] ... Done',
$informer->getOutput(),
);

$migrator->down(new M231015155500ExecuteSql());
}

public function testZeroMaxSqlOutputLength(): void
{
$informer = new StubMigrationInformer();

$migrator = new Migrator(
$this->container->get(ConnectionInterface::class),
$informer,
maxSqlOutputLength: 0,
);

$migrator->up(new M231015155500ExecuteSql());

$this->assertStringContainsString(
'Execute SQL: [... hidden] ... Done',
$informer->getOutput(),
);

$migrator->down(new M231015155500ExecuteSql());
}
}
33 changes: 33 additions & 0 deletions tests/Support/Migrations/M231015155500ExecuteSql.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Db\Migration\Tests\Support\Migrations;

use Yiisoft\Db\Migration\MigrationBuilder;
use Yiisoft\Db\Migration\RevertibleMigrationInterface;
use Yiisoft\Db\Migration\TransactionalMigrationInterface;

/**
* Execute SQL
*/
final class M231015155500ExecuteSql implements RevertibleMigrationInterface, TransactionalMigrationInterface
{
public function up(MigrationBuilder $b): void
{
$b->execute(
<<<SQL
CREATE TABLE person (
id INT,
first_name VARCHAR(100),
last_name VARCHAR(100)
)
SQL,
);
}

public function down(MigrationBuilder $b): void
{
$b->execute('DROP TABLE person');
}
}

0 comments on commit 59f8546

Please sign in to comment.