Something went wrong. Try again.
Helpers for using SQL views with Ecto
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142# SPDX-FileCopyrightText: 2026 Łukasz Niemier <~@hauleth.dev>## SPDX-License-Identifier: Apache-2.0
defmodule EctoView do @moduledoc """ Helper functions for refreshing materialised views """
defmacro __using__([]) do quote do @behaviour unquote(__MODULE__)
@impl unquote(__MODULE__) def refresh_materialized_view(queryable, opts \\ []) do unquote(__MODULE__).refresh_materialized_view(__MODULE__, queryable) end end end
@doc """ Refresh materialised view """ @callback refresh_materialized_view(table :: Ecto.Queryable.t()) :: :ok | {:error, term()}
## Implementation functions @doc """ Refresh materialised view for given `queryable`. """ def refresh_materialized_view(repo, queryable, opts \\ []) do # This way we ensure that `schema` is properly escaped schema = schema_for_query(Ecto.Queryable.to_query(queryable))
repo.query("REFRESH MATERIALIZED VIEW \"#{schema}\"", opts) end
defp schema_for_query(%Ecto.Query{from: %Ecto.Query.FromExpr{source: {schema, _}}}) do schema endend