Skip to content

Commit

Permalink
Merge pull request emqx#11761 from savonarola/1013-refactor-pgsql-con…
Browse files Browse the repository at this point in the history
…nector

chore(source dir): split out postgresql connector
  • Loading branch information
savonarola authored Oct 13, 2023
2 parents 7a6c756 + 4f14e8d commit 57a4e9a
Show file tree
Hide file tree
Showing 25 changed files with 89 additions and 33 deletions.
2 changes: 1 addition & 1 deletion apps/emqx_auth_postgresql/src/emqx_auth_postgresql.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
stdlib,
emqx,
emqx_auth,
emqx_connector
emqx_postgresql
]},
{env, []},
{modules, []},
Expand Down
4 changes: 2 additions & 2 deletions apps/emqx_auth_postgresql/src/emqx_authn_postgresql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ create(Config0) ->
{Config, State} = parse_config(Config0, ResourceId),
{ok, _Data} = emqx_authn_utils:create_resource(
ResourceId,
emqx_connector_pgsql,
emqx_postgresql,
Config
),
{ok, State#{resource_id => ResourceId}}.

update(Config0, #{resource_id := ResourceId} = _State) ->
{Config, NState} = parse_config(Config0, ResourceId),
case emqx_authn_utils:update_resource(emqx_connector_pgsql, Config, ResourceId) of
case emqx_authn_utils:update_resource(emqx_postgresql, Config, ResourceId) of
{error, Reason} ->
error({load_config_error, Reason});
{ok, _} ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fields(postgresql) ->
{query, fun query/1}
] ++
emqx_authn_schema:common_fields() ++
proplists:delete(prepare_statement, emqx_connector_pgsql:fields(config)).
proplists:delete(prepare_statement, emqx_postgresql:fields(config)).

desc(postgresql) ->
?DESC(postgresql);
Expand Down
6 changes: 3 additions & 3 deletions apps/emqx_auth_postgresql/src/emqx_authz_postgresql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ description() ->

create(#{query := SQL0} = Source) ->
{SQL, PlaceHolders} = emqx_authz_utils:parse_sql(SQL0, '$n', ?PLACEHOLDERS),
ResourceID = emqx_authz_utils:make_resource_id(emqx_connector_pgsql),
ResourceID = emqx_authz_utils:make_resource_id(emqx_postgresql),
{ok, _Data} = emqx_authz_utils:create_resource(
ResourceID,
emqx_connector_pgsql,
emqx_postgresql,
Source#{prepare_statement => #{ResourceID => SQL}}
),
Source#{annotations => #{id => ResourceID, placeholders => PlaceHolders}}.
Expand All @@ -62,7 +62,7 @@ update(#{query := SQL0, annotations := #{id := ResourceID}} = Source) ->
{SQL, PlaceHolders} = emqx_authz_utils:parse_sql(SQL0, '$n', ?PLACEHOLDERS),
case
emqx_authz_utils:update_resource(
emqx_connector_pgsql,
emqx_postgresql,
Source#{prepare_statement => #{ResourceID => SQL}}
)
of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type() -> ?AUTHZ_TYPE.

fields(postgresql) ->
emqx_authz_schema:authz_common_fields(?AUTHZ_TYPE) ++
emqx_connector_pgsql:fields(config) ++
emqx_postgresql:fields(config) ++
[{query, query()}].

desc(postgresql) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-compile(nowarn_export_all).
-compile(export_all).

-include_lib("emqx_connector/include/emqx_connector.hrl").
-include_lib("emqx_postgresql/include/emqx_postgresql.hrl").
-include_lib("emqx_auth/include/emqx_authn.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("common_test/include/ct.hrl").
Expand Down Expand Up @@ -64,7 +64,7 @@ init_per_suite(Config) ->
{ok, _} = emqx_resource:create_local(
?PGSQL_RESOURCE,
?AUTHN_RESOURCE_GROUP,
emqx_connector_pgsql,
emqx_postgresql,
pgsql_config(),
#{}
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
-compile(nowarn_export_all).
-compile(export_all).

-include_lib("emqx_connector/include/emqx_connector.hrl").
-include_lib("emqx_postgresql/include/emqx_postgresql.hrl").
-include_lib("emqx_auth/include/emqx_authn.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("common_test/include/ct.hrl").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
-compile(nowarn_export_all).
-compile(export_all).

-include("emqx_connector.hrl").
-include_lib("emqx_postgresql/include/emqx_postgresql.hrl").
-include_lib("emqx_auth/include/emqx_authz.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("common_test/include/ct.hrl").
Expand Down Expand Up @@ -438,7 +438,7 @@ create_pgsql_resource() ->
emqx_resource:create_local(
?PGSQL_RESOURCE,
?AUTHZ_RESOURCE_GROUP,
emqx_connector_pgsql,
emqx_postgresql,
pgsql_config(),
#{}
).
Expand Down
2 changes: 1 addition & 1 deletion apps/emqx_bridge/src/emqx_bridge.app.src
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
%% -*- mode: erlang -*-
{application, emqx_bridge, [
{description, "EMQX bridges"},
{vsn, "0.1.28"},
{vsn, "0.1.29"},
{registered, [emqx_bridge_sup]},
{mod, {emqx_bridge_app, []}},
{applications, [
Expand Down
6 changes: 3 additions & 3 deletions apps/emqx_bridge/src/schema/emqx_bridge_enterprise.erl
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ resource_type(influxdb_api_v2) -> emqx_bridge_influxdb_connector;
resource_type(redis_single) -> emqx_bridge_redis_connector;
resource_type(redis_sentinel) -> emqx_bridge_redis_connector;
resource_type(redis_cluster) -> emqx_bridge_redis_connector;
resource_type(pgsql) -> emqx_connector_pgsql;
resource_type(timescale) -> emqx_connector_pgsql;
resource_type(matrix) -> emqx_connector_pgsql;
resource_type(pgsql) -> emqx_postgresql;
resource_type(timescale) -> emqx_postgresql;
resource_type(matrix) -> emqx_postgresql;
resource_type(tdengine) -> emqx_bridge_tdengine_connector;
resource_type(clickhouse) -> emqx_bridge_clickhouse_connector;
resource_type(dynamo) -> emqx_bridge_dynamo_connector;
Expand Down
3 changes: 2 additions & 1 deletion apps/emqx_bridge_pgsql/rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
{deps, [
{emqx_connector, {path, "../../apps/emqx_connector"}},
{emqx_resource, {path, "../../apps/emqx_resource"}},
{emqx_bridge, {path, "../../apps/emqx_bridge"}}
{emqx_bridge, {path, "../../apps/emqx_bridge"}},
{emqx_postgresql, {path, "../../apps/emqx_postgresql"}}
]}.
5 changes: 3 additions & 2 deletions apps/emqx_bridge_pgsql/src/emqx_bridge_pgsql.app.src
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{application, emqx_bridge_pgsql, [
{description, "EMQX Enterprise PostgreSQL Bridge"},
{vsn, "0.1.3"},
{vsn, "0.1.4"},
{registered, []},
{applications, [
kernel,
stdlib,
emqx_resource
emqx_resource,
emqx_postgresql
]},
{env, []},
{modules, []},
Expand Down
2 changes: 1 addition & 1 deletion apps/emqx_bridge_pgsql/src/emqx_bridge_pgsql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fields("config") ->
#{desc => ?DESC("local_topic"), default => undefined}
)}
] ++ emqx_resource_schema:fields("resource_opts") ++
(emqx_connector_pgsql:fields(config) --
(emqx_postgresql:fields(config) --
emqx_connector_schema_lib:prepare_statement_fields());
fields("post") ->
fields("post", pgsql);
Expand Down
1 change: 0 additions & 1 deletion apps/emqx_connector/include/emqx_connector.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
-define(MYSQL_DEFAULT_PORT, 3306).
-define(MONGO_DEFAULT_PORT, 27017).
-define(REDIS_DEFAULT_PORT, 6379).
-define(PGSQL_DEFAULT_PORT, 5432).
-define(CLICKHOUSE_DEFAULT_PORT, 8123).

-define(AUTO_RECONNECT_INTERVAL, 2).
Expand Down
3 changes: 1 addition & 2 deletions apps/emqx_connector/rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
{deps, [
{emqx, {path, "../emqx"}},
{emqx_utils, {path, "../emqx_utils"}},
{emqx_resource, {path, "../emqx_resource"}},
{epgsql, {git, "https://github.com/emqx/epgsql", {tag, "4.7.0.1"}}}
{emqx_resource, {path, "../emqx_resource"}}
]}.

{shell, [
Expand Down
2 changes: 0 additions & 2 deletions apps/emqx_connector/src/emqx_connector.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
ecpool,
emqx_resource,
eredis_cluster,
eredis,
epgsql,
ehttpc,
jose,
emqx,
Expand Down
1 change: 1 addition & 0 deletions apps/emqx_machine/priv/reboot_lists.eterm
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
emqx_mongodb,
emqx_redis,
emqx_mysql,
emqx_postgresql,
emqx_plugins,
emqx_opentelemetry,
quicer,
Expand Down
14 changes: 14 additions & 0 deletions apps/emqx_postgresql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# PostgreSQL Connector

This application houses the PostgreSQL Database connector.
It provides the APIs to connect to PostgreSQL Databases.

It is used by the PostgreSQL bridge to insert messages and by the `emqx_auth_postgresql` application to check user permissions.

## Contributing

Please see our [contributing.md](../../CONTRIBUTING.md).

## License

See [APL](../../APL.txt).
File renamed without changes.
17 changes: 17 additions & 0 deletions apps/emqx_postgresql/include/emqx_postgresql.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2021-2023 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------

-define(PGSQL_DEFAULT_PORT, 5432).
8 changes: 8 additions & 0 deletions apps/emqx_postgresql/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%% -*- mode: erlang; -*-

{erl_opts, [debug_info]}.
{deps, [
{epgsql, {git, "https://github.com/emqx/epgsql", {tag, "4.7.0.1"}}},
{emqx_connector, {path, "../../apps/emqx_connector"}},
{emqx_resource, {path, "../../apps/emqx_resource"}}
]}.
16 changes: 16 additions & 0 deletions apps/emqx_postgresql/src/emqx_postgresql.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{application, emqx_postgresql, [
{description, "EMQX PostgreSQL Database Connector"},
{vsn, "0.1.0"},
{registered, []},
{applications, [
kernel,
stdlib,
epgsql,
emqx_connector,
emqx_resource
]},
{env, []},
{modules, []},

{links, []}
]}.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(emqx_connector_pgsql).
-module(emqx_postgresql).

-include("emqx_connector.hrl").
-include("emqx_postgresql.hrl").
-include_lib("emqx_connector/include/emqx_connector.hrl").
-include_lib("typerefl/include/types.hrl").
-include_lib("emqx/include/logger.hrl").
-include_lib("hocon/include/hoconsc.hrl").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@
% %% limitations under the License.
% %%--------------------------------------------------------------------

-module(emqx_connector_pgsql_SUITE).
-module(emqx_postgresql_SUITE).

-compile(nowarn_export_all).
-compile(export_all).

-include("emqx_connector.hrl").
-include("emqx_connector/include/emqx_connector.hrl").
-include_lib("emqx_postgresql/include/emqx_postgresql.hrl").
-include_lib("eunit/include/eunit.hrl").
-include_lib("emqx/include/emqx.hrl").
-include_lib("stdlib/include/assert.hrl").

-define(PGSQL_HOST, "pgsql").
-define(PGSQL_RESOURCE_MOD, emqx_connector_pgsql).
-define(PGSQL_RESOURCE_MOD, emqx_postgresql).

all() ->
emqx_common_test_helpers:all(?MODULE).
Expand Down Expand Up @@ -60,7 +61,7 @@ end_per_testcase(_, _Config) ->

t_lifecycle(_Config) ->
perform_lifecycle_check(
<<"emqx_connector_pgsql_SUITE">>,
<<"emqx_postgresql_SUITE">>,
pgsql_config()
).

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
emqx_connector_pgsql {
emqx_postgresql {

server.desc:
"""The IPv4 or IPv6 address or the hostname to connect to.<br/>
Expand Down

0 comments on commit 57a4e9a

Please sign in to comment.