# Copyright 2007, Rowan Rodrik van der Molen, # Wiebe Cazemier (http://www.halfgaar.net) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . $yellow_begin = "\033[01;33m" $green_begin = "\033[01;32m" $red_begin = "\033[01;31m" $color_end = "\033[00m" namespace :db do task :pg_migrate => :environment do last_version = nil Dir["#{RAILS_ROOT}/db/migrate/[0-9]*_*.pgsql"].sort.each do |f| unless f =~ /_down.pgsql$/ file_version = f.scan(/([0-9]+)_([_a-z0-9]*).pgsql$/).first.first.to_i raise "duplicate versioned migration found." if file_version == last_version current_version = ActiveRecord::Base.connection.select_value('SELECT version FROM public.schema_info LIMIT 1').to_i if file_version > current_version puts "#{$yellow_begin}Migrating from version #{current_version} to #{file_version}...#{$color_end}" begin # Because our migration files already contain begin and commit statements, this can no longer # be executed in a migration. It's not too much of a problem, but it could theoretically cause # problems. ActiveRecord::Base.connection.execute( File.open(f).read ) ActiveRecord::Base.connection.execute("UPDATE public.schema_info SET version = #{file_version}") rescue puts "#{$red_begin}Migrating from version #{current_version} to #{file_version} failed!#{$color_end}" raise end end last_version = file_version end end end end