still more cases of using the engine and not the executable in transaction

This commit is contained in:
Friedrich Lindenberg 2017-09-02 17:17:24 +02:00
parent 17ef44485d
commit cc7787036b
3 changed files with 29 additions and 20 deletions

View File

@ -44,11 +44,13 @@ class Table(object):
sure to get a fresh instance from the :py:class:`Database <dataset.Database>`. sure to get a fresh instance from the :py:class:`Database <dataset.Database>`.
""" """
self.database._acquire() self.database._acquire()
try:
self._is_dropped = True self._is_dropped = True
self.database._tables.pop(self.table.name, None) self.database._tables.pop(self.table.name, None)
self.table.drop(self.database.engine) self.table.drop(self.database.executable)
self.database._release()
return True return True
finally:
self.database._release()
def _check_dropped(self): def _check_dropped(self):
if self._is_dropped: if self._is_dropped:
@ -338,9 +340,11 @@ class Table(object):
if self.database.engine.dialect.name == 'sqlite': if self.database.engine.dialect.name == 'sqlite':
raise NotImplementedError("SQLite does not support dropping columns.") raise NotImplementedError("SQLite does not support dropping columns.")
self._check_dropped() self._check_dropped()
if name not in self.table.columns.keys():
log.debug("Column does not exist: %s", name)
return
self.database._acquire() self.database._acquire()
try: try:
if name in self.table.columns.keys():
self.database.op.drop_column( self.database.op.drop_column(
self.table.name, self.table.name,
name, name,
@ -379,7 +383,7 @@ class Table(object):
self.database._acquire() self.database._acquire()
columns = [self.table.c[c] for c in columns] columns = [self.table.c[c] for c in columns]
idx = Index(name, *columns, **kw) idx = Index(name, *columns, **kw)
idx.create(self.database.engine) idx.create(self.database.executable)
except: except:
idx = None idx = None
finally: finally:
@ -398,11 +402,13 @@ class Table(object):
row = table.find_one(country='United States') row = table.find_one(country='United States')
""" """
kwargs['_limit'] = 1 kwargs['_limit'] = 1
iterator = self.find(*args, **kwargs) kwargs['_step'] = None
resiter = self.find(*args, **kwargs)
try: try:
return next(iterator) for row in resiter:
except StopIteration: return row
return None finally:
resiter.close()
def _args_to_order_by(self, order_by): def _args_to_order_by(self, order_by):
if not isinstance(order_by, (list, tuple)): if not isinstance(order_by, (list, tuple)):

View File

@ -47,6 +47,7 @@ class ResultIter(object):
def __init__(self, result_proxy, row_type=row_type, step=None): def __init__(self, result_proxy, row_type=row_type, step=None):
self.row_type = row_type self.row_type = row_type
self.result_proxy = result_proxy
self.keys = list(result_proxy.keys()) self.keys = list(result_proxy.keys())
self._iter = iter_result_proxy(result_proxy, step=step) self._iter = iter_result_proxy(result_proxy, step=step)
@ -58,6 +59,9 @@ class ResultIter(object):
def __iter__(self): def __iter__(self):
return self return self
def close(self):
self.result_proxy.close()
def safe_url(url): def safe_url(url):
"""Remove password from printed connection URLs.""" """Remove password from printed connection URLs."""

View File

@ -22,8 +22,7 @@ class DatabaseTestCase(unittest.TestCase):
os.environ.setdefault('DATABASE_URL', 'sqlite:///:memory:') os.environ.setdefault('DATABASE_URL', 'sqlite:///:memory:')
self.db = connect(os.environ['DATABASE_URL']) self.db = connect(os.environ['DATABASE_URL'])
self.tbl = self.db['weather'] self.tbl = self.db['weather']
for row in TEST_DATA: self.tbl.insert_many(TEST_DATA)
self.tbl.insert(row)
def tearDown(self): def tearDown(self):
for table in self.db.tables: for table in self.db.tables:
@ -54,9 +53,9 @@ class DatabaseTestCase(unittest.TestCase):
assert table.table.exists() assert table.table.exists()
assert len(table.table.columns) == 1, table.table.columns assert len(table.table.columns) == 1, table.table.columns
assert pid in table.table.c, table.table.c assert pid in table.table.c, table.table.c
table.insert({pid: 'foobar'}) table.insert({pid: 'foobar'})
assert table.find_one(string_id='foobar')[pid] == 'foobar' assert table.find_one(string_id='foobar')[pid] == 'foobar'
table.drop()
def test_create_table_custom_id2(self): def test_create_table_custom_id2(self):
pid = "string_id" pid = "string_id"