pact_broker 0.0.10 → 1.0.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +0 -3
- data/README.md +28 -1
- data/Rakefile +4 -2
- data/assets/d3.v3.js +9263 -0
- data/assets/force.csv +29 -0
- data/assets/index.html +186 -0
- data/assets/index2.html +224 -0
- data/assets/relationships +4 -0
- data/assets/stylesheets/github.css +387 -0
- data/bethtest.rb +67 -0
- data/config.ru +10 -4
- data/config/boot.rb +4 -0
- data/example/Gemfile +5 -0
- data/example/config.ru +18 -0
- data/lib/db.rb +52 -0
- data/lib/pact_broker.rb +2 -0
- data/lib/pact_broker/api.rb +2 -0
- data/lib/pact_broker/api/decorators/pact_collection_decorator.rb +1 -1
- data/lib/pact_broker/api/decorators/pacticipant_collection_decorator.rb +1 -1
- data/lib/pact_broker/api/decorators/relationships_csv_decorator.rb +32 -0
- data/lib/pact_broker/api/renderers/html_pact_renderer.rb +43 -0
- data/lib/pact_broker/api/resources/latest_pact.rb +7 -1
- data/lib/pact_broker/api/resources/relationships.rb +29 -0
- data/lib/pact_broker/app.rb +82 -0
- data/lib/pact_broker/configuration.rb +47 -0
- data/lib/pact_broker/db.rb +7 -39
- data/lib/pact_broker/models/relationship.rb +27 -0
- data/lib/pact_broker/project_root.rb +7 -0
- data/lib/pact_broker/services/pacticipant_service.rb +6 -0
- data/lib/pact_broker/ui/controllers/base_controller.rb +15 -0
- data/lib/pact_broker/ui/controllers/clusters.rb +28 -0
- data/lib/pact_broker/ui/controllers/relationships.rb +21 -0
- data/lib/pact_broker/ui/view_models/clusters.rb +0 -0
- data/lib/pact_broker/ui/view_models/relationship.rb +35 -0
- data/lib/pact_broker/ui/view_models/relationships.rb +21 -0
- data/lib/pact_broker/ui/views/clusters/show.haml +2 -0
- data/lib/pact_broker/ui/views/layouts/main.haml +2 -0
- data/lib/pact_broker/ui/views/relationships/show.haml +44 -0
- data/lib/pact_broker/version.rb +1 -1
- data/lib/rack/hal_browser.rb +1 -0
- data/lib/rack/hal_browser/redirect.rb +45 -0
- data/pact_broker.gemspec +10 -8
- data/public/bootstrap.zip +0 -0
- data/public/config.json +412 -0
- data/public/css/bootstrap-theme.css +346 -0
- data/public/css/bootstrap-theme.min.css +7 -0
- data/public/css/bootstrap.css +5780 -0
- data/public/css/bootstrap.min.css +7 -0
- data/public/fonts/glyphicons-halflings-regular.eot +0 -0
- data/public/fonts/glyphicons-halflings-regular.svg +229 -0
- data/public/fonts/glyphicons-halflings-regular.ttf +0 -0
- data/public/fonts/glyphicons-halflings-regular.woff +0 -0
- data/public/javascripts/jquery-2.1.1.min.js +4 -0
- data/public/javascripts/jquery.tablesorter.js +1765 -0
- data/public/javascripts/jquery.tablesorter.min.js +5 -0
- data/public/js/bootstrap.js +1943 -0
- data/public/js/bootstrap.min.js +7 -0
- data/public/stylesheets/github.css +387 -0
- data/public/stylesheets/relationships.css +18 -0
- data/script/update-hal-browser +6 -0
- data/spec/fixtures/renderer_pact.json +34 -0
- data/spec/lib/pact_broker/api/decorators/relationships_csv_decorator_spec.rb +30 -0
- data/spec/lib/pact_broker/api/renderers/html_pact_renderer_spec.rb +29 -0
- data/spec/lib/pact_broker/api/resources/latest_pact_spec.rb +57 -0
- data/spec/lib/pact_broker/configuration_spec.rb +21 -0
- data/spec/lib/pact_broker/services/pacticipant_service_spec.rb +30 -0
- data/spec/lib/pact_broker/ui/controllers/clusters_spec.rb +27 -0
- data/spec/lib/pact_broker/ui/controllers/relationships_spec.rb +37 -0
- data/spec/lib/pact_broker/ui/view_models/relationship_spec.rb +29 -0
- data/spec/lib/rack/hal_browser/redirect_spec.rb +63 -0
- data/spec/service_consumers/pact_helper.rb +1 -1
- data/spec/spec_helper.rb +10 -3
- data/tasks/database.rb +57 -0
- data/tasks/db.rake +57 -12
- data/tasks/pact.rake +1 -1
- data/tasks/rspec.rake +1 -9
- data/vendor/hal-browser/.gitignore +1 -0
- data/vendor/hal-browser/MIT-LICENSE.txt +20 -0
- data/vendor/hal-browser/README.md +35 -0
- data/vendor/hal-browser/browser.html +246 -0
- data/vendor/hal-browser/js/hal.js +57 -0
- data/vendor/hal-browser/js/hal/browser.js +60 -0
- data/vendor/hal-browser/js/hal/http/client.js +38 -0
- data/vendor/hal-browser/js/hal/resource.js +34 -0
- data/vendor/hal-browser/js/hal/views/browser.js +29 -0
- data/vendor/hal-browser/js/hal/views/documentation.js +7 -0
- data/vendor/hal-browser/js/hal/views/embedded_resource.js +56 -0
- data/vendor/hal-browser/js/hal/views/embedded_resources.js +41 -0
- data/vendor/hal-browser/js/hal/views/explorer.js +23 -0
- data/vendor/hal-browser/js/hal/views/inspector.js +39 -0
- data/vendor/hal-browser/js/hal/views/links.js +54 -0
- data/vendor/hal-browser/js/hal/views/location_bar.js +40 -0
- data/vendor/hal-browser/js/hal/views/navigation.js +19 -0
- data/vendor/hal-browser/js/hal/views/non_safe_request_dialog.js +53 -0
- data/vendor/hal-browser/js/hal/views/properties.js +14 -0
- data/vendor/hal-browser/js/hal/views/query_uri_dialog.js +69 -0
- data/vendor/hal-browser/js/hal/views/request_headers.js +30 -0
- data/vendor/hal-browser/js/hal/views/resource.js +38 -0
- data/vendor/hal-browser/js/hal/views/response.js +24 -0
- data/vendor/hal-browser/js/hal/views/response_body.js +40 -0
- data/vendor/hal-browser/js/hal/views/response_headers.js +19 -0
- data/vendor/hal-browser/styles.css +67 -0
- data/vendor/hal-browser/vendor/css/bootstrap-responsive.css +1109 -0
- data/vendor/hal-browser/vendor/css/bootstrap.css +6158 -0
- data/vendor/hal-browser/vendor/img/ajax-loader.gif +0 -0
- data/vendor/hal-browser/vendor/img/glyphicons-halflings-white.png +0 -0
- data/vendor/hal-browser/vendor/img/glyphicons-halflings.png +0 -0
- data/vendor/hal-browser/vendor/js/backbone.js +1487 -0
- data/vendor/hal-browser/vendor/js/bootstrap.js +2276 -0
- data/vendor/hal-browser/vendor/js/jquery-1.9.1.js +9597 -0
- data/vendor/hal-browser/vendor/js/underscore.js +1227 -0
- data/vendor/hal-browser/vendor/js/uritemplates.js +438 -0
- metadata +162 -47
- data/Gemfile.lock +0 -110
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pact_broker/api/decorators/relationships_csv_decorator'
|
3
|
+
|
4
|
+
module PactBroker
|
5
|
+
|
6
|
+
module Api
|
7
|
+
|
8
|
+
module Decorators
|
9
|
+
|
10
|
+
describe RelationshipsCsvDecorator do
|
11
|
+
|
12
|
+
let(:pact) { ProviderStateBuilder.new.create_pact_with_hierarchy "My Consumer", "1.0", "My Provider"}
|
13
|
+
let(:pacts) { [pact]}
|
14
|
+
let(:line_1) { 'source,target,weight'}
|
15
|
+
let(:line_2) { 'My Consumer,My Provider,1' }
|
16
|
+
let(:expected_text) { [line_1, line_2].join("\n") + "\n" }
|
17
|
+
|
18
|
+
subject { RelationshipsCsvDecorator.new(pacts) }
|
19
|
+
|
20
|
+
|
21
|
+
describe "#to_csv" do
|
22
|
+
it "returns the relationships as CSV" do
|
23
|
+
expect(subject.to_csv).to eq expected_text
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pact_broker/api/renderers/html_pact_renderer'
|
3
|
+
|
4
|
+
module PactBroker
|
5
|
+
module Api
|
6
|
+
module Renderers
|
7
|
+
describe HtmlPactRenderer do
|
8
|
+
|
9
|
+
let(:json_content) { load_fixture('renderer_pact.json') }
|
10
|
+
|
11
|
+
subject { HtmlPactRenderer.call json_content }
|
12
|
+
|
13
|
+
describe ".call" do
|
14
|
+
it "renders the pact as HTML" do
|
15
|
+
expect(subject).to include("<html>")
|
16
|
+
expect(subject).to include("</html>")
|
17
|
+
expect(subject).to include('<link rel="stylesheet"')
|
18
|
+
expect(subject).to include('href="/stylesheets/github.css"')
|
19
|
+
expect(subject).to include('<pre><code')
|
20
|
+
expect(subject).to include('"method":')
|
21
|
+
expect(subject).to match /<h\d>.*Some Consumer/
|
22
|
+
expect(subject).to match /<h\d>.*Some Provider/
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pact_broker/api/resources/latest_pact'
|
3
|
+
require 'rack/test'
|
4
|
+
|
5
|
+
module PactBroker::Api
|
6
|
+
|
7
|
+
module Resources
|
8
|
+
|
9
|
+
describe LatestPact do
|
10
|
+
|
11
|
+
include Rack::Test::Methods
|
12
|
+
|
13
|
+
let(:app) { PactBroker::API }
|
14
|
+
|
15
|
+
describe "GET" do
|
16
|
+
|
17
|
+
context "Accept: text/html" do
|
18
|
+
|
19
|
+
let(:json_content) { 'json_content' }
|
20
|
+
let(:pact) { double("pact", json_content: json_content)}
|
21
|
+
let(:html) { 'html' }
|
22
|
+
let(:pact_id_params) { {:provider_name=>"provider_name", :consumer_name=>"consumer_name", :consumer_version_number=>"latest"} }
|
23
|
+
|
24
|
+
before do
|
25
|
+
allow(PactBroker::Services::PactService).to receive(:find_pact).and_return(pact)
|
26
|
+
allow(PactBroker.configuration.html_pact_renderer).to receive(:call).and_return(html)
|
27
|
+
end
|
28
|
+
|
29
|
+
subject { get "/pact/provider/provider_name/consumer/consumer_name/latest",{}, {'HTTP_ACCEPT' => "text/html"} }
|
30
|
+
|
31
|
+
it "find the pact" do
|
32
|
+
expect(PactBroker::Services::PactService).to receive(:find_pact).with(pact_id_params)
|
33
|
+
subject
|
34
|
+
end
|
35
|
+
|
36
|
+
it "uses the configured HTML renderer" do
|
37
|
+
expect(PactBroker.configuration.html_pact_renderer).to receive(:call).with(json_content)
|
38
|
+
subject
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns a HTML body" do
|
42
|
+
subject
|
43
|
+
expect(last_response.body).to eq html
|
44
|
+
end
|
45
|
+
|
46
|
+
it "returns a content type of HTML" do
|
47
|
+
subject
|
48
|
+
expect(last_response.headers['Content-Type']).to eq 'text/html'
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pact_broker/configuration'
|
3
|
+
require 'pact_broker/api/renderers/html_pact_renderer'
|
4
|
+
|
5
|
+
module PactBroker
|
6
|
+
describe Configuration do
|
7
|
+
|
8
|
+
context "default configuration" do
|
9
|
+
describe ".html_pact_renderer" do
|
10
|
+
|
11
|
+
let(:json_content) { {a: 'b'}.to_json }
|
12
|
+
|
13
|
+
it "calls the inbuilt HtmlPactRenderer" do
|
14
|
+
expect(PactBroker::Api::Renderers::HtmlPactRenderer).to receive(:call).with(json_content)
|
15
|
+
PactBroker.configuration.html_pact_renderer.call json_content
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pact_broker/services/pacticipant_service'
|
3
|
+
|
4
|
+
module PactBroker
|
5
|
+
|
6
|
+
module Services
|
7
|
+
describe PacticipantService do
|
8
|
+
|
9
|
+
subject{ PacticipantService }
|
10
|
+
|
11
|
+
describe ".find_relationships" do
|
12
|
+
|
13
|
+
let(:consumer) { instance_double("PactBroker::Models::Pacticpant")}
|
14
|
+
let(:provider) { instance_double("PactBroker::Models::Pacticpant")}
|
15
|
+
let(:pact) { instance_double("PactBroker::Models::Pact", consumer: consumer, provider: provider)}
|
16
|
+
let(:pacts) { [pact]}
|
17
|
+
|
18
|
+
before do
|
19
|
+
allow_any_instance_of(PactBroker::Repositories::PactRepository).to receive(:find_latest_pacts).and_return(pacts)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "returns a list of relationships" do
|
23
|
+
expect(subject.find_relationships).to eq([PactBroker::Models::Relationship.create(consumer, provider)])
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pact_broker/ui/controllers/clusters'
|
3
|
+
require 'rack/test'
|
4
|
+
|
5
|
+
module PactBroker
|
6
|
+
module UI
|
7
|
+
module Controllers
|
8
|
+
describe Clusters do
|
9
|
+
|
10
|
+
include Rack::Test::Methods
|
11
|
+
|
12
|
+
let(:app) { Clusters }
|
13
|
+
|
14
|
+
describe "/" do
|
15
|
+
describe "GET" do
|
16
|
+
|
17
|
+
xit "does something" do
|
18
|
+
get "/"
|
19
|
+
puts last_response.body
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pact_broker/ui/controllers/relationships'
|
3
|
+
|
4
|
+
require 'rack/test'
|
5
|
+
|
6
|
+
module PactBroker
|
7
|
+
module UI
|
8
|
+
module Controllers
|
9
|
+
describe Relationships do
|
10
|
+
|
11
|
+
include Rack::Test::Methods
|
12
|
+
|
13
|
+
let(:app) { Relationships }
|
14
|
+
|
15
|
+
describe "/" do
|
16
|
+
describe "GET" do
|
17
|
+
|
18
|
+
let(:consumer) { instance_double("PactBroker::Models::Pacticipant", name: 'consumer_name')}
|
19
|
+
let(:provider) { instance_double("PactBroker::Models::Pacticipant", name: 'provider_name')}
|
20
|
+
let(:relationship) { PactBroker::Models::Relationship.new(consumer, provider)}
|
21
|
+
let(:relationships) { [relationship] }
|
22
|
+
|
23
|
+
before do
|
24
|
+
allow(PactBroker::Services::PacticipantService).to receive(:find_relationships).and_return(relationships)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "does something" do
|
28
|
+
get "/"
|
29
|
+
puts last_response.body
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'pact_broker/ui/view_models/relationship'
|
3
|
+
|
4
|
+
module PactBroker
|
5
|
+
module UI
|
6
|
+
module ViewModels
|
7
|
+
describe Relationship do
|
8
|
+
|
9
|
+
let(:consumer) { instance_double("PactBroker::Models::Pacticipant", name: 'consumer_name')}
|
10
|
+
let(:provider) { instance_double("PactBroker::Models::Pacticipant", name: 'provider_name')}
|
11
|
+
let(:relationship) { PactBroker::Models::Relationship.new(consumer, provider)}
|
12
|
+
|
13
|
+
subject { Relationship.new(relationship) }
|
14
|
+
|
15
|
+
its(:consumer_name) { should eq 'consumer_name'}
|
16
|
+
its(:provider_name) { should eq 'provider_name'}
|
17
|
+
its(:latest_pact_url) { should eq "/pact/provider/provider_name/consumer/consumer_name/latest" }
|
18
|
+
|
19
|
+
describe "<=>" do
|
20
|
+
|
21
|
+
xit "sorts by consumer name then provider name" do
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'rack/test'
|
2
|
+
require 'rack/hal_browser/redirect'
|
3
|
+
|
4
|
+
module Rack
|
5
|
+
module HalBrowser
|
6
|
+
describe Redirect do
|
7
|
+
include Rack::Test::Methods
|
8
|
+
|
9
|
+
let(:inner_app) do
|
10
|
+
->(env) { [200, {'Content-Type' => 'text/html'}, ['All good!']] }
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:app) { Redirect.new(inner_app) }
|
14
|
+
|
15
|
+
it "passes non-html requests straight through" do
|
16
|
+
get '/', {}, 'HTTP_ACCEPT' => 'application/hal+json'
|
17
|
+
expect(last_response).to be_ok
|
18
|
+
expect(last_response.headers['Content-Type']).to eq 'text/html'
|
19
|
+
expect(last_response.body).to eq 'All good!'
|
20
|
+
end
|
21
|
+
|
22
|
+
context "when client accepts html and json" do
|
23
|
+
|
24
|
+
it "redirects to the HAL browser" do
|
25
|
+
get '/', {}, 'HTTP_ACCEPT' => 'text/html,application/hal+json'
|
26
|
+
follow_redirect!
|
27
|
+
expect(last_request.url).to eq 'http://example.org/hal-browser/browser.html'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "passes the original request path to the HAL browser via the fragment" do
|
31
|
+
get '/foo', {}, 'HTTP_ACCEPT' => 'text/html,application/hal+json'
|
32
|
+
expect(last_response.headers['Location']).to eq '/hal-browser/browser.html#/foo'
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
context "when a path is excluded" do
|
38
|
+
|
39
|
+
let(:app) { Redirect.new(inner_app, :exclude => '/foo') }
|
40
|
+
|
41
|
+
it "passes requests to the excluded path straight through" do
|
42
|
+
get '/foo', {}, 'HTTP_ACCEPT' => 'text/html'
|
43
|
+
expect(last_response).to be_ok
|
44
|
+
expect(last_response.headers['Content-Type']).to eq 'text/html'
|
45
|
+
expect(last_response.body).to eq 'All good!'
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'when clent uses non GET verb' do
|
51
|
+
|
52
|
+
it "passes requests to the excluded path straight through" do
|
53
|
+
post '/foo', {}, 'HTTP_ACCEPT' => 'text/html'
|
54
|
+
expect(last_response).to be_ok
|
55
|
+
expect(last_response.headers['Content-Type']).to eq 'text/html'
|
56
|
+
expect(last_response.body).to eq 'All good!'
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,19 +1,26 @@
|
|
1
|
-
require 'simplecov' # At the top because simplecov needs to watch files being loaded
|
2
1
|
ENV['RACK_ENV'] = 'test'
|
3
2
|
RACK_ENV = 'test'
|
4
3
|
|
5
4
|
$: << File.expand_path("../../", __FILE__)
|
6
5
|
|
7
6
|
require 'rack/test'
|
8
|
-
require '
|
7
|
+
require 'db'
|
9
8
|
require './spec/support/provider_state_builder'
|
10
9
|
require 'pact_broker/api'
|
11
10
|
require 'rspec/fire'
|
12
11
|
|
12
|
+
def load_fixture(file_name)
|
13
|
+
File.read(File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', file_name)))
|
14
|
+
end
|
15
|
+
|
16
|
+
def load_json_fixture(file_name)
|
17
|
+
require 'json'
|
18
|
+
JSON.parse(load_fixture(file_name))
|
19
|
+
end
|
20
|
+
|
13
21
|
RSpec.configure do | config |
|
14
22
|
config.before :suite do
|
15
23
|
raise "Wrong environment!!! Don't run this script!! ENV['RACK_ENV'] is #{ENV['RACK_ENV']} and RACK_ENV is #{RACK_ENV}" if ENV['RACK_ENV'] != 'test' || RACK_ENV != 'test'
|
16
|
-
# puts caller.take 20
|
17
24
|
end
|
18
25
|
|
19
26
|
config.before :each do
|
data/tasks/database.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require 'pact_broker/project_root'
|
2
|
+
require 'sequel'
|
3
|
+
require 'db'
|
4
|
+
|
5
|
+
Sequel.extension :migration
|
6
|
+
|
7
|
+
module PactBroker
|
8
|
+
module Database
|
9
|
+
|
10
|
+
extend self
|
11
|
+
|
12
|
+
def migrate target = nil
|
13
|
+
opts = target ? {target: target} : {}
|
14
|
+
Sequel::Migrator.run(database, migrations_dir, opts)
|
15
|
+
end
|
16
|
+
|
17
|
+
def version
|
18
|
+
if database.tables.include?(:schema_info)
|
19
|
+
database[:schema_info].first[:version]
|
20
|
+
else
|
21
|
+
0
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def delete_database_file
|
26
|
+
ensure_not_production
|
27
|
+
FileUtils.rm_rf database_file_path
|
28
|
+
end
|
29
|
+
|
30
|
+
def ensure_database_dir_exists
|
31
|
+
ensure_not_production
|
32
|
+
FileUtils.mkdir_p File.dirname(database_file_path)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def ensure_not_production
|
38
|
+
raise "Cannot delete production database using this task" if env == 'production'
|
39
|
+
end
|
40
|
+
|
41
|
+
def database
|
42
|
+
::DB.connection_for_env env
|
43
|
+
end
|
44
|
+
|
45
|
+
def migrations_dir
|
46
|
+
PactBroker.project_root.join('db','migrations')
|
47
|
+
end
|
48
|
+
|
49
|
+
def database_file_path
|
50
|
+
::DB.configuration_for_env(env)['database']
|
51
|
+
end
|
52
|
+
|
53
|
+
def env
|
54
|
+
ENV.fetch('RACK_ENV')
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
data/tasks/db.rake
CHANGED
@@ -1,24 +1,69 @@
|
|
1
1
|
require 'pact_broker/tasks'
|
2
2
|
|
3
3
|
PactBroker::DB::MigrationTask.new do | task |
|
4
|
-
require '
|
4
|
+
require 'db'
|
5
5
|
task.database_connection = DB::PACT_BROKER_DB
|
6
6
|
end
|
7
7
|
|
8
|
+
namespace :bundler do
|
9
|
+
task :setup do
|
10
|
+
require 'rubygems'
|
11
|
+
require 'bundler/setup'
|
12
|
+
end
|
13
|
+
end
|
8
14
|
|
9
15
|
namespace :db do
|
10
|
-
|
11
|
-
|
16
|
+
task :env => ['bundler:setup'] do
|
17
|
+
# Require RACK_ENV to be set for tasks that will be called in production
|
18
|
+
raise "Please specify RACK_ENV" unless ENV['RACK_ENV']
|
19
|
+
RACK_ENV = ENV['RACK_ENV']
|
20
|
+
require File.dirname(__FILE__) + '/database.rb'
|
21
|
+
end
|
22
|
+
|
23
|
+
desc 'Print current schema version'
|
24
|
+
task :version => 'db:env' do
|
25
|
+
puts "Schema Version: #{PactBroker::Database.version}"
|
26
|
+
end
|
12
27
|
|
13
|
-
desc '
|
14
|
-
task :
|
15
|
-
|
16
|
-
puts "Removing database #{db_file}"
|
17
|
-
FileUtils.rm_f db_file
|
18
|
-
FileUtils.mkdir_p File.dirname(db_file)
|
28
|
+
desc 'Migrate the Database'
|
29
|
+
task :migrate => 'db:env' do
|
30
|
+
PactBroker::Database.migrate
|
19
31
|
end
|
20
32
|
|
21
|
-
|
22
|
-
|
33
|
+
desc 'Rollback database to specified version'
|
34
|
+
task :rollback, [:target] => 'db:env' do |t, args|
|
35
|
+
args.with_defaults(target: 0)
|
36
|
+
PactBroker::Database.migrate(args[:target].to_i)
|
37
|
+
end
|
38
|
+
|
39
|
+
desc 'Prepare the test database for running specs - RACK_ENV will be hardcoded to "test"'
|
40
|
+
task 'prepare:test' => ['db:set_test_env','db:prepare_dir','db:migrate']
|
41
|
+
|
42
|
+
desc 'Reset the database (rollback then migrate) - uses RACK_ENV, defaulting to "development"'
|
43
|
+
task :reset => ['db:rollback', 'db:migrate']
|
44
|
+
|
45
|
+
desc 'Delete the dev/test database - uses RACK_ENV, defaulting to "development"'
|
46
|
+
task 'delete' => 'db:env' do
|
47
|
+
PactBroker::Database.delete_database_file
|
23
48
|
end
|
24
|
-
|
49
|
+
|
50
|
+
# Private: Ensure the dev/test database directory exists
|
51
|
+
task 'prepare_dir' => 'db:env' do
|
52
|
+
PactBroker::Database.ensure_database_dir_exists
|
53
|
+
PactBroker::Database.delete_database_file
|
54
|
+
end
|
55
|
+
|
56
|
+
# Private
|
57
|
+
task :set_test_env do
|
58
|
+
ENV['RACK_ENV'] = 'test'
|
59
|
+
end
|
60
|
+
|
61
|
+
# Private
|
62
|
+
task 'env:nonprod' => ['bundler:setup'] do
|
63
|
+
# Allow default RACK_ENV to be set when not in production
|
64
|
+
RACK_ENV = ENV['RACK_ENV'] ||= 'development'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
task 'db:env' => 'db:env:nonprod'
|
69
|
+
task 'db:migrate' => 'db:prepare_dir'
|