hauleth

hauleth

Guidance Counsellor

Ecto supports SQL views, even though these aren’t used that often. I think that one of the main reasons why these aren’t used is that creating them in migrations isn’t that simple (another one is that plain, non-materialised, views aren’t that needed when using Ecto).

I decided to write small library that will help with that.

This provide 2 functionalities:

Simplify writing views by utilising familiar Ecto.Query:

defmodule MyApplication.Repo.Migration.CreateViewFoo do
  use Ecto.Migration
  use EctoView.Migration # This line **MUST** be after `use Ecto.Migration`

  def change do
    query = from foo in "foos", # This **MUST** select from table name, no schemas allowed
      where: foo.bar == 2137,
      select: %{ # This **MUST** be a map of selected fields
        a: foo.a,
        b: foo.b
      }

    create view("foos_view", query)
  end
end

This will create view foos_view that is created with query SELECT a AS a, b AS b FROM foos.

There are restrictions for queries though - due to nature of migrations, we cannot rely on schemas in application, so the query must use raw table names passed as string. In addition to that, we need to provide select which must be a map of selected values.

Materialised views can be created the same way as plain views, just use materialised_view/2 instead of view/2.

Additional functionality is helper function for refreshing materialised views:

defmodule MyApplication.Repo do
  use Ecto.Repo, # …
  use EctoView

  # …
end

And now you can use Repo.refresh_materialized_view("my_view") to refresh content of that view.

There currently is no mechanism to automate refreshments of materialised views and that is left to the user to implement one.

Showing Posts 1 to 2

al2o3cr

al2o3cr

Haven’t tried it yet, but would it work to declare the specific schema-bits that the migration cares about locally (inside defmodule MyApplication.Repo.Migration.CreateViewFoo)?

That’s an Elixir translation of an approach I’ve used before in Rails migrations.

hauleth

hauleth OP

Theoretically you could, but IMHO that wouldn’t be that different from simply using select in query with a map. Writing something like:

defmodule MyApplication.Repo.Migration.CreateViewFoo do
  use Ecto.Migration
  use EctoView.Migration # This line **MUST** be after `use Ecto.Migration`

  defmodule Foo do
    use EctoView.Schema

    migration_schema "foos" do
      field :a
      field :b
      field :bar
    end
  end

  def change do
    query = from foo in Foo,
      where: foo.bar == 2137,

    create view("foos_view", query)
  end
end

For me isn’t more readable. It introduces needless new module and a lot of visual cruft, where simple Ecto.Query can contain all that information without need for such magic.

— All posts loaded —

Where Next? Top

Trending in Announcing Top

woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New
anuaralfetahe
Hello Published a new library - ProcessHub! ProcessHub is a library designed to manage process distribution within the Elixir cluster. ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews