alexdesousa / ayesql

Library for using raw SQL in Elixir
MIT License
135 stars 13 forks source link

Recompile Query module when .sql is changed #2

Closed omh closed 5 years ago

omh commented 5 years ago

Hi,

Currently, if you change the .sql file the query module that refers to via defqueries, it isn't recompiled. This means new queries are unavailable until the Query module is recompiled. Which means making an arbitrary change to it even though nothing has changed.

I got a prototype working with a Mix compiler that checks MD5 of the .sql and touches the Query module if it changed. This triggers a recompile. Would a pull request for that be of interest?

To achieve it I had to change the API a tad:

From:

defmodule MyApp.Query do
  use AyeSQL
  defqueries("queries.sql")
end

To:

defmodule MyApp.Query do
  use AyeSQL
  @defqueries "queries.sql"
end

It's a breaking change, but it's very easy to update. Let me know what you think.

alexdesousa commented 5 years ago

@omh Hey! Thanks for the suggestion. I think this could be resolved by adding @external_resource <filename> module attribute inside the defqueries macro.

omh commented 5 years ago

Yes, you're right! I did not know about @external_resource, thanks! Could have saved me from a fair bit of work :) Oh well, learned a lot anyways! I'll see if I can make a PR to tweak the documentation a bit.

alexdesousa commented 5 years ago

Still, I'm going to add this as an built-in enhancement for defqueries/1, so I'll reopen this to keep it in the backlog.

omh commented 5 years ago

Cool. Made a small PR that uses @external_resource -> #3

alexdesousa commented 5 years ago

This feature is supported now :)