]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Create index in add_column if requested
authorDavid Szotten <davidszotten@gmail.com>
Thu, 16 Oct 2014 16:25:34 +0000 (17:25 +0100)
committerDavid Szotten <davidszotten@gmail.com>
Thu, 16 Oct 2014 16:25:34 +0000 (17:25 +0100)
Fixes #174

alembic/operations.py
tests/test_op.py

index 11319f733bb2a5da3ff7638461f24622388e4418..d3cc1b0819f113035e49138fc82338e30f5325ea 100644 (file)
@@ -468,6 +468,8 @@ class Operations(object):
         for constraint in t.constraints:
             if not isinstance(constraint, sa_schema.PrimaryKeyConstraint):
                 self.impl.add_constraint(constraint)
+        for index in t.indexes:
+            self.impl._exec(sa_schema.CreateIndex(index))
 
     def drop_column(self, table_name, column_name, **kw):
         """Issue a "drop column" instruction using the current
index 835183eb938fa7758b5f26ec9c6bca28e8de0bb8..c157030a473a963ad429f15b513bdd64e958a190 100644 (file)
@@ -147,6 +147,15 @@ class OpTest(TestBase):
         context.assert_(
             "ALTER TABLE t1 ADD COLUMN c1 INTEGER DEFAULT '12' NOT NULL")
 
+    def test_add_column_with_index(self):
+        context = op_fixture()
+        op.add_column(
+            't1', Column('c1', Integer, nullable=False, index=True))
+        context.assert_(
+            "ALTER TABLE t1 ADD COLUMN c1 INTEGER NOT NULL",
+            "CREATE INDEX ix_t1_c1 ON t1 (c1)",
+        )
+
     def test_add_column_schema_with_default(self):
         context = op_fixture()
         op.add_column('t1',