diff --git a/lib/cloak_ecto/migrator.ex b/lib/cloak_ecto/migrator.ex index 0f64aca..6a76836 100644 --- a/lib/cloak_ecto/migrator.ex +++ b/lib/cloak_ecto/migrator.ex @@ -69,6 +69,10 @@ defmodule Cloak.Ecto.Migrator do false end + defp cloak_field?({_field, {:array, type}}) do + Code.ensure_loaded?(type) && function_exported?(type, :__cloak__, 0) + end + defp cloak_field?({_field, {:parameterized, Ecto.Embedded, %Ecto.Embedded{}}}) do false end diff --git a/test/cloak_ecto/migrator_test.exs b/test/cloak_ecto/migrator_test.exs index 4b812d6..b28981b 100644 --- a/test/cloak_ecto/migrator_test.exs +++ b/test/cloak_ecto/migrator_test.exs @@ -71,17 +71,20 @@ defmodule Cloak.Ecto.MigratorTest do end @post_title "Test Title" + @post_tag "test-tag" describe ".migrate/2 with binary ids" do setup do now = DateTime.utc_now() encrypted_title = Cloak.Ecto.TestVault.encrypt!(@post_title, :secondary) + encrypted_tag = Cloak.Ecto.TestVault.encrypt!(@post_tag, :secondary) posts = for _ <- 1..500 do %{ title: encrypted_title, comments: [%{author: "Daniel", body: "Comment"}], + tags: [encrypted_tag], inserted_at: now, updated_at: now } @@ -105,8 +108,19 @@ defmodule Cloak.Ecto.MigratorTest do |> Enum.map(&decrypt(&1.title, :default)) |> Enum.uniq() + tags = + "posts" + |> select([:tags]) + |> Repo.all() + |> Enum.map(fn post -> + [tag] = post.tags + decrypt(tag, :default) + end) + |> Enum.uniq() + assert io =~ "__cloak_cursor_fields__", "Did not call __cloak_cursor_fields__ on schema!" assert titles == [{:ok, @post_title}], "Not all titles were migrated!" + assert tags == [{:ok, @post_tag}] end end diff --git a/test/support/migrations/20200918214428_give_posts_array_field.exs b/test/support/migrations/20200918214428_give_posts_array_field.exs new file mode 100644 index 0000000..91c46fb --- /dev/null +++ b/test/support/migrations/20200918214428_give_posts_array_field.exs @@ -0,0 +1,9 @@ +defmodule ScannerData.Repo.Migrations.GivePostsArrayField do + use Ecto.Migration + + def change do + alter table(:posts) do + add(:tags, {:array, :binary}, default: []) + end + end +end diff --git a/test/support/post.ex b/test/support/post.ex index 0951844..f8ab64c 100644 --- a/test/support/post.ex +++ b/test/support/post.ex @@ -8,6 +8,7 @@ defmodule Cloak.Ecto.TestPost do schema "posts" do field(:title, Cloak.Ecto.Encrypted.Binary) embeds_many(:comments, Cloak.Ecto.TestComment) + field(:tags, {:array, Cloak.Ecto.Encrypted.Binary}, default: []) timestamps(type: :utc_datetime) end