File 0001-The-class-sqlalchemy.schema.Table-object-is-now-retu.patch of Package python-alembic

From 8272c85faadb234af25e5a4f029a8c24879ef67d Mon Sep 17 00:00:00 2001
From: Mike Bayer <mike_mp@zzzcomputing.com>
Date: Sat, 8 Nov 2014 18:06:26 -0500
Subject: [PATCH] - The :class:`~sqlalchemy.schema.Table` object is now
 returned when the :meth:`.Operations.create_table` method is used.  This
 ``Table`` is suitable for use in subsequent SQL operations, in particular the
 :meth:`.Operations.bulk_insert` operation. fixes #205

---
 alembic/operations.py     | 41 +++++++++++++++++++++++++++++++++++++----
 docs/build/changelog.rst  |  9 +++++++++
 tests/test_bulk_insert.py | 37 +++++++++++++++++++++++++++++++++++++
 tests/test_op.py          |  7 +++++--
 4 files changed, 88 insertions(+), 6 deletions(-)

Index: alembic-0.6.7/alembic/operations.py
===================================================================
--- alembic-0.6.7.orig/alembic/operations.py
+++ alembic-0.6.7/alembic/operations.py
@@ -698,7 +698,7 @@ class Operations(object):
                 'account',
                 Column('id', INTEGER, primary_key=True),
                 Column('name', VARCHAR(50), nullable=False),
-                Column('description', NVARCHAR(200))
+                Column('description', NVARCHAR(200)),
                 Column('timestamp', TIMESTAMP, server_default=func.now())
             )
 
@@ -718,6 +718,33 @@ class Operations(object):
                 Column('timestamp', TIMESTAMP, server_default=func.now())
             )
 
+        The function also returns a newly created
+        :class:`~sqlalchemy.schema.Table` object, corresponding to the table
+        specification given, which is suitable for
+        immediate SQL operations, in particular
+        :meth:`.Operations.bulk_insert`::
+
+            from sqlalchemy import INTEGER, VARCHAR, NVARCHAR, Column
+            from alembic import op
+
+            account_table = op.create_table(
+                'account',
+                Column('id', INTEGER, primary_key=True),
+                Column('name', VARCHAR(50), nullable=False),
+                Column('description', NVARCHAR(200)),
+                Column('timestamp', TIMESTAMP, server_default=func.now())
+            )
+
+            op.bulk_insert(
+                account_table,
+                [
+                    {"name": "A1", "description": "account 1"},
+                    {"name": "A2", "description": "account 2"},
+                ]
+            )
+
+        .. versionadded:: 0.7.0
+
         :param name: Name of the table
         :param \*columns: collection of :class:`~sqlalchemy.schema.Column`
          objects within
@@ -728,10 +755,16 @@ class Operations(object):
         :param \**kw: Other keyword arguments are passed to the underlying
          :class:`sqlalchemy.schema.Table` object created for the command.
 
+        :return: the :class:`~sqlalchemy.schema.Table` object corresponding
+         to the parameters given.
+
+         .. versionadded:: 0.7.0 - the :class:`~sqlalchemy.schema.Table`
+            object is returned.
+
         """
-        self.impl.create_table(
-            self._table(name, *columns, **kw)
-        )
+        table = self._table(name, *columns, **kw)
+        self.impl.create_table(table)
+        return table
 
     def drop_table(self, name, **kw):
         """Issue a "drop table" instruction using the current
Index: alembic-0.6.7/tests/test_bulk_insert.py
===================================================================
--- alembic-0.6.7.orig/tests/test_bulk_insert.py
+++ alembic-0.6.7/tests/test_bulk_insert.py
@@ -175,6 +175,25 @@ def test_bulk_insert_as_sql_mssql():
         'SET IDENTITY_INSERT ins_table OFF'
     )
 
+    def test_bulk_insert_from_new_table(self):
+        context = op_fixture("postgresql", True)
+        t1 = op.create_table(
+            "ins_table",
+            Column('id', Integer),
+            Column('v1', String()),
+            Column('v2', String()),
+        )
+        op.bulk_insert(t1, [
+            {'id': 1, 'v1': 'row v1', 'v2': 'row v5'},
+            {'id': 2, 'v1': 'row v2', 'v2': 'row v6'},
+        ])
+        context.assert_(
+            'CREATE TABLE ins_table (id INTEGER, v1 VARCHAR, v2 VARCHAR)',
+            "INSERT INTO ins_table (id, v1, v2) VALUES "
+            "(1, 'row v1', 'row v5')",
+            "INSERT INTO ins_table (id, v1, v2) VALUES "
+            "(2, 'row v2', 'row v6')"
+        )
 
 def test_invalid_format():
     context, t1 = _table_fixture("sqlite", False)
@@ -261,3 +280,20 @@ class RoundTripTest(TestCase):
                 (2, "d2"),
             ]
         )
+
+    def test_bulk_insert_from_new_table(self):
+        t1 = self.op.create_table(
+            "ins_table",
+            Column('id', Integer),
+            Column('v1', String()),
+            Column('v2', String()),
+        )
+        self.op.bulk_insert(t1, [
+            {'id': 1, 'v1': 'row v1', 'v2': 'row v5'},
+            {'id': 2, 'v1': 'row v2', 'v2': 'row v6'},
+        ])
+        eq_(
+            self.conn.execute(
+                "select id, v1, v2 from ins_table order by id").fetchall(),
+            [(1, u'row v1', u'row v5'), (2, u'row v2', u'row v6')]
+        )
Index: alembic-0.6.7/tests/test_op.py
===================================================================
--- alembic-0.6.7.orig/tests/test_op.py
+++ alembic-0.6.7/tests/test_op.py
@@ -704,7 +704,7 @@ def test_create_table_selfref():
 
 def test_create_table_fk_and_schema():
     context = op_fixture()
-    op.create_table(
+    t1 = op.create_table(
         "some_table",
         Column('id', Integer, primary_key=True),
         Column('foo_id', Integer, ForeignKey('foo.id')),
@@ -717,11 +717,12 @@ def test_create_table_fk_and_schema():
         "PRIMARY KEY (id), "
         "FOREIGN KEY(foo_id) REFERENCES foo (id))"
     )
-
+    eq_(t1.c.id.name, "id")
+    eq_(t1.schema, "schema")
 
 def test_create_table_no_pk():
     context = op_fixture()
-    op.create_table(
+    t1 = op.create_table(
         "some_table",
         Column('x', Integer),
         Column('y', Integer),
@@ -730,6 +731,7 @@ def test_create_table_no_pk():
     context.assert_(
         "CREATE TABLE some_table (x INTEGER, y INTEGER, z INTEGER)"
     )
+    assert not t1.primary_key
 
 
 def test_create_table_two_fk():
openSUSE Build Service is sponsored by