kit-clj / kit

Lightweight, modular framework for scalable web development in Clojure
https://kit-clj.github.io/
MIT License
463 stars 43 forks source link

Running migrations fails (HikariProxyConnection cannot be cast to class java.sql.PreparedStatement) #105

Closed caleb closed 1 year ago

caleb commented 1 year ago

I have a new Kit application that uses postgres/conman/migratus and I get this error when I try to migrate my database (even if there are no migrations, I added one and got the same result):

user=> (migrate)
2023-06-05 10:31:17,404 [main] INFO  migratus.core - Starting migrations
2023-06-05 10:31:17,429 [main] INFO  migratus.database - updating migration table 'schema_migrations'
2023-06-05 10:31:17,431 [main] INFO  migratus.core - Ending migrations
Execution error (ClassCastException) at next.jdbc.prepare$eval23259$fn__23260/invokePrim (prepare.clj:39).
class com.zaxxer.hikari.pool.HikariProxyConnection cannot be cast to class java.sql.PreparedStatement (com.zaxxer.hikari.pool.HikariProxyConnection is in unnamed module of loader 'app'; java.sql.PreparedStatement is in module java.sql of loader 'platform')

user=>        (println (:db.sql/connection state/system))
#object[com.zaxxer.hikari.HikariDataSource 0x2402b297 HikariDataSource (HikariPool-1)]
nil

I am able to use my query-fn to execute queries, and they correctly connect to the database and return the expected results.

Here is my system.edn:

{
  ...

 :db.sql/connection
 #profile {:dev  {:jdbc-url #env JDBC_URL}
           :test {}
           :prod {:jdbc-url   #env JDBC_URL
                  :init-size  1
                  :min-idle   1
                  :max-idle   8
                  :max-active 32}}

 :db.sql/query-fn
 {:conn     #ig/ref :db.sql/connection
  :options  {}
  :filename "queries.sql"}

 :db.sql/migrations
 {:store            :database
  :db               {:datasource #ig/ref :db.sql/connection}
  :migrate-on-init? false}
}

Any ideas?

yogthos commented 1 year ago

Hmm not sure what's causing the issue. I tried creating a fresh project locally, and can't seem to reproduce the error locally. Could it be something with the migrations themselves as opposed to system.edn config?

One note with Migratus is that it requires individual statements to be separated using --;;.

caleb commented 1 year ago

That’s odd, I ran it with no migrations and it gave me the same error.I will try to create a test case. I didn’t really do anything to the app (except for pull the JDBC URI out to an environment variable), but I did create it with the +full profile.

yogthos commented 1 year ago

Yeah that's odd, nothing jumps out at me in the config. The error seems to indicate that there's some mismatch between the HikariCP connection object and what the driver is expecting.

caleb commented 1 year ago

I apologize. I was connecting to a database that apparently already had a schema_migrations table, which was causing the error above.

Thanks for trying to help.

yogthos commented 1 year ago

Ah no worries, glad to hear things are working. :)