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

Handle {:array, $type} types in Migrator #9

Closed
Closed
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 lib/cloak_ecto/migrator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions test/cloak_ecto/migrator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/support/post.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down