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

Fix quotes for "on_entity" in generated sql for PGFunction and PGTrigger #148

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/alembic_utils/pg_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ def from_sql(cls, sql: str) -> "PGPolicy":
def to_sql_statement_create(self):
"""Generates a SQL "create poicy" statement for PGPolicy"""

return sql_text(f"CREATE POLICY {self.signature} on {self.on_entity} {self.definition}")
return sql_text(f'CREATE POLICY "{self.signature}" on {coerce_to_quoted(self.on_entity)} {self.definition}')

def to_sql_statement_drop(self, cascade=False):
"""Generates a SQL "drop policy" statement for PGPolicy"""
cascade = "cascade" if cascade else ""
return sql_text(f"DROP POLICY {self.signature} on {self.on_entity} {cascade}")
return sql_text(f'DROP POLICY "{self.signature}" on {coerce_to_quoted(self.on_entity)} {cascade}')

def to_sql_statement_create_or_replace(self):
"""Not implemented, postgres policies do not support replace."""
yield sql_text(f"DROP POLICY IF EXISTS {self.signature} on {self.on_entity};")
yield sql_text(f"CREATE POLICY {self.signature} on {self.on_entity} {self.definition};")
yield sql_text(f'DROP POLICY IF EXISTS "{self.signature}" on {coerce_to_quoted(self.on_entity)};')
yield sql_text(f'CREATE POLICY "{self.signature}" on {coerce_to_quoted(self.on_entity)} {self.definition};')

@classmethod
def from_database(cls, connection, schema):
Expand Down
5 changes: 3 additions & 2 deletions src/alembic_utils/pg_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from alembic_utils.exceptions import SQLParseFailure
from alembic_utils.on_entity_mixin import OnEntityMixin
from alembic_utils.replaceable_entity import ReplaceableEntity
from alembic_utils.statement import coerce_to_quoted


class PGTrigger(OnEntityMixin, ReplaceableEntity):
Expand Down Expand Up @@ -133,11 +134,11 @@ def to_sql_statement_create(self):
def to_sql_statement_drop(self, cascade=False):
"""Generates a SQL "drop trigger" statement for PGTrigger"""
cascade = "cascade" if cascade else ""
return sql_text(f'DROP TRIGGER "{self.signature}" ON {self.on_entity} {cascade}')
return sql_text(f'DROP TRIGGER "{self.signature}" ON {coerce_to_quoted(self.on_entity)} {cascade}')

def to_sql_statement_create_or_replace(self):
"""Generates a SQL "replace trigger" statement for PGTrigger"""
yield sql_text(f'DROP TRIGGER IF EXISTS "{self.signature}" ON {self.on_entity};')
yield sql_text(f'DROP TRIGGER IF EXISTS "{self.signature}" ON {coerce_to_quoted(self.on_entity)};')
yield self.to_sql_statement_create()

@classmethod
Expand Down