Skip to content

Commit df2a962

Browse files
committed
Updated postgresql permissions grants
1 parent 74c54e0 commit df2a962

File tree

5 files changed

+35
-27
lines changed

5 files changed

+35
-27
lines changed

cli/macrostrat/cli/database/__init__.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,24 @@
44
from typing import Any, Callable, Iterable
55

66
import typer
7-
from pydantic import BaseModel
8-
from rich import print
9-
from sqlalchemy import make_url, text
10-
from typer import Argument, Option
11-
12-
from macrostrat.core import MacrostratSubsystem, app
13-
from macrostrat.core.migrations import run_migrations
147
from macrostrat.database import Database
158
from macrostrat.database.transfer import pg_dump_to_file, pg_restore_from_file
169
from macrostrat.database.transfer.utils import raw_database_url
1710
from macrostrat.database.utils import get_sql_files
1811
from macrostrat.utils import get_logger
1912
from macrostrat.utils.shell import run
13+
from pydantic import BaseModel
14+
from rich import print
15+
from sqlalchemy import make_url, text
16+
from typer import Argument, Option
2017

18+
from macrostrat.core import MacrostratSubsystem, app
19+
from macrostrat.core.migrations import run_migrations
2120
from ._legacy import get_db
22-
2321
# First, register all migrations
2422
# NOTE: right now, this is quite implicit.
2523
from .migrations import load_migrations
26-
from .utils import engine_for_db_name
24+
from .utils import engine_for_db_name, setup_postgrest_access
2725

2826
log = get_logger(__name__)
2927

@@ -398,6 +396,19 @@ def run_scripts(migration: str = Argument(None)):
398396
db_app.command(name="migrations", rich_help_panel="Schema management")(run_migrations)
399397

400398

399+
def update_permissions():
400+
"""Setup permissions for the PostgREST API.
401+
402+
NOTE: This is a stopgap until we have a better permssions system.
403+
"""
404+
db = get_db()
405+
setup_postgrest_access("macrostrat_api")(db)
406+
db.run_sql("NOTIFY pgrst, 'reload schema';")
407+
408+
409+
db_app.command(name="permissions", rich_help_panel="Helpers")(update_permissions)
410+
411+
401412
### Helpers
402413

403414

cli/macrostrat/cli/database/utils.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22
from typing import Optional
33
from uuid import uuid4
44

5+
from macrostrat.database import Database
6+
from macrostrat.database.utils import run_query, run_sql
57
from psycopg2.sql import Identifier
68
from rich import print
79
from sqlalchemy.engine import Engine, create_engine
810
from sqlalchemy.engine.url import URL, make_url
911

1012
from macrostrat.core.config import settings
11-
from macrostrat.database import Database
12-
from macrostrat.database.utils import run_query, run_sql
13-
1413
from ._legacy import get_db
1514

1615

@@ -336,3 +335,13 @@ def grant_schema_usage(
336335
""",
337336
params,
338337
)
338+
339+
340+
def setup_postgrest_access(schema: str):
341+
"""Run basic grant statements to allow PostgREST to access the schema"""
342+
343+
def run_updates(db):
344+
grant_schema_usage(db, schema, "web_anon")
345+
grant_schema_usage(db, schema, "web_user", tables=False, sequences=True)
346+
347+
return run_updates

cli/macrostrat/cli/subsystems/macrostrat_api/__init__.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,15 @@
66
from pathlib import Path
77

88
from macrostrat.app_frame import compose
9+
910
from macrostrat.core import MacrostratSubsystem
1011
from macrostrat.core.migrations import Migration, view_exists
11-
1212
from ...database import SubsystemSchemaDefinition, get_db
13-
from ...database.utils import grant_schema_usage
13+
from ...database.utils import setup_postgrest_access
1414

1515
__here__ = Path(__file__).parent
1616
fixtures_dir = __here__ / "schema"
1717

18-
19-
def setup_postgrest_access(schema: str):
20-
"""Run basic grant statements to allow PostgREST to access the schema"""
21-
22-
def run_updates(db):
23-
grant_schema_usage(db, schema, "web_anon")
24-
grant_schema_usage(db, schema, "web_user", tables=False, sequences=True)
25-
26-
return run_updates
27-
28-
2918
macrostrat_api = SubsystemSchemaDefinition(
3019
name="macrostrat-api",
3120
fixtures=[fixtures_dir, setup_postgrest_access("macrostrat_api")],
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from macrostrat.database import Database
44

5-
from .config import PG_DATABASE
5+
from ..config import PG_DATABASE
66

77
db_ctx: ContextVar[Database | None] = ContextVar("db_ctx", default=None)
88

local-root/docker-compose.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ services:
7373
- PGRST_DB_URI=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/${POSTGRES_DB}
7474
- PGRST_DB_SCHEMA=macrostrat_api
7575
- PGRST_DB_ANON_ROLE=web_anon
76-
#- PGRST_SERVER_PROXY_URI=http://database:5432
7776
- PGRST_SERVER_PORT=3000
7877
- PGRST_SERVER_HOST=
7978
# Tileserver

0 commit comments

Comments
 (0)