Purge or recreate a Ruby on Rails database

前端 未结 19 1160
清酒与你
清酒与你 2020-11-28 17:01

I have a dev Ruby on Rails database full of data. I want to delete everything and rebuild the database. I\'m thinking of using something like:

rake db:recrea         


        
相关标签:
19条回答
  • 2020-11-28 17:23

    You can use this following command line:

    rake db:drop db:create db:migrate db:seed db:test:clone
    
    0 讨论(0)
  • 2020-11-28 17:24

    On Rails 4, all needed is

    $ rake db:schema:load
    

    That would delete the entire contents on your DB and recreate the schema from your schema.rb file, without having to apply all migrations one by one.

    0 讨论(0)
  • 2020-11-28 17:24

    According to Rails guide, this one liner should be used because it would load from the schema.rb instead of reloading the migration files one by one:

    rake db:reset
    
    0 讨论(0)
  • 2020-11-28 17:26

    On rails 4.2, to remove all data but preserve the database

    $ bin/rake db:purge && bin/rake db:schema:load
    

    https://github.com/rails/rails/blob/4-2-stable/activerecord/CHANGELOG.md

    0 讨论(0)
  • 2020-11-28 17:27

    Use like

    rake db:drop db:create db:migrate db:seed
    

    All in one line. This is faster since the environment doesn't get reloaded again and again.

    db:drop - will drop database.

    db:create - will create database (host/db/password will be taken from config/database.yml)

    db:migrate - will run existing migrations from directory (db/migration/.rb)*.

    db:seed - will run seed data possible from directory (db/migration/seed.rb)..

    I usually prefer:

    rake db:reset
    

    to do all at once.

    Cheers!

    0 讨论(0)
  • 2020-11-28 17:28

    To drop a particular database, you can do this on rails console:

    $rails console
    Loading development environment
    1.9.3 > ActiveRecord::Migration.drop_table(:<table_name>)
    1.9.3 > exit
    

    And then migrate DB again

    $bundle exec rake db:migrate 
    
    0 讨论(0)
提交回复
热议问题