From c46f896600f5ffb0fd003da75a7d38d705fe98c6 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Thu, 26 Sep 2024 13:13:00 -0400 Subject: [PATCH] fix: support pg <= 14 in resource generator, and update tests --- lib/resource_generator/spec.ex | 102 ++++++++++++++++++++++----------- test/aggregate_test.exs | 2 + test/test_helper.exs | 23 +++++++- 3 files changed, 92 insertions(+), 35 deletions(-) diff --git a/lib/resource_generator/spec.ex b/lib/resource_generator/spec.ex index fffaf97e..8114a780 100644 --- a/lib/resource_generator/spec.ex +++ b/lib/resource_generator/spec.ex @@ -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 diff --git a/test/aggregate_test.exs b/test/aggregate_test.exs index 630aeaac..257f2f26 100644 --- a/test/aggregate_test.exs +++ b/test/aggregate_test.exs @@ -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 @@ -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 diff --git a/test/test_helper.exs b/test/test_helper.exs index 10ae3340..5b02474b 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -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()