]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Emit deprecation warning for general DDLElement.bind argument
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 14 Dec 2020 15:36:57 +0000 (10:36 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 14 Dec 2020 16:51:07 +0000 (11:51 -0500)
Change-Id: I8ab1fd98340cb30aa43075508b3a0b9feffa290c

lib/sqlalchemy/sql/ddl.py
test/engine/test_deprecations.py
test/sql/test_deprecations.py

index a592419f413b54ebd1675217e9e3d064d10929ea..d471f29d5592b932f97415b5943ff4fb319ee597 100644 (file)
@@ -79,7 +79,7 @@ class DDLElement(roles.DDLRole, Executable, _DDLCompiles):
         )
 
     @util.deprecated_20(
-        ":meth:`.DDL.execute`",
+        ":meth:`.DDLElement.execute`",
         alternative="All statement execution in SQLAlchemy 2.0 is performed "
         "by the :meth:`_engine.Connection.execute` method of "
         ":class:`_engine.Connection`, "
@@ -369,11 +369,27 @@ class _CreateDropBase(DDLElement):
 
     """
 
+    @util.deprecated_params(
+        bind=(
+            "2.0",
+            "The :paramref:`_ddl.DDLElement.bind` argument is "
+            "deprecated and "
+            "will be removed in SQLAlchemy 2.0.",
+        ),
+    )
     def __init__(
-        self, element, bind=None, if_exists=False, if_not_exists=False
+        self,
+        element,
+        bind=None,
+        if_exists=False,
+        if_not_exists=False,
+        _legacy_bind=None,
     ):
         self.element = element
-        self.bind = bind
+        if bind:
+            self.bind = bind
+        elif _legacy_bind:
+            self.bind = _legacy_bind
         self.if_exists = if_exists
         self.if_not_exists = if_not_exists
 
@@ -430,6 +446,13 @@ class CreateTable(_CreateDropBase):
 
     __visit_name__ = "create_table"
 
+    @util.deprecated_params(
+        bind=(
+            "2.0",
+            "The :paramref:`_ddl.CreateTable.bind` argument is deprecated and "
+            "will be removed in SQLAlchemy 2.0.",
+        ),
+    )
     def __init__(
         self,
         element,
@@ -457,7 +480,7 @@ class CreateTable(_CreateDropBase):
 
         """
         super(CreateTable, self).__init__(
-            element, bind=bind, if_not_exists=if_not_exists
+            element, _legacy_bind=bind, if_not_exists=if_not_exists
         )
         self.columns = [CreateColumn(column) for column in element.columns]
         self.include_foreign_key_constraints = include_foreign_key_constraints
@@ -588,6 +611,14 @@ class DropTable(_CreateDropBase):
 
     __visit_name__ = "drop_table"
 
+    @util.deprecated_params(
+        bind=(
+            "2.0",
+            "The :paramref:`_ddl.DropTable.bind` argument is "
+            "deprecated and "
+            "will be removed in SQLAlchemy 2.0.",
+        ),
+    )
     def __init__(self, element, bind=None, if_exists=False):
         """Create a :class:`.DropTable` construct.
 
@@ -602,7 +633,7 @@ class DropTable(_CreateDropBase):
 
         """
         super(DropTable, self).__init__(
-            element, bind=bind, if_exists=if_exists
+            element, _legacy_bind=bind, if_exists=if_exists
         )
 
 
@@ -623,6 +654,14 @@ class CreateIndex(_CreateDropBase):
 
     __visit_name__ = "create_index"
 
+    @util.deprecated_params(
+        bind=(
+            "2.0",
+            "The :paramref:`_ddl.CreateIndex.bind` argument is "
+            "deprecated and "
+            "will be removed in SQLAlchemy 2.0.",
+        ),
+    )
     def __init__(self, element, bind=None, if_not_exists=False):
         """Create a :class:`.Createindex` construct.
 
@@ -637,7 +676,7 @@ class CreateIndex(_CreateDropBase):
 
         """
         super(CreateIndex, self).__init__(
-            element, bind=bind, if_not_exists=if_not_exists
+            element, _legacy_bind=bind, if_not_exists=if_not_exists
         )
 
 
@@ -646,6 +685,14 @@ class DropIndex(_CreateDropBase):
 
     __visit_name__ = "drop_index"
 
+    @util.deprecated_params(
+        bind=(
+            "2.0",
+            "The :paramref:`_ddl.DropIndex.bind` argument is "
+            "deprecated and "
+            "will be removed in SQLAlchemy 2.0.",
+        ),
+    )
     def __init__(self, element, bind=None, if_exists=False):
         """Create a :class:`.DropIndex` construct.
 
@@ -660,7 +707,7 @@ class DropIndex(_CreateDropBase):
 
         """
         super(DropIndex, self).__init__(
-            element, bind=bind, if_exists=if_exists
+            element, _legacy_bind=bind, if_exists=if_exists
         )
 
 
index 6acc2967d8838075129e5a6cf74e1902a68981d5..4ca081be2ebf815507ba85fcd53de5a8de08fcb9 100644 (file)
@@ -1648,7 +1648,7 @@ class DDLExecutionTest(fixtures.TestBase):
         ddl = DDL("SELECT 1")
 
         eng_msg = r"The Engine.execute\(\) method is considered legacy"
-        ddl_msg = r"The DDL.execute\(\) method is considered legacy"
+        ddl_msg = r"The DDLElement.execute\(\) method is considered legacy"
         for spec in (
             (engine.execute, ddl, eng_msg),
             (engine.execute, ddl, table, eng_msg),
index e082cf55d09b02d3ec5d3e0b9d126f661da15526..acc12a5febb0281dd49b1b56a02a09ae9c5fea8e 100644 (file)
@@ -22,6 +22,7 @@ from sqlalchemy import literal_column
 from sqlalchemy import MetaData
 from sqlalchemy import null
 from sqlalchemy import or_
+from sqlalchemy import schema
 from sqlalchemy import select
 from sqlalchemy import Sequence
 from sqlalchemy import sql
@@ -49,6 +50,7 @@ from sqlalchemy.testing import eq_
 from sqlalchemy.testing import fixtures
 from sqlalchemy.testing import in_
 from sqlalchemy.testing import is_
+from sqlalchemy.testing import is_false
 from sqlalchemy.testing import is_true
 from sqlalchemy.testing import mock
 from sqlalchemy.testing import not_in
@@ -2569,3 +2571,121 @@ class LegacySequenceExecTest(fixtures.TestBase):
             r"The Engine.scalar\(\) method is considered legacy"
         ):
             self._assert_seq_result(testing.db.scalar(select(s.next_value())))
+
+
+class DDLDeprecatedBindTest(fixtures.TestBase):
+    def teardown(self):
+        with testing.db.begin() as conn:
+            if inspect(conn).has_table("foo"):
+                conn.execute(schema.DropTable(table("foo")))
+
+    def test_bind_ddl_deprecated(self, connection):
+        with testing.expect_deprecated_20(
+            "The DDL.bind argument is deprecated"
+        ):
+            ddl = schema.DDL("create table foo(id integer)", bind=connection)
+
+        with testing.expect_deprecated_20(
+            r"The DDLElement.execute\(\) method is considered legacy"
+        ):
+            ddl.execute()
+
+    def test_bind_create_table_deprecated(self, connection):
+        t1 = Table("foo", MetaData(), Column("id", Integer))
+
+        with testing.expect_deprecated_20(
+            "The CreateTable.bind argument is deprecated"
+        ):
+            ddl = schema.CreateTable(t1, bind=connection)
+
+        with testing.expect_deprecated_20(
+            r"The DDLElement.execute\(\) method is considered legacy"
+        ):
+            ddl.execute()
+
+        is_true(inspect(connection).has_table("foo"))
+
+    def test_bind_create_index_deprecated(self, connection):
+        t1 = Table("foo", MetaData(), Column("id", Integer))
+        t1.create(connection)
+
+        idx = schema.Index("foo_idx", t1.c.id)
+
+        with testing.expect_deprecated_20(
+            "The CreateIndex.bind argument is deprecated"
+        ):
+            ddl = schema.CreateIndex(idx, bind=connection)
+
+        with testing.expect_deprecated_20(
+            r"The DDLElement.execute\(\) method is considered legacy"
+        ):
+            ddl.execute()
+
+        is_true(
+            "foo_idx"
+            in [ix["name"] for ix in inspect(connection).get_indexes("foo")]
+        )
+
+    def test_bind_drop_table_deprecated(self, connection):
+        t1 = Table("foo", MetaData(), Column("id", Integer))
+
+        t1.create(connection)
+
+        with testing.expect_deprecated_20(
+            "The DropTable.bind argument is deprecated"
+        ):
+            ddl = schema.DropTable(t1, bind=connection)
+
+        with testing.expect_deprecated_20(
+            r"The DDLElement.execute\(\) method is considered legacy"
+        ):
+            ddl.execute()
+
+        is_false(inspect(connection).has_table("foo"))
+
+    def test_bind_drop_index_deprecated(self, connection):
+        t1 = Table("foo", MetaData(), Column("id", Integer))
+        idx = schema.Index("foo_idx", t1.c.id)
+        t1.create(connection)
+
+        is_true(
+            "foo_idx"
+            in [ix["name"] for ix in inspect(connection).get_indexes("foo")]
+        )
+
+        with testing.expect_deprecated_20(
+            "The DropIndex.bind argument is deprecated"
+        ):
+            ddl = schema.DropIndex(idx, bind=connection)
+
+        with testing.expect_deprecated_20(
+            r"The DDLElement.execute\(\) method is considered legacy"
+        ):
+            ddl.execute()
+
+        is_false(
+            "foo_idx"
+            in [ix["name"] for ix in inspect(connection).get_indexes("foo")]
+        )
+
+    @testing.combinations(
+        (schema.AddConstraint,),
+        (schema.DropConstraint,),
+        (schema.CreateSequence,),
+        (schema.DropSequence,),
+        (schema.CreateSchema,),
+        (schema.DropSchema,),
+        (schema.SetTableComment,),
+        (schema.DropTableComment,),
+        (schema.SetColumnComment,),
+        (schema.DropColumnComment,),
+    )
+    def test_bind_other_constructs(self, const):
+        m1 = mock.Mock()
+
+        with testing.expect_deprecated_20(
+            "The DDLElement.bind argument is deprecated"
+        ):
+            c1 = const(m1, bind=testing.db)
+
+            is_(c1.bind, testing.db)