diff --git a/CHANGES.rst b/CHANGES.rst index d5fa3b4..b372939 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 diff --git a/crate.go b/crate.go index b2e2659..564b5b4 100644 --- a/crate.go +++ b/crate.go @@ -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) diff --git a/pyproject.toml b/pyproject.toml index f25a3ac..e676a08 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", ] diff --git a/tests/conftest.py b/tests/conftest.py index 9248606..16a8134 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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) diff --git a/tests/cpa-config.yml b/tests/cpa-config.yml new file mode 100644 index 0000000..4ed09d7 --- /dev/null +++ b/tests/cpa-config.yml @@ -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). diff --git a/tests/test_adapter.py b/tests/test_adapter.py index 38ea313..a1563c5 100644 --- a/tests/test_adapter.py +++ b/tests/test_adapter.py @@ -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):