dfalling

dfalling

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

Showing Posts 1 to 1

dfalling

dfalling OP

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
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews