Helpers for using SQL views with Ecto
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Initial release of EctoView

+508
+8
.formatter.exs
··· 1 + # SPDX-FileCopyrightText: 2026 Łukasz Niemier <~@hauleth.dev> 2 + # 3 + # SPDX-License-Identifier: Apache-2.0 4 + 5 + # Used by "mix format" 6 + [ 7 + inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"] 8 + ]
+30
.gitignore
··· 1 + # SPDX-FileCopyrightText: 2026 Łukasz Niemier <~@hauleth.dev> 2 + # 3 + # SPDX-License-Identifier: Apache-2.0 4 + 5 + # The directory Mix will write compiled artifacts to. 6 + /_build/ 7 + 8 + # If you run "mix test --cover", coverage assets end up here. 9 + /cover/ 10 + 11 + # The directory Mix downloads your dependencies sources to. 12 + /deps/ 13 + 14 + # Where third-party dependencies like ExDoc output generated docs. 15 + /doc/ 16 + 17 + # Ignore .fetch files in case you like to edit your project deps locally. 18 + /.fetch 19 + 20 + # If the VM crashes, it generates a dump, let's ignore it too. 21 + erl_crash.dump 22 + 23 + # Also ignore archive artifacts (built via "mix archive.build"). 24 + *.ez 25 + 26 + # Ignore package tarball (built via "mix hex.build"). 27 + ecto_view-*.tar 28 + 29 + # Temporary files, for example, from tests. 30 + /tmp/
+73
LICENSES/Apache-2.0.txt
··· 1 + Apache License 2 + Version 2.0, January 2004 3 + http://www.apache.org/licenses/ 4 + 5 + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 + 7 + 1. Definitions. 8 + 9 + "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. 10 + 11 + "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. 12 + 13 + "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 14 + 15 + "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. 16 + 17 + "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. 18 + 19 + "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. 20 + 21 + "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). 22 + 23 + "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. 24 + 25 + "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." 26 + 27 + "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 28 + 29 + 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 30 + 31 + 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 32 + 33 + 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: 34 + 35 + (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and 36 + 37 + (b) You must cause any modified files to carry prominent notices stating that You changed the files; and 38 + 39 + (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and 40 + 41 + (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. 42 + 43 + You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 44 + 45 + 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 46 + 47 + 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 48 + 49 + 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 50 + 51 + 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 52 + 53 + 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. 54 + 55 + END OF TERMS AND CONDITIONS 56 + 57 + APPENDIX: How to apply the Apache License to your work. 58 + 59 + To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. 60 + 61 + Copyright [yyyy] [name of copyright owner] 62 + 63 + Licensed under the Apache License, Version 2.0 (the "License"); 64 + you may not use this file except in compliance with the License. 65 + You may obtain a copy of the License at 66 + 67 + http://www.apache.org/licenses/LICENSE-2.0 68 + 69 + Unless required by applicable law or agreed to in writing, software 70 + distributed under the License is distributed on an "AS IS" BASIS, 71 + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 72 + See the License for the specific language governing permissions and 73 + limitations under the License.
+62
README.md
··· 1 + <!-- 2 + SPDX-FileCopyrightText: 2026 Łukasz Niemier <~@hauleth.dev> 3 + 4 + SPDX-License-Identifier: Apache-2.0 5 + --> 6 + 7 + # EctoView 8 + 9 + Helper functions for creating database views using `Ecto.Query` helpers. 10 + 11 + ## Installation 12 + 13 + Add `ecto_view` to your list of dependencies in `mix.exs`: 14 + 15 + ```elixir 16 + def deps do 17 + [ 18 + {:ecto_view, "~> 1.0"} 19 + ] 20 + end 21 + ``` 22 + 23 + ## Usage 24 + 25 + ```elixir 26 + defmodule MyApplication.Repo.Migration.CreateViewFoo do 27 + use Ecto.Migration 28 + use EctoView.Migration # This line **MUST** be after `use Ecto.Migration` 29 + 30 + def change do 31 + query = from foo in "foos", # This **MUST** select from table name, no schemas allowed 32 + where: foo.bar == 2137, 33 + select: %{ # This **MUST** be a map of selected fields 34 + a: foo.a, 35 + b: foo.b 36 + } 37 + 38 + create view("foos_view", query) 39 + end 40 + end 41 + ``` 42 + 43 + If you want, you can also create materialised view using `materialized_view/2` 44 + function. 45 + 46 + If you create materialised view, then you can use `EctoView.refresh_materialized_view/3` 47 + function to refresh content of given view. If you will add: 48 + 49 + ```elixir 50 + defmodule MyApplication.Repo do 51 + use Ecto.Repo, … 52 + use EctoView 53 + 54 + # … 55 + end 56 + ``` 57 + 58 + Then there will be a helper function `Repo.refresh_materialised_view/2` that 59 + will call respective function for that repo. 60 + 61 + The docs can be found at [https://hexdocs.pm/ecto_view](https://hexdocs.pm/ecto_view). 62 +
+16
config/config.exs
··· 1 + # SPDX-FileCopyrightText: 2026 Łukasz Niemier <~@hauleth.dev> 2 + # 3 + # SPDX-License-Identifier: Apache-2.0 4 + 5 + import Config 6 + 7 + config :ecto_view, ecto_repos: [EctoView.Repo] 8 + 9 + config :ecto_view, EctoView.Repo, 10 + username: System.get_env("POSTGRES_USER", "postgres"), 11 + password: System.get_env("POSTGRES_PASSWORD", "postgres"), 12 + database: System.get_env("POSTGRES_DB", "ecto_view"), 13 + hostname: "postgres", 14 + socket_dir: System.get_env("PGHOST"), 15 + show_sensitive_data_on_connection_error: true, 16 + pool_size: 10
+41
lib/ecto_view.ex
··· 1 + # SPDX-FileCopyrightText: 2026 Łukasz Niemier <~@hauleth.dev> 2 + # 3 + # SPDX-License-Identifier: Apache-2.0 4 + 5 + defmodule EctoView do 6 + @moduledoc """ 7 + Helper functions for refreshing materialised views 8 + """ 9 + 10 + defmacro __using__([]) do 11 + quote do 12 + @behaviour unquote(__MODULE__) 13 + 14 + @impl unquote(__MODULE__) 15 + def refresh_materialized_view(queryable, opts \\ []) do 16 + unquote(__MODULE__).refresh_materialized_view(__MODULE__, queryable) 17 + end 18 + end 19 + end 20 + 21 + @doc """ 22 + Refresh materialised view 23 + """ 24 + @callback refresh_materialized_view(table :: Ecto.Queryable.t()) :: 25 + :ok | {:error, term()} 26 + 27 + ## Implementation functions 28 + @doc """ 29 + Refresh materialised view for given `queryable`. 30 + """ 31 + def refresh_materialized_view(repo, queryable, opts \\ []) do 32 + # This way we ensure that `schema` is properly escaped 33 + schema = schema_for_query(Ecto.Queryable.to_query(queryable)) 34 + 35 + repo.query("REFRESH MATERIALIZED VIEW \"#{schema}\"", opts) 36 + end 37 + 38 + defp schema_for_query(%Ecto.Query{from: %Ecto.Query.FromExpr{source: {schema, _}}}) do 39 + schema 40 + end 41 + end
+145
lib/ecto_view/migration.ex
··· 1 + defmodule EctoView.Migration do 2 + @moduledoc """ 3 + 4 + """ 5 + 6 + defmodule View do 7 + @moduledoc false 8 + 9 + defstruct [:name, :query, materialized?: false] 10 + end 11 + 12 + defmodule InvalidSource do 13 + defexception [:schema] 14 + 15 + @impl true 16 + def message(%__MODULE__{schema: schema}) do 17 + "You cannot query the schema module (#{inspect(schema)}) in migration, as it can fail in future. Use schema name as binary instead" 18 + end 19 + end 20 + 21 + defmodule InvalidSelect do 22 + defexception [:ast] 23 + 24 + @impl true 25 + def message(%__MODULE__{ast: ast}) do 26 + "Selected values must be a map, got #{Macro.to_string(ast)}" 27 + end 28 + end 29 + 30 + defmodule MissingQuery do 31 + defexception message: "Query is required for creating view" 32 + end 33 + 34 + @doc false 35 + defmacro __using__(_opts) do 36 + create_mod = 37 + case Macro.Env.lookup_import(__ENV__, {:create, 1}) do 38 + [{_, mod}] -> mod 39 + [] -> raise "Ecto.Migration is not imported" 40 + end 41 + 42 + quote do 43 + import unquote(create_mod), except: [create: 1, drop: 1, drop_if_exists: 1] 44 + import unquote(__MODULE__) 45 + 46 + def create(%unquote(View){} = view), do: unquote(__MODULE__).__create_view__(view, false) 47 + def create(other), do: unquote(create_mod).create(other) 48 + 49 + def create_or_replace(%unquote(View){} = view), do: unquote(__MODULE__).__create_view__(view, true) 50 + 51 + def drop(%View{} = view), do: unquote(__MODULE__).__drop__(view, false) 52 + def drop(other), do: unquote(create_mod).drop(other) 53 + 54 + def drop_if_exists(%View{} = view), do: unquote(__MODULE__).__drop__(view, true) 55 + def drop_if_exists(other), do: unquote(create_mod).drop(other) 56 + end 57 + end 58 + 59 + @doc false 60 + def __create_view__(%View{name: name, query: query} = view, or_replace?) when query != nil do 61 + {sql, []} = Ecto.Migration.repo().to_sql(:all, query) 62 + or_replace = if or_replace?, do: "OR REPLACE ", else: "" 63 + if_exist = if or_replace?, do: "IF NOT EXIST ", else: "" 64 + materialized = if view.materialized?, do: "MATERIALIZED ", else: "" 65 + 66 + Ecto.Migration.execute( 67 + "CREATE #{or_replace}#{materialized}VIEW #{name} AS #{sql}", 68 + "DROP #{if_exist}#{materialized}VIEW #{name}" 69 + ) 70 + end 71 + 72 + def __create_view__(_, _) do 73 + raise MissingQuery 74 + end 75 + 76 + @doc false 77 + def __drop__(%View{name: name}, if_exists?) do 78 + if_exist = if if_exists?, do: "IF NOT EXIST ", else: "" 79 + 80 + Ecto.Migration.execute("DROP #{if_exist}VIEW #{name}") 81 + end 82 + 83 + @doc """ 84 + Returns view struct that can be given to `Ecto.Migration.create/1` 85 + 86 + ```elixir 87 + create view("products_with_discount", 88 + from(p in "products", 89 + inner_join: d in "discounts", 90 + on: d.product_id == p.id, 91 + select: %{name: p.name})) 92 + ``` 93 + 94 + Passed `query` will be checked if that query: 95 + 96 + - Uses raw table name (usage of schemas is disallowed) 97 + 98 + This is required to prevent making migration in future by editing schema. 99 + Each migration should be independent from the application code. 100 + 101 + - Uses `Ecto.Query.select/3` with map 102 + 103 + This is required to be able to set proper names for all columns in created 104 + view. So in case of `%{foo: a.foo, bar: b.bar}` resulting view with 2 105 + columns (`foo` and `bar`) containing respective selected fields. 106 + 107 + If any of the above is false, then it will be rejected. 108 + """ 109 + def view(name), do: %View{name: name, query: nil} 110 + 111 + def view(name, query), do: %View{name: name, query: expand_select(query)} 112 + 113 + @doc """ 114 + Returns materialized view struct that can be given to `Ecto.Migration.create/1` 115 + 116 + See `view/2` for details. 117 + """ 118 + def materialized_view(name, query), 119 + do: %View{name: name, query: expand_select(query), materialized?: true} 120 + 121 + defp expand_select( 122 + %Ecto.Query{select: %Ecto.Query.SelectExpr{expr: {:%{}, _, fields}} = select} = query 123 + ) do 124 + validate_source(query) 125 + 126 + encoded = 127 + for {name, value} <- fields do 128 + {:fragment, [], expr: value, raw: "AS #{name}"} 129 + end 130 + 131 + %Ecto.Query{query | select: %Ecto.Query.SelectExpr{select | expr: encoded}} 132 + end 133 + 134 + defp expand_select(%Ecto.Query{select: %Ecto.Query.SelectExpr{expr: ast}}) do 135 + raise InvalidSelect, ast: ast 136 + end 137 + 138 + defp validate_source(%Ecto.Query{from: %Ecto.Query.FromExpr{source: source}}) do 139 + case source do 140 + {schema, nil} when is_binary(schema) -> :ok 141 + {_schema, mod} when is_atom(mod) -> 142 + raise InvalidSource, schema: mod 143 + end 144 + end 145 + end
+54
mix.exs
··· 1 + # SPDX-FileCopyrightText: 2026 Łukasz Niemier <~@hauleth.dev> 2 + # 3 + # SPDX-License-Identifier: Apache-2.0 4 + 5 + defmodule EctoView.MixProject do 6 + use Mix.Project 7 + 8 + def project do 9 + [ 10 + app: :ecto_view, 11 + description: "Ecto helpers for creating and refreshing SQL views", 12 + package: [ 13 + licenses: ["Apache-2.0"], 14 + links: %{ 15 + "Tangled" => "https://tangled.org/hauleth.dev/ecto_view" 16 + } 17 + ], 18 + version: "1.0.0", 19 + elixir: "~> 1.12", 20 + start_permanent: Mix.env() == :prod, 21 + elixirc_paths: paths(Mix.env()), 22 + deps: deps(), 23 + docs: docs() 24 + ] 25 + end 26 + 27 + # Run "mix help compile.app" to learn about applications. 28 + def application do 29 + [ 30 + extra_applications: [:logger] 31 + ] 32 + end 33 + 34 + defp docs do 35 + [ 36 + main: "readme", 37 + extras: ["README.md"] 38 + ] 39 + end 40 + 41 + defp paths(env) when env in ~w[dev test]a, do: ~w[lib test/support] 42 + defp paths(_), do: ~w[lib] 43 + 44 + # Run "mix help deps" to learn about dependencies. 45 + defp deps do 46 + [ 47 + {:ecto_sql, "~> 3.0"}, 48 + {:postgrex, ">= 0.0.0", only: [:dev]}, 49 + {:ex_doc, ">= 0.0.0", only: [:dev]} 50 + # {:dep_from_hexpm, "~> 0.3.0"}, 51 + # {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"} 52 + ] 53 + end 54 + end
+15
mix.lock
··· 1 + %{ 2 + "connection": {:hex, :connection, "1.1.0", "ff2a49c4b75b6fb3e674bfc5536451607270aac754ffd1bdfe175abe4a6d7a68", [:mix], [], "hexpm", "722c1eb0a418fbe91ba7bd59a47e28008a189d47e37e0e7bb85585a016b2869c"}, 3 + "db_connection": {:hex, :db_connection, "2.9.0", "a6a97c5c958a2d7091a58a9be40caf41ab496b0701d21e1d1abff3fa27a7f371", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "17d502eacaf61829db98facf6f20808ed33da6ccf495354a41e64fe42f9c509c"}, 4 + "decimal": {:hex, :decimal, "2.3.0", "3ad6255aa77b4a3c4f818171b12d237500e63525c2fd056699967a3e7ea20f62", [:mix], [], "hexpm", "a4d66355cb29cb47c3cf30e71329e58361cfcb37c34235ef3bf1d7bf3773aeac"}, 5 + "earmark_parser": {:hex, :earmark_parser, "1.4.44", "f20830dd6b5c77afe2b063777ddbbff09f9759396500cdbe7523efd58d7a339c", [:mix], [], "hexpm", "4778ac752b4701a5599215f7030989c989ffdc4f6df457c5f36938cc2d2a2750"}, 6 + "ecto": {:hex, :ecto, "3.13.5", "9d4a69700183f33bf97208294768e561f5c7f1ecf417e0fa1006e4a91713a834", [:mix], [{:decimal, "~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "df9efebf70cf94142739ba357499661ef5dbb559ef902b68ea1f3c1fabce36de"}, 7 + "ecto_sql": {:hex, :ecto_sql, "3.13.5", "2f8282b2ad97bf0f0d3217ea0a6fff320ead9e2f8770f810141189d182dc304e", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.13.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.7", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.19 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "aa36751f4e6a2b56ae79efb0e088042e010ff4935fc8684e74c23b1f49e25fdc"}, 8 + "ex_doc": {:hex, :ex_doc, "0.40.1", "67542e4b6dde74811cfd580e2c0149b78010fd13001fda7cfeb2b2c2ffb1344d", [:mix], [{:earmark_parser, "~> 1.4.44", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "bcef0e2d360d93ac19f01a85d58f91752d930c0a30e2681145feea6bd3516e00"}, 9 + "makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"}, 10 + "makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"}, 11 + "makeup_erlang": {:hex, :makeup_erlang, "1.0.3", "4252d5d4098da7415c390e847c814bad3764c94a814a0b4245176215615e1035", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "953297c02582a33411ac6208f2c6e55f0e870df7f80da724ed613f10e6706afd"}, 12 + "nimble_parsec": {:hex, :nimble_parsec, "1.4.2", "8efba0122db06df95bfaa78f791344a89352ba04baedd3849593bfce4d0dc1c6", [:mix], [], "hexpm", "4b21398942dda052b403bbe1da991ccd03a053668d147d53fb8c4e0efe09c973"}, 13 + "postgrex": {:hex, :postgrex, "0.22.0", "fb027b58b6eab1f6de5396a2abcdaaeb168f9ed4eccbb594e6ac393b02078cbd", [:mix], [{:db_connection, "~> 2.9", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "a68c4261e299597909e03e6f8ff5a13876f5caadaddd0d23af0d0a61afcc5d84"}, 14 + "telemetry": {:hex, :telemetry, "1.4.1", "ab6de178e2b29b58e8256b92b382ea3f590a47152ca3651ea857a6cae05ac423", [:rebar3], [], "hexpm", "2172e05a27531d3d31dd9782841065c50dd5c3c7699d95266b2edd54c2dafa1c"}, 15 + }
+3
mix.lock.license
··· 1 + SPDX-FileCopyrightText: 2026 Łukasz Niemier <~@hauleth.dev> 2 + 3 + SPDX-License-Identifier: Apache-2.0
+35
test/ecto_view/migration_test.exs
··· 1 + # SPDX-FileCopyrightText: 2026 Łukasz Niemier <~@hauleth.dev> 2 + # 3 + # SPDX-License-Identifier: Apache-2.0 4 + 5 + defmodule EctoView.MigrationTest do 6 + use ExUnit.Case 7 + 8 + import Ecto.Query 9 + 10 + @subject EctoView.Migration 11 + 12 + doctest @subject 13 + 14 + defmodule ExampleSchema do 15 + use Ecto.Schema 16 + 17 + schema "example_schema", do: nil 18 + end 19 + 20 + test "using schema as a source for view fails" do 21 + query = from(e in ExampleSchema, select: %{}) 22 + 23 + assert_raise @subject.InvalidSource, fn -> 24 + @subject.view("foo", query) 25 + end 26 + end 27 + 28 + test "using schema as a source for materialized view fails" do 29 + query = from(e in ExampleSchema, select: %{}) 30 + 31 + assert_raise @subject.InvalidSource, fn -> 32 + @subject.materialized_view("foo", query) 33 + end 34 + end 35 + end
+9
test/ecto_view_test.exs
··· 1 + # SPDX-FileCopyrightText: 2026 Łukasz Niemier <~@hauleth.dev> 2 + # 3 + # SPDX-License-Identifier: Apache-2.0 4 + 5 + defmodule EctoViewTest do 6 + use ExUnit.Case 7 + 8 + doctest EctoView 9 + end
+12
test/support/repo.ex
··· 1 + # SPDX-FileCopyrightText: 2026 Łukasz Niemier <~@hauleth.dev> 2 + # 3 + # SPDX-License-Identifier: Apache-2.0 4 + 5 + defmodule EctoView.Repo do 6 + @moduledoc false 7 + 8 + use Ecto.Repo, 9 + otp_app: :ecto_view, 10 + adapter: Ecto.Adapters.Postgres 11 + use EctoView 12 + end
+5
test/test_helper.exs
··· 1 + # SPDX-FileCopyrightText: 2026 Łukasz Niemier <~@hauleth.dev> 2 + # 3 + # SPDX-License-Identifier: Apache-2.0 4 + 5 + ExUnit.start()