Import .sql file on a migration

I am currently trying to import a .sql file containing database tables on an Ecto migration, basically my Phoenix application adds some extra funcionality to the given database, so I need to import it:

defmodule XXX.Repo.Migrations.XXX do
  use Ecto.Migration

  def change do
    execute File.read!("schema.sql")
  end
end

However the migration fails, complaining its not valid SQL, the migration success if my .sql file only contains one table to create, when I add more the syntax fails for some reason…

The file is valid SQL and it works just fine doing “mysql -u root -p XX < file.sql” or even copy/paste into terminal. This is the file I am trying to import https://hastebin.com/zifosewayi.sql

No clue why it’s failing – I think I remember it only accepts one SQL statement but I wouldn’t bet my life on it.

One thing you could try is splitting the SQL at the ; delimiter and then loop over the results and execute them one by one?

1 Like

You could also use the --dump-path option for mix ecto.load — Ecto SQL v3.6.2, doesn’t fully solve the problem of rerunning it. But I personally set up an alias in my mix.exs to run our “base” SQL file and then migrations after that.

That seems to work, strange limitation, thank you

I will try that approach, my problem was indeed rerunning but just modifying the flow of ecto.reset should be enough for me, thank you

It is limitation of prepared statements which are used internally in Ecto. This is security measure among other things.

1 Like