Import Plain SQL file in Ecto

Is there a way to import a 10mb plain sql file in ecto?

2 Likes

why do you need to use Ecto for that? Can’t you use psql directly?

1 Like

I can upload it directly from MariaDB, but I was trying to automate this import through the seeds file, is it possible or I have to do i manually?

1 Like

Ah, I know nothing about MySQL/MariaDB and Ecto’s driver. Maybe there is some way to hack it around but I don’t want to mislead you. You could call a system command from your seed script however.

1 Like

I will try placing the whole plain sql and import it (I already know how to import a query but I wanted to import a whole file)

1 Like

Hi!
You can fire a plain SQL request with Ecto (this code is dependent on version 2):

alias Ecto.Adapters.SQL

querystring = File.read!("file.sql")

result = SQL.query(Repo, querystring , [])

=)

2 Likes

does it actually work with multiple statements?

1 Like

I don’t have the slightest idea :confused: I wouldn’t be surprised that it is not the case, but I can’t test it at the moment

1 Like

I will check it in a moment, I assume that it will work for a 10mb file with many multiple statements

1 Like

Again, I know nothing about Maria/MySQL driver, but I would not be surprised if it would not work and allowed you to do single statement/query only.

1 Like

Maybe you could use mix ecto.load - it loads from priv/structure.sql by default

See:
https://hexdocs.pm/ecto/Mix.Tasks.Ecto.Load.html#content

4 Likes

Which does call “mysql” shell command behind the scenes. So, most likely, you need to do the same.

2 Likes

Thanks benperiton, the answer was correct:

mix ecto.load -d "my.sql"
3 Likes

FYI: (…but mostly because I was just looking for this mix task in the current ecto library)

As of Ecto 3.x (commit), the mix ecto.load has been moved to Ecto SQL

3 Likes