is not being changed; else MySQL sets this to NULL.
"""
- if existing_type:
+ compiler = self.impl.dialect.statement_compiler(
+ self.impl.dialect,
+ None
+ )
+ def _count_constraint(constraint):
+ return not isinstance(constraint, schema.PrimaryKeyConstraint) and \
+ (not constraint._create_rule or
+ constraint._create_rule(compiler))
+
+ if existing_type and type_:
t = self._table(table_name,
schema.Column(column_name, existing_type)
)
for constraint in t.constraints:
- if not isinstance(constraint, schema.PrimaryKeyConstraint):
+ if _count_constraint(constraint):
self.impl.drop_constraint(constraint)
self.impl.alter_column(table_name, column_name,
if type_:
t = self._table(table_name, schema.Column(column_name, type_))
for constraint in t.constraints:
- if not isinstance(constraint, schema.PrimaryKeyConstraint):
+ if _count_constraint(constraint):
self.impl.add_constraint(constraint)
def add_column(self, table_name, column):
'ALTER TABLE t ALTER COLUMN c INTEGER'
)
+ def test_alter_column_dont_touch_constraints(self):
+ context = op_fixture('mssql')
+ from sqlalchemy import Boolean
+ op.alter_column('tests', 'col',
+ existing_type=Boolean(),
+ nullable=False)
+ context.assert_('ALTER TABLE tests ALTER COLUMN col BIT NOT NULL')
+
def test_drop_index(self):
context = op_fixture('mssql')
op.drop_index('my_idx', 'my_table')
context = op_fixture('mssql')
op.alter_column("t", "c", name="c2", nullable=True, type_=Integer, server_default="5")
context.assert_(
- 'ALTER TABLE t ALTER COLUMN c INTEGER NULL',
- "ALTER TABLE t ADD DEFAULT '5' FOR c",
+ 'ALTER TABLE t ALTER COLUMN c INTEGER NULL',
+ "ALTER TABLE t ADD DEFAULT '5' FOR c",
"EXEC sp_rename 't.c', 'c2', 'COLUMN'"
)
context = op_fixture()
op.add_column('t1', Column('c1', Boolean, nullable=False))
context.assert_(
- 'ALTER TABLE t1 ADD COLUMN c1 BOOLEAN NOT NULL',
+ 'ALTER TABLE t1 ADD COLUMN c1 BOOLEAN NOT NULL',
'ALTER TABLE t1 ADD CHECK (c1 IN (0, 1))'
)
def test_add_column_schema_type_checks_rule():
- """Test that a schema type doesn't generate a
+ """Test that a schema type doesn't generate a
constraint based on check rule."""
context = op_fixture('postgresql')
op.add_column('t1', Column('c1', Boolean, nullable=False))
context.assert_(
- 'ALTER TABLE t1 ADD COLUMN c1 BOOLEAN NOT NULL',
+ 'ALTER TABLE t1 ADD COLUMN c1 BOOLEAN NOT NULL',
)
def test_add_column_fk_self_referential():
context = op_fixture()
op.add_column('t1', Column('c1', Integer, ForeignKey('remote.t2.c2'), nullable=False))
context.assert_(
- 'ALTER TABLE t1 ADD COLUMN c1 INTEGER NOT NULL',
+ 'ALTER TABLE t1 ADD COLUMN c1 INTEGER NOT NULL',
'ALTER TABLE t1 ADD FOREIGN KEY(c1) REFERENCES remote.t2 (c2)'
)
context = op_fixture()
op.alter_column("t", "c", nullable=True)
context.assert_(
- # TODO: not sure if this is PG only or standard
+ # TODO: not sure if this is PG only or standard
# SQL
"ALTER TABLE t ALTER COLUMN c DROP NOT NULL"
)
context = op_fixture()
op.alter_column("t", "c", nullable=False)
context.assert_(
- # TODO: not sure if this is PG only or standard
+ # TODO: not sure if this is PG only or standard
# SQL
"ALTER TABLE t ALTER COLUMN c SET NOT NULL"
)
'ALTER TABLE t ALTER COLUMN c VARCHAR(10)'
)
+def test_alter_column_schema_type_existing_type_no_const():
+ context = op_fixture('postgresql')
+ op.alter_column("t", "c", type_=String(10), existing_type=Boolean(name="xyz"))
+ context.assert_(
+ 'ALTER TABLE t ALTER COLUMN c TYPE VARCHAR(10)'
+ )
+
+def test_alter_column_schema_type_existing_type_no_new_type():
+ context = op_fixture('postgresql')
+ op.alter_column("t", "c", nullable=False, existing_type=Boolean(name="xyz"))
+ context.assert_(
+ 'ALTER TABLE t ALTER COLUMN c SET NOT NULL'
+ )
+
def test_add_foreign_key():
context = op_fixture()
- op.create_foreign_key('fk_test', 't1', 't2',
+ op.create_foreign_key('fk_test', 't1', 't2',
['foo', 'bar'], ['bat', 'hoho'])
context.assert_(
"ALTER TABLE t1 ADD CONSTRAINT fk_test FOREIGN KEY(foo, bar) "
def test_create_table_selfref():
context = op_fixture()
op.create_table(
- "some_table",
+ "some_table",
Column('id', Integer, primary_key=True),
Column('st_id', Integer, ForeignKey('some_table.id'))
)
def test_create_table_fk_and_schema():
context = op_fixture()
op.create_table(
- "some_table",
+ "some_table",
Column('id', Integer, primary_key=True),
Column('foo_id', Integer, ForeignKey('foo.id')),
schema='schema'
def test_create_table_two_fk():
context = op_fixture()
op.create_table(
- "some_table",
+ "some_table",
Column('id', Integer, primary_key=True),
Column('foo_id', Integer, ForeignKey('foo.id')),
Column('foo_bar', Integer, ForeignKey('foo.bar')),
from sqlalchemy.sql import table, column
from sqlalchemy import String, Integer
- account = table('account',
+ account = table('account',
column('name', String),
column('id', Integer)
)