148

Due to some deployment issues I stopped tracking schema.rb in git. Somehow I have stuffed this up and somewhere along the way my schema.rb file has disappeared.

Is there a way of regenerating schema.rb from the database or from the migrations? I would prefer not to lose the existing data.

Mike Atlas
  • 8,193
  • 4
  • 46
  • 62
brad
  • 9,573
  • 12
  • 62
  • 89

7 Answers7

256

If you run a rake -T it will list all possible rake tasks for your Rails project. One of them is db:schema:dump which will recreate the schema.rb for the Rails app from the database.

bundle exec rake db:schema:dump
mguymon
  • 8,946
  • 2
  • 39
  • 61
  • Great thanks, many answers but looks like you were first (just) so tick for you. Just curious, does this generate the schema from the database itself or from the migrations? – brad Mar 16 '12 at 04:03
  • 8
    From the database itself, so be careful if there are changes that happened outside of the migrations. – mguymon Mar 16 '12 at 04:08
  • 1
    schema.rb still contains empty schema after `rake db:schema:dump` on rails 2.0 – Will Hardwick-Smith Jan 07 '16 at 03:30
  • My schema.rb file remains exactly the same after running the above commands – stevec May 27 '18 at 11:47
62

Careful,

rake db:schema:dump

will dump the current DB schema FROM the DB. This means that if you made any changes to your migrations, they will NOT be reflected in schema.rb file which is not what you want IMO.

If you want to re-create the schema from the migrations, do the following:

rake db:drop  # ERASES THE DATABASE !!!! 
rake db:create
rake db:migrate
gamov
  • 3,789
  • 1
  • 31
  • 28
  • 2
    That would result in loss of data, which the OP said they wanted to avoid. Additionally, as Colin points out, regenerating the database purely from the migrations is potentially a very bad idea because of the increased possibility of running in to odd dependency issues (generally speaking). If there are pending migrations, it would likely be best to run those last migrations on the development machine, and then run the `rake db:schema:dump` command. – Paul Richter Sep 10 '14 at 15:26
  • 4
    Every canvas is clearly explained in my answer. I've got bitten EXACTLY by running only schema:dump and not getting a clean schema. The OP is talking about tracking the schema in a CVS. I would like to have my schema aligned with my definitions in my migrations and not an obsolete version from a production DB or an old development DB – gamov Sep 11 '14 at 03:50
13

RAILS 5 Way:

rails db:schema:dump

or if you Encounter Gem::LoadError then:

bundle exec rails db:schema:dump

Note:

in rails 5 it is recommended that task are generated/executed by using rails instead of rake, this is just to remember, rails generated task are of extension .rake see in lib/tasks/myTask.rake. which means these task can also be executed by prepending rake.

Kaleem Ullah
  • 6,799
  • 3
  • 42
  • 47
12
rake db:schema:dump

I think this is still valid in Rails 3 - it regenerates the schema.rb from the database.

psugar
  • 1,897
  • 2
  • 18
  • 27
7

Directly from the schema.rb file itself:

If you need to create the application database on another system, you should be using db:schema:load, not running all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations you'll amass, the slower it'll run and the greater likelihood for issues).

So do NOT do the suggestion of rake db:migrate, which was suggested in the - at the time of this writing - lowest rated answer.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Colin Summers
  • 133
  • 2
  • 5
  • You would probably want to run the last few pending migrations if there are any on your development machine before regenerating the schema, but yes regenerating the database purely from the migrations is a very bad idea, especially since that would result in loss of data. – Paul Richter Sep 10 '14 at 15:23
6

If you regenerate schema.rb locally, you should be alright. It simply holds a representation of the structure of your database tables. The data itself is not contained in this file.

To regenerate your schema.rb file, run:

bundle exec rake db:schema:dump

Then simply commit the new schema.rb file and you should be in good shape!

Graham Swan
  • 4,818
  • 2
  • 30
  • 39
2

I also had a similar problem where my old schema was not refreshing even if I deleted migration.

So, what I did was dropping all existing tables in the database and migrating them again. Then running "db:schema:load" command gave me a fresh schema.rb.

drop table my_table_name // deleted them individually
rake db:migrate
rake db:schema:dump // re-created a new schema
dubucha
  • 1,027
  • 10
  • 16