Skip to content

Commit

Permalink
lookup type for aliased schema fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Nov 11, 2023
1 parent 26b0ad6 commit bdaa2c4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
14 changes: 13 additions & 1 deletion lib/ecto/adapters/clickhouse/schema.ex
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,23 @@ defmodule Ecto.Adapters.ClickHouse.Schema do

defp extract_types(schema, fields) do
Enum.map(fields, fn field ->
type = schema.__schema__(:type, field) || raise "missing type for field " <> inspect(field)
type =
schema.__schema__(:type, field) || find_field_source_type(schema, field) ||
raise "missing type for field " <> inspect(field)

type |> Ecto.Type.type() |> remap_type(type, schema, field)
end)
end

defp find_field_source_type(schema, field) do
reverse_field_source =
Enum.find(schema.__schema__(:fields), &(schema.__schema__(:field_source, &1) == field))

if reverse_field_source do
schema.__schema__(:type, reverse_field_source)
end
end

defp prepare_types(schema, header, opts) do
cond do
schema ->
Expand Down
4 changes: 1 addition & 3 deletions test/support/ecto_schemas.exs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ defmodule Ecto.Integration.Permalink do
use Ecto.Integration.Schema

schema "permalinks" do
# TODO support aliased fields
# field :url, :string, source: :uniform_resource_locator
field :url, :string, default: ""
field :url, :string, source: :uniform_resource_locator, default: ""
field :title, :string, default: ""
field :posted, :date, virtual: true

Expand Down
3 changes: 1 addition & 2 deletions test/support/migrations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ defmodule EctoClickHouse.Integration.Migration do

create table(:permalinks, primary_key: false, engine: "MergeTree") do
add :id, :UInt64, primary_key: true
# add :uniform_resource_locator, :string
add :url, :string
add :uniform_resource_locator, :string
add :title, :string
add :post_id, :UInt64
add :user_id, :UInt64
Expand Down

0 comments on commit bdaa2c4

Please sign in to comment.