Mayel de Borniol 2024-01-09 14:18:37 +00:00
parent 8ab1799e14
commit 93245a6ffe

View file

@ -0,0 +1,37 @@
defmodule Bonfire.Repo.Migrations.GinIndexes do
use Ecto.Migration
@disable_ddl_transaction true
@disable_migration_lock true
# ^ Needed to migrate indexes concurrently.
# Disabling DDL transactions removes the guarantee that all of the changes in the migration will happen at once.
# Disabling the migration lock removes the guarantee only a single node will run a given migration if multiple nodes are attempting to migrate at the same time.
def up do
execute "CREATE EXTENSION IF NOT EXISTS pg_trgm;"
create_index("bonfire_data_identity_character", "username")
create_index("bonfire_data_social_profile", "name")
create_index_fields(
"bonfire_data_social_post_content",
"name gin_trgm_ops, summary gin_trgm_ops, html_body gin_trgm_ops"
)
end
def down do
# TODO
end
def create_index(table, field) do
create_index_fields(table, "#{field} gin_trgm_ops")
end
def create_index_fields(table, fields) do
execute """
CREATE INDEX CONCURRENTLY #{table}_gin_index
ON #{table}
USING gin (#{fields});
"""
end
end