Got tired of having to manually drop tables when working on migrations

Posted by jpb Thu, 15 Jun 2006 06:16:00 GMT

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


Posted in , ,  | Tags , , ,  | no comments

Gem wouldn't update some Ruby extensions with XCode 2.2 & 10.4.3

Posted by jpb Wed, 15 Feb 2006 01:33:00 GMT

I went to update the mysql extension for Ruby with gem, and got the following error:


[jpb@athena:~/svn/external/typo]$ sudo gem install mysql -- --with-mysql-lib=/sw/lib/mysql --with-mysql-include=/sw/include 
Attempting local installation of 'mysql'
Local gem file not found: mysql*.gem
Attempting remote installation of 'mysql'
Building native extensions.  This could take a while...
can't find header files for ruby.
ERROR:  While executing gem ... (RuntimeError)
    ERROR: Failed to build gem native extension.
Gem files will remain installed in /usr/lib/ruby/gems/1.8/gems/mysql-2.7 for inspection.
  ruby extconf.rb install mysql -- --with-mysql-lib=/sw/lib/mysql --with-mysql-include=/sw/include

I’ve been working on other projects for a while, and it’s been a while since I tinkered with Ruby. I’ve updated my powerbook to 10.4.3 and XCode 2.2 since the last time I did anything with gem that wasn’t pure Ruby, so this was puzzling me since I’d documented how I got it to build the last time, and was using the exact same command that worked a few months back.

Google to rescue though, it turns out that a bunch of header files that used to be searched for in /usr/lib/ruby/1.8/universal-darwin8.0 are now expected to be in /usr/lib/ruby/1.8/powerpc-darwin8.0, and of course, aren’t, so gem can’t build the extension any more.

Fortunately this is easily fixed with


cd /usr/lib/ruby/1.8/powerpc-darwin8.0 
sudo ln -s ../universal-darwin8.0/* ./ 

Anyway, posting it here so Google can find it.

Posted in , ,  | Tags , ,  | 1 comment