Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use maxSqlOutputLength #211

Merged
merged 11 commits into from
Oct 16, 2023
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' => 0,
vjik marked this conversation as resolved.
Show resolved Hide resolved
],
],
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');
}
}