ash-project / ash_postgres

The PostgreSQL data layer for Ash Framework
https://hexdocs.pm/ash_postgres
MIT License
139 stars 75 forks source link

Changing identities leads to generation of erroneous migrations #303

Closed spacebat closed 5 months ago

spacebat commented 5 months ago

Describe the bug Attempting to use the latest Ash 3.0.7 support for where clauses in identities:

# in the identities block
identity :name, [:name] do
  nils_distinct? false
  where expr(is_nil(archived_at))
end

# and in the postgres block
identity_wheres_to_sql name: "archived_at is null"

The migration generated for this, when run throws this error:

** (Postgrex.Error) ERROR 42601 (syntax_error) syntax error at or near "NULLS"
    query: CREATE UNIQUE INDEX "pipeline_defns_name_index" ON "pipeline_defns" (name) NULLS NOT DISTINCT WHERE archived_at is null

Upon removing the nils_distinct? false line and regenerating the migration then running it gives:

** (Postgrex.Error) ERROR 42P07 (duplicate_table) relation "pipeline_defns_name_index" already exists

Indeed, the migration up function just does a create unique_index(...) and does not attempt to alter the index or delete and recreate it.

To Reproduce See above for reproduction.

Expected behavior The SQL for NULLS NOT DISTINCT should be correct, but postgresql doesn't like it. The migration generated should alter or recreate the index, not attempt to create an already-extant index.

Runtime

zachdaniel commented 5 months ago

Fixed in 9f1936aef4b65ac1699187ef366ee1f8a67de175