Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create database from schema.rb without initializing Rails?

I am trying to create all my tables from schema.rb

I used the command: "rake db:schema:load"

However, this fails because in one of my initializers, it is referencing a model/table that obviously doesn't exist (since the database is empty)

I could comment out these lines, and then run schema:load again, but is there an alternative?

like image 600
Henley Avatar asked Feb 23 '13 00:02

Henley


People also ask

What is DB schema RB?

The schema. rb serves mainly two purposes: It documents the final current state of the database schema. Often, especially when you have more than a couple of migrations, it's hard to deduce the schema just from the migrations alone.

How is schema RB generated?

It is a Ruby representation of your database; schema. rb is created by inspecting the database and expressing its structure using Ruby.

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.

Which command is true to rollback migration in Rails?

You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.


1 Answers

Probably the fastest way is to just move the offending initializer to a temporary directory that is outside of the app, and then run your schema load. But if that doesn't work, or isn't an option for some reason, you could always work around that by creating a bare bones rails app to do the schema load:

  1. Create a new rails app: rails new (app)-fixer
  2. Copy your gemfile (unless there are specific exceptions) to the fixer app.
  3. Copy your database.yml config to the fixer app.
  4. Copy your schema.rb file to the fixer app.
  5. Do all appropriate "bundle install" commands as needed for your app.
  6. Then run "rake db:drop db:create db:schema:load"

That will build up a new database from scratch, based on your current schema.

like image 184
Christopher WJ Rueber Avatar answered Sep 19 '22 00:09

Christopher WJ Rueber