Add foreign key constraints
This commit is contained in:
parent
98999b1703
commit
d8956a7cd5
14 changed files with 93 additions and 21 deletions
1
Gemfile
1
Gemfile
|
|
@ -2,6 +2,7 @@ source 'http://rubygems.org'
|
||||||
|
|
||||||
gem 'mysql2', '0.2.6'
|
gem 'mysql2', '0.2.6'
|
||||||
gem 'rails', '3.0.3'
|
gem 'rails', '3.0.3'
|
||||||
|
gem 'foreigner', '0.9.1'
|
||||||
|
|
||||||
gem 'bundler', '>= 1.0.0'
|
gem 'bundler', '>= 1.0.0'
|
||||||
gem 'chef', '0.9.12', :require => false
|
gem 'chef', '0.9.12', :require => false
|
||||||
|
|
|
||||||
|
|
@ -180,6 +180,7 @@ GEM
|
||||||
net-ssh (~> 2.0.23)
|
net-ssh (~> 2.0.23)
|
||||||
nokogiri (~> 1.4.3.1)
|
nokogiri (~> 1.4.3.1)
|
||||||
ruby-hmac
|
ruby-hmac
|
||||||
|
foreigner (0.9.1)
|
||||||
formatador (0.0.16)
|
formatador (0.0.16)
|
||||||
fuubar (0.0.3)
|
fuubar (0.0.3)
|
||||||
rspec (~> 2.0)
|
rspec (~> 2.0)
|
||||||
|
|
@ -404,6 +405,7 @@ DEPENDENCIES
|
||||||
fastercsv (= 1.5.4)
|
fastercsv (= 1.5.4)
|
||||||
fixture_builder (~> 0.2.0)
|
fixture_builder (~> 0.2.0)
|
||||||
fog (= 0.3.25)
|
fog (= 0.3.25)
|
||||||
|
foreigner (= 0.9.1)
|
||||||
fuubar
|
fuubar
|
||||||
googlecharts
|
googlecharts
|
||||||
haml (= 3.0.25)
|
haml (= 3.0.25)
|
||||||
|
|
|
||||||
|
|
@ -88,8 +88,8 @@ class AspectsController < ApplicationController
|
||||||
current_user.drop_aspect @aspect
|
current_user.drop_aspect @aspect
|
||||||
flash[:notice] = I18n.t 'aspects.destroy.success',:name => @aspect.name
|
flash[:notice] = I18n.t 'aspects.destroy.success',:name => @aspect.name
|
||||||
redirect_to :back
|
redirect_to :back
|
||||||
rescue RuntimeError => e
|
rescue ActiveRecord::StatementInvalid => e
|
||||||
flash[:error] = e.message
|
flash[:error] = I18n.t 'aspects.destroy.failure',:name => @aspect.name
|
||||||
redirect_to :back
|
redirect_to :back
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ class Contact < ActiveRecord::Base
|
||||||
belongs_to :person
|
belongs_to :person
|
||||||
validates_presence_of :person
|
validates_presence_of :person
|
||||||
|
|
||||||
has_many :aspect_memberships, :dependent => :delete_all
|
has_many :aspect_memberships
|
||||||
has_many :aspects, :through => :aspect_memberships
|
has_many :aspects, :through => :aspect_memberships
|
||||||
validate :not_contact_for_self
|
validate :not_contact_for_self
|
||||||
validates_uniqueness_of :person_id, :scope => :user_id
|
validates_uniqueness_of :person_id, :scope => :user_id
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@ class Notification < ActiveRecord::Base
|
||||||
include Diaspora::Socketable
|
include Diaspora::Socketable
|
||||||
|
|
||||||
belongs_to :recipient, :class_name => 'User'
|
belongs_to :recipient, :class_name => 'User'
|
||||||
has_many :notification_actors, :dependent => :destroy
|
has_many :notification_actors
|
||||||
has_many :actors, :class_name => 'Person', :through => :notification_actors, :source => :person
|
has_many :actors, :class_name => 'Person', :through => :notification_actors, :source => :person
|
||||||
belongs_to :target, :polymorphic => true
|
belongs_to :target, :polymorphic => true
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,8 @@ class Person < ActiveRecord::Base
|
||||||
has_many :notification_actors
|
has_many :notification_actors
|
||||||
has_many :notifications, :through => :notification_actors
|
has_many :notifications, :through => :notification_actors
|
||||||
|
|
||||||
|
has_many :mentions, :dependent => :destroy
|
||||||
|
|
||||||
before_destroy :remove_all_traces
|
before_destroy :remove_all_traces
|
||||||
before_validation :clean_url
|
before_validation :clean_url
|
||||||
|
|
||||||
|
|
@ -188,9 +190,6 @@ class Person < ActiveRecord::Base
|
||||||
|
|
||||||
private
|
private
|
||||||
def remove_all_traces
|
def remove_all_traces
|
||||||
Post.where(:person_id => id).delete_all
|
|
||||||
Comment.where(:person_id => id).delete_all
|
|
||||||
Contact.where(:person_id => id).delete_all
|
|
||||||
Notification.joins(:notification_actors).where(:notification_actors => {:person_id => self.id}).all.each{ |n| n.destroy}
|
Notification.joins(:notification_actors).where(:notification_actors => {:person_id => self.id}).all.each{ |n| n.destroy}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ class Post < ActiveRecord::Base
|
||||||
xml_attr :public
|
xml_attr :public
|
||||||
xml_attr :created_at
|
xml_attr :created_at
|
||||||
|
|
||||||
has_many :comments, :order => 'created_at ASC', :dependent => :destroy
|
has_many :comments, :order => 'created_at ASC'
|
||||||
has_many :post_visibilities
|
has_many :post_visibilities
|
||||||
has_many :aspects, :through => :post_visibilities
|
has_many :aspects, :through => :post_visibilities
|
||||||
has_many :mentions, :dependent => :destroy
|
has_many :mentions, :dependent => :destroy
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
has_many :invitations_from_me, :class_name => 'Invitation', :foreign_key => :sender_id
|
has_many :invitations_from_me, :class_name => 'Invitation', :foreign_key => :sender_id
|
||||||
has_many :invitations_to_me, :class_name => 'Invitation', :foreign_key => :recipient_id
|
has_many :invitations_to_me, :class_name => 'Invitation', :foreign_key => :recipient_id
|
||||||
has_many :aspects, :dependent => :destroy
|
has_many :aspects
|
||||||
has_many :aspect_memberships, :through => :aspects
|
has_many :aspect_memberships, :through => :aspects
|
||||||
has_many :contacts
|
has_many :contacts
|
||||||
has_many :contact_people, :through => :contacts, :source => :person
|
has_many :contact_people, :through => :contacts, :source => :person
|
||||||
|
|
@ -66,11 +66,7 @@ class User < ActiveRecord::Base
|
||||||
|
|
||||||
######### Aspects ######################
|
######### Aspects ######################
|
||||||
def drop_aspect(aspect)
|
def drop_aspect(aspect)
|
||||||
if aspect.contacts.count == 0
|
|
||||||
aspect.destroy
|
aspect.destroy
|
||||||
else
|
|
||||||
raise "Aspect not empty"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def move_contact(person, to_aspect, from_aspect)
|
def move_contact(person, to_aspect, from_aspect)
|
||||||
|
|
@ -287,9 +283,11 @@ class User < ActiveRecord::Base
|
||||||
Contact.unscoped.where(:user_id => self.id).each { |contact|
|
Contact.unscoped.where(:user_id => self.id).each { |contact|
|
||||||
if contact.person.owner_id
|
if contact.person.owner_id
|
||||||
contact.person.owner.disconnected_by self.person
|
contact.person.owner.disconnected_by self.person
|
||||||
|
remove_contact(contact)
|
||||||
else
|
else
|
||||||
self.disconnect contact
|
self.disconnect contact
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
|
self.aspects.delete_all
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -199,6 +199,7 @@ en:
|
||||||
failure: "Aspect creation failed."
|
failure: "Aspect creation failed."
|
||||||
destroy:
|
destroy:
|
||||||
success: "%{name} was successfully removed."
|
success: "%{name} was successfully removed."
|
||||||
|
failure: "%{name} is not empty and could not be removed."
|
||||||
update:
|
update:
|
||||||
success: "Your aspect, %{name}, has been successfully edited."
|
success: "Your aspect, %{name}, has been successfully edited."
|
||||||
failure: "Your aspect, %{name}, had too long name to be saved."
|
failure: "Your aspect, %{name}, had too long name to be saved."
|
||||||
|
|
|
||||||
24
db/schema.rb
24
db/schema.rb
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended to check this file into your version control system.
|
# It's strongly recommended to check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(:version => 20110217044519) do
|
ActiveRecord::Schema.define(:version => 20110228201109) do
|
||||||
|
|
||||||
create_table "aspect_memberships", :force => true do |t|
|
create_table "aspect_memberships", :force => true do |t|
|
||||||
t.integer "aspect_id", :null => false
|
t.integer "aspect_id", :null => false
|
||||||
|
|
@ -476,4 +476,26 @@ ActiveRecord::Schema.define(:version => 20110217044519) do
|
||||||
add_index "users", ["mongo_id"], :name => "index_users_on_mongo_id"
|
add_index "users", ["mongo_id"], :name => "index_users_on_mongo_id"
|
||||||
add_index "users", ["username"], :name => "index_users_on_username", :unique => true
|
add_index "users", ["username"], :name => "index_users_on_username", :unique => true
|
||||||
|
|
||||||
|
add_foreign_key "aspect_memberships", "aspects", :name => "aspect_memberships_aspect_id_fk"
|
||||||
|
add_foreign_key "aspect_memberships", "contacts", :name => "aspect_memberships_contact_id_fk", :dependent => :delete
|
||||||
|
|
||||||
|
add_foreign_key "comments", "people", :name => "comments_person_id_fk", :dependent => :delete
|
||||||
|
add_foreign_key "comments", "posts", :name => "comments_post_id_fk", :dependent => :delete
|
||||||
|
|
||||||
|
add_foreign_key "contacts", "people", :name => "contacts_person_id_fk", :dependent => :delete
|
||||||
|
|
||||||
|
add_foreign_key "invitations", "users", :name => "invitations_recipient_id_fk", :column => "recipient_id", :dependent => :delete
|
||||||
|
add_foreign_key "invitations", "users", :name => "invitations_sender_id_fk", :column => "sender_id", :dependent => :delete
|
||||||
|
|
||||||
|
add_foreign_key "notification_actors", "notifications", :name => "notification_actors_notification_id_fk", :dependent => :delete
|
||||||
|
|
||||||
|
add_foreign_key "posts", "people", :name => "posts_person_id_fk", :dependent => :delete
|
||||||
|
|
||||||
|
add_foreign_key "profiles", "people", :name => "profiles_person_id_fk", :dependent => :delete
|
||||||
|
|
||||||
|
add_foreign_key "requests", "people", :name => "requests_recipient_id_fk", :column => "recipient_id", :dependent => :delete
|
||||||
|
add_foreign_key "requests", "people", :name => "requests_sender_id_fk", :column => "sender_id", :dependent => :delete
|
||||||
|
|
||||||
|
add_foreign_key "services", "users", :name => "services_user_id_fk", :dependent => :delete
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -37,8 +37,8 @@ describe DataConversion::ImportToMysql do
|
||||||
Mongo::Service.delete_all
|
Mongo::Service.delete_all
|
||||||
|
|
||||||
User.delete_all
|
User.delete_all
|
||||||
Aspect.delete_all
|
|
||||||
AspectMembership.delete_all
|
AspectMembership.delete_all
|
||||||
|
Aspect.delete_all
|
||||||
Comment.delete_all
|
Comment.delete_all
|
||||||
Invitation.delete_all
|
Invitation.delete_all
|
||||||
Notification.delete_all
|
Notification.delete_all
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ describe Contact do
|
||||||
@user = alice
|
@user = alice
|
||||||
@user2 = bob
|
@user2 = bob
|
||||||
end
|
end
|
||||||
it 'set to dependant delete_all' do
|
it 'deletes dependent aspect memberships' do
|
||||||
lambda{
|
lambda{
|
||||||
@user.contact_for(@user2.person).destroy
|
@user.contact_for(@user2.person).destroy
|
||||||
}.should change(AspectMembership, :count).by(-1)
|
}.should change(AspectMembership, :count).by(-1)
|
||||||
|
|
|
||||||
|
|
@ -131,6 +131,28 @@ describe Person do
|
||||||
lambda {@deleter.destroy}.should change(Post, :count).by(-1)
|
lambda {@deleter.destroy}.should change(Post, :count).by(-1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "deletes a person's profile" do
|
||||||
|
lambda {
|
||||||
|
@deleter.destroy
|
||||||
|
}.should change(Profile, :count).by(-1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'deletes all requests to a person' do
|
||||||
|
alice.send_contact_request_to(eve.person, alice.aspects.first)
|
||||||
|
Request.count.should == 1
|
||||||
|
lambda {
|
||||||
|
eve.person.destroy
|
||||||
|
}.should change(Request, :count).by(-1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'deletes all requests from a person' do
|
||||||
|
Request.create(:sender_id => @deleter.id, :recipient_id => alice.person.id)
|
||||||
|
Request.count.should == 1
|
||||||
|
lambda {
|
||||||
|
@deleter.destroy
|
||||||
|
}.should change(Request, :count).by(-1)
|
||||||
|
end
|
||||||
|
|
||||||
it "deletes a person's comments on person deletion" do
|
it "deletes a person's comments on person deletion" do
|
||||||
Factory.create(:comment, :person_id => @deleter.id, :diaspora_handle => @deleter.diaspora_handle, :text => "i love you", :post => @other_status)
|
Factory.create(:comment, :person_id => @deleter.id, :diaspora_handle => @deleter.diaspora_handle, :text => "i love you", :post => @other_status)
|
||||||
Factory.create(:comment, :person_id => @person.id,:diaspora_handle => @person.diaspora_handle, :text => "you are creepy", :post => @other_status)
|
Factory.create(:comment, :person_id => @person.id,:diaspora_handle => @person.diaspora_handle, :text => "you are creepy", :post => @other_status)
|
||||||
|
|
|
||||||
|
|
@ -302,7 +302,7 @@ describe User do
|
||||||
it 'should not delete an aspect with contacts' do
|
it 'should not delete an aspect with contacts' do
|
||||||
aspect = alice.aspects.first
|
aspect = alice.aspects.first
|
||||||
aspect.contacts.count.should > 0
|
aspect.contacts.count.should > 0
|
||||||
proc { alice.drop_aspect(aspect) }.should raise_error /Aspect not empty/
|
proc { alice.drop_aspect(aspect) }.should raise_error ActiveRecord::StatementInvalid
|
||||||
alice.aspects.include?(aspect).should == true
|
alice.aspects.include?(aspect).should == true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -323,6 +323,20 @@ describe User do
|
||||||
alice.destroy
|
alice.destroy
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'removes invitations from the user' do
|
||||||
|
alice.invite_user alice.aspects.first.id, 'email', 'blah@blah.blah'
|
||||||
|
lambda {
|
||||||
|
alice.destroy
|
||||||
|
}.should change {alice.invitations_from_me(true).count }.by(-1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'removes invitations to the user' do
|
||||||
|
Invitation.create(:sender_id => eve.id, :recipient_id => alice.id, :aspect_id => eve.aspects.first.id)
|
||||||
|
lambda {
|
||||||
|
alice.destroy
|
||||||
|
}.should change {alice.invitations_to_me(true).count }.by(-1)
|
||||||
|
end
|
||||||
|
|
||||||
it 'should remove person' do
|
it 'should remove person' do
|
||||||
alice.should_receive(:remove_person)
|
alice.should_receive(:remove_person)
|
||||||
alice.destroy
|
alice.destroy
|
||||||
|
|
@ -334,6 +348,19 @@ describe User do
|
||||||
}.should change{ alice.aspects(true).count }.by(-1)
|
}.should change{ alice.aspects(true).count }.by(-1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'removes all contacts' do
|
||||||
|
lambda {
|
||||||
|
alice.destroy
|
||||||
|
}.should change { alice.contacts(true).count }.by(-1)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'removes all service connections' do
|
||||||
|
Services::Facebook.create(:access_token => 'what', :user_id => alice.id)
|
||||||
|
lambda {
|
||||||
|
alice.destroy
|
||||||
|
}.should change { alice.services(true).count }.by(-1)
|
||||||
|
end
|
||||||
|
|
||||||
describe '#remove_person' do
|
describe '#remove_person' do
|
||||||
it 'should remove the person object' do
|
it 'should remove the person object' do
|
||||||
person = alice.person
|
person = alice.person
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue