def drop_table(self, table):
raise NotImplementedError("Can't drop table in batch mode")
+ def create_column_comment(self, column):
+ self.batch.append(("create_column_comment", (column,), {}))
+
class ApplyBatchImpl(object):
def __init__(
name=None,
type_=None,
autoincrement=None,
+ comment=False,
**kw
):
existing = self.columns[column_name]
if autoincrement is not None:
existing.autoincrement = bool(autoincrement)
+ if comment is not False:
+ existing.comment = comment
+
def _setup_dependencies_for_add_column(
self, colname, insert_before, insert_after
):
del self.column_transfers[column.name]
self.existing_ordering.remove(column.name)
+ def create_column_comment(self, column):
+ """the batch table creation function will issue create_column_comment
+ on the real "impl" as part of the create table process.
+
+ That is, the Column object will have the comment on it already,
+ so when it is received by add_column() it will be a normal part of
+ the CREATE TABLE and doesn't need an extra step here.
+
+ """
+
def add_constraint(self, const):
if not const.name:
raise ValueError("Constraint must have a name")
new_table = self._assert_impl(impl)
eq_(new_table.c.x.name, "q")
+ def test_alter_column_comment(self):
+ impl = self._simple_fixture()
+ impl.alter_column("tname", "x", comment="some comment")
+ new_table = self._assert_impl(impl)
+ eq_(new_table.c.x.comment, "some comment")
+
+ def test_add_column_comment(self):
+ impl = self._simple_fixture()
+ impl.add_column("tname", Column("q", Integer, comment="some comment"))
+ new_table = self._assert_impl(impl, colnames=["id", "x", "y", "q"])
+ eq_(new_table.c.q.comment, "some comment")
+
def test_rename_col_boolean(self):
impl = self._boolean_fixture()
impl.alter_column("tname", "flag", name="bflag")
]
)
+ def _assert_column_comment(self, tname, cname, comment):
+ insp = inspect(config.db)
+
+ cols = {col["name"]: col for col in insp.get_columns(tname)}
+ eq_(cols[cname]["comment"], comment)
+
+ @config.requirements.comments
+ def test_add_column_comment(self):
+ with self.op.batch_alter_table("foo") as batch_op:
+ batch_op.add_column(Column("y", Integer, comment="some comment"))
+
+ self._assert_column_comment("foo", "y", "some comment")
+
+ self._assert_data(
+ [
+ {"id": 1, "data": "d1", "x": 5, "y": None},
+ {"id": 2, "data": "22", "x": 6, "y": None},
+ {"id": 3, "data": "8.5", "x": 7, "y": None},
+ {"id": 4, "data": "9.46", "x": 8, "y": None},
+ {"id": 5, "data": "d5", "x": 9, "y": None},
+ ]
+ )
+
+ @config.requirements.comments
+ def test_add_column_comment_recreate(self):
+ with self.op.batch_alter_table("foo", recreate="always") as batch_op:
+ batch_op.add_column(Column("y", Integer, comment="some comment"))
+
+ self._assert_column_comment("foo", "y", "some comment")
+
+ self._assert_data(
+ [
+ {"id": 1, "data": "d1", "x": 5, "y": None},
+ {"id": 2, "data": "22", "x": 6, "y": None},
+ {"id": 3, "data": "8.5", "x": 7, "y": None},
+ {"id": 4, "data": "9.46", "x": 8, "y": None},
+ {"id": 5, "data": "d5", "x": 9, "y": None},
+ ]
+ )
+
+ @config.requirements.comments
+ def test_alter_column_comment(self):
+ with self.op.batch_alter_table("foo") as batch_op:
+ batch_op.alter_column(
+ "x", existing_type=Integer(), comment="some comment"
+ )
+
+ self._assert_column_comment("foo", "x", "some comment")
+
+ self._assert_data(
+ [
+ {"id": 1, "data": "d1", "x": 5},
+ {"id": 2, "data": "22", "x": 6},
+ {"id": 3, "data": "8.5", "x": 7},
+ {"id": 4, "data": "9.46", "x": 8},
+ {"id": 5, "data": "d5", "x": 9},
+ ]
+ )
+
+ @config.requirements.comments
+ def test_alter_column_comment_recreate(self):
+ with self.op.batch_alter_table("foo", recreate="always") as batch_op:
+ batch_op.alter_column("x", comment="some comment")
+
+ self._assert_column_comment("foo", "x", "some comment")
+
+ self._assert_data(
+ [
+ {"id": 1, "data": "d1", "x": 5},
+ {"id": 2, "data": "22", "x": 6},
+ {"id": 3, "data": "8.5", "x": 7},
+ {"id": 4, "data": "9.46", "x": 8},
+ {"id": 5, "data": "d5", "x": 9},
+ ]
+ )
+
def test_rename_column(self):
with self.op.batch_alter_table("foo") as batch_op:
batch_op.alter_column("x", new_column_name="y")