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

Postgresql migrations using rename column cause compilation error e.g ALTER TABLE X RENAME COLUMN y TO Z #4879

Closed
griffio opened this issue Dec 4, 2023 · 3 comments · Fixed by #4880

Comments

@griffio
Copy link
Contributor

griffio commented Dec 4, 2023

SQLDelight Version

2.0.*

Operating System

Any

Gradle Version

Any

Kotlin Version

Any

Dialect

Postgresql

AGP Version

No response

Describe the Bug

When using migration files (sqm) the rename column causes compilation error - happens in 2.0.0 and 2.0.1

ALTER TABLE Address RENAME COLUMN address TO address_1;

Work-around - rename query files first as the problem appears to be the error annotations erroring with a class cast exception
either ColumnDefMixin or SqlColumnName

Stacktrace

Postgresql
Cause: class app.cash.sqldelight.dialects.postgresql.grammar.psi.impl.PostgreSqlAlterTableRenameColumnImpl cannot be cast to class com.alecstrong.sql.psi.core.psi.mixins.ColumnDefMixin (app.cash.sqldelight.dialects.postgresql.grammar.psi.impl.PostgreSqlAlterTableRenameColumnImpl and com.alecstrong.sql.psi.core.psi.mixins.ColumnDefMixin are in unnamed module of loader org.gradle.internal.classloader.VisitableURLClassLoader @22a8bdc3)

Gradle Build Script

postgresql

sqldelight {
    databases {
        create("Sample") {
            deriveSchemaFromMigrations.set(true)
            migrationOutputDirectory = file("$buildDir/generated/migrations")
            migrationOutputFileFormat = ".sql" // Defaults to .sql
            packageName.set("griffio.queries")
            dialect(libs.sqldelight.postgresql.dialect)
        }
    }
}
@griffio griffio added the bug label Dec 4, 2023
@griffio griffio changed the title Migrations using rename column cause compilation error e.g ALTER TABLE X RENAME COLUMN y TO Z Postgresql migrations using rename column cause compilation error e.g ALTER TABLE X RENAME COLUMN y TO Z Dec 7, 2023
@griffio
Copy link
Contributor Author

griffio commented Dec 7, 2023

🚬 🤔 Sqlite has similar issues but needs it's own issue #4897 and PR as fixes are slightly different

@0xEDU
Copy link

0xEDU commented Jan 15, 2024

Could you elaborate on the work around for this? I couldn't manage to make it work

@griffio
Copy link
Contributor Author

griffio commented Jan 16, 2024

🤔 @0xEDU - If I remember - this issue was one of several problems running migrations that I created PRs.
I believe that an error is thrown because as a result of changing - for example a column name with ALTER TABLE Address RENAME COLUMN address TO address_1; then naturally any queries won't compile that have the old name. However the class cast is thrown during that check. So - the work around was simply to ensure any queries using address are changed to address_1 before running the migration.
If you are running into problems - I have a project https://github.com/griffio/sqldelight-postgres-01 where I am tracking the migration fixes that I am making to save you from bashing your head 🤕

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