diaspora/db/migrate/20120202190701_remove_public_share_visibilities.rb
Ricardo 6fa548e427 fix syntax for psql backend
- PostgreSQL doesn't allow double quotes for values. Double quotes are
  used for surrounding table names that contain spaces. Single quotes
  should be used for values.

- DELETE in a JOIN is not permitted in SQL-99 (see
  http://drupal.org/node/555562) so we use USING with WHERE instead
2012-02-04 12:45:13 +08:00

47 lines
1.5 KiB
Ruby

# NOTE: this migration will remove a lot of unused rows. It is highly suggested
# that you run `OPTIMIZE TABLE share_visibilities` after this
# `OPTIMIZE NO_WRITE_TO_BINLOG TABLE share_visibilities;` will run faster but has a greater chance of corrupting data
# and will only work on an unsharded database (which should be the case for everyone right now)
# you probably want to backup your db before you do any of this.
# migration is complete.
#
# caution: you may want to take your pod offline during the OPTIMIZE command.
class RemovePublicShareVisibilities < ActiveRecord::Migration
def self.up
%w{Post Photo}.each do |type|
index = 0
table_name = type.tableize
if postgres?
shareable_size = ActiveRecord::Base.connection.execute("SELECT COUNT(*) FROM #{table_name}").first['count'].to_i
else
shareable_size = ActiveRecord::Base.connection.execute("SELECT COUNT(*) FROM #{table_name}").first.first
end
while index < shareable_size + 100 do
sql = <<-SQL
DELETE
FROM share_visibilities AS sv
USING #{table_name} as p
WHERE sv.shareable_id = p.id
AND sv.shareable_type = '#{type}'
AND p.public IS TRUE
AND p.id < #{index};
SQL
puts "deleted public share vis up to #{index} of #{type}"
ActiveRecord::Base.connection.execute(sql)
index += 100
end
end
end
def self.down
raise ActiveRecord::IrreversibleMigration
end
end