Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Enable to delete logically.

Using `default_scope` to keep compatibility may be halmful...
  • Loading branch information...
commit baa23b185bfaf8e2558a21d6b23a5b16768af947 1 parent 3914a79
Kentaro Kuribayashi authored
2  app/models/configuration_parameter.rb
View
@@ -1,4 +1,6 @@
class ConfigurationParameter < ActiveRecord::Base
+ include LogicallyDeletable
+
attr_accessible :name, :value, :prompt_on_deploy
validates :name,
1  app/models/deployment.rb
View
@@ -1,4 +1,5 @@
class Deployment < ActiveRecord::Base
+ include LogicallyDeletable
DEPLOY_TASKS = ['deploy', 'deploy:default', 'deploy:migrations']
SETUP_TASKS = ['deploy:setup']
2  app/models/host.rb
View
@@ -1,4 +1,6 @@
class Host < ActiveRecord::Base
+ include LogicallyDeletable
+
has_many :roles, :dependent => :destroy, :uniq => true
has_many :stages, :through => :roles, :uniq => true # XXX uniq does not seem to work! You get all stages, even doubles
has_many :activities, :as => :target, :dependent => :destroy
20 app/models/logically_deletable.rb
View
@@ -0,0 +1,20 @@
+module LogicallyDeletable
+ class << self
+ def included(klass)
+ klass.class_eval do
+ default_scope where("#{table_name}.deleted_at IS NULL")
+ end
+ end
+ end
+
+ class NotLogicallyDeletable < RuntimeError; end
+
+ def deleted?
+ !!deleted_at
+ end
+
+ def delete_logically
+ raise NotLogicallyDeletable.new("`deleted_at' column is required to be logically deleted") unless respond_to?(:deleted_at)
+ update_column(:deleted_at, Time.now)
+ end
+end
2  app/models/project.rb
View
@@ -1,4 +1,6 @@
class Project < ActiveRecord::Base
+ include LogicallyDeletable
+
has_many :stages, :dependent => :destroy, :order => 'name ASC'
has_many :deployments, :through => :stages
has_many :configuration_parameters, :dependent => :destroy, :class_name => "ProjectConfiguration", :order => 'name ASC'
2  app/models/project_configuration.rb
View
@@ -1,4 +1,6 @@
class ProjectConfiguration < ConfigurationParameter
+ include LogicallyDeletable
+
belongs_to :project
validates :project,
2  app/models/recipe.rb
View
@@ -1,4 +1,6 @@
class Recipe < ActiveRecord::Base
+ include LogicallyDeletable
+
has_and_belongs_to_many :stages
validates :name,
2  app/models/role.rb
View
@@ -1,4 +1,6 @@
class Role < ActiveRecord::Base
+ include LogicallyDeletable
+
belongs_to :stage
belongs_to :host
has_and_belongs_to_many :deployments
2  app/models/stage.rb
View
@@ -1,4 +1,6 @@
class Stage < ActiveRecord::Base
+ include LogicallyDeletable
+
belongs_to :project
has_and_belongs_to_many :recipes
has_many :roles, :dependent => :destroy, :order => "name ASC"
2  app/models/stage_configuration.rb
View
@@ -1,4 +1,6 @@
class StageConfiguration < ConfigurationParameter
+ include LogicallyDeletable
+
belongs_to :stage
validates :stage,
2  app/models/user.rb
View
@@ -1,4 +1,6 @@
class User < ActiveRecord::Base
+ include LogicallyDeletable
+
# Include default devise modules. Others available are:
# :token_authenticatable, :registerable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable,
26 db/migrate/20120803083647_add_logical_deletion_columns.rb
View
@@ -0,0 +1,26 @@
+class AddLogicalDeletionColumns < ActiveRecord::Migration
+ DELETABLES = %w(
+ configuration_parameters
+ deployments
+ hosts
+ project_configurations
+ projects
+ recipes
+ roles
+ stage_configurations
+ stages
+ users
+ )
+
+ def self.up
+ DELETABLES.each do |t|
+ add_column t.to_sym, :deleted_at, :datetime
+ end
+ end
+
+ def self.down
+ DELETABLES.each do |t|
+ remove_column t.to_sym, :deleted_at
+ end
+ end
+end
12 db/schema.rb
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120614061529) do
+ActiveRecord::Schema.define(:version => 20120803083647) do
create_table "activities", :force => true do |t|
t.integer "user_id"
@@ -32,6 +32,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.integer "prompt_on_deploy", :default => 0
+ t.datetime "deleted_at"
end
create_table "deployments", :force => true do |t|
@@ -47,6 +48,7 @@
t.string "revision"
t.integer "pid"
t.string "status", :default => "running"
+ t.datetime "deleted_at"
end
create_table "deployments_roles", :id => false, :force => true do |t|
@@ -58,9 +60,11 @@
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
+ t.datetime "deleted_at"
end
create_table "project_configurations", :force => true do |t|
+ t.datetime "deleted_at"
end
create_table "projects", :force => true do |t|
@@ -69,6 +73,7 @@
t.string "template"
t.datetime "created_at"
t.datetime "updated_at"
+ t.datetime "deleted_at"
end
create_table "recipe_versions", :force => true do |t|
@@ -88,6 +93,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.integer "version", :default => 1
+ t.datetime "deleted_at"
end
create_table "recipes_stages", :id => false, :force => true do |t|
@@ -105,9 +111,11 @@
t.integer "no_release", :default => 0
t.integer "ssh_port"
t.integer "no_symlink", :default => 0
+ t.datetime "deleted_at"
end
create_table "stage_configurations", :force => true do |t|
+ t.datetime "deleted_at"
end
create_table "stages", :force => true do |t|
@@ -118,6 +126,7 @@
t.text "alert_emails"
t.integer "locked_by_deployment_id"
t.integer "locked", :default => 0
+ t.datetime "deleted_at"
end
create_table "users", :force => true do |t|
@@ -137,6 +146,7 @@
t.string "last_sign_in_ip"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
+ t.datetime "deleted_at"
end
add_index "users", ["disabled_at"], :name => "index_users_on_disabled_at"
12 test/test_helper.rb
View
@@ -2,6 +2,10 @@
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
+require 'database_cleaner'
+DatabaseCleaner.strategy = :truncation
+DatabaseCleaner.clean_with(:truncation)
+
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
@@ -13,6 +17,14 @@ class ActiveSupport::TestCase
# Add more helper methods to be used by all tests here...
#
+ def setup
+ DatabaseCleaner.start
+ end
+
+ def setup
+ DatabaseCleaner.clean
+ end
+
def prepare_email
ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
38 test/unit/host_test.rb
View
@@ -1,88 +1,84 @@
require 'test_helper'
class HostTest < ActiveSupport::TestCase
-
def setup
Host.delete_all
end
test "creation" do
assert_equal 0, Host.count
-
+
assert_nothing_raised{
h = Host.create!(:name => "test.example.com")
}
-
+
assert_equal 1, Host.count
end
-
+
test "validation" do
h = Host.create!(:name => "192.168.0.1")
-
+
# try to create another host with the same name
h = Host.new(:name => "192.168.0.1")
assert !h.valid?
assert_not_empty h.errors["name"]
-
+
# try to create a host with a name that is too long
name = "com." * 251
name = name.chop
h = Host.new(:name => name)
assert !h.valid?
assert_not_empty h.errors["name"]
-
+
# make it pass
name = "example.com"
h = Host.new(:name => name)
assert h.valid?
end
-
+
test "validation_of_name_if_ip" do
# some valid IPs
assert valid_host_name('192.168.0.1 ')
assert valid_host_name(' 192.168.0.110')
end
-
+
test "validation_of_name_if_domain_name" do
assert valid_host_name('map.example.com')
assert valid_host_name('web12.example.com')
assert valid_host_name('localhost')
-
+
# some invalid domains
assert invalid_host_name('mail:example.com')
assert invalid_host_name('mail*.#.example.com')
end
-
+
test "stages" do
host = FactoryGirl.create(:host)
-
+
stage_1 = FactoryGirl.create(:stage)
role_1 = FactoryGirl.create(:role, :name => 'web', :stage => stage_1, :host => host)
role_2 = FactoryGirl.create(:role, :name => 'app', :stage => stage_1, :host => host)
-
+
stage_2 = FactoryGirl.create(:stage)
role_3 = FactoryGirl.create(:role, :name => 'web', :stage => stage_2, :host => host)
- role_4 = FactoryGirl.create(:role, :name => 'app', :stage => stage_2, :host => host)
-
+ role_4 = FactoryGirl.create(:role, :name => 'app', :stage => stage_2, :host => host)
+
assert_equal 4, host.roles.count
assert_equal 2, host.stages.uniq.size # XXX pure count does not work!!!
assert_equal [stage_1.id, stage_2.id].sort, host.stages.collect(&:id).sort
-
+
assert_equal [host], stage_1.hosts
assert_equal [host], stage_2.hosts
end
-
-
+
# helper functions
-
def valid_host_name(name)
h = Host.new
h.name = name
h.valid?
end
-
+
def invalid_host_name(name)
!valid_host_name(name)
end
-
end
25 test/unit/logically_deletable_test.rb
View
@@ -0,0 +1,25 @@
+require 'test_helper'
+
+class HostTest < ActiveSupport::TestCase
+ test "logical deletion" do
+ h = Host.create!(:name => "test.example.com")
+ assert_not_nil h.deleted?
+
+ h.delete_logically
+ assert h.deleted?
+
+ deleted = Host.unscoped.find(h.id)
+ assert deleted
+ assert deleted.deleted?
+ end
+
+ test "default scope" do
+ hosts = []
+
+ 3.times { |i| hosts.push Host.create(name: "test#{i}.example.com") }
+ assert_equal Host.all.size, 3
+
+ hosts.first.delete_logically
+ assert_equal Host.all.size, 2
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.