Lazy Migrations Table Drop

Liquid error: undefined method `login' for nil:NilClass : March 2nd, 2006

I recently had a migration that had a lot create_table statements in it, so instead of carelessly copying the statements down to the self.down function, I figured I could just parse the file to do it for me.
1
2
3
4
5
6
7
8
9
10

  def self.down
    File.open(__FILE__) do |fp|
      fp.readlines.each do |line|
        next unless line =~ /^\s+create_table.*$/
        drop_table line.sub(/create_table :([^,]+)(,.*)$/, '\1'))
      end
    end
    puts "Done dropping tables."
  end

7 Responses to “Lazy Migrations Table Drop”

  1. Jon Says:
    This should be a plugin. def self.down drop_created_tables end I'd do it but I'm totally plugin illiterate.
  2. Jon Says:
    Grr. What are these comments, markdown? Guess I should have previed. I *meant*: def self.down drop_created_tables end
  3. KyleMaxwell Says:
    Or, you could have just used the reverse migration auto-generator: http://lunchroom.lunchboxsoftware.com/articles/2005/11/29/auto-fill-your-reverse-migrations
  4. punkboy Says:

    This is a great idea and I have incorporated it in jEdit as a Abbrev. As a note, this assumes that you have a create table line with a force or some other option like:

    create_table :table_name, :force => true do |t|
    
    Also, there seems to be an extra ')', I believe the line should read:
    drop_table line.sub(/create_table :([^,]+)(,.*)$/, '\1')
    
    Anyway, Thanks for the great tip!
  5. hangon Says:
    Why is the main column of this site SO TINY???? even in 1024x768 the site look small.....
  6. ThatGuy Says:
    Here's one that will work for people using quoted-style table syntax as well as symbol-style: /create_table [:"']?([^,]\w+)["']?(,.*)$/
  7. ThatGuy Says:
    Sorry, This one doesn't assume the :force on the line. Should work for most variants of create_table. /create_table [:"']?([^,]\w+)["']?(,?.*)$/

Leave a Reply

I am a human (check this)

Remember: escape your underscores \_ and indent code at least 4 spaces or incur the wrath of smartypants.