Skip to content

Commit 226cc58

Browse files
iwpndARolek
authored andcommitted
refactor: deprecate non-uri connection
docs: update ci: update fix: remove default options chore: resolve issues docs: update mvt provider readme too
1 parent df62604 commit 226cc58

File tree

7 files changed

+90
-312
lines changed

7 files changed

+90
-312
lines changed

.github/workflows/on_pr_push.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,8 @@ jobs:
7979

8080
# PostGIS tests
8181
RUN_POSTGIS_TESTS: yes
82-
PGHOST: localhost
83-
PGPORT: 5432
84-
PGDATABASE: tegola
85-
PGUSER: postgres
86-
PGPASSWORD: postgres
87-
PGUSER_NO_ACCESS: tegola_no_access
82+
PGURI: postgres://postgres:postgres@localhost:5432/tegola
83+
PGURI_NO_ACCESS: postgres://tegola_no_access:postgres@localhost:5432/tegola
8884
PGSSLMODE: disable
8985
PGSSLKEY: ""
9086
PGSSLCERT: ""

mvtprovider/postgis/README.md

Lines changed: 55 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,48 @@
11
# PostGIS MVT Provider
22

3-
The PostGIS MVT provider manages querying for tile requests against a Postgres database (version 12+) with the [PostGIS](http://postgis.net/)(version 3.0+) extension installed and leverages [ST_AsMVT](https://postgis.net/docs/ST_AsMVT.html) to handle the MVT encoding at the database.
3+
The PostGIS MVT provider manages querying for tile requests against a Postgres database (version 12+) with the [PostGIS](http://postgis.net/)(version 3.0+) extension installed and leverages [ST_AsMVT](https://postgis.net/docs/ST_AsMVT.html) to handle the MVT encoding at the database.
44

55
The connection between tegola and PostGIS is configured in a `tegola.toml` file. An example minimum connection config:
66

7-
87
```toml
98
[[providers]]
109
name = "test_postgis" # provider name is referenced from map layers (required)
1110
type = "mvt_postgis" # the type of data provider must be "mvt_postgis" for this data provider (required)
12-
host = "localhost" # PostGIS database host (required)
13-
port = 5432 # PostGIS database port (required)
14-
database = "tegola" # PostGIS database name (required)
15-
user = "tegola" # PostGIS database user (required)
16-
password = "" # PostGIS database password (required)
11+
uri = "postgres://postgres:postgres@localhost:5432/tegola" # PostGIS database uri (required)
12+
```
13+
14+
## Connection Properties
15+
16+
Establishing a connection via connection string (`uri`) will become the default
17+
connection method as of v0.16.0. Connecting via host/port/database is deprecated.
18+
19+
- `name` (string): [Required] provider name is referenced from map layers.
20+
- `type` (string): [Required] the type of data provider. must be "postgis" to use this data provider
21+
- `uri` (string): [Required] PostGIS database uri (postgres://user:password@host:port/database)
22+
- `srid` (int): [Optional] The default SRID for the provider. Defaults to WebMercator (3857) but also supports WGS84 (4326)
23+
24+
### Connection string properties
25+
26+
#### Example
27+
1728
```
29+
# {protocol}://{user}:{password}@{host}:{port}/{database}?{options}=
30+
postgres://tegola:supersecret@localhost:5432/tegola?sslmode=prefer&pool_max_conns=10
31+
```
32+
33+
#### Options
1834

19-
### Connection Properties
35+
Tegola uses [pgx](https://github.com/jackc/pgx/blob/master/pgxpool/pool.go#L111) to manage
36+
PostgresSQL connections that allows the following configurations to be passed
37+
as parameters.
2038

21-
- `name` (string): [Required] provider name is referenced from map layers.
22-
- `type` (string): [Required] the type of data provider. must be "postgis" to use this data provider
23-
- `host` (string): [Required] PostGIS database host
24-
- `port` (int): [Required] PostGIS database port (required)
25-
- `database` (string): [Required] PostGIS database name
26-
- `user` (string): [Required] PostGIS database user
27-
- `password` (string): [Required] PostGIS database password
28-
- `max_connections` (int): [Optional] The max connections to maintain in the connection pool. Defaults to 100. 0 means no max.
29-
- `srid` (int): [Optional] The default SRID for the provider. Defaults to WebMercator (3857) but also supports WGS84 (4326)
39+
- `sslmode`: [Optional] PostGIS SSL mode. Default: "prefer"
40+
- `pool_min_conns`: [Optional] The min connections to maintain in the connection pool. Defaults to 100. 0 means no max.
41+
- `pool_max_conns`: [Optional] The max connections to maintain in the connection pool. Defaults to 100. 0 means no max.
42+
- `pool_max_conn_idle_time`: [Optional] The maximum time an idle connection is kept alive. Defaults to "30m".
43+
- `pool_max_connection_lifetime` [Optional] The maximum time a connection lives before it is terminated and recreated. Defaults to "1h".
44+
- `pool_max_conn_lifetime_jitter` [Optional] Duration after `max_conn_lifetime` to randomly decide to close a connection.
45+
- `pool_health_check_period` [Optional] Is the duration between checks of the health of idle connections. Defaults to 1m
3046

3147
## Provider Layers
3248

@@ -37,41 +53,37 @@ In addition to the connection configuration above, Provider Layers need to be co
3753
name = "landuse"
3854
# MVT data provider must use SQL statements
3955
# this table uses "geom" for the geometry_fieldname and "gid" for the id_fieldname so they don't need to be configured
40-
# Wrapping the geom with ST_AsMVTGeom is required.
56+
# Wrapping the geom with ST_AsMVTGeom is required.
4157
sql = "SELECT ST_AsMVTGeom(geom,!BBOX!) AS geom, gid FROM gis.landuse WHERE geom && !BBOX!"
4258
```
4359

4460
### Provider Layers Properties
4561

46-
- `name` (string): [Required] the name of the layer. This is used to reference this layer from map layers.
47-
- `geometry_fieldname` (string): [Optional] the name of the filed which contains the geometry for the feature. defaults to `geom`.
48-
- `id_fieldname` (string): [Optional] the name of the feature id field. defaults to `gid`.
49-
- `geometry_type` (string): [Optional] the layer geometry type. If not set, the table will be inspected at startup to try and infer the gemetry type. Valid values are: `Point`, `LineString`, `Polygon`, `MultiPoint`, `MultiLineString`, `MultiPolygon`, `GeometryCollection`.
50-
- `srid` (int): [Optional] the SRID of the layer. Supports `3857` (WebMercator) or `4326` (WGS84).
51-
- `sql` (string): [Required] custom SQL to use use. Supports the following tokens:
52-
- `!BBOX!` - [Required] will be replaced with the bounding box of the tile before the query is sent to the database. `!bbox!` and`!BOX!` are supported as well for compatibilitiy with queries from Mapnik and MapServer styles.
53-
- `!X!` - [Optional] will replaced with the "X" value of the requested tile.
54-
- `!Y!` - [Optional] will replaced with the "Y" value of the requested tile.
55-
- `!Z!` - [Optional] will replaced with the "Z" value of the requested tile.
56-
- `!ZOOM!` - [Optional] will be replaced with the "Z" (zoom) value of the requested tile.
57-
- `!SCALE_DENOMINATOR!` - [Optional] scale denominator, assuming 90.7 DPI (i.e. 0.28mm pixel size)
58-
- `!PIXEL_WIDTH!` - [Optional] the pixel width in meters, assuming 256x256 tiles
59-
- `!PIXEL_HEIGHT!` - [Optional] the pixel height in meters, assuming 256x256 tiles
60-
- `!ID_FIELD!` - [Optional] the id field name
61-
- `!GEOM_FIELD!` - [Optional] the geom field name
62-
- `!GEOM_TYPE!` - [Optional] the geom type if defined otherwise ""
62+
- `name` (string): [Required] the name of the layer. This is used to reference this layer from map layers.
63+
- `geometry_fieldname` (string): [Optional] the name of the filed which contains the geometry for the feature. defaults to `geom`.
64+
- `id_fieldname` (string): [Optional] the name of the feature id field. defaults to `gid`.
65+
- `geometry_type` (string): [Optional] the layer geometry type. If not set, the table will be inspected at startup to try and infer the gemetry type. Valid values are: `Point`, `LineString`, `Polygon`, `MultiPoint`, `MultiLineString`, `MultiPolygon`, `GeometryCollection`.
66+
- `srid` (int): [Optional] the SRID of the layer. Supports `3857` (WebMercator) or `4326` (WGS84).
67+
- `sql` (string): [Required] custom SQL to use use. Supports the following tokens:
68+
- `!BBOX!` - [Required] will be replaced with the bounding box of the tile before the query is sent to the database. `!bbox!` and`!BOX!` are supported as well for compatibilitiy with queries from Mapnik and MapServer styles.
69+
- `!X!` - [Optional] will replaced with the "X" value of the requested tile.
70+
- `!Y!` - [Optional] will replaced with the "Y" value of the requested tile.
71+
- `!Z!` - [Optional] will replaced with the "Z" value of the requested tile.
72+
- `!ZOOM!` - [Optional] will be replaced with the "Z" (zoom) value of the requested tile.
73+
- `!SCALE_DENOMINATOR!` - [Optional] scale denominator, assuming 90.7 DPI (i.e. 0.28mm pixel size)
74+
- `!PIXEL_WIDTH!` - [Optional] the pixel width in meters, assuming 256x256 tiles
75+
- `!PIXEL_HEIGHT!` - [Optional] the pixel height in meters, assuming 256x256 tiles
76+
- `!ID_FIELD!` - [Optional] the id field name
77+
- `!GEOM_FIELD!` - [Optional] the geom field name
78+
- `!GEOM_TYPE!` - [Optional] the geom type if defined otherwise ""
6379

6480
## Example mvt_postgis and map config
6581

6682
```toml
6783
[[providers]]
6884
name = "test_postgis"
6985
type = "mvt_postgis"
70-
host = "localhost"
71-
port = 5432
72-
database = "tegola"
73-
user = "tegola"
74-
password = ""
86+
uri = "postgres://postgres:postgres@localhost:5432/tegola"
7587

7688
[[providers.layers]]
7789
name = "landuse"
@@ -96,11 +108,7 @@ When using a 4326 projection with ST_AsMVT the SQL statement needs to be modifie
96108
[[providers]]
97109
name = "test_postgis"
98110
type = "mvt_postgis"
99-
host = "localhost"
100-
port = 5432
101-
database = "tegola"
102-
user = "tegola"
103-
password = ""
111+
uri = "postgres://postgres:postgres@localhost:5432/tegola"
104112
srid = 4326 # setting the srid on the provider to 4326 will cause the !BBOX! value to use the 4326 projection.
105113

106114
[[providers.layers]]
@@ -127,11 +135,8 @@ Testing is designed to work against a live PostGIS database. To see how to set u
127135

128136
```bash
129137
$ export RUN_POSTGIS_TESTS=yes
130-
$ export PGHOST="localhost"
131-
$ export PGPORT=5432
132-
$ export PGDATABASE="tegola"
133-
$ export PGUSER="postgres"
134-
$ export PGUSER_NO_ACCESS="tegola_no_access" # used for testing errors when user does not have read permissions on a table
138+
$ export PGURI="postgres://postgres:postgres@localhost:5432/tegola"
139+
$ export PGURI_NO_ACCESS="postgres://tegola_no_access:postgres@localhost:5432/tegola" # used for testing errors when user does not have read permissions on a table
135140
$ export PGPASSWORD=""
136141
$ export PGSSLMODE="disable"
137142
```

provider/postgis/README.md

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,12 @@ application_name = "tegola"
2828
# The default is OFF (read/write).
2929
# (optional)
3030
default_transaction_read_only = "off"
31-
32-
host = "localhost" # PostGIS database host (deprecated)
33-
port = 5432 # PostGIS database port (deprecated)
34-
database = "tegola" # PostGIS database name (deprecated)
35-
user = "tegola" # PostGIS database user (deprecated)
36-
password = "supersecret" # PostGIS database password (deprecated)
37-
max_connections = 10 # PostGIS max connections (deprecated)
38-
max_connection_idle_time = "30m" # PostGIS max connection idle time (deprecated)
39-
max_connection_lifetime = "1h" # PostGIS max connection life time (deprecated)
4031
```
4132

4233
## Connection Properties
4334

4435
Establishing a connection via connection string (`uri`) will become the default
45-
connection method as of v0.16.0. Connecting via host/port/database is flagged
46-
for deprecation as of v0.15.0 but will be possible until v0.16.0 still.
36+
connection method as of v0.16.0. Connecting via host/port/database is deprecated.
4737

4838
- `uri` (string): [Required] PostGIS connection string
4939
- `name` (string): [Required] provider name is referenced from map layers
@@ -61,26 +51,17 @@ postgres://tegola:supersecret@localhost:5432/tegola?sslmode=prefer&pool_max_conn
6151

6252
#### Options
6353

54+
Tegola uses [pgx](https://github.com/jackc/pgx/blob/master/pgxpool/pool.go#L111) to manage
55+
PostgresSQL connections that allows the following configurations to be passed
56+
as parameters.
57+
6458
- `sslmode`: [Optional] PostGIS SSL mode. Default: "prefer"
59+
- `pool_min_conns`: [Optional] The min connections to maintain in the connection pool. Defaults to 100. 0 means no max.
6560
- `pool_max_conns`: [Optional] The max connections to maintain in the connection pool. Defaults to 100. 0 means no max.
6661
- `pool_max_conn_idle_time`: [Optional] The maximum time an idle connection is kept alive. Defaults to "30m".
67-
- `max_connection_lifetime` [Optional] The maximum time a connection lives before it is terminated and recreated. Defaults to "1h".
68-
69-
### [DEPRECATED] Connection Properties
70-
71-
- `uri` (string): [Required] PostGIS connection string
72-
- `name` (string): [Required] provider name is referenced from map layers
73-
- `type` (string): [Required] the type of data provider. must be "postgis" to use this data provider
74-
- `host` (string): [deprecated] PostGIS database host
75-
- `port` (int): [deprecated] PostGIS database port (required)
76-
- `database` (string): [deprecated] PostGIS database name
77-
- `user` (string): [deprecated] PostGIS database user
78-
- `password` (string): [deprecated] PostGIS database password
79-
- `srid` (int): [Optional] The default SRID for the provider. Defaults to WebMercator (3857) but also supports WGS84 (4326)
80-
- `ssl_mode`: (string): [Optional]. PostGIS SSL mode. Default is "prefer".
81-
- `max_connections` (int): [deprecated] The max connections to maintain in the connection pool. Defaults to 100. 0 means no max.
82-
- `max_connection_idle_time` (duration string): [deprecated] The maximum time an idle connection is kept alive.
83-
- `max_connection_lifetime` (duration string): [deprecated] The maximum time a connection lives before it is terminated and recreated.
62+
- `pool_max_connection_lifetime` [Optional] The maximum time a connection lives before it is terminated and recreated. Defaults to "1h".
63+
- `pool_max_conn_lifetime_jitter` [Optional] Duration after `max_conn_lifetime` to randomly decide to close a connection.
64+
- `pool_health_check_period` [Optional] Is the duration between checks of the health of idle connections. Defaults to 1m
8465

8566
## Provider Layers
8667

@@ -151,11 +132,8 @@ To run the PostGIS tests, the following environment variables need to be set:
151132

152133
```bash
153134
$ export RUN_POSTGIS_TESTS=yes
154-
$ export PGHOST="localhost"
155-
$ export PGPORT=5432
156-
$ export PGDATABASE="tegola"
157-
$ export PGUSER="postgres"
158-
$ export PGUSER_NO_ACCESS="tegola_no_access" # used for testing errors when user does not have read permissions on a table
135+
$ export PGURI="postgres://postgres:postgres@localhost:5432/tegola"
136+
$ export PGURI_NO_ACCESS="postgres://tegola_no_access:@localhost:5432/tegola" # used for testing errors when user does not have read permissions on a table
159137
$ export PGPASSWORD=""
160138
$ export PGSSLMODE="disable"
161139
```

provider/postgis/postgis.go

Lines changed: 10 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,7 @@ const (
175175
)
176176

177177
const (
178-
DefaultURI = ""
179-
DefaultPort = 5432
180178
DefaultSRID = tegola.WebMercator
181-
DefaultMaxConn = 100
182-
DefaultMaxConnIdleTime = "30m"
183-
DefaultMaxConnLifetime = "1h"
184179
DefaultSSLMode = "prefer"
185180
DefaultSSLKey = ""
186181
DefaultSSLCert = ""
@@ -191,18 +186,10 @@ const (
191186
const (
192187
ConfigKeyName = "name"
193188
ConfigKeyURI = "uri"
194-
ConfigKeyHost = "host"
195-
ConfigKeyPort = "port"
196-
ConfigKeyDB = "database"
197-
ConfigKeyUser = "user"
198-
ConfigKeyPassword = "password"
199189
ConfigKeySSLMode = "ssl_mode"
200190
ConfigKeySSLKey = "ssl_key"
201191
ConfigKeySSLCert = "ssl_cert"
202192
ConfigKeySSLRootCert = "ssl_root_cert"
203-
ConfigKeyMaxConn = "max_connections"
204-
ConfigKeyMaxConnIdleTime = "max_connection_idle_time"
205-
ConfigKeyMaxConnLifetime = "max_connection_lifetime"
206193
ConfigKeySRID = "srid"
207194
ConfigKeyLayers = "layers"
208195
ConfigKeyLayerName = "name"
@@ -262,7 +249,6 @@ func validateURI(u string) error {
262249
return nil
263250
}
264251

265-
// TODO: (iwpnd) to be removed/refactored in v0.17.0
266252
// BuildURI creates a database URI from config
267253
func BuildURI(config dict.Dicter) (*url.URL, *url.Values, error) {
268254

@@ -272,97 +258,33 @@ func BuildURI(config dict.Dicter) (*url.URL, *url.Values, error) {
272258
return nil, nil, err
273259
}
274260

275-
uri := DefaultURI
276-
uri, err = config.String(ConfigKeyURI, &uri)
261+
uri, err := config.String(ConfigKeyURI, nil)
277262
if err != nil {
278263
return nil, nil, err
279264
}
280265

281-
// if uri is set in the config, we add sslmode and return early
282-
if uri != "" {
283-
if err := validateURI(uri); err != nil {
284-
return nil, nil, err
285-
}
286-
287-
parsedUri, err := url.Parse(uri)
288-
if err != nil {
289-
return nil, nil, err
290-
}
291-
292-
// parse query to make sure sslmode is attached
293-
parsedQuery, err := url.ParseQuery(parsedUri.RawQuery)
294-
if err != nil {
295-
return &url.URL{}, nil, err
296-
}
297-
298-
if ok := parsedQuery.Get("sslmode"); ok == "" {
299-
parsedQuery.Add("sslmode", sslmode)
300-
}
301-
302-
parsedUri.RawQuery = parsedQuery.Encode()
303-
304-
return parsedUri, &parsedQuery, nil
305-
}
306-
307-
host, err := config.String(ConfigKeyHost, nil)
308-
if err != nil {
309-
return nil, nil, err
310-
}
311-
312-
port := DefaultPort
313-
if port, err = config.Int(ConfigKeyPort, &port); err != nil {
266+
if err := validateURI(uri); err != nil {
314267
return nil, nil, err
315268
}
316269

317-
db, err := config.String(ConfigKeyDB, nil)
270+
parsedUri, err := url.Parse(uri)
318271
if err != nil {
319272
return nil, nil, err
320273
}
321274

322-
user, err := config.String(ConfigKeyUser, nil)
275+
// parse query to make sure sslmode is attached
276+
parsedQuery, err := url.ParseQuery(parsedUri.RawQuery)
323277
if err != nil {
324-
return nil, nil, err
325-
}
326-
327-
password, err := config.String(ConfigKeyPassword, nil)
328-
if err != nil {
329-
return nil, nil, err
330-
}
331-
332-
maxcon := DefaultMaxConn
333-
if maxcon, err = config.Int(ConfigKeyMaxConn, &maxcon); err != nil {
334-
return nil, nil, err
278+
return &url.URL{}, nil, err
335279
}
336280

337-
idletime := DefaultMaxConnIdleTime
338-
if idletime, err = config.String(ConfigKeyMaxConnIdleTime, &idletime); err != nil {
339-
return nil, nil, err
340-
}
341-
342-
lifetime := DefaultMaxConnLifetime
343-
if lifetime, err = config.String(ConfigKeyMaxConnLifetime, &lifetime); err != nil {
344-
return nil, nil, err
281+
if ok := parsedQuery.Get("sslmode"); ok == "" {
282+
parsedQuery.Add("sslmode", sslmode)
345283
}
346284

347-
params := &url.Values{}
348-
params.Add("sslmode", sslmode)
349-
params.Add("pool_max_conns", fmt.Sprintf("%v", maxcon))
350-
params.Add("pool_max_conn_lifetime", lifetime)
351-
params.Add("pool_max_conn_idle_time", idletime)
352-
353-
if uri == "" {
354-
log.Warn("Connecting to PostGIS with connection parameters is deprecated. Use 'uri' instead.")
355-
}
356-
357-
u := &url.URL{
358-
Scheme: "postgres",
359-
Host: fmt.Sprintf("%v:%v", host, port),
360-
User: url.UserPassword(user, password),
361-
Path: db,
362-
RawQuery: params.Encode(),
363-
}
285+
parsedUri.RawQuery = parsedQuery.Encode()
364286

365-
return u, params, nil
287+
return parsedUri, &parsedQuery, nil
366288
}
367289

368290
type DBConfigOptions struct {

0 commit comments

Comments
 (0)