packageelixirpackage-managementhex-pm

Can I use different versions of a package for different environments?


I have a project where I need to use SQLite in a local environment but Postgres on a normal server.

Unfortunately, there is no SQLite adapter for Ecto 3 yet, forcing me to keep the Ecto and some related packages at 2.x, which caused some problems such as this one: Ecto 2.0 SQL Sandbox Error on tests

I wonder if it would be possible to specify two different versions of Ecto and thus dependencies for the environments :local and :prod. Currently it seems impossible since there is only one lockfile per project. The only way to achieve it seems to be to store two different lockfiles in the project directory? e.g. https://elixirforum.com/t/only-fetch-deps-compatible-for-a-specific-version-of-elixir/16213


Solution

  • I haven't tried it in depth, but maybe changing the mix.exs file like this would help:

    defmodule YourProject.MixProject do
      use Mix.Project
    
      def project do
        [
          app: :your_project,
          version: "0.1.0",
          elixir: "~> 1.7",
          start_permanent: Mix.env() == :prod,
          deps: deps(Mix.env()),
          lockfile: lockfile(Mix.env())
        ]
      end
    
      # Run "mix help compile.app" to learn about applications.
      def application do
        [
          extra_applications: [:logger]
        ]
      end
    
      defp lockfile(:local), do: "mix-local.lock"
      defp lockfile(_), do: "mix.lock"
    
      # Run "mix help deps" to learn about dependencies.
      defp deps(:local) do
         [{:ecto, "~> 2.0"}]
      end
    
      defp deps(_) do
         [{:ecto, "~> 3.0"}]
      end
    end
    

    Both the lockfile and deps are different for the :local environment.