How do you skip failed migrations? (rake db:migrate)

前端 未结 7 1678
忘了有多久
忘了有多久 2021-01-30 16:32

I can\'t seem to find an option or anything that allows me to skip migrations.

I know what you\'re thinking: \"you should never have to do that...\"

I need to sk

7条回答
  •  一生所求
    2021-01-30 17:28

    sometimes, it is necessary to re-fill schema_migrations table with definitely correct migrations... ONLY FOR THIS PURPOSE i have created this method

    def self.insert_missing_migrations(stop_migration=nil)
      files = Dir.glob("db/migrate/*")
      timestamps = files.collect{|f| f.split("/").last.split("_").first}
      only_n_first_migrations = timestamps.split(stop_migration).first
    
      only_n_first_migrations.each do |version|
        sql = "insert into `schema_migrations` (`version`) values (#{version})"
        ActiveRecord::Base.connection.execute(sql) rescue nil
      end
    end
    

    you can copy-paste it into any model you want and use it from console

    YourModel.insert_missing_migrations("xxxxxxxxxxxxxx")
    

    (or somehow else)

    where "xxxxxxxxxxxxxx" - is timestamp of migration before which you want to stop insertion (you can leave it empty)

    !!! use it only if you absolutely understand what result will you get !!!

提交回复
热议问题