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

Add method QueryExecuted::toRawSql() #52192

Merged
merged 2 commits into from
Jul 22, 2024

Conversation

spawnia
Copy link
Contributor

@spawnia spawnia commented Jul 19, 2024

This makes it easier to debug the executed queries when using DB::listen().

Before, we did something like this and got a result that was hardly readable:

DB::listen(function (QueryExecuted $query): void {
    $sql = str_replace("\n", ' ', $query->sql);
    $bindings = json_encode($query->bindings);

    file_put_contents(
        filename: storage_path('logs/query.log'),
        data: "SQL: {$sql} ||| Bindings: {$bindings} ||| Time: {$query->time}ms\n",
        flags: FILE_APPEND,
    );
});

// SQL: insert into `competence_detail_criteria` (`competence_criteria_id`, `competence_detail_id`, `valid_from`, `valid_to`, `userid`, `first_id`) values (?, ?, ?, ?, ?, ?) ||| Bindings: [3,1,"2024-07-19 10:59:02","9999-12-31 23:55:55",1,0] ||| Time: 0.84ms

With this added method, achieving a readable result becomes much simpler:

DB::listen(function (QueryExecuted $query): void {
    file_put_contents(
        filename: storage_path('logs/query.log'),
        data: "SQL: {$query->toRawSql()} ||| Time: {$query->time}ms\n",
        flags: FILE_APPEND,
    );
});

// SQL: insert into `competence_detail_criteria` (`competence_criteria_id`, `competence_detail_id`, `valid_from`, `valid_to`, `userid`, `first_id`) values (4, 1, '2024-07-19 11:10:29', '9999-12-31 23:55:55', 1, 0) ||| Time: 0.2ms

@taylorotwell
Copy link
Member

Could you make the test an integration test?

@taylorotwell taylorotwell marked this pull request as draft July 19, 2024 16:16
@taylorotwell
Copy link
Member

Please mark as ready for review when you want me to take another look. 👍

@spawnia spawnia marked this pull request as ready for review July 22, 2024 07:41
@spawnia
Copy link
Contributor Author

spawnia commented Jul 22, 2024

Could you make the test an integration test?

Done. I could not find a test class that is more fitting for this use case, so I created a new one that just generally tests database functionality.

This makes it easier to debug the executed queries when using `DB::listen()`.

Before, we did something like this and got a result that was hardly readable:

```php
DB::listen(function (QueryExecuted $query): void {
    $sql = str_replace("\n", ' ', $query->sql);
    $bindings = json_encode($query->bindings);

    file_put_contents(
        filename: storage_path('logs/query.log'),
        data: "SQL: {$sql} ||| Bindings: {$bindings} ||| Time: {$query->time}ms\n",
        flags: FILE_APPEND,
    );
});

// SQL: insert into `competence_detail_criteria` (`competence_criteria_id`, `competence_detail_id`, `valid_from`, `valid_to`, `userid`, `first_id`) values (?, ?, ?, ?, ?, ?) ||| Bindings: [3,1,"2024-07-19 10:59:02","9999-12-31 23:55:55",1,0] ||| Time: 0.84ms
```

With this added method, achieving a readable result becomes much simpler:

```php
DB::listen(function (QueryExecuted $query): void {
    file_put_contents(
        filename: storage_path('logs/query.log'),
        data: "SQL: {$query->toRawSql()} ||| Time: {$query->time}ms\n",
        flags: FILE_APPEND,
    );
});

// SQL: insert into `competence_detail_criteria` (`competence_criteria_id`, `competence_detail_id`, `valid_from`, `valid_to`, `userid`, `first_id`) values (4, 1, '2024-07-19 11:10:29', '9999-12-31 23:55:55', 1, 0) ||| Time: 0.2ms
```
@spawnia
Copy link
Contributor Author

spawnia commented Jul 22, 2024

The failing tests seem unrelated to the changes in this pull request, see https://github.com/laravel/framework/actions/runs/10009570585.

@taylorotwell taylorotwell merged commit a13b726 into laravel:11.x Jul 22, 2024
16 of 29 checks passed
@spawnia spawnia deleted the query-executed-to-raw-sql branch July 23, 2024 08:43
spawnia added a commit to mll-lab/sentry-laravel that referenced this pull request Jul 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants