memEx/priv/repo/migrations/20220726001039_create_contexts.exs

36 lines
929 B
Elixir
Raw Permalink Normal View History

2022-07-25 20:11:08 -04:00
defmodule Memex.Repo.Migrations.CreateContexts do
use Ecto.Migration
def change do
create table(:contexts, primary_key: false) do
add :id, :binary_id, primary_key: true
add :title, :string
add :content, :text
2022-11-24 12:44:34 -05:00
add :tags, {:array, :string}
2022-07-25 20:11:08 -04:00
add :visibility, :string
2022-11-24 12:44:34 -05:00
add :user_id, references(:users, on_delete: :delete_all, type: :binary_id)
2022-07-25 20:11:08 -04:00
timestamps()
end
2022-11-24 12:44:34 -05:00
flush()
execute """
ALTER TABLE contexts
ADD COLUMN search tsvector
GENERATED ALWAYS AS (
setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
setweight(to_tsvector('english', coalesce(immutable_array_to_string(tags, ' '), '')), 'B') ||
setweight(to_tsvector('english', coalesce(content, '')), 'C')
) STORED
"""
execute("CREATE INDEX contexts_trgm_idx ON contexts USING GIN (search)")
end
def down do
drop table(:contexts)
2022-07-25 20:11:08 -04:00
end
end