fixed migration, added deletion of unused post_visibilities
This commit is contained in:
parent
97d8b34599
commit
3615c448d2
2 changed files with 32 additions and 12 deletions
|
|
@ -8,16 +8,9 @@ class PostVisibilitiesOnContacts < ActiveRecord::Migration
|
|||
WHERE people.owner_id = aspects.user_id
|
||||
SQL
|
||||
|
||||
ids = execute("SELECT pv.id #{where_clause}").to_a
|
||||
execute("INSERT into aspect_visibilities SELECT pv.id, pv.post_id, pv.aspect_id, pv.created_at, pv.updated_at #{where_clause}")
|
||||
|
||||
unless ids.blank?
|
||||
execute("INSERT into aspect_visibilities SELECT pv.post_id, pv.aspect_id #{where_clause}")
|
||||
|
||||
execute <<SQL
|
||||
DELETE FROM post_visibilities
|
||||
WHERE post_visibilities.id IN (#{ids.join(',')})
|
||||
SQL
|
||||
end
|
||||
execute("DELETE pv #{where_clause}")
|
||||
end
|
||||
|
||||
def self.set_pv_contact_ids
|
||||
|
|
@ -33,23 +26,48 @@ SQL
|
|||
SQL
|
||||
end
|
||||
|
||||
def self.delete_duplicate_pvs
|
||||
execute('DELETE FROM post_visibilities WHERE post_visibilities.contact_id = 0')
|
||||
|
||||
duplicate_rows = execute <<SQL
|
||||
SELECT COUNT(pv.contact_id), pv.contact_id, pv.post_id from post_visibilities AS pv
|
||||
GROUP BY pv.contact_id, pv.post_id
|
||||
HAVING COUNT(*)>1;
|
||||
SQL
|
||||
duplicate_rows.each do |row|
|
||||
count = row.first
|
||||
contact_id = row[1]
|
||||
post_id = row.last
|
||||
|
||||
execute <<SQL
|
||||
DELETE FROM post_visibilities AS pv
|
||||
WHERE pv.contact_id = #{contact_id} AND pv.post_id = #{post_id}
|
||||
LIMIT #{count-1}
|
||||
SQL
|
||||
end
|
||||
end
|
||||
|
||||
def self.up
|
||||
create_table :aspect_visibilities do |t|
|
||||
t.integer :post_id, :null => false
|
||||
t.integer :aspect_id, :null => false
|
||||
t.timestamps
|
||||
end
|
||||
add_index :aspect_visibilities, [:post_id, :aspect_id], :unique => true
|
||||
add_foreign_key :aspect_visibilities, :aspects, :dependent => :delete
|
||||
add_foreign_key :aspect_visibilities, :posts, :dependent => :delete
|
||||
|
||||
add_column :post_visibilities, :contact_id, :integer, :null => false
|
||||
add_index :post_visibilities, [:contact_id, :post_id], :unique => true
|
||||
|
||||
move_author_pvs_to_aspect_pvs
|
||||
set_pv_contact_ids
|
||||
|
||||
remove_index :post_visibilities, [:aspect_id, :post_id]
|
||||
remove_column :post_visibilities, :aspect_id
|
||||
|
||||
delete_duplicate_pvs
|
||||
|
||||
add_index :post_visibilities, [:contact_id, :post_id], :unique => true
|
||||
add_foreign_key :post_visibilities, :contacts, :dependent => :delete
|
||||
add_foreign_key :post_visibilities, :posts, :dependent => :delete
|
||||
end
|
||||
|
|
|
|||
|
|
@ -24,8 +24,10 @@ ActiveRecord::Schema.define(:version => 20110328202414) do
|
|||
add_index "aspect_memberships", ["contact_id"], :name => "index_aspect_memberships_on_contact_id"
|
||||
|
||||
create_table "aspect_visibilities", :force => true do |t|
|
||||
t.integer "post_id", :null => false
|
||||
t.integer "aspect_id", :null => false
|
||||
t.integer "post_id", :null => false
|
||||
t.integer "aspect_id", :null => false
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
end
|
||||
|
||||
add_index "aspect_visibilities", ["aspect_id"], :name => "aspect_visibilities_aspect_id_fk"
|
||||
|
|
|
|||
Loading…
Reference in a new issue