Created
April 14, 2009 00:42
-
-
Save dsturnbull/94859 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ENV['RACK_ENV'] ||= 'development' | |
require 'app_title' | |
namespace :db do | |
desc 'create' | |
task :create do | |
require 'db/initial' | |
ret = psql_cmd("create database app_title_#{ENV['RACK_ENV']}") | |
Initial.apply(DB, :up) unless ret == "" | |
end | |
desc 'drop' | |
task :drop do | |
psql_cmd("drop database app_title_#{ENV['RACK_ENV']}") | |
end | |
desc 'migrate' | |
task :migrate do | |
revision_matcher = Proc.new { |m| | |
m.sub(/db\/migrations\//, '').to_i | |
} | |
migration_matcher = Proc.new { |m| | |
eval(m.match(/db\/migrations\/[0-9]+-(.*)\.rb/)[1] \ | |
.gsub(/-(.)/) { $1.upcase } \ | |
.sub(/^(.)/) { $1.upcase }) | |
} | |
revisions = DB[:revisions] | |
Dir['db/migrations/*.rb'].sort_by(&revision_matcher).each do |mig| | |
revision = revision_matcher.call(mig) | |
unless revisions.include?(:revision => revision) | |
DB.transaction do | |
require mig | |
migration = migration_matcher.call(mig) | |
migration.apply(DB, :up) | |
revisions << revision | |
end | |
end | |
end | |
end | |
end | |
def psql_cmd(cmd) | |
out = `echo #{cmd} | psql template1 postgres` | |
print out | |
out | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment