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

Extract a database view for streaks to enable simpler and more powerful querying #4

merged 4 commits into from Jan 12, 2017


Copy link

stevegrossi commented Jan 12, 2017

I ❤️ ❤️ ❤️ how you can build Ecto schemas on top of database views just like you can with tables. This let me push the data logic for streaks down into the database and allow a much simpler querying syntax (no raw SQL) in the Elixir code. A refactor of the streaks query also enabled richer querying, such as for the longest streak as well as the current one:

screen shot 2017-01-12 at 5 59 21 am

stevegrossi added 4 commits Jan 11, 2017
SO COOL that you can base schemas off of database views just like tables

Begone, raw SQL!
|> assoc(:streaks)
|> where([s], s.end >= ^Habits.Date.yesterday)
|> select([s], s.length)

This comment has been minimized.

Copy link

stevegrossi Jan 12, 2017

Author Owner

I thought the binding syntax (used here) would be equivalent with the bindingless syntax, e.g.

|> select([:length])

but they return different results. The binding syntax returns the bare value of the selected column, while the bindingless syntax returns an entire %Streak{} struct with only the selected attribute present.

@stevegrossi stevegrossi merged commit 5508c97 into master Jan 12, 2017
1 check passed
1 check passed
continuous-integration/codeship Build succeeded
@stevegrossi stevegrossi deleted the stevegrossi/streaks_view branch Jan 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

1 participant
You can’t perform that action at this time.