Return statement in Elixir

This is an interesting problem because you need to perform multiple checks, exit early, and in the process transform some state (connection). I typically approach this problem as follows: I implement each check as a function which takes state as an input and returns {:ok, new_state} or {:error, reason}. Then, I build a generic function … Read more

Get current environment name

Mix.env() doesn’t work in production or other environments where you use compiled releases (built using Exrm / Distillery) or when Mix just isn’t available. The solution is to specify it in your config/config.exs file: config :your_app, env: Mix.env() You can then get the environment atom in your application like this: Application.get_env(:your_app, :env) #=> :prod Update … Read more

Modify foreign key in Ecto

I’m not sure when this was added to Ecto, but at least in 2.1.6 there’s no need for raw SQL anymore. drop/1 now supports constraints (drop_if_exists/1 doesn’t though): def up do drop constraint(:videos, “videos_user_id_fkey”) alter table(:videos) do modify :user_id, references(:users, on_delete: :delete_all) end end def down do drop constraint(:videos, “videos_user_id_fkey”) alter table(:videos) do modify :user_id, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)