diff --git a/README.md b/README.md index 58acbcc..e2c38a5 100644 --- a/README.md +++ b/README.md @@ -26,10 +26,10 @@ To try `pgo` simply modify `config/example.config` by replacing the `host`, `dat ```erlang [ - {pgo, [{pools, [{default, #{pool_size => 10, - host => "127.0.0.1", - database => "test", - user => "test"}}]}]} + {pgo, [{pools, [{pgo_default, #{pool_size => 10, + host => "127.0.0.1", + database => "test", + user => "test"}}]}]} ]. ``` @@ -38,7 +38,7 @@ To try `pgo` simply modify `config/example.config` by replacing the `host`, `dat ``` erlang > application:ensure_all_started(pgo). {ok,[backoff,opentelemetry_api,pg_types,pgo]} -> pgo:start_pool(default, #{pool_size => 5, host => "127.0.0.1", database => "test", user => "test"}). +> pgo:start_pool(pgo_default, #{pool_size => 5, host => "127.0.0.1", database => "test", user => "test"}). ``` Or start a pool as a child of your application's supervisor: @@ -104,7 +104,7 @@ decode_option() :: return_rows_as_maps | {return_rows_as_maps, boolean()} | ### Query Options -* `pool` (default: `default`): Name of the pool to use for checking out a connection to the database. +* `pool` (default: `pgo_default`): Name of the pool to use for checking out a connection to the database. * `return_rows_as_maps` (default: `false`): When `true` each row is returned as a map of column name to value instead of a list of values. * `column_name_as_atom` (default: `false`): If `true` converts each column name in the result to an atom. * `decode_fun` (default: `undefined`): Optional function for performing transformations on each row in a result. It must be a 2-arity function returning a list or map for the row and takes the row (as a list or map) and a list of `#row_description_field{}` records. diff --git a/src/pgo.erl b/src/pgo.erl index d08a259..1585582 100644 --- a/src/pgo.erl +++ b/src/pgo.erl @@ -98,7 +98,7 @@ query(Query, Params) -> query(Query, Params, Options) -> case get(pgo_transaction_connection) of undefined -> - Pool = maps:get(pool, Options, default), + Pool = maps:get(pool, Options, pgo_default), PoolOptions = maps:get(pool_options, Options, []), case checkout(Pool, PoolOptions) of {ok, Ref, Conn} -> @@ -149,7 +149,7 @@ transaction(Fun) -> %% @equiv transaction(default, Fun, Options) -spec transaction(fun(() -> any()), options()) -> any() | {error, any()}. transaction(Fun, Options) when is_function(Fun) -> - Pool = maps:get(pool, Options, default), + Pool = maps:get(pool, Options, pgo_default), transaction(Pool, Fun, Options). %% @doc Runs a function, passing it a connection, in a SQL transaction. @@ -234,3 +234,4 @@ format_error(Error=#{module := Module}) -> Module:format_error(Error); format_error(Error) -> io_lib:format("Unknown error: ~p", [Error]). + diff --git a/test/pgo_basic_SUITE.erl b/test/pgo_basic_SUITE.erl index e089ac8..5f60b82 100644 --- a/test/pgo_basic_SUITE.erl +++ b/test/pgo_basic_SUITE.erl @@ -37,51 +37,51 @@ end_per_suite(_Config) -> init_per_group(clear, Config) -> application:ensure_all_started(pgo), - {ok, _} = pgo_sup:start_child(default, #{pool_size => 1, - port => 5432, - database => "test", - user => "test", - password => "password"}), + {ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1, + port => 5432, + database => "test", + user => "test", + password => "password"}), Config; init_per_group(ssl, Config) -> application:ensure_all_started(pgo), - {ok, _} = pgo_sup:start_child(default, #{pool_size => 1, - port => 5434, - ssl => true, - ssl_options => [{verify_type, verify_peer}, - {cacertfile, "../../../../test/certs/server.crt"}, - {verify_fun, {fun (_, valid, U) -> - {valid, U}; - (_, valid_peer, U) -> - {valid, U}; - (_, {bad_cert, selfsigned_peer}, U) -> - {valid, U}; - (_, Reason, _U) -> - {fail, Reason} - end, []}} - ], - database => "test", - user => "test", - password => "password"}), + {ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1, + port => 5434, + ssl => true, + ssl_options => [{verify_type, verify_peer}, + {cacertfile, "../../../../test/certs/server.crt"}, + {verify_fun, {fun (_, valid, U) -> + {valid, U}; + (_, valid_peer, U) -> + {valid, U}; + (_, {bad_cert, selfsigned_peer}, U) -> + {valid, U}; + (_, Reason, _U) -> + {fail, Reason} + end, []}} + ], + database => "test", + user => "test", + password => "password"}), Config; init_per_group(domain_socket, Config) -> application:ensure_all_started(pgo), - {ok, _} = pgo_sup:start_child(default, #{pool_size => 1, - port => 5432, - host => "/tmp/postgresql", - database => "test", - user => "test", - password => "password"}), + {ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1, + port => 5432, + host => "/tmp/postgresql", + database => "test", + user => "test", + password => "password"}), Config. end_per_group(Group, _Config) when Group =:= ssl ; Group =:= domain_socket -> application:stop(pgo), - pgo_test_utils:clear_types(default), + pgo_test_utils:clear_types(pgo_default), ok; end_per_group(_, _Config) -> pgo:query("drop table tmp"), @@ -94,7 +94,7 @@ end_per_group(_, _Config) -> application:stop(pgo), - pgo_test_utils:clear_types(default), + pgo_test_utils:clear_types(pgo_default), ok. @@ -281,6 +281,8 @@ types(_Config) -> [10, 42, 1099511627776, <<"And in the end, the love you take is equal to the love you make">>, ?BIN_UUID, <<"deadbeef">>, 3.1415])), + ?assertMatch(#{command := insert}, pgo:query("insert into types (id, an_integer, a_bigint, a_text, a_uuid, a_bytea, a_real) values ($1, $2, $3, $4, $5, $6, $7)", [101, null, null, null, null, "deadbeef", null])), + R = pgo:query("select * from types where id = 10"), ?assertMatch(#{command := select, rows := [_Row]}, R), #{command := select, rows := [Row]} = R, @@ -423,7 +425,8 @@ bit_string(_Config) -> ?assertMatch(#{rows := [{<<1:1,1:1,0:1>>}]}, pgo:query("SELECT bit '110' :: varbit", [])). arrays(_Config) -> - ?assertMatch(#{rows := [{[<<"s1">>,null]}]}, pgo:query("SELECT $1::text[]", [[<<"s1">>, null]])). + ?assertMatch(#{rows := [{[<<"s1">>,null]}]}, pgo:query("SELECT $1::text[]", [[<<"s1">>, null]])), + ?assertMatch(#{rows := [{[{array,[{array,[<<"s1">>,null]}]}]}]}, pgo:query("SELECT $1::text[][][]", [[{array, [{array, [<<"s1">>, null]}]}]])). tsvector(_Config) -> ?assertMatch(#{rows := [{[{<<"fat">>,[{2,null}]},{<<"rat">>,[{3,null}]}]}]}, @@ -440,7 +443,7 @@ tsvector(_Config) -> {<<"fat">>,[{2,'B'},{4,'C'}]}]])). query_arity_4(_Config) -> - {ok, Ref, Conn} = pgo:checkout(default), + {ok, Ref, Conn} = pgo:checkout(pgo_default), ?assertMatch(#{rows := [{1}]}, pgo:query("select 1", [], #{}, Conn)), diff --git a/test/pgo_datetime_SUITE.erl b/test/pgo_datetime_SUITE.erl index f49c01e..2c203cb 100644 --- a/test/pgo_datetime_SUITE.erl +++ b/test/pgo_datetime_SUITE.erl @@ -26,7 +26,7 @@ init_per_group(erl_datetime, Config) -> {ok, _} = application:ensure_all_started(pgo), - {ok, _} = pgo_sup:start_child(default, #{pool_size => 1, + {ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1, database => "test", user => "test", password => "password"}), @@ -37,7 +37,7 @@ init_per_group(as_micro, Config) -> application:ensure_all_started(pgo), - {ok, _} = pgo_sup:start_child(default, #{pool_size => 1, + {ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1, database => "test", user => "test", password => "password"}), @@ -48,10 +48,10 @@ init_per_group(as_integer, Config) -> application:ensure_all_started(pgo), - {ok, _} = pgo_sup:start_child(default, #{pool_size => 1, - database => "test", - user => "test", - password => "password"}), + {ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1, + database => "test", + user => "test", + password => "password"}), Config; init_per_group(as_float, Config) -> application:load(pg_types), @@ -59,10 +59,10 @@ init_per_group(as_float, Config) -> application:ensure_all_started(pgo), - {ok, _} = pgo_sup:start_child(default, #{pool_size => 1, - database => "test", - user => "test", - password => "password"}), + {ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1, + database => "test", + user => "test", + password => "password"}), Config. end_per_group(_, _Config) -> @@ -70,7 +70,7 @@ end_per_group(_, _Config) -> application:stop(pgo), application:unload(pg_types), - pgo_test_utils:clear_types(default), + pgo_test_utils:clear_types(pgo_default), ok. diff --git a/test/pgo_enum_SUITE.erl b/test/pgo_enum_SUITE.erl index 40413b0..d562607 100644 --- a/test/pgo_enum_SUITE.erl +++ b/test/pgo_enum_SUITE.erl @@ -10,10 +10,10 @@ all() -> [select, insert]. init_per_suite(Config) -> application:ensure_all_started(pgo), - {ok, _} = pgo_sup:start_child(default, #{pool_size => 1, - database => "test", - user => "test", - password => "password"}), + {ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1, + database => "test", + user => "test", + password => "password"}), ?assertMatch(#{command := create}, pgo:query("CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy')")), @@ -25,7 +25,7 @@ end_per_suite(_Config) -> application:stop(pgo), - pgo_test_utils:clear_types(default), + pgo_test_utils:clear_types(pgo_default), ok. diff --git a/test/pgo_geometric_SUITE.erl b/test/pgo_geometric_SUITE.erl index 5c62604..c03a58b 100644 --- a/test/pgo_geometric_SUITE.erl +++ b/test/pgo_geometric_SUITE.erl @@ -10,16 +10,16 @@ all() -> [select, insert]. init_per_suite(Config) -> application:ensure_all_started(pgo), - {ok, _} = pgo_sup:start_child(default, #{pool_size => 1, - database => "test", - user => "test", - password => "password"}), + {ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1, + database => "test", + user => "test", + password => "password"}), Config. end_per_suite(_Config) -> application:stop(pgo), - pgo_test_utils:clear_types(default), + pgo_test_utils:clear_types(pgo_default), ok. select(_Config) -> diff --git a/test/pgo_query_opts_SUITE.erl b/test/pgo_query_opts_SUITE.erl index 732129f..8a99f7e 100644 --- a/test/pgo_query_opts_SUITE.erl +++ b/test/pgo_query_opts_SUITE.erl @@ -11,11 +11,11 @@ all() -> init_per_suite(Config) -> application:ensure_all_started(pgo), - {ok, _} = pgo_sup:start_child(default, #{database => "test", - user => "test", - password => "password", - pool_size => 1, - decode_opts => [return_rows_as_maps]}), + {ok, _} = pgo_sup:start_child(pgo_default, #{database => "test", + user => "test", + password => "password", + pool_size => 1, + decode_opts => [return_rows_as_maps]}), Config. @@ -27,7 +27,7 @@ end_per_suite(_Config) -> application:stop(pgo), - pgo_test_utils:clear_types(default), + pgo_test_utils:clear_types(pgo_default), ok. diff --git a/test/pgo_trace_SUITE.erl b/test/pgo_trace_SUITE.erl index b87046f..d5e4885 100644 --- a/test/pgo_trace_SUITE.erl +++ b/test/pgo_trace_SUITE.erl @@ -12,12 +12,12 @@ all() -> init_per_suite(Config) -> application:ensure_all_started(pgo), application:ensure_all_started(tls_certificate_check), - {ok, _} = pgo_sup:start_child(default, #{pool_size => 1, - port => 5432, - database => "test", - user => "test", - password => "password", - trace => true}), + {ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1, + port => 5432, + database => "test", + user => "test", + password => "password", + trace => true}), _ = application:load(opentelemetry), application:set_env(opentelemetry, processors, [{otel_simple_processor, #{}}]), {ok, _} = application:ensure_all_started(opentelemetry),