Skip to content
This repository has been archived by the owner on Oct 16, 2019. It is now read-only.

Create Environment model for better organization #177

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions app/models/deployment.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
# A record of a deployment processes
class Deployment < ActiveRecord::Base
validates_presence_of :name, :name_with_owner
validates :name, :name_with_owner, :environment, :repository,
:presence => true

belongs_to :environment
belongs_to :repository

def self.latest_for_name_with_owner(name_with_owner)
sets = self.select(:name, :environment)
sets = self.select(:name, :environment_id)
.where(:name_with_owner => name_with_owner)
.group("name,environment")
.group([:name, :environment_id])

sets.map do |deployment|
params = {
:name => deployment.name,
:environment => deployment.environment,
:environment_id => deployment.environment_id,
:name_with_owner => name_with_owner
}
Deployment.where(params).order("created_at desc").limit(1)
Deployment.where(params).order(arel_table[:created_at].desc).limit(1)
end.flatten
end

Expand Down
6 changes: 6 additions & 0 deletions app/models/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# A location for the deployment (i.e. production, staging)
class Environment < ActiveRecord::Base
validates :name, :presence => true, :uniqueness => true

has_many :deployments, :dependent => :delete_all
end
5 changes: 5 additions & 0 deletions db/migrate/20150903184055_add_repositories_indexes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddRepositoriesIndexes < ActiveRecord::Migration
def change
add_index :repositories, [:owner, :name], unique: true
end
end
33 changes: 33 additions & 0 deletions db/migrate/20150903204319_create_environments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class CreateEnvironments < ActiveRecord::Migration
def up
create_table :environments do |t|
t.string :name, required: true, index: true, unique: true
t.timestamps
end

add_column :deployments, :environment_id, :integer

Deployment.connection.schema_cache.clear!
Deployment.reset_column_information
Deployment.find_each do |deployment|
deployment.update_column(:environment_id, Environment.where(name: deployment.read_attribute(:environment)).first_or_create!.id)
end

change_column :deployments, :environment_id, :integer, null: false
remove_column :deployments, :environment
end

def down
add_column :deployments, :environment, :string, default: "production"

Deployment.connection.schema_cache.clear!
Deployment.reset_column_information
Deployment.includes(:environment).find_each do |deployment|
deployment.update_column(:environment, Environment.find(deployment.environment_id).first!.name)
end

remove_column :deployments, :environment_id

drop_table :environments
end
end
9 changes: 9 additions & 0 deletions db/migrate/20150904161019_add_avatar_url_for_deployments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddAvatarUrlForDeployments < ActiveRecord::Migration
def change
add_column :deployments, :sender_login, :string
add_column :deployments, :sender_avatar_url, :string
add_index :deployments, [:repository_id, :environment_id]
add_index :deployments, [:name, :environment_id, :name_with_owner], name: "index_deployments_on_latest_for_name_with_owner"
add_index :deployments, :created_at
end
end
20 changes: 18 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140728040201) do
ActiveRecord::Schema.define(version: 20150904161019) do

create_table "deployments", force: :cascade do |t|
t.text "custom_payload"
t.string "environment", default: "production"
t.string "guid"
t.string "name"
t.string "name_with_owner"
Expand All @@ -25,8 +24,23 @@
t.datetime "created_at"
t.datetime "updated_at"
t.integer "repository_id"
t.integer "environment_id", null: false
t.string "sender_login"
t.string "sender_avatar_url"
end

add_index "deployments", ["created_at"], name: "index_deployments_on_created_at"
add_index "deployments", ["name", "environment_id", "name_with_owner"], name: "index_deployments_on_latest_for_name_with_owner"
add_index "deployments", ["repository_id", "environment_id"], name: "index_deployments_on_repository_id_and_environment_id"

create_table "environments", force: :cascade do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "environments", ["name"], name: "index_environments_on_name"

create_table "repositories", force: :cascade do |t|
t.string "owner"
t.string "name"
Expand All @@ -35,4 +49,6 @@
t.datetime "updated_at"
end

add_index "repositories", ["owner", "name"], name: "index_repositories_on_owner_and_name", unique: true

end
22 changes: 14 additions & 8 deletions lib/heaven/provider/default_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def name_without_owner
data["repository"]["name"]
end

def sender
data["sender"]
end

def sha
deployment_data["sha"][0..7]
end
Expand Down Expand Up @@ -137,14 +141,16 @@ def notify
end

def record
Deployment.create(:custom_payload => JSON.dump(custom_payload),
:environment => environment,
:guid => guid,
:name => name,
:name_with_owner => name_with_owner,
:output => output.url,
:ref => ref,
:sha => sha)
Deployment.create(:custom_payload => JSON.dump(custom_payload),
:environment => environment,
:guid => guid,
:name => name,
:name_with_owner => name_with_owner,
:output => output.url,
:ref => ref,
:sha => sha,
:sender_login => sender["login"],
:sender_avatar_url => sender["avatar_url"])
end

def update_output
Expand Down
9 changes: 7 additions & 2 deletions spec/models/deployment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
let(:payload) { fixture_data("deployment") }
let!(:data) { JSON.parse(payload)["payload"] }

let(:repository) { Repository.create(:owner => "atmos", :name => "heaven") }
let(:production) { Environment.create(:name => "production") }
let(:staging) { Environment.create(:name => "staging") }

let!(:create_data) do
{
:custom_payload => JSON.dump(data),
:environment => "production",
:repository => repository,
:environment => production,
:guid => SecureRandom.uuid,
:name => "hubot",
:name_with_owner => "github/hubot",
Expand All @@ -34,7 +39,7 @@

Deployment.create create_data.merge(:name_with_owner => "atmos/heaven")

present << Deployment.create(create_data.merge(:environment => "staging"))
present << Deployment.create(create_data.merge(:environment => staging))

deployments = Deployment.latest_for_name_with_owner("github/hubot")

Expand Down
8 changes: 8 additions & 0 deletions spec/models/environment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require "spec_helper"

describe Environment do
it "doesn't recreate environments with the same name" do
expect(Environment.create(:name => "production")).to be_valid
expect { Environment.create!(:name => "production") }.to raise_exception
end
end