mariaex icon indicating copy to clipboard operation
mariaex copied to clipboard

Exceptions while running trigger in Ecto Migrations

Open bvjebin opened this issue 8 years ago • 5 comments

I am running into an issue where in after running triggers via ecto migrations, match error exception is thrown. Issue is reported here in the ecto repo. This is blocking the migration to be registered in the database. Can you guys see what is going wrong?

bvjebin avatar Oct 10 '16 20:10 bvjebin

@bvjebin stumbled upon this in the ecto repo. If you're are still having an issue with this, the solution may be to separate the triggers into two separate execute functions. I know that this is a thing: "cannot insert multiple commands into a prepared statement" when using psql.

tbash avatar Nov 15 '16 22:11 tbash

@bvjebin Is it still actual, should I try to reproduce/work on it?

liveforeverx avatar Dec 09 '16 10:12 liveforeverx

@liveforeverx Yes. It'd be helpful if you can work on it.

bvjebin avatar Dec 09 '16 10:12 bvjebin

@bvjebin Can you create some small application, where the crashes are reproducible?

liveforeverx avatar Dec 09 '16 11:12 liveforeverx

@liveforeverx Just create a phoenix application or a simple Elixir application(you have to add ecto manually to the application in this case). Create a new Ecto migration using mix task(mix gen.ecto.migration). Replace the content with the following code and try to migrate it. You will see the issue.

defmodule AppServer.Repo.Migrations.AddEmailOrPhoneValidationTriggerToUsersTable do
  use Ecto.Migration

  def change do
    execute "CREATE TRIGGER InsertFieldemailPhoneNotNull BEFORE INSERT ON users
    FOR EACH ROW BEGIN
      IF (NEW.email IS NULL AND NEW.phone IS NULL) THEN
        SIGNAL SQLSTATE '45000'
        SET MESSAGE_TEXT = 'Email and Phone cannot both be null';
      END IF;
    END;
    CREATE TRIGGER UpdateFieldemailPhoneNotNull BEFORE UPDATE ON users
      FOR EACH ROW BEGIN
        IF (NEW.email IS NULL AND NEW.phone IS NULL) THEN
          SIGNAL SQLSTATE '45000'
          SET MESSAGE_TEXT = 'Email and Phone cannot both be null';
        END IF;
      END;"
  end
end

bvjebin avatar Dec 11 '16 06:12 bvjebin