-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #749 from degica/deployment-tracking-models
Add deployment tracking models
- Loading branch information
Showing
9 changed files
with
242 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class ServiceDeployment < ApplicationRecord | ||
belongs_to :service | ||
|
||
validates :service, presence: true | ||
validate :finished_cannot_both_be_true | ||
|
||
def finished_cannot_both_be_true | ||
if completed_at.present? && failed_at.present? | ||
errors.add(:completed_at, "can't be true with failed_at") | ||
end | ||
end | ||
|
||
scope :unfinished, -> { where('completed_at is null and failed_at is null') } | ||
|
||
def finished? | ||
completed? || failed? | ||
end | ||
|
||
def completed? | ||
!completed_at.nil? | ||
end | ||
|
||
def failed? | ||
!failed_at.nil? | ||
end | ||
|
||
def complete! | ||
self.completed_at = Time.now | ||
self.save! | ||
end | ||
|
||
def fail! | ||
self.failed_at = Time.now | ||
self.save! | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CreateServiceDeployments < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :service_deployments do |t| | ||
t.references :service, foreign_key: true, null: false | ||
t.datetime :completed_at | ||
t.datetime :failed_at | ||
|
||
t.timestamps | ||
end | ||
|
||
add_index :service_deployments, [:completed_at, :failed_at] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
FactoryBot.define do | ||
factory :service_deployment do | ||
service | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
require 'rails_helper' | ||
|
||
describe ServiceDeployment do | ||
|
||
it { should validate_presence_of :service } | ||
|
||
describe 'both completed and failed true' do | ||
it 'will not be valid' do | ||
sd = build :service_deployment, completed_at: DateTime.now, failed_at: DateTime.now | ||
expect(sd).to_not be_valid | ||
end | ||
end | ||
|
||
describe '#completed?' do | ||
it 'returns true if completed' do | ||
sd = create :service_deployment, completed_at: DateTime.now | ||
expect(sd).to be_completed | ||
end | ||
|
||
it 'returns false if not completed' do | ||
sd = create :service_deployment, completed_at: nil | ||
expect(sd).to_not be_completed | ||
end | ||
end | ||
|
||
describe '#failed?' do | ||
it 'returns true if failed' do | ||
sd = create :service_deployment, failed_at: DateTime.now | ||
expect(sd).to be_failed | ||
end | ||
|
||
it 'returns false if not failed' do | ||
sd = create :service_deployment, failed_at: nil | ||
expect(sd).to_not be_failed | ||
end | ||
end | ||
|
||
describe '#finished?' do | ||
it 'returns true if completed' do | ||
sd = create :service_deployment, completed_at: DateTime.now | ||
expect(sd).to be_finished | ||
end | ||
|
||
it 'returns true if failed' do | ||
sd = create :service_deployment, failed_at: DateTime.now | ||
expect(sd).to be_finished | ||
end | ||
|
||
it 'returns false if not failed or completed' do | ||
sd = create :service_deployment, failed_at: nil, completed_at: nil | ||
expect(sd).to_not be_finished | ||
end | ||
end | ||
|
||
let(:example_time) { Time.zone.local(2004, 11, 24, 01, 04, 44) } | ||
describe 'fail!' do | ||
it 'sets failed' do | ||
sd = create :service_deployment | ||
expect(sd).to_not be_failed | ||
sd.fail! | ||
expect(sd).to be_failed | ||
end | ||
|
||
it 'sets failed_at to current time' do | ||
travel_to example_time do | ||
sd = create :service_deployment | ||
expect(sd.failed_at).to be_nil | ||
sd.fail! | ||
expect(sd.failed_at).to eq example_time | ||
end | ||
end | ||
end | ||
|
||
describe 'complete!' do | ||
it 'sets completed' do | ||
sd = create :service_deployment | ||
expect(sd).to_not be_completed | ||
sd.complete! | ||
expect(sd).to be_completed | ||
end | ||
|
||
it 'sets completed_at to current time' do | ||
travel_to example_time do | ||
sd = create :service_deployment | ||
expect(sd.completed_at).to be_nil | ||
sd.complete! | ||
expect(sd.completed_at).to eq example_time | ||
end | ||
end | ||
end | ||
|
||
describe '.unfinished' do | ||
it 'returns unfinished deployments' do | ||
sd1 = create :service_deployment, completed_at: nil | ||
sd2 = create :service_deployment, completed_at: DateTime.now | ||
|
||
expect(ServiceDeployment.unfinished).to eq [sd1] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters