You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here's my full(ish) code, because I hate it when I find a solution online and a crucial piece is left out.
defmoduleMyApp.Repo.Migrations.AlterTransactionDatetimedouseEcto.MigrationimportEcto.Query,warn: falsealiasMyApp.RepoaliasMyApp.Encryption.Typesdefupdodates=from(tin"table",select: {t.id,t.datetime})|>Repo.all()|>Enum.map(fn{id,datetime_encrypted}->{:ok,datetime}=Types.DateTime.load(datetime_encrypted){:ok,new_val}=datetime|>DateTime.to_date()|>Types.Date.dump(){id,new_val}end)altertable(:table)doremove:datetimeadd:date,:binaryendflush()for{id,date}<-datesdofrom(tin"table",where: t.id==^id,update: [set: [date: ^date]])|>Repo.update_all([])endendenddefmoduleMyApp.Releasedo@moduledoc""" Used for executing DB release tasks when run in production without Mix installed. """@app:my_appdefmigratedoMyApp.Vault.start_link()load_app()forrepo<-repos()do{:ok,_,_}=Ecto.Migrator.with_repo(repo,&Ecto.Migrator.run(&1,:up,all: true))endend[...]end
Suggestions
There should be an actual migration guide, like the above (or better since i don't know if this is how the package is supposed to be used). and encrypt_existing_data.md imho should be deprecated in it's current form.
Document crucial functions like start_link, load and dump. Nothing fancy, for starters that they exist at all.
Problem
It's common to need to do migrations in the database and convert data between encrypted fields with the following conditions:
Unfortunately this process is not documented and trying it myself resulted in the same error as danielberkompas/cloak#125 .
TLDR encrypt_existing_data.md solution is suboptimal.
Migration steps
I want to change a DateTime field to Date.
DateTime.to_date()
Workaround
I did manage to read through a bunch of cloak's code and come up with a solution.
MyApp.Vault.start_link()
to start the Vault in Release (solves ETS error when running Vault without mix cloak#125).load()
to decrypt your data.dump()
to encrypt data.Here's my full(ish) code, because I hate it when I find a solution online and a crucial piece is left out.
Suggestions
The text was updated successfully, but these errors were encountered: