new rakefile

This commit is contained in:
carlad 2016-02-04 21:27:20 +01:00
parent 14ec02da79
commit 4d98ccafd6
2 changed files with 94 additions and 43 deletions

View File

@ -18,10 +18,7 @@ services:
- redis - redis
before_script: before_script:
- RAILS_ENV=test - 'RAILS_ENV=test bundle exec rake db:create db:migrate --trace'
- createdb travis_test
- createdb travis_pro_test
- bundle exec rake db:migrate --trace
script: script:
- bundle exec rspec spec - bundle exec rspec spec

132
Rakefile
View File

@ -1,47 +1,101 @@
require 'rspec/core/rake_task'
require 'bundler/setup' require 'bundler/setup'
require 'micro_migrations'
require 'travis' require 'travis'
require 'travis/engine'
begin ActiveRecord::Base.schema_format = :sql
# ENV['SCHEMA'] = File.expand_path('../db/schema.rb', $:.detect { |p| p.include?('travis-core') }) Rails.application.config.paths["db/migrate"] = ["db/migrate", "#{Gem.loaded_specs['travis-core'].full_gem_path}/db/migrate"]
require 'micro_migrations'
rescue LoadError desc 'Run specs'
# we can't load micro migrations on production RSpec::Core::RakeTask.new do |t|
t.pattern = './spec/**/*_spec.rb'
end end
begin module ActiveRecord
require 'rspec/core/rake_task' class Migration
RSpec::Core::RakeTask.new class << self
task default: :spec attr_accessor :disable_ddl_transaction
rescue LoadError end
warn "could not load rspec"
end # Disable DDL transactions for this migration.
def self.disable_ddl_transaction!
desc "generate gemspec" @disable_ddl_transaction = true
task 'travis-api.gemspec' do end
content = File.read 'travis-api.gemspec'
def disable_ddl_transaction # :nodoc:
fields = { self.class.disable_ddl_transaction
authors: `git shortlog -sn`.scan(/[^\d\s].*/), end
email: `git shortlog -sne`.scan(/[^<]+@[^>]+/), end
files: `git ls-files`.split("\n").reject { |f| f =~ /^(\.|Gemfile)/ }
} class Migrator
def use_transaction?(migration)
fields.each do |field, values| !migration.disable_ddl_transaction && Base.connection.supports_ddl_transactions?
updated = " s.#{field} = [" end
updated << values.map { |v| "\n %p" % v }.join(',')
updated << "\n ]" def ddl_transaction(migration, &block)
content.sub!(/ s\.#{field} = \[\n( .*\n)* \]/, updated) if use_transaction?(migration)
end Base.transaction { block.call }
else
File.open('travis-api.gemspec', 'w') { |f| f << content } block.call
end end
end
task default: 'travis-api.gemspec'
def migrate(&block)
tasks_path = File.expand_path('../lib/tasks/*.rake', __FILE__) current = migrations.detect { |m| m.version == current_version }
Dir.glob(tasks_path).each { |r| import r } target = migrations.detect { |m| m.version == @target_version }
if target.nil? && @target_version && @target_version > 0
raise UnknownMigrationVersionError.new(@target_version)
end
start = up? ? 0 : (migrations.index(current) || 0)
finish = migrations.index(target) || migrations.size - 1
runnable = migrations[start..finish]
# skip the last migration if we're headed down, but not ALL the way down
runnable.pop if down? && target
ran = []
runnable.each do |migration|
if block && !block.call(migration)
next
end
Base.logger.info "Migrating to #{migration.name} (#{migration.version})" if Base.logger
seen = migrated.include?(migration.version.to_i)
# On our way up, we skip migrating the ones we've already migrated
next if up? && seen
# On our way down, we skip reverting the ones we've never migrated
if down? && !seen
migration.announce 'never migrated, skipping'; migration.write
next
end
begin
ddl_transaction(migration) do
migration.migrate(@direction)
record_version_state_after_migrating(migration.version)
end
ran << migration
rescue => e
canceled_msg = Base.connection.supports_ddl_transactions? ? "this and " : ""
raise StandardError, "An error has occurred, #{canceled_msg}all later migrations canceled:\n\n#{e}", e.backtrace
end
end
ran
end
end
class MigrationProxy
delegate :disable_ddl_transaction, to: :migration
end
end
task :default => :spec
module ActiveRecord module ActiveRecord
class Migration class Migration
class << self class << self