Skip to content

Instantly share code, notes, and snippets.

@lfzawacki
Created May 14, 2014 15:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lfzawacki/0f94e6719f5af6140d94 to your computer and use it in GitHub Desktop.
Save lfzawacki/0f94e6719f5af6140d94 to your computer and use it in GitHub Desktop.
Simple script to manage different database.yml files in a rails development environment
#!/home/lucas/.rbenv/shims/ruby
# Use an appropriate file path for your ruby interpreter
require 'fileutils'
class ChangeDB
def initialize dir='config/.changedb', db_yml='config/database.yml'
@dir = dir
@db_yml = db_yml
@current_file = "#{@dir}/current"
# create if doesnt exist
Dir.mkdir(@dir) if !File.directory?(@dir)
@files = Dir.glob("#{@dir}/*").map{|filename| filename.split('/')[-1]}
@files -= ['current']
end
def classy_exit
puts "Run it like this:"
puts " $ changedb list"
puts " $ changedb use [dbname]"
puts " $ changedb save [dbname]"
exit 0
end
def current
IO.read(@current_file).chomp
end
def set_current filename
File.open(@current_file, 'w') {|f| f.puts filename }
end
def list
puts @files.map{ |file| " #{current == file ? '*' : ' '} #{file}" }
end
def use filename
FileUtils.cp "#{@dir}/#{filename}", @db_yml
set_current filename
end
def save filename
FileUtils.cp @db_yml, "#{@dir}/#{filename}"
end
end
# Do real stuff
if __FILE__ == $0
changedb = ChangeDB.new
changedb.classy_exit if ARGV.empty?
case ARGV[0]
when 'list' then
changedb.list
when 'use' then
changedb.use ARGV[1]
when 'save'
changedb.save ARGV[1]
else
changedb.classy_exit
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment