from alembic.testing import TestBase
from alembic.testing.fixtures import op_fixture
from alembic.util import exc as alembic_exc
+from alembic.util.sqla_compat import _safe_commit_connection_transaction
from alembic.util.sqla_compat import _select
from alembic.util.sqla_compat import has_computed
from alembic.util.sqla_compat import has_identity
context = MigrationContext.configure(self.conn)
self.op = Operations(context)
+ def tearDown(self):
+ # why commit? because SQLite has inconsistent treatment
+ # of transactional DDL. A test that runs CREATE TABLE and then
+ # ALTER TABLE to change the name of that table, will end up
+ # committing the CREATE TABLE but not the ALTER. As batch mode
+ # does this with a temp table name that's not even in the
+ # metadata collection, we don't have an explicit drop for it
+ # (though we could do that too). calling commit means the
+ # ALTER will go through and the drop_all() will then catch it.
+ _safe_commit_connection_transaction(self.conn)
+ with self.conn.begin():
+ self.metadata.drop_all(self.conn)
+ self.conn.close()
+
@contextmanager
def _sqlite_referential_integrity(self):
self.conn.exec_driver_sql("PRAGMA foreign_keys=ON")
type_=Integer,
existing_type=Boolean(create_constraint=True, name="ck1"),
)
- insp = inspect(config.db)
+ insp = inspect(self.conn)
eq_(
[
batch_op.drop_column(
"x", existing_type=Boolean(create_constraint=True, name="ck1")
)
- insp = inspect(config.db)
+ insp = inspect(self.conn)
assert "x" not in (c["name"] for c in insp.get_columns("hasbool"))
batch_op.alter_column(
"x", type_=Boolean(create_constraint=True, name="ck1")
)
- insp = inspect(config.db)
+ insp = inspect(self.conn)
if exclusions.against(config, "sqlite"):
eq_(
[Integer],
)
- def tearDown(self):
- in_t = getattr(self.conn, "in_transaction", lambda: False)
- if in_t():
- self.conn.rollback()
- with self.conn.begin():
- self.metadata.drop_all(self.conn)
- self.conn.close()
-
def _assert_data(self, data, tablename="foo"):
res = self.conn.execute(text("select * from %s" % tablename))
if sqla_14:
batch_op.alter_column("data", type_=String(30))
batch_op.create_index("ix_data", ["data"])
- insp = inspect(config.db)
+ insp = inspect(self.conn)
eq_(
set(
(ix["name"], tuple(ix["column_names"]))
)
def _assert_table_comment(self, tname, comment):
- insp = inspect(config.db)
+ insp = inspect(self.conn)
tcomment = insp.get_table_comment(tname)
eq_(tcomment, {"text": comment})
self._assert_table_comment("foo", None)
def _assert_column_comment(self, tname, cname, comment):
- insp = inspect(config.db)
+ insp = inspect(self.conn)
cols = {col["name"]: col for col in insp.get_columns(tname)}
eq_(cols[cname]["comment"], comment)
]
)
eq_(
- [col["name"] for col in inspect(config.db).get_columns("foo")],
+ [col["name"] for col in inspect(self.conn).get_columns("foo")],
["id", "data", "x", "data2"],
)
]
)
eq_(
- [col["name"] for col in inspect(config.db).get_columns("foo")],
+ [col["name"] for col in inspect(self.conn).get_columns("foo")],
["id", "data", "x", "data2"],
)
tablename="nopk",
)
eq_(
- [col["name"] for col in inspect(config.db).get_columns("foo")],
+ [col["name"] for col in inspect(self.conn).get_columns("foo")],
["id", "data", "x"],
)
]
)
eq_(
- [col["name"] for col in inspect(config.db).get_columns("foo")],
+ [col["name"] for col in inspect(self.conn).get_columns("foo")],
["id", "data2", "data", "x"],
)
]
)
eq_(
- [col["name"] for col in inspect(config.db).get_columns("foo")],
+ [col["name"] for col in inspect(self.conn).get_columns("foo")],
["id", "data", "data2", "x"],
)
]
)
eq_(
- [col["name"] for col in inspect(config.db).get_columns("foo")],
+ [col["name"] for col in inspect(self.conn).get_columns("foo")],
["id", "data", "x", "data2"],
)
def test_create_drop_index(self):
- insp = inspect(config.db)
+ insp = inspect(self.conn)
eq_(insp.get_indexes("foo"), [])
with self.op.batch_alter_table("foo", recreate="always") as batch_op:
{"id": 5, "data": "d5", "x": 9},
]
)
-
- insp = inspect(config.db)
+ insp = inspect(self.conn)
eq_(
[
dict(
with self.op.batch_alter_table("foo", recreate="always") as batch_op:
batch_op.drop_index("ix_data")
- insp = inspect(config.db)
+ insp = inspect(self.conn)
eq_(insp.get_indexes("foo"), [])
) as batch_op:
batch_op.add_column(Column("data", Integer))
- insp = inspect(config.db)
+ insp = inspect(self.conn)
eq_(
[