Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SET/RESET ROLE to migrations #82

Merged
merged 12 commits into from
Apr 29, 2021
Prev Previous commit
Next Next commit
Clean up
  • Loading branch information
rod-glover committed Feb 5, 2021
commit 7bc31553f867df857f082ff00f90cb72849e9538
8 changes: 3 additions & 5 deletions pycds/alembic/extensions/operation_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ def __init__(self, role_name):
@classmethod
def set_role(cls, operations, name, **kw):
"""Issue a SET ROLE command."""
op = SetRoleOp(name)
return operations.invoke(op)
return operations.invoke(cls(name))


@Operations.implementation_for(SetRoleOp)
Expand All @@ -84,10 +83,9 @@ class ResetRoleOp(MigrateOperation):
@classmethod
def reset_role(cls, operations, **kw):
"""Issue a RESET ROLE command."""
op = ResetRoleOp()
return operations.invoke(op)
return operations.invoke(cls())


@Operations.implementation_for(ResetRoleOp)
def set_role(operations, operation):
def reset_role(operations, operation):
operations.execute(f"RESET ROLE")
24 changes: 14 additions & 10 deletions tests/alembic_migrations/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def db_setup(schema_name):
def f(engine):
test_user = "testuser"

print(f"### initial user {engine.execute('SELECT current_user').scalar()}")
# print(f"### initial user {engine.execute('SELECT current_user').scalar()}")

engine.execute('CREATE EXTENSION postgis')
engine.execute('CREATE EXTENSION plpythonu')
Expand All @@ -57,17 +57,21 @@ def f(engine):
]
engine.execute("".join(privs))

# One of the following should set the current user to test_user.
# But it's hard to tell if it does, because `SELECT current_user` *always*
# returns `postgres`, except when it is executed in the same engine.execute
# block as the SET ROLE/AUTH statement. Subsequent SELECT current_user queries
# then return `postgres` again, so it's very hard to tell what is actually
# happening.
# One of the following *should* set the current user to `test_user`.
# But it's hard to tell if it does, because `SELECT current_user`
# *always* returns `postgres`, except when it is executed in the same
# `engine.execute` operation as the `SET ROLE/AUTH` statement.
# Subsequent `SELECT current_user` queries then return `postgres` again,
# so it's very hard to tell what is actually happening.

# engine.execute(f"SET ROLE '{test_user}';")
# engine.execute(f"SET SESSION AUTHORIZATION '{test_user}';")
engine.execute(f"SET SESSION AUTHORIZATION '{test_user}';")

result = engine.execute(f"SET SESSION AUTHORIZATION '{test_user}'; SELECT current_user").scalar()
print(f'### final user {result}')
# result = engine.execute(f"SELECT current_user").scalar()
# --> "postgres"
# result = engine.execute(f"SET SESSION AUTHORIZATION '{test_user}'; SELECT current_user").scalar()
# --> "testuser"
# print(f'### final user {result}')

return f

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ def prepared_schema_from_migrations_left(
db_setup=db_setup,
revision='4a2f1879293a'
)
schemas = engine.execute("select schema_name from information_schema.schemata").fetchall()
print(f"### 4af2 schemas: {[x[0] for x in schemas]}")
result = engine.execute(f"SELECT current_user").scalar()
print(f'### 4af2 user {result}')

yield engine, script

Expand Down
8 changes: 4 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def pytest_runtest_setup():
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
logging.getLogger('tests').setLevel(logging.DEBUG)
logging.getLogger('alembic').setLevel(logging.DEBUG)
# logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
# logging.getLogger("sqlalchemy.pool").setLevel(logging.DEBUG)

@fixture(scope='session')
Expand Down Expand Up @@ -63,9 +63,9 @@ def set_up_db_cluster(db_uri, user="testuser"):
def base_database_uri():
"""Test-session scoped base database."""
with testing.postgresql.Postgresql() as pg:
db_uri = pg.url()
set_up_db_cluster(db_uri)
yield db_uri
uri = pg.url()
set_up_db_cluster(uri)
yield uri


# TODO: Separate out add_functions
Expand Down