Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for nulls_distinct in custom indexes #175

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ spark_locals_without_parens = [
migration_ignore_attributes: 1,
migration_types: 1,
name: 1,
nulls_distinct: 1,
on_delete: 1,
on_update: 1,
polymorphic?: 1,
Expand Down
20 changes: 20 additions & 0 deletions documentation/dsls/DSL:-AshPostgres.DataLayer.cheatmd
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,26 @@ index ["column", "column2"], unique: true, where: "thing = TRUE"
</td>
</tr>

<tr>
<td style="text-align: left">
<a id="postgres-custom_indexes-index-nulls_distinct" href="#postgres-custom_indexes-index-nulls_distinct">
<span style="font-family: Inconsolata, Menlo, Courier, monospace;">
nulls_distinct
</span>
</a>

</td>
<td style="text-align: left">
<code class="inline">boolean</code>
</td>
<td style="text-align: left">

</td>
<td style="text-align: left" colspan=2>
indicates whether the index should treat nil values as distinct or not.
</td>
</tr>

<tr>
<td style="text-align: left">
<a id="postgres-custom_indexes-index-using" href="#postgres-custom_indexes-index-using">
Expand Down
5 changes: 5 additions & 0 deletions lib/custom_index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule AshPostgres.CustomIndex do
:name,
:unique,
:concurrently,
:nulls_distinct,
:using,
:prefix,
:where,
Expand Down Expand Up @@ -36,6 +37,10 @@ defmodule AshPostgres.CustomIndex do
doc: "indicates whether the index should be created/dropped concurrently.",
default: false
],
nulls_distinct: [
type: :boolean,
doc: "indicates whether the index should treat nil values as distinct or not."
],
using: [
type: :string,
doc: "configures the index type."
Expand Down
8 changes: 8 additions & 0 deletions lib/migration_generator/operation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ defmodule AshPostgres.MigrationGenerator.Operation do
end
end

def non_nullable_option(key, value) do
if not is_nil(value) do
"#{as_atom(key)}: #{inspect(value)}"
end
end

def on_delete(%{on_delete: on_delete}) when on_delete in [:delete, :nilify] do
"on_delete: :#{on_delete}_all"
end
Expand Down Expand Up @@ -826,6 +832,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
option(:name, index.name),
option(:unique, index.unique),
option(:concurrently, index.concurrently),
non_nullable_option(:nulls_distinct, index.nulls_distinct),
option(:using, index.using),
option(:prefix, index.prefix),
option(:where, index.where),
Expand Down Expand Up @@ -937,6 +944,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
option(:name, index.name),
option(:unique, index.unique),
option(:concurrently, index.concurrently),
non_nullable_option(:nulls_distinct, index[:nulls_distinct]),
option(:using, index.using),
option(:prefix, index.prefix),
option(:where, index.where),
Expand Down
42 changes: 42 additions & 0 deletions test/migration_generator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,48 @@ defmodule AshPostgres.MigrationGeneratorTest do
end
end

describe "custom_indexes with `nulls_distinct: false`" do
setup do
on_exit(fn ->
File.rm_rf!("test_snapshots_path")
File.rm_rf!("test_migration_path")
end)

defposts do
postgres do
custom_indexes do
# need one without any opts
index([:title], unique: true, nulls_distinct: false)
end
end

attributes do
uuid_primary_key(:id)
attribute(:title, :string)
end
end

defapi([Post])
Mix.shell(Mix.Shell.Process)

AshPostgres.MigrationGenerator.generate(Api,
snapshot_path: "test_snapshots_path",
migration_path: "test_migration_path",
quiet: true,
format: false
)
end

test "it creates multiple migration files" do
assert [custom_index_migration] =
Enum.sort(Path.wildcard("test_migration_path/**/*_migrate_resources*.exs"))

file = File.read!(custom_index_migration)

assert file =~ ~S<create index(:posts, ["title"], unique: true, nulls_distinct: false)>
end
end

describe "creating follow up migrations with a schema" do
setup do
on_exit(fn ->
Expand Down
Loading