Added PostgreSQL-compatible versions of raw SQL migration code in ShareAnything migration.
This commit is contained in:
parent
fdcd6d47ed
commit
0ef81d7112
1 changed files with 52 additions and 34 deletions
|
|
@ -2,65 +2,83 @@ class ShareAnything < ActiveRecord::Migration
|
|||
def self.up
|
||||
remove_foreign_key :aspect_visibilities, :posts
|
||||
|
||||
start_sql = "ALTER TABLE aspect_visibilities "
|
||||
sql = []
|
||||
if postgres?
|
||||
execute "DROP INDEX index_aspect_visibilities_on_post_id_and_aspect_id"
|
||||
execute "DROP INDEX index_aspect_visibilities_on_post_id"
|
||||
execute "ALTER TABLE aspect_visibilities RENAME COLUMN post_id TO shareable_id"
|
||||
execute "ALTER TABLE aspect_visibilities ADD COLUMN shareable_type VARCHAR(255) NOT NULL DEFAULT 'Post'"
|
||||
execute "CREATE INDEX shareable_and_aspect_id ON aspect_visibilities ( shareable_id, shareable_type, aspect_id )"
|
||||
execute "CREATE INDEX index_aspect_visibilities_on_shareable_id_and_shareable_type ON aspect_visibilities ( shareable_id, shareable_type )"
|
||||
else
|
||||
start_sql = "ALTER TABLE aspect_visibilities "
|
||||
sql = []
|
||||
|
||||
#remove_index :aspect_visibilities, :post_id_and_aspect_id
|
||||
sql << "DROP INDEX `index_aspect_visibilities_on_post_id_and_aspect_id`"
|
||||
#remove_index :aspect_visibilities, :post_id_and_aspect_id
|
||||
sql << "DROP INDEX `index_aspect_visibilities_on_post_id_and_aspect_id`"
|
||||
|
||||
#remove_index :aspect_visibilities, :post_id
|
||||
sql << "DROP INDEX `index_aspect_visibilities_on_post_id`"
|
||||
#remove_index :aspect_visibilities, :post_id
|
||||
sql << "DROP INDEX `index_aspect_visibilities_on_post_id`"
|
||||
|
||||
|
||||
|
||||
# change_table :aspect_visibilities do |t|
|
||||
# change_table :aspect_visibilities do |t|
|
||||
|
||||
# t.rename :post_id, :shareable_id
|
||||
# t.string :shareable_type, :default => 'Post', :null => false
|
||||
# end
|
||||
# t.rename :post_id, :shareable_id
|
||||
# t.string :shareable_type, :default => 'Post', :null => false
|
||||
# end
|
||||
|
||||
sql << "CHANGE COLUMN post_id shareable_id int NOT NULL"
|
||||
sql << "ADD shareable_type varchar(255) NOT NULL DEFAULT 'Post'"
|
||||
sql << "CHANGE COLUMN post_id shareable_id int NOT NULL"
|
||||
sql << "ADD shareable_type varchar(255) NOT NULL DEFAULT 'Post'"
|
||||
|
||||
|
||||
# add_index :aspect_visibilities, [:shareable_id, :shareable_type, :aspect_id], :name => 'shareable_and_aspect_id'
|
||||
# add_index :aspect_visibilities, [:shareable_id, :shareable_type]
|
||||
# add_index :aspect_visibilities, [:shareable_id, :shareable_type, :aspect_id], :name => 'shareable_and_aspect_id'
|
||||
# add_index :aspect_visibilities, [:shareable_id, :shareable_type]
|
||||
|
||||
sql << "add index `shareable_and_aspect_id` (`shareable_id`, `shareable_type`, `aspect_id`)"
|
||||
sql << "add index `index_aspect_visibilities_on_shareable_id_and_shareable_type` (`shareable_id`, `shareable_type`)"
|
||||
sql << "add index `shareable_and_aspect_id` (`shareable_id`, `shareable_type`, `aspect_id`)"
|
||||
sql << "add index `index_aspect_visibilities_on_shareable_id_and_shareable_type` (`shareable_id`, `shareable_type`)"
|
||||
|
||||
|
||||
execute(start_sql + sql.join(', ') + ';')
|
||||
execute(start_sql + sql.join(', ') + ';')
|
||||
end
|
||||
|
||||
|
||||
|
||||
remove_foreign_key :post_visibilities, :posts
|
||||
rename_table :post_visibilities, :share_visibilities
|
||||
|
||||
start_sql = "ALTER TABLE share_visibilities "
|
||||
sql = []
|
||||
if postgres?
|
||||
execute "DROP INDEX index_post_visibilities_on_contact_id_and_post_id"
|
||||
execute "DROP INDEX index_post_visibilities_on_post_id_and_hidden_and_contact_id"
|
||||
execute "ALTER TABLE share_visibilities RENAME COLUMN post_id TO shareable_id"
|
||||
execute "ALTER TABLE share_visibilities ADD COLUMN shareable_type VARCHAR(60) NOT NULL DEFAULT 'Post'"
|
||||
execute "CREATE INDEX shareable_and_contact_id ON share_visibilities ( shareable_id, shareable_type, contact_id )"
|
||||
execute "CREATE INDEX shareable_and_hidden_and_contact_id ON share_visibilities ( shareable_id, shareable_type, hidden, contact_id )"
|
||||
else
|
||||
start_sql = "ALTER TABLE share_visibilities "
|
||||
sql = []
|
||||
|
||||
#remove_index :post_visibilities, :contact_id_and_post_id
|
||||
#remove_index :post_visibilities, :post_id_and_hidden_and_contact_id
|
||||
#remove_index :post_visibilities, :contact_id_and_post_id
|
||||
#remove_index :post_visibilities, :post_id_and_hidden_and_contact_id
|
||||
|
||||
sql << "DROP INDEX `index_post_visibilities_on_contact_id_and_post_id`"
|
||||
sql << "DROP INDEX `index_post_visibilities_on_post_id_and_hidden_and_contact_id`"
|
||||
sql << "DROP INDEX `index_post_visibilities_on_contact_id_and_post_id`"
|
||||
sql << "DROP INDEX `index_post_visibilities_on_post_id_and_hidden_and_contact_id`"
|
||||
|
||||
#change_table :post_visibilities do |t|
|
||||
# t.rename :post_id, :shareable_id
|
||||
# t.string :shareable_type, :default => 'Post', :null => false
|
||||
#end
|
||||
#change_table :post_visibilities do |t|
|
||||
# t.rename :post_id, :shareable_id
|
||||
# t.string :shareable_type, :default => 'Post', :null => false
|
||||
#end
|
||||
|
||||
sql << "CHANGE COLUMN post_id shareable_id int NOT NULL"
|
||||
sql << "ADD shareable_type varchar(60) NOT NULL DEFAULT 'Post'"
|
||||
sql << "CHANGE COLUMN post_id shareable_id int NOT NULL"
|
||||
sql << "ADD shareable_type varchar(60) NOT NULL DEFAULT 'Post'"
|
||||
|
||||
#add_index :share_visibilities, [:shareable_id, :shareable_type, :contact_id], :name => 'shareable_and_contact_id'
|
||||
#add_index :share_visibilities, [:shareable_id, :shareable_type, :hidden, :contact_id], :name => 'shareable_and_hidden_and_contact_id'
|
||||
#add_index :share_visibilities, [:shareable_id, :shareable_type, :contact_id], :name => 'shareable_and_contact_id'
|
||||
#add_index :share_visibilities, [:shareable_id, :shareable_type, :hidden, :contact_id], :name => 'shareable_and_hidden_and_contact_id'
|
||||
|
||||
sql << "add index `shareable_and_contact_id` (`shareable_id`, `shareable_type`, `contact_id`)"
|
||||
sql << "add index `shareable_and_hidden_and_contact_id` (`shareable_id`, `shareable_type`, `hidden`, `contact_id`)"
|
||||
sql << "add index `shareable_and_contact_id` (`shareable_id`, `shareable_type`, `contact_id`)"
|
||||
sql << "add index `shareable_and_hidden_and_contact_id` (`shareable_id`, `shareable_type`, `hidden`, `contact_id`)"
|
||||
|
||||
execute(start_sql + sql.join(', ') + ';')
|
||||
execute(start_sql + sql.join(', ') + ';')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue