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

Darmani72
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
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
tduccuong
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
vac
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
dokuzbir
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
vonH
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
ashish173
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
dblack
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
gshaw
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
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
aalberti333
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
joaquinalcerro
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

We're in Beta

About us Mission Statement