Database views are a powerful tool, but it can be hard to fit them into your Ecto migrations.
This tool provides a generator to create migrations for views that are written directly in SQL.
mix ecto.gen.view_migration my_view_nameThis will generate two files:
priv/repo/migrations/[TIMESTAMP]_load_sql_my_view_name.exspriv/repo/sql/my_view_name_[TIMESTAMP].sql
You write your view in the .sql file and the autogenerated migration will handle loading it and dropping it on revert.
If you are changing an existing view, the migration generator will detect this and load the old version on revert accordingly.
Note that, at least in Postgres, create or replace view lets you modify the query not the columns. We will want to let the user specify alter view or some other mechanism if they need to add or delete columns. Currently, you'd have to do this by hand in the migration.
The package can be installed by adding ecto_view_migrations to your list of dependencies in mix.exs:
def deps do
[
{:ecto_view_migrations, "~> 0.1.0"}
]
endDocumentation can be at https://hexdocs.pm/ecto_view_migrations.