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

Trigger events before and after drop table statements #1036

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 6 additions & 0 deletions alembic/ddl/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,13 @@ def create_table(self, table: "Table") -> None:
self.create_column_comment(column)

def drop_table(self, table: "Table") -> None:
table.dispatch.before_drop(
table, self.connection, checkfirst=False, _ddl_runner=self
)
self._exec(schema.DropTable(table))
table.dispatch.after_drop(
table, self.connection, checkfirst=False, _ddl_runner=self
)

def create_index(self, index: "Index") -> None:
self._exec(schema.CreateIndex(index))
Expand Down
49 changes: 49 additions & 0 deletions tests/test_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from sqlalchemy import Boolean
from sqlalchemy import CheckConstraint
from sqlalchemy import Column
from sqlalchemy import event
from sqlalchemy import exc
from sqlalchemy import ForeignKey
from sqlalchemy import Index
Expand Down Expand Up @@ -1087,6 +1088,54 @@ def test_drop_table_comment_op(self):

context.assert_("COMMENT ON TABLE some_table IS NULL")

def test_create_table_event(self):
context = op_fixture()

events_triggered = []

TestTable = Table("tb_test", MetaData(), Column("c1", Integer, nullable=False))

@event.listens_for(Table, "before_create")
def record_before_event(table, conn, **kwargs):
events_triggered.append(("before_create", table.name))

@event.listens_for(Table, "after_create")
def record_after_event(table, conn, **kwargs):
events_triggered.append(("after_create", table.name))

op.create_table(TestTable)
op.drop_table(TestTable)
context.assert_("CREATE TABLE tb_test ()", "DROP TABLE tb_test")

assert events_triggered == [
("before_create", "tb_test"),
("after_create", "tb_test"),
]

def test_drop_table_event(self):
context = op_fixture()

events_triggered = []

TestTable = Table("tb_test", MetaData(), Column("c1", Integer, nullable=False))

@event.listens_for(Table, "before_drop")
def record_before_event(table, conn, **kwargs):
events_triggered.append(("before_drop", table.name))

@event.listens_for(Table, "after_drop")
def record_after_event(table, conn, **kwargs):
events_triggered.append(("after_drop", table.name))

op.create_table(TestTable)
op.drop_table(TestTable)
context.assert_("CREATE TABLE tb_test ()", "DROP TABLE tb_test")

assert events_triggered == [
("before_drop", "tb_test"),
("after_drop", "tb_test"),
]


class SQLModeOpTest(TestBase):
def test_auto_literals(self):
Expand Down