
This commit shows an example usage in a Phoenix project from adding enum_ecto to the project and using the enumerated type in views and models. Its usage is well documented on its github page. Second, in the model file either add a field with an Elixir type : schema "posts" doĪ good example for the latter is the ecto_enum package and it can be used as a template. The example below adds an enumerated type to a postgres database.įirst, edit the migration file (created with mix ): def up doĮxecute("CREATE TYPE post_status AS ENUM ('published', 'editing')")Īdd :post_status, :post_status, null: false some_field = :idįrom p in Post, where: field(p, ^some_field) = ^some_valueĪdd custom data types to migration and to schema To query a field which name is contained in a variable, use the field function.
#ELIXIR ECTO CODE#
Creating a highly reusable Ecto API is one of the ways we can create long-term sustainable code for ourselves, while growing it with our application to allow for infinite combination possibilites and high code reusability. You can fetch the Post with title: "hello" and description : "world" by performing : MyRepo.get_by(Post, )Īll of this is possible because Repo.get_by expects in second argument a Keyword List. Creating Reusable Ecto Code in Elixir December 21st 2021 - Mika Kalathil.

If you have an Ecto.Queryable, named Post, which has a title and an description. It can be done with registering Ecto in lib/custom_app.ex as a supervisor. Url: "postgres://postgres_dev: /ecto_custom_dev"īefore using Ecto in your application, you need to ensure that Ecto is started before your app is started. You must also define some config for the Repo which will allow you to connect to the database. Thank you, Josef Strzibny, for pointing out the ability to use temporary module inside migration.You must define an elixir module which use Ecto.Repo and register your app as an otp_app. Thank you, Felipe Pereira Stival, for pointing out this ambiguity. It was not very precise as the Ecto API can still be used. In the previous version, I used the phrase “raw SQL”. Simple changes today can save us from critical errors in the future. This will eliminate problems when running our application with a clean database or changing fields in schemas. You can still use Ecto API, but in my opinion, you should use it without schemas. Instead of structures, we can use schemaless Ecto queries. I am updating the post to avoid inaccuracies as there is also an approach beyond what was initially proposed. Theoretically, both versions will lead to the same state.įor me personally, the use of SQL seems like a more straightforward solution. Query, only: from ( q in Log, where: q. Schema schema "logs" do field ( :action, :string ) field ( :details, :string ) end end def up do import Ecto. # priv/repo/migrations/timestamp_modify_log_action.exs defmodule Project. In our project, we have prepared the following migration: We can make our application unable to start! The greater risk occurs when we decide to use data structures: structs or Ecto schemas. When we use modules such as Enum or Map, there should be no problems. The use of the Elixir code in migration files can be divided into two categories.

It is crucial because it allows us to modify and improve already created files. The table stores version and inserted_at columns.

It guarantees that only one server can make changes at the same time. To ensure that the migration is performed once, Ecto will lock 2 the schema_migrations table when running migrations. We can configure this table’s name with the :migration_source in Ecto configuration option 1. Migrations are performed one file after the other.Įcto uses the schema_migrations table, which stores all migrations that have already been executed. Simple changes can save us from unpleasant consequences. It is important to understand the problem and the consequences better. In this post, I would like to present a problem that may occur. We often do not realize that incorrectly used functionality may block our applications. Using Ecto allows us to manage data migrations easily.
