Merge pull request #228 from travis-ci/cd-coreless-migrations
Make api use travis-migrations for test db
This commit is contained in:
commit
a45178c67d
|
@ -18,4 +18,7 @@ services:
|
||||||
- redis
|
- redis
|
||||||
|
|
||||||
before_script:
|
before_script:
|
||||||
- 'RAILS_ENV=test bundle exec rake db:create db:migrate --trace'
|
- 'RAILS_ENV=test bundle exec rake db:create --trace'
|
||||||
|
|
||||||
|
script:
|
||||||
|
- bundle exec rspec spec
|
||||||
|
|
1
Gemfile
1
Gemfile
|
@ -41,6 +41,7 @@ group :test do
|
||||||
gem 'factory_girl', '~> 2.4.0'
|
gem 'factory_girl', '~> 2.4.0'
|
||||||
gem 'mocha', '~> 0.12'
|
gem 'mocha', '~> 0.12'
|
||||||
gem 'database_cleaner', '~> 0.8.0'
|
gem 'database_cleaner', '~> 0.8.0'
|
||||||
|
gem 'travis-migrations', github: 'travis-ci/travis-migrations'
|
||||||
end
|
end
|
||||||
|
|
||||||
group :development do
|
group :development do
|
||||||
|
|
10
Gemfile.lock
10
Gemfile.lock
|
@ -72,6 +72,12 @@ GIT
|
||||||
travis-config (~> 0.1.0)
|
travis-config (~> 0.1.0)
|
||||||
virtus (~> 1.0.0)
|
virtus (~> 1.0.0)
|
||||||
|
|
||||||
|
GIT
|
||||||
|
remote: git://github.com/travis-ci/travis-migrations.git
|
||||||
|
revision: d6105ad77303bcd04d1e376070a11dbc8b3077c7
|
||||||
|
specs:
|
||||||
|
travis-migrations (0.0.1)
|
||||||
|
|
||||||
GIT
|
GIT
|
||||||
remote: git://github.com/travis-ci/travis-sidekiqs.git
|
remote: git://github.com/travis-ci/travis-sidekiqs.git
|
||||||
revision: 21a2fee158e25252dd78f5fa31e81b4f6583be23
|
revision: 21a2fee158e25252dd78f5fa31e81b4f6583be23
|
||||||
|
@ -376,8 +382,12 @@ DEPENDENCIES
|
||||||
travis-api!
|
travis-api!
|
||||||
travis-config (~> 0.1.0)
|
travis-config (~> 0.1.0)
|
||||||
travis-core!
|
travis-core!
|
||||||
|
travis-migrations!
|
||||||
travis-sidekiqs!
|
travis-sidekiqs!
|
||||||
travis-support!
|
travis-support!
|
||||||
travis-yaml!
|
travis-yaml!
|
||||||
unicorn
|
unicorn
|
||||||
yard-sinatra!
|
yard-sinatra!
|
||||||
|
|
||||||
|
BUNDLED WITH
|
||||||
|
1.10.6
|
||||||
|
|
|
@ -17,8 +17,10 @@ This is the app running on https://api.travis-ci.org/
|
||||||
|
|
||||||
### Database setup
|
### Database setup
|
||||||
|
|
||||||
1. `rake db:create db:migrate`
|
NB detail for how `rake` sets up the database can be found in the `Rakefile`. In the `namespace :db` block you will see the database name for development is hardcoded to `travis-development`. If you are using a different configuration you will have to make your own adjustments.
|
||||||
2. for testing 'RAILS_ENV=test bundle exec rake db:create db:migrate --trace'
|
|
||||||
|
1. `bundle exec rake db:create`
|
||||||
|
2. for testing 'RAILS_ENV=test bundle exec rake db:create --trace'
|
||||||
1. Clone `travis-logs` and copy the `logs` database (assume the PostgreSQL user is `postgres`):
|
1. Clone `travis-logs` and copy the `logs` database (assume the PostgreSQL user is `postgres`):
|
||||||
```sh-session
|
```sh-session
|
||||||
cd ..
|
cd ..
|
||||||
|
@ -31,7 +33,7 @@ pg_dump -t logs travis_logs_development | psql -U postgres travis_development
|
||||||
|
|
||||||
Repeat the database steps for `RAILS_ENV=test`.
|
Repeat the database steps for `RAILS_ENV=test`.
|
||||||
```sh-session
|
```sh-session
|
||||||
RAILS_ENV=test rake db:create db:structure:load
|
RAILS_ENV=test bundle exec rake db:create
|
||||||
pushd ../travis-logs
|
pushd ../travis-logs
|
||||||
RAILS_ENV=test rvm jruby do bundle exec rake db:migrate
|
RAILS_ENV=test rvm jruby do bundle exec rake db:migrate
|
||||||
psql -c "DROP TABLE IF EXISTS logs CASCADE" -U postgres travis_test
|
psql -c "DROP TABLE IF EXISTS logs CASCADE" -U postgres travis_test
|
||||||
|
|
122
Rakefile
122
Rakefile
|
@ -1,21 +1,22 @@
|
||||||
require 'bundler/setup'
|
require 'rake'
|
||||||
require 'travis'
|
require 'travis/migrations'
|
||||||
require 'travis/engine'
|
|
||||||
|
|
||||||
begin
|
task default: :spec
|
||||||
ENV['SCHEMA'] = File.expand_path('../db/schema.rb', $:.detect { |p| p.include?('travis-core') })
|
|
||||||
require 'micro_migrations'
|
|
||||||
rescue LoadError
|
|
||||||
# we can't load micro migrations on production
|
|
||||||
end
|
|
||||||
require 'travis'
|
|
||||||
|
|
||||||
begin
|
namespace :db do
|
||||||
require 'rspec/core/rake_task'
|
if ENV["RAILS_ENV"] == 'test'
|
||||||
RSpec::Core::RakeTask.new
|
desc 'Create and migrate the test database'
|
||||||
task default: :spec
|
task :create do
|
||||||
rescue LoadError
|
sh 'createdb travis_test' rescue nil
|
||||||
warn "could not load rspec"
|
sh "psql -q travis_test < #{Gem.loaded_specs['travis-migrations'].full_gem_path}/db/structure.sql"
|
||||||
|
end
|
||||||
|
else
|
||||||
|
desc 'Create and migrate the development database'
|
||||||
|
task :create do
|
||||||
|
sh 'createdb travis_development' rescue nil
|
||||||
|
sh "psql -q travis_development < #{Gem.loaded_specs['travis-migrations'].full_gem_path}/db/structure.sql"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
desc "generate gemspec"
|
desc "generate gemspec"
|
||||||
|
@ -36,92 +37,9 @@ task 'travis-api.gemspec' do
|
||||||
end
|
end
|
||||||
|
|
||||||
File.open('travis-api.gemspec', 'w') { |f| f << content }
|
File.open('travis-api.gemspec', 'w') { |f| f << content }
|
||||||
end
|
|
||||||
|
|
||||||
task default: 'travis-api.gemspec'
|
|
||||||
|
|
||||||
tasks_path = File.expand_path('../lib/tasks/*.rake', __FILE__)
|
|
||||||
Dir.glob(tasks_path).each { |r| import r }
|
|
||||||
|
|
||||||
module ActiveRecord
|
|
||||||
class Migration
|
|
||||||
class << self
|
|
||||||
attr_accessor :disable_ddl_transaction
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Disable DDL transactions for this migration.
|
task default: 'travis-api.gemspec'
|
||||||
def self.disable_ddl_transaction!
|
|
||||||
@disable_ddl_transaction = true
|
|
||||||
end
|
|
||||||
|
|
||||||
def disable_ddl_transaction # :nodoc:
|
tasks_path = File.expand_path('../lib/tasks/*.rake', __FILE__)
|
||||||
self.class.disable_ddl_transaction
|
Dir.glob(tasks_path).each { |r| import r }
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
class Migrator
|
|
||||||
def use_transaction?(migration)
|
|
||||||
!migration.disable_ddl_transaction && Base.connection.supports_ddl_transactions?
|
|
||||||
end
|
|
||||||
|
|
||||||
def ddl_transaction(migration, &block)
|
|
||||||
if use_transaction?(migration)
|
|
||||||
Base.transaction { block.call }
|
|
||||||
else
|
|
||||||
block.call
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def migrate(&block)
|
|
||||||
current = migrations.detect { |m| m.version == current_version }
|
|
||||||
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
|
|
||||||
|
|
|
@ -20,4 +20,3 @@ development:
|
||||||
test:
|
test:
|
||||||
<<: *defaults
|
<<: *defaults
|
||||||
database: travis_test
|
database: travis_test
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user