cashapp / sqldelight

SQLDelight - Generates typesafe Kotlin APIs from SQL
https://cashapp.github.io/sqldelight/
Apache License 2.0
6.15k stars 516 forks source link

Cannot use `IF NOT EXISTS` when creating extensions #5356

Open Angel-Ponce opened 2 months ago

Angel-Ponce commented 2 months ago

SQLDelight Version

2.0.2

SQLDelight Dialect

PostgreSQL

Describe the Bug

I'm trying to execute:

CREATE EXTENSION IF NOT EXISTS unaccent;

But it seems that it's not supported yet.

image

Stacktrace

No response

griffio commented 2 months ago

🤔 It should already be in 2.0.2

https://github.com/cashapp/sqldelight/blob/a82c18430fd2cdb7543a0740f1a833f58858adb7/dialects/postgresql/src/main/kotlin/app/cash/sqldelight/dialects/postgresql/grammar/PostgreSql.bnf#L402

(Though your screen shot shows "CREATE IF NOT EXISTS EXTENSION..." that is invalid)

Here is an example project https://github.com/griffio/sqldelight-postgres-textsearch/blob/master/src/main/sqldelight/griffio/migrations/V1__Initial_version.sqm that uses pg_trgm for improved text searching in Postgres.

Note: The big issue with using PostgreSql extensions with SqlDelight is that any functions and types that are loaded by the extension, SqlDelight will not recognise them. The example here shows this can be mitigated with creating a custom dialect module https://github.com/griffio/sqldelight-custom-dialect/tree/master adding in some of the text search functions