From 5d9ea4bb754f8792a897b2092b6b9e469197610c Mon Sep 17 00:00:00 2001 From: David Szotten Date: Thu, 16 Oct 2014 17:25:34 +0100 Subject: [PATCH] Create index in add_column if requested Fixes #174 --- alembic/operations.py | 2 ++ tests/test_op.py | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/alembic/operations.py b/alembic/operations.py index 11319f73..d3cc1b08 100644 --- a/alembic/operations.py +++ b/alembic/operations.py @@ -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 diff --git a/tests/test_op.py b/tests/test_op.py index 835183eb..c157030a 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -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', -- 2.47.3