Solved

How to undo an incorrect rails migration after rake db:migrate

Posted on 2007-11-29
1
6,103 Views
Last Modified: 2012-08-14
I forgot to put any code in the 'down' section. How do I undo a 'rake db:migrate'?


class User < ActiveRecord::Migration

  def self.up

    create_table :users do |t|

      t.column "username", :string

      t.column "fname", :string

      t.column "lname", :string

      t.column "email", :string

      t.column "hashed_password", :string

      t.column "campus_id", :integer

    end

  end
 

  def self.down

  end

end

Open in new window

0
Comment
Question by:smetterd
1 Comment
 
LVL 7

Accepted Solution

by:
Wod earned 500 total points
ID: 20376647
http://blog.apesseekingknowledge.net/articles/category/ruby-on-rails

"I kept having typos in my migrations, and it turned out to be easier to just zap all the tables and re-run rake migrate than to undo just enough of the migration to get the db back to the way the migration was expecting.

Anyway, so I wrote this wipedb script that reads database.yml so you don’t need to maintain the database user & password in multiple places."


#! /usr/bin/env ruby

#

# $Id: wipedb 1171 2006-06-10 16:36:57Z jpb $

#

# Copyright 2006 J. P. Block <jpb@ApesSeekingKnowledge.net>
 

require 'ostruct'

require 'optparse'

require 'yaml'
 

class MyArgs

  MODENAMES = %w[development test production]
 

  # return structure showing what opts were picked
 

  def self.parse(args)

    options = OpenStruct.new

    options.mode = 'development'

    options.debug = 0
 

    opts = OptionParser.new do |opts|

      opts.banner = "Usage: script/wipedb [options]" 

      opts.separator "" 

      opts.separator "Specific options:" 

      mode_list = MODENAMES.join(',')

      opts.on("-m MODENAME", "--mode MODENAME", MODENAMES, "Select mode",

                      "  (#{mode_list})") do |the_mode|

        options.mode = the_mode

      end

      opts.on("-d", "--debug", "Turn on debug") do

        options.debug = 1

      end

      opts.on_tail("-h", "--help", "Show this message") do

        puts opts

        exit

      end
 

      opts.on_tail("--version", "Show version") do

        puts "$Id: wipedb 1171 2006-06-10 16:36:57Z jpb $" 

        exit

      end

    end
 

    opts.parse!(args)

    options

  end # parse()

end
 

options = MyArgs.parse(ARGV)
 

mode = options.mode
 

config = YAML.load_file('config/database.yml')
 

database = config[options.mode]['database']

host = config[options.mode]['host']

password = config[options.mode]['password']

username = config[options.mode]['username']
 

if options.debug == 1:  

  puts "mode: #{mode}" 

  puts "database: #{database}" 

  puts "host: #{host}" 

  puts "username: #{username}" 

  puts "password: #{password}" 

end
 

zap_list = `mysqldump -u #{username} -p#{password} --complete-insert --add-drop-table --quick --single-transaction #{database} | grep DROP`
 

zapper = IO.popen("mysql -u #{username} -p#{password} #{database}", "w+")

zap_list.each do |z|

  zapper.puts(z)

end

zapper.close_write

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now