crystal-db icon indicating copy to clipboard operation
crystal-db copied to clipboard

`DB::Serializable` deserialization breaks at compile time if a property type includes a `DB` type

Open jgaskins opened this issue 1 year ago • 4 comments

I have the concept of a Transaction in my app, and when I added a property whose type included that name (in my case it was a Transaction::Status enum), it stopped compiling with this error message:

 > 40 |                   __temp_2301 =
 > 41 |
 > 42 |                       rs.read(Transaction::Status)
                                      ^------------------
Error: undefined constant Transaction::Status

But it very clearly exists in my model file. It turns out, the issue is that DB::Serializable sees Transaction and the Crystal compiler interprets that as DB::Transaction, but the error message doesn't make that clear. I assume this would also be the case if you had top-level types like Error or Driver.

require "pg"

pg = DB.open("postgres:///")

pg.query_all <<-SQL, as: Transaction
  SELECT
    gen_random_uuid() id,
    0 status
  FROM generate_series(1, 10) AS transactions
  SQL

struct Transaction
  include DB::Serializable

  getter id : UUID
  getter status : Status

  enum Status
    Pending
    Complete
    Canceled
  end
end

jgaskins avatar Mar 10 '24 20:03 jgaskins