Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lookup type for aliased schema fields #137

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/ecto/adapters/clickhouse/schema.ex
Original file line number Diff line number Diff line change
@@ -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 ->
4 changes: 1 addition & 3 deletions test/support/ecto_schemas.exs
Original file line number Diff line number Diff line change
@@ -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

3 changes: 1 addition & 2 deletions test/support/migrations.ex
Original file line number Diff line number Diff line change
@@ -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