Rails: Alter migrations during early development phases

前端 未结 6 576
栀梦
栀梦 2021-02-04 22:08

During the very early phases of development in a Rails app, I prefer to modify the migrations files directly to add new columns (fields) to my tables (models) instead of piling

相关标签:
6条回答
  • 2021-02-04 22:35

    I run the following commands to solve this problem. Save it in a script and you are ready to go! Of course, this presumes that losing the data is fine. Loading the fixtures is easy enough to add to this script in any case.

    #!/bin/sh
    rake db:drop
    rake db:create
    rake db:migrate --trace
    

    PS: My friend who gave me this idea calls this script rake_dance :)

    0 讨论(0)
  • 2021-02-04 22:40

    Cleanest solution, driven from Vijay Dev's solution is to create a rake task:

    namespace :db do
      desc "Drops, recreates and seeds the database."
      task :reload => [:drop, :create, :migrate, :seed] do
        Rake::Task['db:drop'].invoke
        Rake::Task['db:create'].invoke
        Rake::Task['db:migrate'].invoke
        Rake::Task['db:seed'].invoke
      end
    end
    
    0 讨论(0)
  • 2021-02-04 22:42

    Found out we can combine everything in one command:

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

    Using a shell aliase makes it a snap to redo all old migrations.

    0 讨论(0)
  • 2021-02-04 22:45

    Presumably the fact that you are doing it indicates that it's possible!

    0 讨论(0)
  • I recommend using fixtures when taking this approach (as it allows quickly recreating sample data for the application after dropping the database to recreate). Another option is http://datamapper.org/ (does not require migrations).

    0 讨论(0)
  • 2021-02-04 22:52

    db:reset does what Omar suggests.

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