Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion lib/migration_generator/migration_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,7 @@ defmodule AshPostgres.MigrationGenerator do
destination_attribute: merge_uniq!(references, table, :destination_attribute, name),
deferrable: merge_uniq!(references, table, :deferrable, name),
index?: merge_uniq!(references, table, :index?, name),
index_where: merge_uniq!(references, table, :index_where, name),
destination_attribute_default:
merge_uniq!(references, table, :destination_attribute_default, name),
destination_attribute_generated:
Expand Down Expand Up @@ -2610,8 +2611,11 @@ defmodule AshPostgres.MigrationGenerator do
old_attribute ->
new_index? = attribute.references && attribute.references[:index?]
old_index? = old_attribute.references && old_attribute.references[:index?]
new_index_where = attribute.references && attribute.references[:index_where]
old_index_where = old_attribute.references && old_attribute.references[:index_where]

old_index? != new_index? ||
(new_index? && old_index_where != new_index_where) ||
(new_index? && multitenancy_changed?)
end
end)
Expand All @@ -2620,6 +2624,7 @@ defmodule AshPostgres.MigrationGenerator do
table: snapshot.table,
schema: snapshot.schema,
source: attribute.source,
where: attribute.references[:index_where],
multitenancy: snapshot.multitenancy
}
end)
Expand All @@ -2640,13 +2645,19 @@ defmodule AshPostgres.MigrationGenerator do
attribute && old_attribute[:references][:index?] && attribute[:references][:index?] &&
multitenancy_changed?

has_removed_index? || attribute_doesnt_exist? || multitenancy_change_requires_rewrite?
index_where_change_requires_rewrite? =
attribute && old_attribute[:references][:index?] && attribute[:references][:index?] &&
old_attribute[:references][:index_where] != attribute[:references][:index_where]

has_removed_index? || attribute_doesnt_exist? || multitenancy_change_requires_rewrite? ||
index_where_change_requires_rewrite?
end)
|> Enum.map(fn attribute ->
%Operation.RemoveReferenceIndex{
table: snapshot.table,
schema: snapshot.schema,
source: attribute.source,
where: attribute.references[:index_where],
multitenancy: snapshot.multitenancy,
old_multitenancy: old_snapshot.multitenancy
}
Expand Down Expand Up @@ -3274,11 +3285,13 @@ defmodule AshPostgres.MigrationGenerator do
old_refs
|> clean_references_for_comparison()
|> Map.delete(:index?)
|> Map.delete(:index_where)

new_without_index =
new_refs
|> clean_references_for_comparison()
|> Map.delete(:index?)
|> Map.delete(:index_where)

old_without_index != new_without_index
end
Expand Down Expand Up @@ -3308,6 +3321,9 @@ defmodule AshPostgres.MigrationGenerator do
{left, right} =
normalize_attribute_reference_schema_move(left, right, schema_moves)

left = remove_reference_index_options(left)
right = remove_reference_index_options(right)

left =
if ignore_names? do
Map.drop(left, [:source, :name])
Expand All @@ -3325,6 +3341,13 @@ defmodule AshPostgres.MigrationGenerator do
left != right
end

defp remove_reference_index_options(%{references: references} = attribute)
when is_map(references) do
%{attribute | references: Map.drop(references, [:index?, :index_where])}
end

defp remove_reference_index_options(attribute), do: attribute

defp normalize_attribute_reference_schema_move(left, right, schema_moves) do
old_refs = Map.get(left, :references)
new_refs = Map.get(right, :references)
Expand Down Expand Up @@ -4345,6 +4368,8 @@ defmodule AshPostgres.MigrationGenerator do
destination_attribute: destination_attribute_source,
deferrable: configured_reference.deferrable,
index?: configured_reference.index?,
index_where:
reference_index_where(configured_reference.index_where, attribute.source),
multitenancy: multitenancy(relationship.destination),
on_delete: configured_reference.on_delete,
on_update: configured_reference.on_update,
Expand All @@ -4367,6 +4392,10 @@ defmodule AshPostgres.MigrationGenerator do
end)
end

defp reference_index_where(:not_nil, source), do: "#{source} IS NOT NULL"
defp reference_index_where(index_where, _source) when is_binary(index_where), do: index_where
defp reference_index_where(nil, _source), do: nil

defp configured_reference(resource, table, attribute, relationship) do
ref =
resource
Expand All @@ -4379,6 +4408,7 @@ defmodule AshPostgres.MigrationGenerator do
match_type: nil,
deferrable: false,
index?: false,
index_where: nil,
schema:
relationship.context[:data_layer][:schema] ||
AshPostgres.DataLayer.Info.schema(relationship.destination) ||
Expand Down Expand Up @@ -4796,6 +4826,7 @@ defmodule AshPostgres.MigrationGenerator do
|> Map.update!(:on_delete, &(&1 && load_references_on_delete(&1)))
|> Map.update!(:on_update, &(&1 && maybe_to_atom(&1)))
|> Map.put_new(:index?, false)
|> Map.put_new(:index_where, nil)
|> Map.put_new(:match_with, nil)
|> Map.put_new(:match_type, nil)
|> Map.update!(
Expand Down
8 changes: 5 additions & 3 deletions lib/migration_generator/operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1156,13 +1156,14 @@ defmodule AshPostgres.MigrationGenerator.Operation do

defmodule AddReferenceIndex do
@moduledoc false
defstruct [:table, :schema, :source, :multitenancy, no_phase: true]
defstruct [:table, :schema, :source, :where, :multitenancy, no_phase: true]
import Helper

def up(%{
source: source,
table: table,
schema: schema,
where: where,
multitenancy: multitenancy
}) do
keys =
Expand All @@ -1176,7 +1177,8 @@ defmodule AshPostgres.MigrationGenerator.Operation do

opts =
join([
option(:prefix, schema)
option(:prefix, schema),
option(:where, where)
])

if opts == "" do
Expand Down Expand Up @@ -1378,7 +1380,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do

defmodule RemoveReferenceIndex do
@moduledoc false
defstruct [:schema, :table, :source, :multitenancy, :old_multitenancy, no_phase: true]
defstruct [:schema, :table, :source, :where, :multitenancy, :old_multitenancy, no_phase: true]
import Helper

def up(operation) do
Expand Down
6 changes: 6 additions & 0 deletions lib/reference.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ defmodule AshPostgres.Reference do
:match_type,
:deferrable,
:index?,
:index_where,
:__spark_metadata__,
ignore?: false
]
Expand Down Expand Up @@ -71,6 +72,11 @@ defmodule AshPostgres.Reference do
type: :boolean,
default: false,
doc: "Whether to create or not a corresponding index"
],
index_where: [
type: {:or, [{:one_of, [:not_nil]}, :string]},
doc:
"A condition to use for the corresponding partial index. Use `:not_nil` to exclude rows where the reference is nil."
]
]
end
Expand Down
Loading
Loading