improvement: Add nulls_distinct option to CustomIndex (#221)

This commit is contained in:
Jinkyou Son 2024-03-13 09:22:34 +09:00 committed by GitHub
parent ea853e5171
commit e27ce5e074
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 60 additions and 5 deletions

View file

@ -25,6 +25,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,

View file

@ -111,8 +111,9 @@ index ["column", "column2"], unique: true, where: "thing = TRUE"
| [`using`](#postgres-custom_indexes-index-using){: #postgres-custom_indexes-index-using } | `String.t` | | configures the index type. |
| [`prefix`](#postgres-custom_indexes-index-prefix){: #postgres-custom_indexes-index-prefix } | `String.t` | | specify an optional prefix for the index. |
| [`where`](#postgres-custom_indexes-index-where){: #postgres-custom_indexes-index-where } | `String.t` | | specify conditions for a partial index. |
| [`message`](#postgres-custom_indexes-index-message){: #postgres-custom_indexes-index-message } | `String.t` | | A custom message to use for unique indexes that have been violated |
| [`include`](#postgres-custom_indexes-index-include){: #postgres-custom_indexes-index-include } | `list(String.t)` | | specify fields for a covering index. This is not supported by all databases. For more information on PostgreSQL support, please read the official docs. |
| [`nulls_distinct`](#postgres-custom_indexes-index-nulls_distinct){: #postgres-custom_indexes-index-nulls_distinct } | `boolean` | | specify whether null values should be considered distinct for a unique index. |
| [`message`](#postgres-custom_indexes-index-message){: #postgres-custom_indexes-index-message } | `String.t` | | A custom message to use for unique indexes that have been violated |
| [`all_tenants?`](#postgres-custom_indexes-index-all_tenants?){: #postgres-custom_indexes-index-all_tenants? } | `boolean` | `false` | Whether or not the index should factor in the multitenancy attribute or not. |

View file

@ -11,6 +11,7 @@ defmodule AshPostgres.CustomIndex do
:prefix,
:where,
:include,
:nulls_distinct,
:message,
:all_tenants?
]
@ -54,15 +55,20 @@ defmodule AshPostgres.CustomIndex do
type: :string,
doc: "specify conditions for a partial index."
],
message: [
type: :string,
doc: "A custom message to use for unique indexes that have been violated"
],
include: [
type: {:list, :string},
doc:
"specify fields for a covering index. This is not supported by all databases. For more information on PostgreSQL support, please read the official docs."
],
nulls_distinct: [
type: :boolean,
doc: "specify whether null values should be considered distinct for a unique index.",
default: false
],
message: [
type: :string,
doc: "A custom message to use for unique indexes that have been violated"
],
all_tenants?: [
type: :boolean,
default: false,

View file

@ -2988,6 +2988,7 @@ defmodule AshPostgres.MigrationGenerator do
end)
end)
|> Map.put_new(:include, [])
|> Map.put_new(:nulls_distinct, false)
|> Map.put_new(:message, nil)
|> Map.put_new(:all_tenants?, false)
end)

View file

@ -908,6 +908,7 @@ defmodule AshPostgres.MigrationGenerator.Operation do
option(:prefix, index.prefix),
option(:where, index.where),
option(:include, index.include),
option(:nulls_distinct, index.nulls_distinct),
option(:prefix, schema)
])

View file

@ -298,6 +298,51 @@ defmodule AshPostgres.MigrationGeneratorTest do
end
end
describe "custom_indexes with `null_distinct: true`" 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
index([:uniq_one], nulls_distinct: true)
index([:uniq_two], nulls_distinct: false)
index([:uniq_custom_one])
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 adds nulls_distinct option to create index migration" 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, [:uniq_one], nulls_distinct: true)>
assert file =~ ~S<create index(:posts, [:uniq_two])>
assert file =~ ~S<create index(:posts, [:uniq_custom_one])>
end
end
describe "creating follow up migrations with a schema" do
setup do
on_exit(fn ->