dfalling
Postgrex errors with "cached plan must not change result type" during migration
I have a migration that modifies a table, flushes, and then migrates the data in that table. This worked fine locally, but in CI it fails during tests.
The line in question is:
users = Ido.Repo.all(from(u in "users", select: %{id: u.id}))
The error in CI is: (Postgrex.Error) ERROR 0A000 (feature_not_supported) cached plan must not change result type
All the explanations I’ve found for this error are when you have multiple connections open to the same database. This doesn’t feel like it should apply for me when it’s a single migration file. Does anyone know how to resolve this?
Here’s the full migration (some fields omitted for brevity):
defmodule Ido.Repo.Migrations.CreateUsersAuthTables do
use Ecto.Migration
import Ecto.Query
def change do
execute("CREATE EXTENSION IF NOT EXISTS citext", "")
alter table(:users) do
# temporarily not making password required, as we need to fill in defaults for existing users
add(:hashed_password, :string)
end
# generate random password for existing users
flush()
add_placeholder_passwords()
alter table(:users) do
modify(:hashed_password, :string, null: false)
end
end
defp add_placeholder_passwords do
users = Ido.Repo.all(from(u in "users", select: %{id: u.id}))
# generate random password for existing users
for %{id: id} <- users do
# use a unique uuid for password, users will have to reset password to login
password = Ecto.UUID.generate()
hashed_password = Bcrypt.hash_pwd_salt(password)
Ido.Repo.update_all(
from(u in "users",
where: u.id == ^id
),
set: [hashed_password: hashed_password]
)
end
end
end
Marked As Solved
dfalling
Strangely I fixed this by using a schema in my migration. (Just a local schema- not making the mistake of using my model’s schema…)
Updated migration that works:
defmodule Ido.Repo.Migrations.CreateUsersAuthTables do
import Ecto.Changeset
use Ecto.Migration
defmodule User do
use Ido.Schema
schema "users" do
field(:hashed_password, :string)
end
def changeset(element, attrs) do
element
|> cast(attrs, [
:hashed_password
])
end
end
def change do
execute("CREATE EXTENSION IF NOT EXISTS citext", "")
alter table(:users) do
# temporarily not making password required, as we need to fill in defaults for existing users
add(:hashed_password, :string)
end
# generate random password for existing users
flush()
add_placeholder_passwords()
alter table(:users) do
modify(:hashed_password, :string, null: false)
end
end
defp add_placeholder_passwords do
# generate random password for existing users
for user <- Ido.Repo.all(User) do
# use a unique uuid for password, users will have to reset password to login
password = Ecto.UUID.generate()
hashed_password = Bcrypt.hash_pwd_salt(password)
user
|> User.changeset(%{hashed_password: hashed_password})
|> Ido.Repo.update()
end
end
end
Popular in Questions
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I’m writing a test for one of the...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
Hi,
is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
Hi,
I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
I would like to know what is the best IDE for elixir development?
New
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








