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

Output rawSql() for execute() method #230

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
8 changes: 6 additions & 2 deletions tests/Common/AbstractMigrationBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/Migrations/M231015155500ExecuteSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function up(MigrationBuilder $b): void
{
$b->execute(
<<<SQL
CREATE TABLE person (
CREATE TABLE person (
id INT,
first_name VARCHAR(100),
last_name VARCHAR(100)
Expand Down
Loading