Ecto: Support views in migrations

Created on 16 Nov 2016  路  16Comments  路  Source: elixir-ecto/ecto

The idea is to introduce functions called view/1 and view/2 such that:

create view(:my_view, from(u in "users", where: u.type == :student))
drop view(:my_view)

create requires a query to be given to the view, drop does not.

We will also introduce a create_or_replace/1 function, similar to create/1, but mean to be used with views.

Advanced

Most helpful comment

@joshuataylor we can support a :materialized option in view/3 or a materialized_view/2 function as well. I will need to study the behaviour in different databases before deciding which one works best.

All 16 comments

Definitely want that, I have a few view's in my system right now but I setup and tear them down via SQL.

Will be keeping an eye on this. The frontend to one of our biggest apps relies fairly heavily on views, would be very nice to manage them natively

What about materialized views?

@joshuataylor we can support a :materialized option in view/3 or a materialized_view/2 function as well. I will need to study the behaviour in different databases before deciding which one works best.

can i work on this one?

Yes, please!

I was just thinking about introducing it by myself. What would be useful with this one is:

  • triggers
  • functions

As it would allow us to create additional things that would be useful with views.

We should carefully consider if we're able in any way to provide something more usable than execute(SQL).
I'm not sure I can imagine providing something more than

create function("""
some sql
""")

and I'm not sure I see advantage over

execute """
CREATE FUNCTION some sql
"""

@michalmuskala there is one: autocreate down method for rollbacks. However there is only a little (if any) advantage of doing that. Maybe except allowing to reference function types using Ecto.Type.type/0 which would be handy.

Hi, I want to start contributing to Ecto (and Elixir in general) and I was about to suggest this feature. Is anyone working on it? Is it still in discussion? I am new to open source projects so forgive me if I don't understand part of the workflow on this. Thanks!

I don't think there is someone working on it right now. If you want to start contributing on this area, maybe #1832 is a better starting point, for you to get acquainted with the codebase. :)

@josevalim Nice, I will try that. Tks

We have stepped down from this feature because using Ecto.Query to generate views had unwarranted side-effects. A discussion of other approaches, which could be encapsulated in helpers that will be part of future Ecto versions, can be found in #1805.

Sooo, currently the only way is still execute()? I've already used the from() syntax to create my 20 queries, any way to put those into the execution string?

@KristerV you can use Repo.to_sql to get those queries as SQL statements. But it is probably best if you still pass them as strings.

Yeah I realized, that your migrations need to be static (and not connected to any schemas or anything), so best is raw strings anyway. For future lurkers :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

atsheehan picture atsheehan  路  4Comments

madshargreave picture madshargreave  路  3Comments

shahryarjb picture shahryarjb  路  3Comments

tverlaan picture tverlaan  路  3Comments

fuelen picture fuelen  路  3Comments