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
|
||||
|
||||
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 'mocha', '~> 0.12'
|
||||
gem 'database_cleaner', '~> 0.8.0'
|
||||
gem 'travis-migrations', github: 'travis-ci/travis-migrations'
|
||||
end
|
||||
|
||||
group :development do
|
||||
|
|
10
Gemfile.lock
10
Gemfile.lock
|
@ -72,6 +72,12 @@ GIT
|
|||
travis-config (~> 0.1.0)
|
||||
virtus (~> 1.0.0)
|
||||
|
||||
GIT
|
||||
remote: git://github.com/travis-ci/travis-migrations.git
|
||||
revision: d6105ad77303bcd04d1e376070a11dbc8b3077c7
|
||||
specs:
|
||||
travis-migrations (0.0.1)
|
||||
|
||||
GIT
|
||||
remote: git://github.com/travis-ci/travis-sidekiqs.git
|
||||
revision: 21a2fee158e25252dd78f5fa31e81b4f6583be23
|
||||
|
@ -376,8 +382,12 @@ DEPENDENCIES
|
|||
travis-api!
|
||||
travis-config (~> 0.1.0)
|
||||
travis-core!
|
||||
travis-migrations!
|
||||
travis-sidekiqs!
|
||||
travis-support!
|
||||
travis-yaml!
|
||||
unicorn
|
||||
yard-sinatra!
|
||||
|
||||
BUNDLED WITH
|
||||
1.10.6
|
||||
|
|
10
README.md
10
README.md
|
@ -7,7 +7,7 @@ This is the app running on https://api.travis-ci.org/
|
|||
1. PostgreSQL 9.3 or higher
|
||||
1. Redis
|
||||
1. RabbitMQ
|
||||
1. Nginx *NB: If working on Ubuntu please install Nginx manually from source. [This guide](http://www.rackspace.com/knowledge_center/article/ubuntu-and-debian-installing-nginx-from-source) is helpful but make sure you install the [latest stable version](https://www.nginx.com/resources/wiki/start/topics/tutorials/install/#stable), include the user name on your ubuntu machine when compiling (add `--user=[yourusername]` as an option when running `./configure`), and don't follow any subsequent server configuration steps. Travis-api will start and configure its own nginx server when run locally.
|
||||
1. Nginx *NB: If working on Ubuntu please install Nginx manually from source. [This guide](http://www.rackspace.com/knowledge_center/article/ubuntu-and-debian-installing-nginx-from-source) is helpful but make sure you install the [latest stable version](https://www.nginx.com/resources/wiki/start/topics/tutorials/install/#stable), include the user name on your ubuntu machine when compiling (add `--user=[yourusername]` as an option when running `./configure`), and don't follow any subsequent server configuration steps. Travis-api will start and configure its own nginx server when run locally.
|
||||
|
||||
## Installation
|
||||
|
||||
|
@ -17,8 +17,10 @@ This is the app running on https://api.travis-ci.org/
|
|||
|
||||
### Database setup
|
||||
|
||||
1. `rake db:create db:migrate`
|
||||
2. for testing 'RAILS_ENV=test bundle exec rake db:create db:migrate --trace'
|
||||
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.
|
||||
|
||||
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`):
|
||||
```sh-session
|
||||
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`.
|
||||
```sh-session
|
||||
RAILS_ENV=test rake db:create db:structure:load
|
||||
RAILS_ENV=test bundle exec rake db:create
|
||||
pushd ../travis-logs
|
||||
RAILS_ENV=test rvm jruby do bundle exec rake db:migrate
|
||||
psql -c "DROP TABLE IF EXISTS logs CASCADE" -U postgres travis_test
|
||||
|
|
150
Rakefile
150
Rakefile
|
@ -1,127 +1,45 @@
|
|||
require 'bundler/setup'
|
||||
require 'travis'
|
||||
require 'travis/engine'
|
||||
require 'rake'
|
||||
require 'travis/migrations'
|
||||
|
||||
begin
|
||||
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'
|
||||
task default: :spec
|
||||
|
||||
begin
|
||||
require 'rspec/core/rake_task'
|
||||
RSpec::Core::RakeTask.new
|
||||
task default: :spec
|
||||
rescue LoadError
|
||||
warn "could not load rspec"
|
||||
namespace :db do
|
||||
if ENV["RAILS_ENV"] == 'test'
|
||||
desc 'Create and migrate the test database'
|
||||
task :create do
|
||||
sh 'createdb travis_test' rescue nil
|
||||
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
|
||||
|
||||
desc "generate gemspec"
|
||||
task 'travis-api.gemspec' do
|
||||
content = File.read 'travis-api.gemspec'
|
||||
content = File.read 'travis-api.gemspec'
|
||||
|
||||
fields = {
|
||||
authors: `git shortlog -sn`.scan(/[^\d\s].*/),
|
||||
email: `git shortlog -sne`.scan(/[^<]+@[^>]+/),
|
||||
files: `git ls-files`.split("\n").reject { |f| f =~ /^(\.|Gemfile)/ }
|
||||
}
|
||||
fields = {
|
||||
authors: `git shortlog -sn`.scan(/[^\d\s].*/),
|
||||
email: `git shortlog -sne`.scan(/[^<]+@[^>]+/),
|
||||
files: `git ls-files`.split("\n").reject { |f| f =~ /^(\.|Gemfile)/ }
|
||||
}
|
||||
|
||||
fields.each do |field, values|
|
||||
updated = " s.#{field} = ["
|
||||
updated << values.map { |v| "\n %p" % v }.join(',')
|
||||
updated << "\n ]"
|
||||
content.sub!(/ s\.#{field} = \[\n( .*\n)* \]/, updated)
|
||||
end
|
||||
fields.each do |field, values|
|
||||
updated = " s.#{field} = ["
|
||||
updated << values.map { |v| "\n %p" % v }.join(',')
|
||||
updated << "\n ]"
|
||||
content.sub!(/ s\.#{field} = \[\n( .*\n)* \]/, updated)
|
||||
end
|
||||
|
||||
File.open('travis-api.gemspec', 'w') { |f| f << content }
|
||||
end
|
||||
File.open('travis-api.gemspec', 'w') { |f| f << content }
|
||||
end
|
||||
|
||||
task default: 'travis-api.gemspec'
|
||||
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
|
||||
|
||||
# Disable DDL transactions for this migration.
|
||||
def self.disable_ddl_transaction!
|
||||
@disable_ddl_transaction = true
|
||||
end
|
||||
|
||||
def disable_ddl_transaction # :nodoc:
|
||||
self.class.disable_ddl_transaction
|
||||
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
|
||||
tasks_path = File.expand_path('../lib/tasks/*.rake', __FILE__)
|
||||
Dir.glob(tasks_path).each { |r| import r }
|
||||
|
|
|
@ -20,4 +20,3 @@ development:
|
|||
test:
|
||||
<<: *defaults
|
||||
database: travis_test
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user