Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a structure.sql into a rails database via rake

rake db:schema:load will load a schema.rb file into a rails database. Is there a way to load a structure.sql file into the database through rake or do I just need to do this manually?

like image 440
locoboy Avatar asked Apr 15 '14 18:04

locoboy


People also ask

What does rake db schema load do?

Unlike rake db:migrate that runs migrations that have not run yet, rake db:schema:load loads the schema that is already generated in db/schema. rb into the database. Always use this command when: You run the application for the first time.

How do I run a SQL structure?

Use rake db:structure:load , which will load db/structure. sql . In order to specify the path of SQL file, use the SCHEMA environment variable (not DB_STRUCTURE ) like rake db:structure:load SCHEMA=db/another. sql .

What does rake db migrate do?

A migration means that you move from the current version to a newer version (as is said in the first answer). Using rake db:migrate you can apply any new changes to your schema. But if you want to rollback to a previous migration you can use rake db:rollback to nullify your new changes if they are incorrectly defined.

What is SQL Rails structure?

structure. sql differs from schema. rb in the following ways: It allows for an exact copy of the database structure. This is important when working with a team, as well as if you need to rapidly generate a new database in production from a rails db:setup task.


2 Answers

Use rake db:structure:load, which will load db/structure.sql.

[Update]

If you want to load another file, you can specify its path via

  • SCHEMA environment variable (Rails 5.0 or later)
  • DB_STRUCTURE environment variable (Rails 4.x)

For example, run

rake db:structure:load SCHEMA=db/another.sql 

or

rake db:structure:load DB_STRUCTURE=db/another.sql 
like image 200
Tsutomu Avatar answered Sep 24 '22 13:09

Tsutomu


Just use

rake db:setup 

which will use either schema.rb or structure.sql depending on your configuration.

like image 22
nathanvda Avatar answered Sep 24 '22 13:09

nathanvda