2022-07-25 20:08:40 -04:00
|
|
|
defmodule Memex.Repo.Migrations.CreateNotes do
|
|
|
|
use Ecto.Migration
|
|
|
|
|
2022-11-18 23:45:15 -05:00
|
|
|
def up do
|
2022-07-25 20:08:40 -04:00
|
|
|
create table(:notes, primary_key: false) do
|
|
|
|
add :id, :binary_id, primary_key: true
|
|
|
|
add :title, :string
|
|
|
|
add :content, :text
|
2022-11-18 23:45:15 -05:00
|
|
|
add :tags, {:array, :citext}
|
2022-07-25 20:08:40 -04:00
|
|
|
add :visibility, :string
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
add :user_id, references(:users, on_delete: :delete_all, type: :binary_id)
|
|
|
|
|
2022-07-25 20:08:40 -04:00
|
|
|
timestamps()
|
|
|
|
end
|
2022-11-18 23:45:15 -05:00
|
|
|
|
|
|
|
flush()
|
|
|
|
|
|
|
|
execute """
|
|
|
|
CREATE FUNCTION immutable_array_to_string(text[], text)
|
|
|
|
RETURNS text LANGUAGE sql IMMUTABLE as $$SELECT array_to_string($1, $2)$$
|
|
|
|
"""
|
|
|
|
|
|
|
|
execute """
|
|
|
|
ALTER TABLE notes
|
|
|
|
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 notes_trgm_idx ON notes USING GIN (search)")
|
|
|
|
end
|
|
|
|
|
|
|
|
def down do
|
|
|
|
drop table(:notes)
|
|
|
|
execute("DROP FUNCTION immutable_array_to_string(text[], text)")
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
|
|
|
end
|