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

arc_ecto not working with Ecto 3 #272

Open
AppyCat opened this issue Nov 16, 2018 · 14 comments
Open

arc_ecto not working with Ecto 3 #272

AppyCat opened this issue Nov 16, 2018 · 14 comments

Comments

@AppyCat
Copy link

AppyCat commented Nov 16, 2018

Hi, I'm using Elixir 1.7.3 with Phoenix 1.4.0 with Arc 0.11.0 and Ecto 3.0

Problem is arc_ecto 0.11.0 wants Ecto 2.0

Can arc_ecto work with Ecto 3 on any other branch?

@OvermindDL1
Copy link

You can force override ecto/ecto_sql via override: true in it's dependency, and arc_ecto will 'mostly' work, however there is a bug that still prevents it from working, attempting to use cast_attachments/3 throws an error (which I can get monday if needed, holiday weekend right now ^.^).

@AppyCat
Copy link
Author

AppyCat commented Nov 16, 2018

Thank you, it would be great if it can be fixed next week. I tried override: true but then arc_ecto can't create date function structures, so it dies, and also cast_attachments/3 throws an error.

It seems the differences between Ecto 2 and Ecto 3 break it. Its a great solution though to make trivial what would otherwise require quite a bit of boilerplate.

@dbernheisel
Copy link

If you bump arc_ecto to 0.11.1, I think this will resolve the issue.

@AppyCat
Copy link
Author

AppyCat commented Dec 1, 2018 via email

@bnenu
Copy link

bnenu commented Jan 9, 2019

You can force override ecto/ecto_sql via override: true in it's dependency, and arc_ecto will 'mostly' work, however there is a bug that still prevents it from working, attempting to use cast_attachments/3 throws an error (which I can get monday if needed, holiday weekend right now ^.^).

Seems that the bug with cast_attachments/3 is not fixed by 0.11.1 and Ecto 3.0. Any workaround ideas?

@dbernheisel
Copy link

@bnenu Can you clarify the issue? I'm not seeing any compatibility issues with arc_ecto and Ecto 3.0 with arc_ecto v0.11.1+

@OvermindDL1
Copy link

cast_attachments is working for me as well. What's the error?

@bnenu
Copy link

bnenu commented Jan 10, 2019

Here is the error
(FunctionClauseError) no function clause matching in anonymous fn/2 in Assets.Asset.changeset/2 (assets) lib/asset.ex:51: anonymous fn({"url", %{binary: <<255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 72, 0, 72, 0, 0, 255, 192, 0, 17, 8, 3, 0, 4, 0, 3, 1, 34, 0, 2, 17, 1, 3, 17, 1, 255, 196, 0, 31, 0, 0, 1, 5, ...>>, filename: "image.jpeg"}}, []) in Assets.Asset.changeset/2 (stdlib) maps.erl:257: :maps.fold_1/3 (assets) lib/asset.ex:51: Assets.Asset.changeset/2 (assets) lib/assets.ex:75: Assets.update_asset/2 (backoffice) lib/backoffice_web/controllers/asset_controller.ex:76: BackofficeWeb.AssetController.update/2 (backoffice) lib/backoffice_web/controllers/asset_controller.ex:1: BackofficeWeb.AssetController.action/2 (backoffice) lib/backoffice_web/controllers/asset_controller.ex:1: BackofficeWeb.AssetController.phoenix_controller_pipeline/2 (backoffice) lib/backoffice_web/endpoint.ex:1: BackofficeWeb.Endpoint.instrument/4 (phoenix) lib/phoenix/router.ex:275: Phoenix.Router.__call__/1 (backoffice) lib/backoffice_web/endpoint.ex:1: BackofficeWeb.Endpoint.plug_builder_call/2 (backoffice) lib/plug/debugger.ex:122: BackofficeWeb.Endpoint."call (overridable 3)"/2 (backoffice) lib/backoffice_web/endpoint.ex:1: BackofficeWeb.Endpoint.call/2 (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:34: Phoenix.Endpoint.Cowboy2Handler.init/2 (cowboy) /Users/BN/N/projects/ecom/ctt/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2 (cowboy) /Users/BN/N/projects/ecom/ctt/deps/cowboy/src/cowboy_stream_h.erl:296: :cowboy_stream_h.execute/3 (cowboy) /Users/BN/N/projects/ecom/ctt/deps/cowboy/src/cowboy_stream_h.erl:274: :cowboy_stream_h.request_process/3 (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

and here is my asset schema and changeset

schema "assets" do
    field :title, :string
    field :media_type, :string
    field :slug, :string
    field :body, :string
    field :caption, :string
    field :alt_text, :string
    field :description, :string
    field :url, Assets.MediaUploader.Type
    field :meta, :map
    field :visible, :boolean, default: true

    timestamps()
  end

  @doc false
  def changeset(asset, attrs) do
    asset
    |> cast(attrs, [
      :title,
      :media_type,
      :slug,
      :body,
      :caption,
      :alt_text,
      :description,
      :meta,
      :visible
    ])
    |> cast_attachments(attrs, [:url])
    |> validate_required([:title, :slug, :media_type])

    # |> unique_constraint(:slug)
  end

I have to say that I am fairly new to elixir and I had a previous project that used arc with a similar setup that worked with previous versions of Ecto, Arc and Arc.ecto. This app is part of an umbrella project if that is relevant. Please excuse my mistake if this ends up being something else.

@OvermindDL1
Copy link

What precisely is on line 51 of file lib/asset.ex? Preferably can you show the whole file as well? A tuple is being passed into a function when a tuple is not expected.

@bnenu
Copy link

bnenu commented Jan 10, 2019

line 51 |> cast_attachments(attrs, [:url])

assets.ex

defmodule Assets.Asset do
  use Ecto.Schema
  import Ecto.Changeset
  use Arc.Ecto.Schema

  @primary_key {:id, :binary_id, autogenerate: true}
  # @derive {Phoenix.Param, key: :id}
  @derive {Jason.Encoder,
           only: [
             :id,
             :title,
             :media_type,
             :slug,
             :body,
             :caption,
             :alt_text,
             :description,
             :url,
             :visible
           ]}

  schema "assets" do
    field :title, :string
    field :media_type, :string
    field :slug, :string
    field :body, :string
    field :caption, :string
    field :alt_text, :string
    field :description, :string
    field :url, Assets.MediaUploader.Type
    field :meta, :map
    field :visible, :boolean, default: true

    timestamps()
  end

  @doc false
  def changeset(asset, attrs) do
    asset
    |> cast(attrs, [
      :title,
      :media_type,
      :slug,
      :body,
      :caption,
      :alt_text,
      :description,
      :meta,
      :visible
    ])
    |> cast_attachments(attrs, [:url])
    |> validate_required([:title, :slug, :media_type])

    # |> unique_constraint(:slug)
  end
end

One other thing is that I construct the file passed myself %{ filename: "image.jpeg" , binary: ...}, as I get the image body in base 64 encode string and I parse the binary with Base.decode64!, if that is relevant for the case.

@OvermindDL1
Copy link

OvermindDL1 commented Jan 10, 2019

For note, code fences are done with 3 `'s, like:
```elixir
All the code
goes here
```
:-)

One other thing is that I construct the file passed myself %{ filename: "image.jpeg" , binary: ...}, as I get the image body in base 64 encode string and I parse the binary with Base.decode64!, if that is relevant for the case.

However, this is probably the reason, cast_attachments only supports Plug.Upload structures or a raw string file path. It really should support a %{filename: ..., binary: ...} map though so I'd say this is a missing feature, or perhaps even a bug in arc_ecto to reach feature parity with arc itself.

It looks like it would be a pretty simple fix though, just need to add the proper case to https://github.com/stavro/arc_ecto/blob/master/lib/arc_ecto/schema.ex#L35 so I'd say submit a PR or at least open an issue ticket at https://github.com/stavro/arc_ecto itself. :-)

Based on the existing arc_ecto code though, I'm not sure how that would have ever worked, so this doesn't seem to be an ecto3 thing at all?

@OvermindDL1
Copy link

OvermindDL1 commented Jan 10, 2019

Actually it looks like someone already has an issue open, from over 2 years ago... >.>
stavro/arc_ecto#54
And there's even a PR for it already from a year ago:
stavro/arc_ecto#87

Are there any other contributors that are able to update these projects @Stavros that also have hex credentials to push updates?

@dbernheisel
Copy link

@AppyCat would you mind closing this issue? I don't think this is an issue (as titled) any longer. I believe we've already confirmed this together in this other issue: stavro/arc_ecto#104

@bnenu I believe @OvermindDL1 accurately described the issue. Arc simply doesn't support the format of params you're passing in (yet), and there are other issues/PRs open that would help your situation. It's probably best to move the discussion there :)

@bnenu
Copy link

bnenu commented Jan 10, 2019

Thank you folks for your help! I will be following the discussion on arc_ecto thread, maybe they merge the PR :).Seems that there is a workaround proposed until better days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants