feat: Make db-uri optional

The default is now "postgresql://" which falls back to LIBPQ environment variables.

Resolves #1991, Ref #1823
This commit is contained in:
Wolfgang Walther
2022-01-22 15:13:19 +01:00
parent ec09b87940
commit 9ed30c0ba4
21 changed files with 108 additions and 107 deletions
-1
View File
@@ -1,5 +1,4 @@
db-anon-role = "required"
db-uri = "required"
db-schema = "provided_through_alias"
max-rows = 1000
-1
View File
@@ -1,4 +1,3 @@
db-uri = "required"
db-schemas = "required"
db-anon-role = "required"
-1
View File
@@ -1,4 +1,3 @@
db-uri = "required"
db-schemas = "required"
db-anon-role = "required"
+1 -2
View File
@@ -1,5 +1,4 @@
db-uri = "required"
db-schemas = "required"
db-anon-role = "required"
# Not the default, but only works with proper db-uri
# Not the default, but only works with PG* variables, which are not set
db-config = false
+1 -1
View File
@@ -11,7 +11,7 @@ db-root-spec = "open_alias"
db-schemas = "provided_through_alias"
db-config = true
db-tx-end = "commit"
db-uri = "required"
db-uri = "postgresql://"
db-use-legacy-gucs = true
jwt-aud = ""
jwt-role-claim-key = ".\"aliased\""
@@ -11,7 +11,7 @@ db-root-spec = ""
db-schemas = "required"
db-config = true
db-tx-end = "commit"
db-uri = "required"
db-uri = "postgresql://"
db-use-legacy-gucs = true
jwt-aud = ""
jwt-role-claim-key = ".\"role\""
@@ -11,7 +11,7 @@ db-root-spec = ""
db-schemas = "required"
db-config = true
db-tx-end = "commit"
db-uri = "required"
db-uri = "postgresql://"
db-use-legacy-gucs = true
jwt-aud = ""
jwt-role-claim-key = ".\"role\""
+1 -1
View File
@@ -11,7 +11,7 @@ db-root-spec = ""
db-schemas = "required"
db-config = false
db-tx-end = "commit"
db-uri = "required"
db-uri = "postgresql://"
db-use-legacy-gucs = true
jwt-aud = ""
jwt-role-claim-key = ".\"role\""
@@ -11,7 +11,7 @@ db-root-spec = "other_root"
db-schemas = "test,other_tenant1,other_tenant2"
db-config = true
db-tx-end = "rollback-allow-override"
db-uri = "<REPLACED_WITH_DB_URI>"
db-uri = "postgresql://"
db-use-legacy-gucs = false
jwt-aud = "https://otherexample.org"
jwt-role-claim-key = ".\"other\".\"role\""
@@ -11,7 +11,7 @@ db-root-spec = "root"
db-schemas = "test,tenant1,tenant2"
db-config = true
db-tx-end = "commit-allow-override"
db-uri = "<REPLACED_WITH_DB_URI>"
db-uri = "postgresql://"
db-use-legacy-gucs = false
jwt-aud = "https://example.org"
jwt-role-claim-key = ".\"a\".\"role\""
+1 -1
View File
@@ -11,7 +11,7 @@ db-root-spec = ""
db-schemas = "required"
db-config = true
db-tx-end = "commit"
db-uri = "required"
db-uri = "postgresql://"
db-use-legacy-gucs = true
jwt-aud = ""
jwt-role-claim-key = ".\"role\""
-1
View File
@@ -1,7 +1,6 @@
# tests how config options fall back with invalid types
db-anon-role = "required"
db-schemas = "required"
db-uri = "required"
# expects string
app.settings.test = false
-7
View File
@@ -27,18 +27,11 @@ cli:
- name: missing db-anon-role
expect: error
env:
PGRST_DB_URI: required
PGRST_DB_SCHEMAS: required
- name: missing db-schemas
expect: error
env:
PGRST_DB_ANON_ROLE: required
PGRST_DB_URI: required
- name: missing db-uri
expect: error
env:
PGRST_DB_ANON_ROLE: required
PGRST_DB_SCHEMAS: required
# failures: wrong config values
- name: invalid server-unix-socket-mode not octal
expect: error
+35 -17
View File
@@ -79,14 +79,19 @@ class PostgrestProcess:
@pytest.fixture
def dburi():
"Postgres database connection URI."
return os.getenv("PGRST_DB_URI").encode()
dbname = os.environ["PGDATABASE"]
host = os.environ["PGHOST"]
user = os.environ["PGUSER"]
return f"postgresql://?dbname={dbname}&host={host}&user={user}".encode()
@pytest.fixture
def defaultenv():
"Default environment for PostgREST."
return {
"PGRST_DB_URI": os.environ["PGRST_DB_URI"],
"PGDATABASE": os.environ["PGDATABASE"],
"PGHOST": os.environ["PGHOST"],
"PGUSER": os.environ["PGUSER"],
"PGRST_DB_SCHEMAS": "public",
"PGRST_DB_ANON_ROLE": os.environ["PGRST_DB_ANON_ROLE"],
"PGRST_DB_CONFIG": "false",
@@ -309,20 +314,14 @@ def test_expected_config_from_db_settings(defaultenv, role, expectedconfig):
config = CONFIGSDIR / "no-defaults.config"
db_uri = defaultenv["PGRST_DB_URI"].replace(
"user=postgrest_test_authenticator", f"user={role}"
)
env = {
**defaultenv,
"PGRST_DB_URI": db_uri,
"PGUSER": role,
"PGRST_DB_URI": "postgresql://",
"PGRST_DB_CONFIG": "true",
}
expected = (
(CONFIGSDIR / "expected" / expectedconfig)
.read_text()
.replace("<REPLACED_WITH_DB_URI>", env["PGRST_DB_URI"])
)
expected = (CONFIGSDIR / "expected" / expectedconfig).read_text()
assert dumpconfig(configpath=config, env=env) == expected
@@ -418,9 +417,26 @@ def test_read_secret_from_stdin_dbconfig(defaultenv):
assert response.status_code == 200
def test_connect_with_dburi(dburi, defaultenv):
"Connecting with db-uri instead of LIPQ* environment variables should work."
defaultenv_without_libpq = {
key: value
for key, value in defaultenv.items()
if key not in ["PGDATABASE", "PGHOST", "PGUSER"]
}
env = {**defaultenv_without_libpq, "PGRST_DB_URI": dburi.decode()}
with run(env=env):
pass
def test_read_dburi_from_stdin_without_eol(dburi, defaultenv):
"Reading the dburi from stdin with a single line should work."
env = {**defaultenv, "PGRST_DB_URI": "@/dev/stdin"}
defaultenv_without_libpq = {
key: value
for key, value in defaultenv.items()
if key not in ["PGDATABASE", "PGHOST", "PGUSER"]
}
env = {**defaultenv_without_libpq, "PGRST_DB_URI": "@/dev/stdin"}
with run(env=env, stdin=dburi):
pass
@@ -428,7 +444,12 @@ def test_read_dburi_from_stdin_without_eol(dburi, defaultenv):
def test_read_dburi_from_stdin_with_eol(dburi, defaultenv):
"Reading the dburi from stdin containing a newline should work."
env = {**defaultenv, "PGRST_DB_URI": "@/dev/stdin"}
defaultenv_without_libpq = {
key: value
for key, value in defaultenv.items()
if key not in ["PGDATABASE", "PGHOST", "PGUSER"]
}
env = {**defaultenv_without_libpq, "PGRST_DB_URI": "@/dev/stdin"}
with run(env=env, stdin=dburi + b"\n"):
pass
@@ -802,12 +823,9 @@ def test_admin_ready_wo_channel(defaultenv):
def test_admin_ready_includes_schema_cache_state(defaultenv):
"Should get a failed response from the admin server ready endpoint when the schema cache is not loaded"
db_uri = defaultenv["PGRST_DB_URI"].replace(
"postgrest_test_authenticator", "limited_authenticator"
)
env = {
**defaultenv,
"PGRST_DB_URI": db_uri,
"PGUSER": "limited_authenticator",
"PGRST_DB_ANON_ROLE": "limited_authenticator",
}