arpan

arpan

Starting Xandra process for migrations

Hi everyone, I have been trying to setup xandra in order to use Cassandra in my phoenix project. The xandra connection with Cassandra is made in my application.ex file, by specifying it as a child in my supervision tree like {Xandra, name: :xandra_connection} now I can execute queries like …

statement =
      "CREATE KEYSPACE test WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};"

    Xandra.execute(:xander_connection, statement, [])

This works but in case of migrations involving xandra I need to establish the xandra connection every time like…

defmodule Test.Repo.Migrations.CreateKeyspace do
  use Ecto.Migration

  def up do
    Xandra.start_link(name: :xander_connection)
    statement =
      "CREATE KEYSPACE test WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 3};"

    Xandra.execute(:xander_connection, statement, [])
  end

  def down do
    Xandra.start_link(name: :xander_connection)
    statement = "DROP KEYSPACE test"
    Xandra.execute(:xander_connection, statement, [])
  end
end

I want to establish the connection once before running the migrations and avoid writing Xandra.start_link(name: :xander_connection) every time in the migrations.
Can anyone help me with this?

Most Liked

al2o3cr

al2o3cr

How do you start Xandra normally? If you’re using an application entry in mix.exs, you could try adding a start_apps_before_migration entry referring to it to your Repo’s configuration.

https://github.com/elixir-ecto/ecto_sql/blob/192f235a6aa386fcd3ca241680bd950510740ec6/lib/ecto/migration.ex#L221-L224

SPrio-kt

SPrio-kt

Here xandra is a child process for the Your Application Supervisor, So you can start the Xandra before migration, by writing this line start_apps_before_migration: [:my_app] in your Repo configuration. This means you are starting your application before migration. Your config.exs may look like -

config :my_app, MyApp.Repo,
  username: .....,
  password: .....,
  database: "my_app_db,
  hostname: "localhost",
  pool_size: 10,
  start_apps_before_migration: [:my_app]

Last Post!

thiago

thiago

I tried following @SPrio-kt’s solution, but now everytime I try to run tests or start the server I keep getting errors with the following message: ** (RuntimeError) cannot use Logger, the :logger application is not running.

Has anyone else stumbled upon this issue?

Where Next?

Popular in Questions Top

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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

We're in Beta

About us Mission Statement