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

I forgot to put any code in the 'down' section. How do I undo a 'rake db:migrate'?

Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
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 Select All

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

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."

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
#! /usr/bin/env ruby
#
# $Id: wipedb 1171 2006-06-10 16:36:57Z jpb $
#
# Copyright 2006 J. P. Block 
 
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 Select All
Random Solutions  
 
programming4us programming4us