Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ def to_sql_create(self, replace=False):
components.append(quote_name(self.execute) + f"({','.join(args_quoted)})")
return " ".join(components) + ";"

def to_sql_drop(self):
return f"DROP TRIGGER {quote_name(self.name)} ON {quote_name(self.on)};"

def to_sql_update(self, connection: Connection | None = None):
if connection is not None:
assert connection.dialect.server_version_info
Expand Down
18 changes: 18 additions & 0 deletions tests/trigger/test_drop_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ class Foo(Base):
id = Column(types.Integer(), primary_key=True)


class TableWithSpecialName(Base):
__tablename__ = "user" # This name will trip up unquoted identifiers

id = Column(types.Integer(), primary_key=True)


register_sqlalchemy_events(Base.metadata, triggers=True)

pg = create_postgres_fixture(engine_kwargs={"echo": True}, session=True)


def test_drop(pg):
pg.execute(text("CREATE TABLE foo (id integer primary key);"))
pg.execute(text('CREATE TABLE "user" (id integer primary key);'))
pg.execute(
text(
"""
Expand All @@ -49,17 +56,28 @@ def test_drop(pg):
"WHEN (pg_trigger_depth() < 1) EXECUTE PROCEDURE gimme();"
)
)
pg.execute(
text(
'CREATE TRIGGER "Quoted Name" AFTER INSERT ON "user" FOR EACH ROW '
"WHEN (pg_trigger_depth() < 1) EXECUTE PROCEDURE gimme();"
)
)

pg.commit()

Base.metadata.create_all(bind=pg.connection())
pg.commit()

pg.add(Foo(id=5))
pg.add(TableWithSpecialName(id=6))
pg.commit()

result = [r.id for r in pg.query(Foo).all()]
assert result == [5]

quoted_result = [r.id for r in pg.query(TableWithSpecialName).all()]
assert quoted_result == [6]

connection = pg.connection()
diff = compare_triggers(connection, Base.metadata.info["triggers"])
assert diff == []
Loading