]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- The :class:`~sqlalchemy.schema.Table` object is now returned when
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 8 Nov 2014 23:06:26 +0000 (18:06 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 8 Nov 2014 23:06:26 +0000 (18:06 -0500)
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
docs/build/changelog.rst
tests/test_bulk_insert.py
tests/test_op.py

index 11319f733bb2a5da3ff7638461f24622388e4418..390764c2e1bec64ba74610e43bbdc9c73b494a63 100644 (file)
@@ -749,7 +749,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())
             )
 
@@ -769,6 +769,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
@@ -787,10 +814,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 560cc6d8896fac5ed32a2ecc9e07ef44390c037e..9222a6bf227f14aa3d316a696c760a3bcc49771b 100644 (file)
@@ -5,6 +5,15 @@ Changelog
 .. changelog::
     :version: 0.7.0
 
+    .. change::
+      :tags: feature, operations
+      :tickets: 205
+
+      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.
+
     .. change::
       :tags: feature, autogenerate
       :tickets: 203
index 97a8dd831be2e6ce0a9dafed422359766471c94c..5121e9072c95a3e1679977666a9db4644b30b1fb 100644 (file)
@@ -176,6 +176,26 @@ class BulkInsertTest(TestBase):
             '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(self):
         context, t1 = self._table_fixture("sqlite", False)
         assert_raises_message(
@@ -263,3 +283,20 @@ class RoundTripTest(TestBase):
                 (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')]
+        )
\ No newline at end of file
index 835183eb938fa7758b5f26ec9c6bca28e8de0bb8..789eefb8f88d7e57fd65c73d022e9060b5d908c2 100644 (file)
@@ -699,7 +699,7 @@ class OpTest(TestBase):
 
     def test_create_table_fk_and_schema(self):
         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')),
@@ -712,10 +712,12 @@ class OpTest(TestBase):
             "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(self):
         context = op_fixture()
-        op.create_table(
+        t1 = op.create_table(
             "some_table",
             Column('x', Integer),
             Column('y', Integer),
@@ -724,6 +726,7 @@ class OpTest(TestBase):
         context.assert_(
             "CREATE TABLE some_table (x INTEGER, y INTEGER, z INTEGER)"
         )
+        assert not t1.primary_key
 
     def test_create_table_two_fk(self):
         context = op_fixture()