diff --git a/src/MigrationBuilder.php b/src/MigrationBuilder.php index adf16a47..a3aeef4c 100644 --- a/src/MigrationBuilder.php +++ b/src/MigrationBuilder.php @@ -20,6 +20,7 @@ use function rtrim; use function sprintf; use function substr; +use function trim; final class MigrationBuilder extends AbstractMigrationBuilder { @@ -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); } diff --git a/tests/Common/AbstractMigrationBuilderTest.php b/tests/Common/AbstractMigrationBuilderTest.php index ad78163d..a10cefa9 100644 --- a/tests/Common/AbstractMigrationBuilderTest.php +++ b/tests/Common/AbstractMigrationBuilderTest.php @@ -31,10 +31,14 @@ protected function setUp(): void public function testExecute(): void { $this->builder->createTable('test', ['id' => $this->builder->integer()]); - $this->builder->execute('DROP TABLE {{test}}'); + + $sql = 'DROP TABLE {{test}}'; + $this->builder->execute($sql); + + $sqlOutput = $this->db->getQuoter()->quoteSql($sql); $this->assertEmpty($this->db->getTableSchema('test_table')); - $this->assertInformerOutputContains(' > Execute SQL: DROP TABLE {{test}} ... Done in '); + $this->assertInformerOutputContains(" > Execute SQL: $sqlOutput ... Done in "); } public function testInsert(): void diff --git a/tests/Support/Migrations/M231015155500ExecuteSql.php b/tests/Support/Migrations/M231015155500ExecuteSql.php index 25363ee1..8c2ab8b5 100644 --- a/tests/Support/Migrations/M231015155500ExecuteSql.php +++ b/tests/Support/Migrations/M231015155500ExecuteSql.php @@ -17,7 +17,7 @@ public function up(MigrationBuilder $b): void { $b->execute( <<