fix AddRoleIdColumns migration for qualified names

refs CNVS-26480

Change-Id: I13c37e1feb29e9d135bc7e0c7b72ae5641a45089
Reviewed-on: https://gerrit.instructure.com/71834
Reviewed-by: Jon Jensen <jon@instructure.com>
Product-Review: Jon Jensen <jon@instructure.com>
QA-Review: Jon Jensen <jon@instructure.com>
Tested-by: Jenkins
This commit is contained in:
Cody Cutrer 2016-02-09 11:25:03 -07:00 committed by Jon Jensen
parent da57843b6c
commit 5b152b060e
3 changed files with 11 additions and 6 deletions

View File

@ -226,6 +226,10 @@ module PostgreSQLAdapterExtensions
select_value("SELECT 1 FROM pg_available_extensions WHERE name='#{extension}'").to_i == 1
end
def set_search_path_on_function(function, args = "()", search_path = Shard.current.name)
execute("ALTER FUNCTION #{quote_table_name(function)}#{args} SET search_path TO #{search_path}")
end
private
OID = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID if Rails.version >= '4'

View File

@ -64,6 +64,7 @@ class AddRoleIdColumns < ActiveRecord::Migration
) WHERE id = NEW.id
SQL_ACTIONS
end
connection.set_search_path_on_function("account_user_after_insert_set_role_id__tr")
create_trigger("account_notification_role_after_insert_set_role_id__tr", :generated => true).
on("account_notification_roles").
@ -90,6 +91,7 @@ class AddRoleIdColumns < ActiveRecord::Migration
) WHERE id = NEW.id
SQL_ACTIONS
end
connection.set_search_path_on_function("account_notification_role_after_insert_set_role_id__tr")
create_trigger("enrollment_after_insert_set_role_id_if_role_name__tr", :generated => true).
on("enrollments").
@ -116,6 +118,7 @@ class AddRoleIdColumns < ActiveRecord::Migration
) WHERE id = NEW.id
SQL_ACTIONS
end
connection.set_search_path_on_function("enrollment_after_insert_set_role_id_if_role_name__tr")
create_trigger("enrollment_after_insert_set_role_id_if_no_role_name__tr", :generated => true).
on("enrollments").
@ -127,6 +130,7 @@ class AddRoleIdColumns < ActiveRecord::Migration
WHERE id = NEW.id
SQL_ACTIONS
end
connection.set_search_path_on_function("enrollment_after_insert_set_role_id_if_no_role_name__tr")
create_trigger("role_override_after_insert_set_role_id__tr", :generated => true).
on("role_overrides").
@ -153,6 +157,7 @@ class AddRoleIdColumns < ActiveRecord::Migration
) WHERE id = NEW.id
SQL_ACTIONS
end
connection.set_search_path_on_function("role_override_after_insert_set_role_id__tr")
end
# Populate the role_ids for account_users and role_overrides (and enrollments with custom role_names)
@ -171,7 +176,7 @@ class AddRoleIdColumns < ActiveRecord::Migration
next if role.built_in?
applicable_account_ids[role.account_id] ||= Account.sub_account_ids_recursive(role.account_id) + [role.account_id]
while AccountNotificationRole.where("role_id IS NULL AND role_type = ? AND (SELECT account_id FROM
account_notifications WHERE id = account_notification_roles.account_notification_id LIMIT 1) IN (?)", role.name,
#{AccountNotification.quoted_table_name} WHERE id = account_notification_roles.account_notification_id LIMIT 1) IN (?)", role.name,
applicable_account_ids[role.account_id]).limit(1000).update_all(:role_id => role.id) > 0; end
while AccountUser.where("role_id IS NULL AND membership_type = ? AND account_id IN (?)", role.name,
applicable_account_ids[role.account_id]).limit(1000).update_all(:role_id => role.id) > 0; end
@ -191,7 +196,7 @@ class AddRoleIdColumns < ActiveRecord::Migration
end
while AccountNotificationRole.where("role_id IS NULL AND role_type = ? AND (SELECT account_id FROM
account_notifications WHERE id = account_notification_roles.account_notification_id LIMIT 1) IN (?)", role.name,
#{AccountNotification.quoted_table_name} WHERE id = account_notification_roles.account_notification_id LIMIT 1) IN (?)", role.name,
applicable_account_ids[role.account_id]).limit(1000).update_all(:role_id => role.id) > 0; end
while RoleOverride.where("role_id IS NULL AND enrollment_type = ? AND context_type = ? AND context_id IN (?)", role.name, 'Account',
applicable_account_ids[role.account_id]).limit(1000).update_all(:role_id => role.id) > 0; end

View File

@ -5,10 +5,6 @@ class SetSearchPathsOnFunctions < ActiveRecord::Migration
Delayed::Backend::ActiveRecord::Job.connection
end
def set_search_path_on_function(function, args, search_path = Shard.current.name)
execute("ALTER FUNCTION #{connection.quote_table_name(function)}#{args} SET search_path TO #{search_path}")
end
def up
set_search_path_on_function("delayed_jobs_after_delete_row_tr_fn", "()")
set_search_path_on_function("delayed_jobs_before_insert_row_tr_fn", "()")