vrod

vrod

Is it possible to run migrations as a different user?

Hello! I am wondering if it is possible to run Ecto migrations with a different user than the regular user. Sometimes sysadmins restrict database access so the handle used by the app cannot drop or create tables. Has anyone set up a separate Ecto user/repo only for migrations?

Marked As Solved

hauleth

hauleth

Yes, of course it can be done. Not that easy with “regular” mix ecto.migrate, but there is several ways to do so:

  1. Create additional environment for migrations (for example MIX_ENV=migrate) and then create config/migrate.exs where you configure options for your migrations user (additionally you can add preferred_cli_env: ["ecto.migrate": :migrate] to not need to remember to set proper environment each time you run migrations).
  2. Create custom migration task where you will do:
    {:ok, pid} = MyApp.Repo.start_link(name: :migrations, user: "foo", password: "bar")
    Ecto.Migrator.run(MyApp.Repo, :up, all: true, dynamic_repo: :migrations)
    :ok = MyApp.Repo.stop(pid)
    
  3. Use environment variable with config/runtime.exs or Repo.init/2 callback (often DATABASE_URL is used) and then provide different user each time you run application in different scope.

What approach you will use depends fully on your needs and use cases.

Also Liked

hauleth

hauleth

In that case only option 2 will work for you.

cenotaph

cenotaph

Migrations are ran by the OS user if you are running through mix

su - <username> -c "mix ecto.migrate"

edit: I just noticed you might be talking about different DB user.

Take the user & password - connection string - from the environment variable and set them just before running migrations.

prod.exs

database_url =
  System.get_env("DATABASE_URL") ||
    raise """
    environment variable DATABASE_URL is missing.
    For example: ecto://USER:PASS@HOST/DATABASE
    """
  • running MIX_ENV=prod DATABASE_URL="ecto://app:pass@" app start
  • migration MIX_ENV=prod DATABASE_URL="ecto://super:superpass@" mix ecto.migrate

Where Next?

Popular in Questions Top

Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement