Skip to content

Add support for map and array fields in migrator #38

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

Merged
merged 2 commits into from
Jun 17, 2022
Merged
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
@@ -73,6 +73,10 @@ defmodule Cloak.Ecto.Migrator do
false
end

defp cloak_field?({field, {kind, inner_type}}) when kind in [:array, :map] do
cloak_field?({field, inner_type})
end

defp cloak_field?({_field, type}) do
Code.ensure_loaded?(type) && function_exported?(type, :__cloak__, 0)
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -105,7 +105,8 @@ defmodule Cloak.Ecto.MixProject do

defp aliases do
[
test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"]
test: ["ecto.create --quiet", "ecto.migrate --quiet", "test"],
"ecto.reset": ["ecto.drop", "ecto.create", "ecto.migrate"]
]
end
end
22 changes: 20 additions & 2 deletions test/cloak_ecto/migrator_test.exs
Original file line number Diff line number Diff line change
@@ -71,16 +71,19 @@ defmodule Cloak.Ecto.MigratorTest do
end

@post_title "Test Title"
@post_tags ["test", "encryption"]

describe ".migrate/2 with binary ids" do
setup do
now = DateTime.utc_now()
encrypted_title = Cloak.Ecto.TestVault.encrypt!(@post_title, :secondary)
encrypted_title = Vault.encrypt!(@post_title, :secondary)
encrypted_tags = Enum.map(@post_tags, &Vault.encrypt!(&1, :secondary))

posts =
for _ <- 1..500 do
%{
title: encrypted_title,
tags: encrypted_tags,
comments: [%{author: "Daniel", body: "Comment"}],
inserted_at: now,
updated_at: now
@@ -98,15 +101,30 @@ defmodule Cloak.Ecto.MigratorTest do
Migrator.migrate(Repo, Cloak.Ecto.TestPost)
end)

assert io =~ "__cloak_cursor_fields__", "Did not call __cloak_cursor_fields__ on schema!"

titles =
"posts"
|> select([:title])
|> Repo.all()
|> Enum.map(&decrypt(&1.title, :default))
|> 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!"

tags =
"posts"
|> select([:tags])
|> Repo.all()
|> Enum.map(fn %{tags: tags} ->
tags
|> Enum.map(&decrypt(&1, :default))
|> Enum.map(&elem(&1, 1))
end)
|> Enum.uniq()
|> List.flatten()

assert tags == @post_tags, "Not all tags were migrated!"
end
end

2 changes: 2 additions & 0 deletions test/support/migrations/20180915035851_create_posts.exs
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ defmodule Cloak.Ecto.TestRepo.Migrations.CreatePosts do
create table(:posts, primary_key: false) do
add(:id, :uuid, primary_key: true, default: fragment("uuid_generate_v4()"))
add(:title, :binary)
add(:tags, {:array, :binary})
add(:metadata, {:map, :string})
add(:comments, {:array, :map})
timestamps(type: :utc_datetime)
end
2 changes: 2 additions & 0 deletions test/support/post.ex
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ defmodule Cloak.Ecto.TestPost do

schema "posts" do
field(:title, Cloak.Ecto.Encrypted.Binary)
field(:tags, {:array, Cloak.Ecto.Encrypted.Binary})
field(:metadata, {:map, :string})
embeds_many(:comments, Cloak.Ecto.TestComment)
timestamps(type: :utc_datetime)
end