dfalling

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

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

Where Next?

Popular in Questions Top

pmjoe
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement