ecto_adapters_dynamodb
ecto_adapters_dynamodb copied to clipboard
How to change a schema's table name at runtime?
trafficstars
I was wondering if there's any configuration that I can use in Ecto or the adapter itself that can allow me to change the name of a DynamoDB table at runtime from System ENV? I have different dynamo db table names for dev, staging and prod. Migrating to Elixir from another backend system. My understanding is I can't do it in Ecto's schema macro for a User module since evaluated at compile time.
This is what I've tried so far but it didn't work:
defmodule MyApp.Repo do
use Ecto.Repo,
otp_app: :myapp,
# Specifies the adapter for DynamoDB
adapter: Ecto.Adapters.DynamoDB
@impl true
def prepare_query(_operation, query, opts) do
# Use the dynamic table name for `User` schema
user_table_name = System.fetch_env!("USER_TABLE_NAME")
# Set the source for the `User` schema
query_with_updated_source =
query
|> Ecto.Queryable.to_query()
|> Map.update(:from, %{source: {user_table_name, MyApp.User}}, & &1)
{query_with_updated_source, opts}
end
end
Error when I try to CRUD a record :-
[error] ** (ExAws.Error) ExAws Request Error! {"ResourceNotFoundException", "Cannot do operations on a non-existent table"}
(ecto_adapters_dynamodb 3.4.0) lib/ecto_adapters_dynamodb/cache.ex:58: Ecto.Adapters.DynamoDB.Cache.describe_table!/2
(ecto_adapters_dynamodb 3.4.0) lib/ecto_adapters_dynamodb/info.ex:49: Ecto.Adapters.DynamoDB.Info.index_details/2
(ecto_adapters_dynamodb 3.4.0) lib/ecto_adapters_dynamodb/info.ex:75: Ecto.Adapters.DynamoDB.Info.primary_key!/2
(ecto_adapters_dynamodb 3.4.0) lib/ecto_adapters_dynamodb/query.ex:534: Ecto.Adapters.DynamoDB.Query.get_matching_primary_index/3
(ecto_adapters_dynamodb 3.4.0) lib/ecto_adapters_dynamodb/query.ex:495: Ecto.Adapters.DynamoDB.Query.get_best_index/4
(ecto_adapters_dynamodb 3.4.0) lib/ecto_adapters_dynamodb/query.ex:520: Ecto.Adapters.DynamoDB.Query.get_best_index!/4
(ecto_adapters_dynamodb 3.4.0) lib/ecto_adapters_dynamodb/query.ex:42: Ecto.Adapters.DynamoDB.Query.get_item/4
(ecto_adapters_dynamodb 3.4.0) lib/ecto_adapters_dynamodb.ex:278: Ecto.Adapters.DynamoDB.execute/5
....
I am new to the Elixir ecosystem. Could appreciate some help. Thankyou! :)