use native rails methods for gist indexes

Change-Id: Ia082ab3e55ff91d9488ec165735c69f490ee3560
Reviewed-on: https://gerrit.instructure.com/106114
Tested-by: Jenkins
Reviewed-by: Simon Williams <simon@instructure.com>
Product-Review: Cody Cutrer <cody@instructure.com>
QA-Review: Cody Cutrer <cody@instructure.com>
This commit is contained in:
Cody Cutrer 2017-03-23 12:21:50 -06:00
parent 96c115963e
commit 2e6ffb7218
6 changed files with 28 additions and 22 deletions

View File

@ -30,10 +30,9 @@ class AddGistIndexForUserSearch < ActiveRecord::Migration[4.2]
end
if (schema = connection.extension_installed?(:pg_trgm))
concurrently = " CONCURRENTLY" if connection.open_transactions == 0
execute("create index#{concurrently} index_trgm_users_name on #{User.quoted_table_name} USING gist(lower(name) #{schema}.gist_trgm_ops);")
execute("create index#{concurrently} index_trgm_pseudonyms_sis_user_id on #{Pseudonym.quoted_table_name} USING gist(lower(sis_user_id) #{schema}.gist_trgm_ops);")
execute("create index#{concurrently} index_trgm_communication_channels_path on #{CommunicationChannel.quoted_table_name} USING gist(lower(path) #{schema}.gist_trgm_ops);")
add_index :users, "lower(name) #{schema}.gist_trgm_ops", name: "index_trgm_users_name", using: :gist, algorithm: :concurrently
add_index :pseudonyms, "lower(sis_user_id) #{schema}.gist_trgm_ops", name: "index_trgm_pseudonyms_sis_user_id", using: :gist, algorithm: :concurrently
add_index :communication_channels, "lower(path) #{schema}.gist_trgm_ops", name: "index_trgm_communication_channels_path", using: :gist, algorithm: :concurrently
end
end
end

View File

@ -21,11 +21,10 @@ class AddMoreGistIndexes < ActiveRecord::Migration[4.2]
def self.up
if is_postgres? && (schema = connection.extension_installed?(:pg_trgm))
concurrently = " CONCURRENTLY" if connection.open_transactions == 0
execute("CREATE INDEX#{concurrently} index_trgm_users_short_name ON #{User.quoted_table_name} USING gist(LOWER(short_name) #{schema}.gist_trgm_ops)")
execute("CREATE INDEX#{concurrently} index_trgm_courses_name ON #{Course.quoted_table_name} USING gist(LOWER(name) #{schema}.gist_trgm_ops)")
execute("CREATE INDEX#{concurrently} index_trgm_courses_course_code ON #{Course.quoted_table_name} USING gist(LOWER(course_code) #{schema}.gist_trgm_ops)")
execute("CREATE INDEX#{concurrently} index_trgm_courses_sis_source_id ON #{Course.quoted_table_name} USING gist(LOWER(sis_source_id) #{schema}.gist_trgm_ops)")
add_index :users, "LOWER(short_name) #{schema}.gist_trgm_ops", name: "index_trgm_users_short_name", using: :gist, algorithm: :concurrently
add_index :courses, "LOWER(name) #{schema}.gist_trgm_ops", name: "index_trgm_courses_name", using: :gist, algorithm: :concurrently
add_index :courses, "LOWER(course_code) #{schema}.gist_trgm_ops", name: "index_trgm_courses_course_code", using: :gist, algorithm: :concurrently
add_index :courses, "LOWER(sis_source_id) #{schema}.gist_trgm_ops", name: "index_trgm_courses_sis_source_id", using: :gist, algorithm: :concurrently
end
end

View File

@ -21,8 +21,7 @@ class AddMoreGistIndexesForUserSearch < ActiveRecord::Migration[4.2]
def self.up
if is_postgres? && (schema = connection.extension_installed?(:pg_trgm))
concurrently = " CONCURRENTLY" if connection.open_transactions == 0
execute("create index#{concurrently} index_trgm_pseudonyms_unique_id ON #{Pseudonym.quoted_table_name} USING gist(lower(unique_id) #{schema}.gist_trgm_ops);")
add_index :pseudonyms, "lower(unique_id) #{schema}.gist_trgm_ops", name: "index_trgm_pseudonyms_unique_id", using: :gist, algorithm: :concurrently
end
end

View File

@ -21,13 +21,14 @@ class AddBetterCompositeIndexForCourseSearching < ActiveRecord::Migration[4.2]
def self.up
if is_postgres? && (schema = connection.extension_installed?(:pg_trgm))
concurrently = " CONCURRENTLY" if connection.open_transactions == 0
execute("CREATE INDEX#{concurrently} index_trgm_courses_composite_search ON
#{Course.quoted_table_name} USING gist((
add_index :courses, "(
coalesce(lower(name), '') || ' ' ||
coalesce(lower(sis_source_id), '') || ' ' ||
coalesce(lower(course_code), '')
) #{schema}.gist_trgm_ops)")
) #{schema}.gist_trgm_ops",
name: "index_trgm_courses_composite_search",
using: :gist,
algorithm: :concurrently
end
end

View File

@ -21,11 +21,13 @@ class AddUserActiveOnlyGistIndexes < ActiveRecord::Migration[4.2]
def self.up
if schema = connection.extension_installed?(:pg_trgm)
concurrently = " CONCURRENTLY" if connection.open_transactions == 0
# next line indexes the wrong column, so it's nuked and another migration adds the right one and fixes up
# people who already ran this migration
# execute("CREATE INDEX#{concurrently} index_trgm_users_name_active_only ON #{User.quoted_table_name} USING gist(LOWER(short_name) #{schema}.gist_trgm_ops) WHERE workflow_state IN ('registered', 'pre_registered')")
execute("CREATE INDEX#{concurrently} index_trgm_users_short_name_active_only ON #{User.quoted_table_name} USING gist(LOWER(short_name) #{schema}.gist_trgm_ops) WHERE workflow_state IN ('registered', 'pre_registered')")
add_index :users, "LOWER(short_name) #{schema}.gist_trgm_ops",
name: "index_trgm_users_short_name_active_only",
using: :gist,
algorithm: :concurrently,
where: "workflow_state IN ('registered', 'pre_registered')"
end
end

View File

@ -23,8 +23,11 @@ class FixUserActiveOnlyGistIndexes < ActiveRecord::Migration[4.2]
if schema = connection.extension_installed?(:pg_trgm)
remove_index :users, name: 'index_trgm_users_name_active_only' if index_name_exists?(:users, 'index_trgm_users_name_active_only', false)
concurrently = " CONCURRENTLY" if connection.open_transactions == 0
execute("CREATE INDEX#{concurrently} index_trgm_users_name_active_only ON #{User.quoted_table_name} USING gist(LOWER(name) #{schema}.gist_trgm_ops) WHERE workflow_state IN ('registered', 'pre_registered')")
add_index :users, "LOWER(name) #{schema}.gist_trgm_ops",
name: "index_trgm_users_name_active_only",
using: :gist,
algorithm: :concurrently,
where: "workflow_state IN ('registered', 'pre_registered')"
end
end
@ -32,8 +35,11 @@ class FixUserActiveOnlyGistIndexes < ActiveRecord::Migration[4.2]
if schema = connection.extension_installed?(:pg_trgm)
remove_index :users, name: 'index_trgm_users_name_active_only' if index_name_exists?(:users, 'index_trgm_users_name_active_only', false)
concurrently = " CONCURRENTLY" if connection.open_transactions == 0
execute("CREATE INDEX#{concurrently} index_trgm_users_name_active_only ON #{User.quoted_table_name} USING gist(LOWER(short_name) #{schema}.gist_trgm_ops) WHERE workflow_state IN ('registered', 'pre_registered')")
add_index :users, "LOWER(short_name) #{schema}.gist_trgm_ops",
name: "index_trgm_users_short_name_active_only",
using: :gist,
algorithm: :concurrently,
where: "workflow_state IN ('registered', 'pre_registered')"
end
end
end