Purge or recreate a Ruby on Rails database

前端 未结 19 1163
清酒与你
清酒与你 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:29

    3 options, same result:

    1. All steps:

      $ rake db:drop           # deletes the database for the current env
      $ rake db:create         # creates the database for the current env
      $ rake db:schema:load    # loads the schema already generated from schema.rb / erases data
      $ rake db:seed           # seed with initial data
    

    2. Reset:

      $ rake db:reset          # drop / schema:load / seed
    

    3. Migrate:reset:

      $ rake db:migrate:reset  # drop / create / migrate
      $ rake db:seed
    

    Notes:

    • If schema:load is used is faster than doing all migrations, but same result.
    • All data will be lost.
    • You can run multiple rakes in one line.
    • Works with rails 3.
    0 讨论(0)
  • 2020-11-28 17:32

    I've today made quite a few changes to my rails schema. I realised I needed an additional two models in a hierarchy and some others to be deleted. There were many little changes required to the models and controllers.

    I added the two new models and created them, using:

    rake db:migrate
    

    Then I edited the schema.rb file. I manually removed the old models that were no longer required, changed the foreign key field as required and just reordered it a bit to make it clearer to me. I deleted all the migrations, and then re-ran the build via:

    rake db:reset
    

    It worked perfectly. All the data has to be reloaded, of course. Rails realised the migrations had been deleted and reset the high-water mark:

    -- assume_migrated_upto_version(20121026094813, ["/Users/sean/rails/f4/db/migrate"])
    
    0 讨论(0)
  • 2020-11-28 17:33

    I know two ways to do this:

    This will reset your database and reload your current schema with all:

    rake db:reset db:migrate
    

    This will destroy your db and then create it and then migrate your current schema:

    rake db:drop db:create db:migrate
    

    All data will be lost in both scenarios.

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

    You can manually do:

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

    Or just rake db:reset, which will run the above steps but will also run your db/seeds.rb file.

    An added nuance is that rake db:reset loads directly from your schema.rb file as opposed to running all the migrations files again.

    You data gets blown away in all cases.

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

    I think the best way to run this command:

    **rake db:reset** it does db:drop, db:setup
     rake db:setup does db:create, db:schema:load, db:seed
    
    0 讨论(0)
  • 2020-11-28 17:33

    Simply you can run

    rake db:setup

    It will drop database, create new database and populate db from seed if you created seed file with some data.

    0 讨论(0)
提交回复
热议问题