Skip to content

Commit

Permalink
Fix use of schema configuration setting, it was not honored
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Jan 20, 2024
1 parent 3b79241 commit 126bcd6
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Unreleased

- Packaging: Re-add compatibility with glibc 2.31,
by building on ``golang:1.20-bullseye``.
- Fixed use of ``schema`` configuration setting, it was not honored.


2024-01-12 0.5.0
Expand Down
9 changes: 9 additions & 0 deletions crate.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ func newCrateEndpoint(ep *endpointConfig) *crateEndpoint {
// a connection from the pool manually and prepare it there before use.
// https://github.com/jackc/pgx/issues/791#issuecomment-660508309
poolConf.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {

// Switch to different database schema when requested.
if ep.Schema != "" {
_, err := conn.Exec(ctx, fmt.Sprintf("SET search_path TO '%s';", ep.Schema))
if err != nil {
return fmt.Errorf("error setting search path: %v", err)
}
}

_, err := conn.Prepare(ctx, "write_statement", crateWriteStatement)
if err != nil {
return fmt.Errorf("error preparing write statement: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ addopts = """
-rfEX -p pytester --strict-markers --verbosity=3
"""
env = [
"CRATEDB_CONNECTION_STRING=crate://crate@localhost/?ssl=false",
"CRATEDB_CONNECTION_STRING=crate://crate@localhost/?ssl=false&schema=testdrive",
"CRATEDB_PROMETHEUS_ADAPTER_METRICS_URL=http://localhost:9268/metrics",
"PROMETHEUS_URL=http://localhost:9090",
]
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def cratedb_prometheus_adapter(reset_database, cratedb_client, flush_database):
Start the CrateDB Prometheus Adapter.
"""

command = "go run ."
command = "go run . -config.file tests/cpa-config.yml"
with process(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.STDOUT) as daemon:
# Give the server time to start.
time.sleep(2)
Expand Down
20 changes: 20 additions & 0 deletions tests/cpa-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
cratedb_endpoints:
- host: "localhost" # Host to connect to (default: "localhost").
port: 5432 # Port to connect to (default: 5432).
user: "crate" # Username to use (default: "crate")
password: "" # Password to use (default: "").
schema: "testdrive" # Schema to use (default: "").
max_connections: 0 # The maximum number of concurrent connections (default: runtime.NumCPU()).
# It will get forwarded to pgx's `pool_max_conns`, and determines
# the maximum number of connections in the connection pool for
# both connection pools (read and write).
read_pool_size_max: 0 # Configure the maximum pool size for read operations individually.
# (default: runtime.NumCPU())
write_pool_size_max: 0 # Configure the maximum pool size for write operations individually.
# (default: runtime.NumCPU())
connect_timeout: 10 # TCP connect timeout (seconds) (default: 10).
# It has the same meaning as libpq's `connect_timeout`.
read_timeout: 5 # Query context timeout for read queries (seconds) (default: 5).
write_timeout: 5 # Query context timeout for write queries (seconds) (default: 5).
enable_tls: false # Whether to connect using TLS (default: false).
allow_insecure_tls: false # Whether to allow insecure / invalid TLS certificates (default: false).
2 changes: 1 addition & 1 deletion tests/test_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def test_verify_no_failures(prometheus_client):
result = prometheus_client.get_metric_aggregation(
failed_total_attribute, operations=["sum"]
)
assert result == {"sum": 0.0}
assert result == {"sum": 0.0}, f"{failed_total_attribute} tracked {result['sum']} errors"


def test_verify_write_activity(prometheus_client):
Expand Down

0 comments on commit 126bcd6

Please sign in to comment.