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

Update to latest packages #26

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Elixir CI

on:
push:
branches: [master]
pull_request:
branches: [master]

jobs:
build:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- uses: erlef/setup-elixir@v1
with:
otp-version: "24"
elixir-version: "1.12"

- uses: actions/cache@v2
with:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: ${{ runner.os }}-mix-
- run: mix deps.get
- run: mix test
66 changes: 40 additions & 26 deletions lib/ecto/sql_dust.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ defmodule Ecto.SqlDust do
derived_schema = derive_schema(model)

options
|> __from__(resource)
|> adapter(:postgres)
|> schema(derived_schema)
|> __from__(resource)
|> adapter(:postgres)
|> schema(derived_schema)
end

defp parse(options) do
Expand All @@ -30,20 +30,26 @@ defmodule Ecto.SqlDust do
source = model.__schema__(:source)
associations = model.__schema__(:associations)

schema = schema
|> Map.put(source, Enum.reduce(associations, %{name: source, table_name: source}, fn(association, map) ->
reflection = model.__schema__(:association, association)
Map.put(map, association, derive_association(reflection))
end))
schema =
schema
|> Map.put(
source,
Enum.reduce(associations, %{name: source, table_name: source}, fn association, map ->
reflection = model.__schema__(:association, association)
Map.put(map, association, derive_association(reflection))
end)
)

schema = associations
|> Enum.map(fn(association) ->
schema =
associations
|> Enum.map(fn association ->
model.__schema__(:association, association).queryable
end)
|> Enum.uniq
|> Enum.reduce(schema, fn(model, schema) ->
|> Enum.uniq()
|> Enum.reduce(schema, fn model, schema ->
model_source = model.__schema__(:source)
if (source == model_source) || Map.has_key?(schema, model_source) do

if source == model_source || Map.has_key?(schema, model_source) do
schema
else
derive_schema(schema, model)
Expand All @@ -54,20 +60,28 @@ defmodule Ecto.SqlDust do
end

defp derive_association(reflection) do
cardinality = case reflection.__struct__ do
Ecto.Association.BelongsTo -> :belongs_to
Ecto.Association.Has ->
case reflection.cardinality do
:one -> :has_one
:many -> :has_many
end
Ecto.Association.ManyToMany -> :has_and_belongs_to_many
end
cardinality =
case reflection.__struct__ do
Ecto.Association.BelongsTo ->
:belongs_to

Ecto.Association.Has ->
case reflection.cardinality do
:one -> :has_one
:many -> :has_many
end

Ecto.Association.ManyToMany ->
:has_and_belongs_to_many
end

Map.merge(%{
cardinality: cardinality,
resource: reflection.related.__schema__(:source)
}, derive_association(cardinality, reflection))
Map.merge(
%{
cardinality: cardinality,
resource: reflection.related.__schema__(:source)
},
derive_association(cardinality, reflection)
)
end

defp derive_association(:belongs_to, reflection) do
Expand Down
Loading