Skip to content

Commit

Permalink
fix: support pg <= 14 in resource generator, and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Sep 26, 2024
1 parent fd7b901 commit c46f896
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 35 deletions.
102 changes: 69 additions & 33 deletions lib/resource_generator/spec.ex
Original file line number Diff line number Diff line change
Expand Up @@ -232,39 +232,75 @@ defmodule AshPostgres.ResourceGenerator.Spec do

defp add_indexes(spec) do
%Postgrex.Result{rows: index_rows} =
spec.repo.query!(
"""
SELECT
i.relname AS index_name,
ix.indisunique AS is_unique,
NOT(ix.indnullsnotdistinct) AS nulls_distinct,
pg_get_expr(ix.indpred, ix.indrelid) AS where_clause,
am.amname AS using_method,
idx.indexdef
FROM
pg_index ix
JOIN
pg_class i ON ix.indexrelid = i.oid
JOIN
pg_class t ON ix.indrelid = t.oid
JOIN
pg_am am ON i.relam = am.oid
LEFT JOIN
pg_constraint c ON c.conindid = ix.indexrelid AND c.contype = 'p'
JOIN
pg_indexes idx ON idx.indexname = i.relname AND idx.schemaname = 'public' -- Adjust schema name if necessary
JOIN information_schema.tables ta
ON ta.table_name = t.relname
WHERE
t.relname = $1
AND ta.table_schema = $2
AND c.conindid IS NULL
GROUP BY
i.relname, ix.indisunique, ix.indnullsnotdistinct, pg_get_expr(ix.indpred, ix.indrelid), am.amname, idx.indexdef;
""",
[spec.table_name, spec.schema],
log: false
)
if Version.match?(spec.repo.min_pg_version(), ">= 15.0") do
spec.repo.query!(
"""
SELECT
i.relname AS index_name,
ix.indisunique AS is_unique,
NOT(ix.indnullsnotdistinct) AS nulls_distinct,
pg_get_expr(ix.indpred, ix.indrelid) AS where_clause,
am.amname AS using_method,
idx.indexdef
FROM
pg_index ix
JOIN
pg_class i ON ix.indexrelid = i.oid
JOIN
pg_class t ON ix.indrelid = t.oid
JOIN
pg_am am ON i.relam = am.oid
LEFT JOIN
pg_constraint c ON c.conindid = ix.indexrelid AND c.contype = 'p'
JOIN
pg_indexes idx ON idx.indexname = i.relname AND idx.schemaname = 'public' -- Adjust schema name if necessary
JOIN information_schema.tables ta
ON ta.table_name = t.relname
WHERE
t.relname = $1
AND ta.table_schema = $2
AND c.conindid IS NULL
GROUP BY
i.relname, ix.indisunique, ix.indnullsnotdistinct, pg_get_expr(ix.indpred, ix.indrelid), am.amname, idx.indexdef;
""",
[spec.table_name, spec.schema],
log: false
)
else
spec.repo.query!(
"""
SELECT
i.relname AS index_name,
ix.indisunique AS is_unique,
TRUE AS nulls_distinct,
pg_get_expr(ix.indpred, ix.indrelid) AS where_clause,
am.amname AS using_method,
idx.indexdef
FROM
pg_index ix
JOIN
pg_class i ON ix.indexrelid = i.oid
JOIN
pg_class t ON ix.indrelid = t.oid
JOIN
pg_am am ON i.relam = am.oid
LEFT JOIN
pg_constraint c ON c.conindid = ix.indexrelid AND c.contype = 'p'
JOIN
pg_indexes idx ON idx.indexname = i.relname AND idx.schemaname = 'public' -- Adjust schema name if necessary
JOIN information_schema.tables ta
ON ta.table_name = t.relname
WHERE
t.relname = $1
AND ta.table_schema = $2
AND c.conindid IS NULL
GROUP BY
i.relname, ix.indisunique, pg_get_expr(ix.indpred, ix.indrelid), am.amname, idx.indexdef;
""",
[spec.table_name, spec.schema],
log: false
)
end

%{
spec
Expand Down
2 changes: 2 additions & 0 deletions test/aggregate_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ defmodule AshSql.AggregateTest do
|> Ash.read_one!()
end

@tag :postgres_16
test "returns nil values if `include_nil?` is set to `true`" do
post =
Post
Expand Down Expand Up @@ -616,6 +617,7 @@ defmodule AshSql.AggregateTest do
|> Ash.read_one!()
end

@tag :postgres_16
test "it returns `nil` values when `include_nil?` is `true`" do
post =
Post
Expand Down
23 changes: 21 additions & 2 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# ExUnit.start(capture_log: true)
ExUnit.configure(stacktrace_depth: 100)
ExUnit.start(capture_log: true)

exclude_tags =
case System.get_env("PG_VERSION") do
"13" ->
[:postgres_14, :postgres_15, :postgres_16]

"14" ->
[:postgres_15, :postgres_16]

"15" ->
[:postgres_16]

"16" ->
[]

_ ->
[]
end

ExUnit.configure(stacktrace_depth: 100, exclude: exclude_tags)

AshPostgres.TestRepo.start_link()
AshPostgres.TestNoSandboxRepo.start_link()

0 comments on commit c46f896

Please sign in to comment.