danielberkompas / cloak

Elixir encryption library designed for Ecto
MIT License
582 stars 55 forks source link

Migrator Embedded Schema Error #81

Closed kevin-j-m closed 6 years ago

kevin-j-m commented 6 years ago

I am in the process of upgrading from 0.6 to 0.7 (ultimately up to 0.9) and have run into an issue with the migration mix task. It appears to have an issue handling an Ecto embedded schema. I have a schema that looks similar to this (where the schema and field names have been obviously changed):

schema "foos" do
  field(:encrypted_field, MyApp.Encrypted.Binary)
  embeds_many(:bars, MyApp.Bar)
end

Running the migration task:

mix cloak.migrate -r MyApp.Repo -s MyApp.Foo

Results in the following error:

** (FunctionClauseError) no function clause matching in Code.ensure_loaded?/1

    The following arguments were given to Code.ensure_loaded?/1:

        # 1
        {:embed, %Ecto.Embedded{cardinality: :many, field: :bars, on_cast: nil, on_replace: :raise, owner: MyApp.Foo, related: MyApp.Bar, unique: true}}

    (elixir) lib/code.ex:869: Code.ensure_loaded?/1
    (cloak) lib/cloak/migrator.ex:55: anonymous fn/1 in Cloak.Migrator.cloak_fields/1
    (elixir) lib/enum.ex:2871: Enum.filter_list/2
    (elixir) lib/enum.ex:2872: Enum.filter_list/2
    (cloak) lib/cloak/migrator.ex:54: Cloak.Migrator.cloak_fields/1
    (cloak) lib/cloak/migrator.ex:12: Cloak.Migrator.migrate/2
    (cloak) lib/mix/tasks/cloak.migrate.ex:120: anonymous fn/3 in Mix.Tasks.Cloak.Migrate.run/1
    (elixir) lib/enum.ex:1899: Enum."-reduce/3-lists^foldl/2-0-"/3
    (stdlib) lists.erl:1263: :lists.foldl/3
    (cloak) lib/mix/tasks/cloak.migrate.ex:117: Mix.Tasks.Cloak.Migrate.run/1
    (mix) lib/mix/task.ex:314: Mix.Task.run_task/3
    (mix) lib/mix/cli.ex:80: Mix.CLI.run_task/2
    (elixir) lib/code.ex:677: Code.require_file/2

I'm using Postgres for my database. If there are any other environment or application configuration I can provide, please let me know. If there's anything you can think of for a workaround to handle this, I'd really appreciate it.

Thank you!

danielberkompas commented 6 years ago

Hi @kevin-j-m, the only workaround I can suggest while I look into this would be to use an Cloak.Fields.Map field instead of an embedded schema. Would that work for you?

I'm looking into what it would take to add support for embedded schemas, but I'm not whether it makes sense to release that feature in 0.10 or back-apply it to 0.7.

kevin-j-m commented 6 years ago

Hi @danielberkompas, the embedded schema itself isn't encrypted. However, it seems like the embedded schema being part of a schema that does have an encrypted element is enough to trigger this error. I don't have a need at this point to encrypt the embedded schema, so I don't think a Cloak.Fields.Map makes sense here.

For what it's worth, I'm at the stage of my project where it's not mission critical that this get resolved immediately or with any urgency. As such, I personally see no issue with this being part of a future release.

I wanted to raise the issue to see if there was something obvious/silly I was missing, and to also raise awareness both so others can find it and also in the hopes that it eventually get resolved.

Thanks for the reply and considering an alternative for me to pursue.