Skip to content

Commit d227ecd

Browse files
authored
Output rawSql() for execute() method (#230)
1 parent 5ae8c9d commit d227ecd

3 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/MigrationBuilder.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use function rtrim;
2121
use function sprintf;
2222
use function substr;
23+
use function trim;
2324

2425
final class MigrationBuilder extends AbstractMigrationBuilder
2526
{
@@ -52,13 +53,15 @@ public function getDb(): ConnectionInterface
5253
*/
5354
public function execute(string $sql, array $params = []): void
5455
{
55-
$sqlOutput = $sql;
56-
if ($this->maxSqlOutputLength !== null && $this->maxSqlOutputLength < strlen($sql)) {
57-
$sqlOutput = ltrim(rtrim(substr($sql, 0, $this->maxSqlOutputLength)) . ' [... hidden]');
56+
$command = $this->db->createCommand($sql)->bindValues($params);
57+
$sqlOutput = trim($command->getRawSql());
58+
59+
if ($this->maxSqlOutputLength !== null && $this->maxSqlOutputLength < strlen($sqlOutput)) {
60+
$sqlOutput = ltrim(rtrim(substr($sqlOutput, 0, $this->maxSqlOutputLength)) . ' [... hidden]');
5861
}
5962

6063
$time = $this->beginCommand("Execute SQL: $sqlOutput");
61-
$this->db->createCommand($sql)->bindValues($params)->execute();
64+
$command->execute();
6265
$this->endCommand($time);
6366
}
6467

tests/Common/AbstractMigrationBuilderTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ protected function setUp(): void
3131
public function testExecute(): void
3232
{
3333
$this->builder->createTable('test', ['id' => $this->builder->integer()]);
34-
$this->builder->execute('DROP TABLE {{test}}');
34+
35+
$sql = 'DROP TABLE {{test}}';
36+
$this->builder->execute($sql);
37+
38+
$sqlOutput = $this->db->getQuoter()->quoteSql($sql);
3539

3640
$this->assertEmpty($this->db->getTableSchema('test_table'));
37-
$this->assertInformerOutputContains(' > Execute SQL: DROP TABLE {{test}} ... Done in ');
41+
$this->assertInformerOutputContains(" > Execute SQL: $sqlOutput ... Done in ");
3842
}
3943

4044
public function testInsert(): void

tests/Support/Migrations/M231015155500ExecuteSql.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function up(MigrationBuilder $b): void
1717
{
1818
$b->execute(
1919
<<<SQL
20-
CREATE TABLE person (
20+
CREATE TABLE person (
2121
id INT,
2222
first_name VARCHAR(100),
2323
last_name VARCHAR(100)

0 commit comments

Comments
 (0)