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 table prefix on raw expressions when a table name is in the string #385

Closed
simonecosci opened this issue May 30, 2024 · 1 comment · Fixed by #386
Closed

Add table prefix on raw expressions when a table name is in the string #385

simonecosci opened this issue May 30, 2024 · 1 comment · Fixed by #386
Assignees

Comments

@simonecosci
Copy link

Pulse Version

v1.2.1

Laravel Version

11.8.0

PHP Version

8.2.12

Livewire Version

3.5.0

Database Driver & Version

Postgres 13

Description

I don't know if this can be considered a bug ...

When using a raw Expression consider using the table-prefix before the table name if the table name is hardcoded
I have table names prefixed with 'kp_' and pulse throws the following exception:

#18 {main}An exception happened in PulseSQLSTATE[42P01]: Undefined table: 7 ERROR:  missing FROM-clause entry for table "pulse_aggregates"
LINE 1: ... "aggregate", "key_hash") do update set "value" = "pulse_agg...
                                                             ^ (Connection: pgsql, SQL: insert into "kp_pulse_aggregates" ("aggregate", "bucket", "key", "period", "type", "value") values (count, 1717076700, ["select count(*) as aggregate from \"kp_migrations\"","C:\\Software\\kp\\api\\vendor\\laravel\\pulse\\src\\Pulse.php:594"], 60, slow_query, 1), (count, 1717076520, ["select count(*) as aggregate from \"kp_migrations\"","C:\\Software\\kp\\api\\vendor\\laravel\\pulse\\src\\Pulse.php:594"], 360, slow_query, 1), (count, 1717076160, ["select count(*) as aggregate from \"kp_migrations\"","C:\\Software\\kp\\api\\vendor\\laravel\\pulse\\src\\Pulse.php:594"], 1440, slow_query, 1), (count, 1717067520, ["select count(*) as aggregate from \"kp_migrations\"","C:\\Software\\kp\\api\\vendor\\laravel\\pulse\\src\\Pulse.php:594"], 10080, slow_query, 1) on conflict ("bucket", "period", "type", "aggregate", "key_hash") do update set "value" = "pulse_aggregates"."value" + "excluded"."value")

avoid this behavior by using $this->connection()->getTablePrefix() just before the table name in src/Storage/DatabaseStorage.php

Eg.

            match ($driver = $this->connection()->getDriverName()) {
                'mariadb', 'mysql' => [
                    'value' => new Expression('(`value` * `count` + (values(`value`) * values(`count`))) / (`count` + values(`count`))'),
                    'count' => new Expression('`count` + values(`count`)'),
                ],
                'pgsql', 'sqlite' => [
                    'value' => new Expression('("' . $this->connection()->getTablePrefix() . 'pulse_aggregates"."value" * "' . $this->connection()->getTablePrefix() . 'pulse_aggregates"."count" + ("excluded"."value" * "excluded"."count")) / ("' . $this->connection()->getTablePrefix() . 'pulse_aggregates"."count" + "excluded"."count")'),
                    'count' => new Expression('"' . $this->connection()->getTablePrefix() . 'pulse_aggregates"."count" + "excluded"."count"'),
                ],
                default => throw new RuntimeException("Unsupported database driver [{$driver}]"),
            }

Regards
S.

Steps To Reproduce

Put in .env

DB_PREFIX=kp_

@jessarcher
Copy link
Member

Thanks for reporting @simonecosci. I've created a fix for this at #386.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants