Ecto equivalent of Rails's rake db:schema:load?

453 views
Skip to first unread message

David Escobar

unread,
Jan 21, 2016, 9:26:48 PM1/21/16
to elixir-lang-talk
Hi everyone, I come from a Ruby and Rails background, and I'm currently using Phoenix with Ecto.

In Rails, we have a Rake task for recreating the entire database schema on new systems:
rake db:schema:load

As it says in the auto-generated schema.rb, this is better than running all existing migrations from scratch as errors may accumulate over time, particularly if there are any specific data migrations. Performance is also a consideration after a while.

There doesn't seem to be an equivalent way to do this in Ecto. There is only mix ecto.migrate. Is there an equivalent way of just loading the schema as in Rails? Thanks.

José Valim

unread,
Jan 22, 2016, 3:05:45 AM1/22/16
to elixir-l...@googlegroups.com
There isn't such Ecto but it will likely be in Ecto 2.0 (there is already a pull request for it).



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/c74e1a87-210e-41c3-b576-d75047306f0f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Escobar

unread,
Jan 22, 2016, 3:22:22 PM1/22/16
to elixir-lang-talk, jose....@plataformatec.com.br
That's great news - good to know. Thanks!

David Escobar

unread,
Aug 10, 2016, 4:27:35 PM8/10/16
to elixir-lang-talk, jose....@plataformatec.com.br
Now that Ecto 2.0 has been released, I thought I would revisit this issue. Listing all the Ecto specific Mix tasks, I get:

mix ecto                                # Prints Ecto help information

mix ecto.create                         # Creates the repository storage

mix ecto.drop                           # Drops the repository storage

mix ecto.dump                           # Dumps the repository database structure

mix ecto.gen.migration                  # Generates a new migration for the repo

mix ecto.gen.repo                       # Generates a new repository

mix ecto.load                           # Loads previously dumped database structure

mix ecto.migrate                        # Runs the repository migrations

mix ecto.migrations                     # Displays the repository migration status

mix ecto.rollback                       # Rolls back the repository migrations


I've looked through the various options and cannot seem to find the Rails equivalent of 'rake db:schema:load'. Am I missing the right way to invoke this in Ecto, or did this get pushed out to a future version?


Thanks.



On Friday, January 22, 2016 at 12:05:45 AM UTC-8, José Valim wrote:

Michał Muskała

unread,
Aug 10, 2016, 4:33:58 PM8/10/16
to elixir-l...@googlegroups.com

> On 10 Aug 2016, at 22:27, David Escobar <davidesc...@gmail.com> wrote:
>
>
> I've looked through the various options and cannot seem to find the Rails equivalent of 'rake db:schema:load'. Am I missing the right way to invoke this in Ecto, or did this get pushed out to a future version?
>

You’re looking for ecto.load, and the complementary ecto.dump tasks. They save the database structure in the format native to the database - there’s no special DSL like rails schema has.

Michał.
signature.asc

David Escobar

unread,
Aug 10, 2016, 4:49:09 PM8/10/16
to elixir-lang-talk
I did try ecto.dump followed by ecto.load but that did not work when bringing up the database on a new server. The .sql file it generated did not contain any schema information because the database had just been created with ecto.create.

Is this possible to do with Ecto? If not, what is the community's best practice way of doing this (besides writing custom SQL scripts)? On longer-running projects, there may be a lot of migrations (including data migrations) and running all of them historically (with ecto.migrate) is, at best, time-consuming and at worst, error prone.

Thanks.

David Escobar

unread,
Aug 10, 2016, 4:55:39 PM8/10/16
to elixir-lang-talk
FYI, for now, I'll try doing ecto.dump on the original server and then transfer the generated .sql file to the new server and do ecto.load on that one. That's probably the best way I can think of to do it.

David Escobar

unread,
Aug 10, 2016, 5:03:41 PM8/10/16
to elixir-lang-talk
I just found this helpful blog post on the best practice for these new Ecto Mix tasks, so posting here for reference. Thanks.

Ben Wilson

unread,
Aug 10, 2016, 8:13:18 PM8/10/16
to elixir-lang-talk
Yes, you need to dump a schema that contains the structure you want. This is also true of rake db:schema:dump.
Reply all
Reply to author
Forward
0 new messages