Alf*_*rez 6 elixir ecto tzdata
我有这段代码:
case Timex.Timezone.get(data) do
{:error, _} = error ->
error
data ->
{:ok, data}
end
Run Code Online (Sandbox Code Playgroud)
将保存在数据库中的时区放入结构中。
好吧,当运行通过 Ecto 查询获取一些数据的迁移时,我收到此错误:
** (ArgumentError) argument error
(stdlib) :ets.lookup(:tzdata_current_release, :release_version)
lib/tzdata/release_reader.ex:47: Tzdata.ReleaseReader.current_release_from_table/0
lib/tzdata/release_reader.ex:14: Tzdata.ReleaseReader.simple_lookup/1
lib/tzdata/release_reader.ex:7: Tzdata.ReleaseReader.zone_and_link_list/0
lib/tzdata.ex:40: Tzdata.zone_exists?/1
lib/timezone/timezone.ex:152: Timex.Timezone.name_of/1
lib/timezone/timezone.ex:180: Timex.Timezone.get/2
lib/common/ecto/timezone.ex:27: Common.Ecto.Timezone.load/1
(ecto) lib/ecto/type.ex:661: Ecto.Type.process_loaders/3
(ecto) lib/ecto/schema.ex:1490: Ecto.Schema.load!/5
(ecto) lib/ecto/schema.ex:1442: Ecto.Schema.safe_load_zip/4
(ecto) lib/ecto/schema.ex:1443: Ecto.Schema.safe_load_zip/4
(ecto) lib/ecto/schema.ex:1430: Ecto.Schema.__safe_load__/6
(ecto) lib/ecto/repo/queryable.ex:282: Ecto.Repo.Queryable.process_source/6
(ecto) lib/ecto/repo/queryable.ex:170: Ecto.Repo.Queryable.preprocess/5
(postgrex) lib/postgrex/query.ex:77: DBConnection.Query.Postgrex.Query.decode_map/3
(postgrex) lib/postgrex/query.ex:64: DBConnection.Query.Postgrex.Query.decode/3
(db_connection) lib/db_connection.ex:1019: DBConnection.decode/6
(ecto) lib/ecto/adapters/postgres/connection.ex:73: Ecto.Adapters.Postgres.Connection.prepare_execute/5
(ecto) lib/ecto/adapters/sql.ex:256: Ecto.Adapters.SQL.sql_call/6
Run Code Online (Sandbox Code Playgroud)
在堆栈跟踪中包含该代码并执行一些检查可以验证这确实是触发错误的调用,尽管这样做:
iex(1)> Timex.Timezone.get("America/Los_Angeles")
#<TimezoneInfo(America/Los_Angeles - PDT (-07:00:00))>
Run Code Online (Sandbox Code Playgroud)
在iex -S mix作品中。
发生此错误是因为Timex需要开始运行。如果您mix.exs在应用程序启动时将其添加到依赖项中,这通常会自动完成。但是,在混合任务中,您必须手动选择要启动的应用程序。在您的自定义混合任务中,您可以确保应用程序是通过Application.ensure_all_started(:timex).
在您的ecto.migrate情况下,我们无权访问实际的混音任务,因此我们需要通过在您的mix.exs文件中使用混音别名来更有创意:
def project do
[
...
aliases: aliases(),
...
]
end
defp aliases do
[
"ecto.migrate_s": ["ecto.migrate.startup", "ecto.migrate"],
]
end
Run Code Online (Sandbox Code Playgroud)
和ecto.migrate.startup我们的任务Application.ensure_all_started(:timex)
defmodule Mix.Tasks.Ecto.Migrate.Startup do
use Mix.Task
def run(args) do
Mix.shell.info("Starting apps required for ecto.migrate...")
Application.ensure_all_started(:timex)
end
end
Run Code Online (Sandbox Code Playgroud)
现在您应该能够运行mix ecto.migrate_swhich 首先启动timex然后运行您的迁移。(这不是一个完全干净的解决方案,但我现在不知道替代方案)