use plain sql for update_all to fix postgres
also added a migration model
This commit is contained in:
parent
7011f2961d
commit
c778fc4232
1 changed files with 18 additions and 4 deletions
|
|
@ -1,12 +1,21 @@
|
||||||
class LinkShareVisibilitiesWithUser < ActiveRecord::Migration
|
class LinkShareVisibilitiesWithUser < ActiveRecord::Migration
|
||||||
|
class ShareVisibility < ActiveRecord::Base
|
||||||
|
end
|
||||||
|
|
||||||
def up
|
def up
|
||||||
cleanup_deleted_share_visibilities
|
cleanup_deleted_share_visibilities
|
||||||
|
|
||||||
remove_columns :share_visibilities, :created_at, :updated_at
|
remove_columns :share_visibilities, :created_at, :updated_at
|
||||||
add_column :share_visibilities, :user_id, :integer
|
add_column :share_visibilities, :user_id, :integer
|
||||||
|
|
||||||
|
# update_all from AR doesn't work with postgres, see: https://github.com/rails/rails/issues/13496
|
||||||
|
if AppConfig.postgres?
|
||||||
|
execute "UPDATE share_visibilities SET user_id = contacts.user_id " \
|
||||||
|
"FROM contacts WHERE contacts.id = share_visibilities.contact_id"
|
||||||
|
else
|
||||||
ShareVisibility.joins("INNER JOIN contacts ON share_visibilities.contact_id = contacts.id")
|
ShareVisibility.joins("INNER JOIN contacts ON share_visibilities.contact_id = contacts.id")
|
||||||
.update_all("share_visibilities.user_id = contacts.user_id")
|
.update_all("share_visibilities.user_id = contacts.user_id")
|
||||||
|
end
|
||||||
|
|
||||||
remove_foreign_key :share_visibilities, name: :post_visibilities_contact_id_fk
|
remove_foreign_key :share_visibilities, name: :post_visibilities_contact_id_fk
|
||||||
|
|
||||||
|
|
@ -31,8 +40,13 @@ class LinkShareVisibilitiesWithUser < ActiveRecord::Migration
|
||||||
def down
|
def down
|
||||||
add_column :share_visibilities, :contact_id, :integer
|
add_column :share_visibilities, :contact_id, :integer
|
||||||
|
|
||||||
|
if AppConfig.postgres?
|
||||||
|
execute "UPDATE share_visibilities SET contact_id = contacts.id " \
|
||||||
|
"FROM contacts WHERE contacts.user_id = share_visibilities.user_id"
|
||||||
|
else
|
||||||
ShareVisibility.joins("INNER JOIN contacts ON share_visibilities.user_id = contacts.user_id")
|
ShareVisibility.joins("INNER JOIN contacts ON share_visibilities.user_id = contacts.user_id")
|
||||||
.update_all("share_visibilities.contact_id = contacts.id")
|
.update_all("share_visibilities.contact_id = contacts.id")
|
||||||
|
end
|
||||||
|
|
||||||
remove_foreign_key :share_visibilities, name: :share_visibilities_user_id_fk
|
remove_foreign_key :share_visibilities, name: :share_visibilities_user_id_fk
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue