Add foreign key constraints

This commit is contained in:
Raphael Sofaer 2011-02-28 15:18:26 -08:00
parent 98999b1703
commit d8956a7cd5
14 changed files with 93 additions and 21 deletions

View file

@ -2,6 +2,7 @@ source 'http://rubygems.org'
gem 'mysql2', '0.2.6'
gem 'rails', '3.0.3'
gem 'foreigner', '0.9.1'
gem 'bundler', '>= 1.0.0'
gem 'chef', '0.9.12', :require => false

View file

@ -180,6 +180,7 @@ GEM
net-ssh (~> 2.0.23)
nokogiri (~> 1.4.3.1)
ruby-hmac
foreigner (0.9.1)
formatador (0.0.16)
fuubar (0.0.3)
rspec (~> 2.0)
@ -404,6 +405,7 @@ DEPENDENCIES
fastercsv (= 1.5.4)
fixture_builder (~> 0.2.0)
fog (= 0.3.25)
foreigner (= 0.9.1)
fuubar
googlecharts
haml (= 3.0.25)

View file

@ -88,8 +88,8 @@ class AspectsController < ApplicationController
current_user.drop_aspect @aspect
flash[:notice] = I18n.t 'aspects.destroy.success',:name => @aspect.name
redirect_to :back
rescue RuntimeError => e
flash[:error] = e.message
rescue ActiveRecord::StatementInvalid => e
flash[:error] = I18n.t 'aspects.destroy.failure',:name => @aspect.name
redirect_to :back
end
end

View file

@ -4,14 +4,14 @@
class Contact < ActiveRecord::Base
default_scope where(:pending => false)
belongs_to :user
validates_presence_of :user
belongs_to :person
validates_presence_of :person
has_many :aspect_memberships, :dependent => :delete_all
has_many :aspect_memberships
has_many :aspects, :through => :aspect_memberships
validate :not_contact_for_self
validates_uniqueness_of :person_id, :scope => :user_id

View file

@ -7,7 +7,7 @@ class Notification < ActiveRecord::Base
include Diaspora::Socketable
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
belongs_to :target, :polymorphic => true

View file

@ -33,6 +33,8 @@ class Person < ActiveRecord::Base
has_many :notification_actors
has_many :notifications, :through => :notification_actors
has_many :mentions, :dependent => :destroy
before_destroy :remove_all_traces
before_validation :clean_url
@ -94,7 +96,7 @@ class Person < ActiveRecord::Base
def owns?(post)
self == post.person
end
def url
begin
uri = URI.parse(@attributes['url'])
@ -188,9 +190,6 @@ class Person < ActiveRecord::Base
private
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}
end
end

View file

@ -14,7 +14,7 @@ class Post < ActiveRecord::Base
xml_attr :public
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 :aspects, :through => :post_visibilities
has_many :mentions, :dependent => :destroy

View file

@ -32,7 +32,7 @@ class User < ActiveRecord::Base
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 :aspects, :dependent => :destroy
has_many :aspects
has_many :aspect_memberships, :through => :aspects
has_many :contacts
has_many :contact_people, :through => :contacts, :source => :person
@ -66,11 +66,7 @@ class User < ActiveRecord::Base
######### Aspects ######################
def drop_aspect(aspect)
if aspect.contacts.count == 0
aspect.destroy
else
raise "Aspect not empty"
end
end
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|
if contact.person.owner_id
contact.person.owner.disconnected_by self.person
remove_contact(contact)
else
self.disconnect contact
end
}
self.aspects.delete_all
end
end

View file

@ -199,6 +199,7 @@ en:
failure: "Aspect creation failed."
destroy:
success: "%{name} was successfully removed."
failure: "%{name} is not empty and could not be removed."
update:
success: "Your aspect, %{name}, has been successfully edited."
failure: "Your aspect, %{name}, had too long name to be saved."

View file

@ -10,7 +10,7 @@
#
# 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|
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", ["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

View file

@ -37,8 +37,8 @@ describe DataConversion::ImportToMysql do
Mongo::Service.delete_all
User.delete_all
Aspect.delete_all
AspectMembership.delete_all
Aspect.delete_all
Comment.delete_all
Invitation.delete_all
Notification.delete_all

View file

@ -10,7 +10,7 @@ describe Contact do
@user = alice
@user2 = bob
end
it 'set to dependant delete_all' do
it 'deletes dependent aspect memberships' do
lambda{
@user.contact_for(@user2.person).destroy
}.should change(AspectMembership, :count).by(-1)

View file

@ -24,12 +24,12 @@ describe Person do
person = Factory.create(:person, :url => "https://example.com")
person.should be_valid
end
it 'should always return the correct receive url' do
person = Factory.create(:person, :url => "https://example.com/a/bit/messed/up")
person.receive_url.should == "https://example.com/receive/users/#{person.guid}/"
end
it 'should allow ports in the url' do
person = Factory.create(:person, :url => "https://example.com:3000/")
person.url.should == "https://example.com:3000/"
@ -131,6 +131,28 @@ describe Person do
lambda {@deleter.destroy}.should change(Post, :count).by(-1)
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
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)

View file

@ -302,7 +302,7 @@ describe User do
it 'should not delete an aspect with contacts' do
aspect = alice.aspects.first
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
end
end
@ -323,6 +323,20 @@ describe User do
alice.destroy
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
alice.should_receive(:remove_person)
alice.destroy
@ -334,6 +348,19 @@ describe User do
}.should change{ alice.aspects(true).count }.by(-1)
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
it 'should remove the person object' do
person = alice.person