Skip to content

Commit 91be693

Browse files
committed
Merge pull request #1503 from dhermes/happybase-delete-table
Adding HappyBase Connection.delete_table().
2 parents 7de859f + 82ff259 commit 91be693

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

gcloud/bigtable/happybase/connection.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
from gcloud.bigtable.client import Client
2323
from gcloud.bigtable.happybase.table import Table
24+
from gcloud.bigtable.table import Table as _LowLevelTable
2425

2526

2627
# Constants reproduced here for HappyBase compatibility, though values
@@ -264,6 +265,30 @@ def tables(self):
264265

265266
return table_names
266267

268+
def delete_table(self, name, disable=False):
269+
"""Delete the specified table.
270+
271+
:type name: str
272+
:param name: The name of the table to be deleted. If ``table_prefix``
273+
is set, a prefix will be added to the ``name``.
274+
275+
:type disable: bool
276+
:param disable: Whether to first disable the table if needed. This
277+
is provided for compatibility with HappyBase, but is
278+
not relevant for Cloud Bigtable since it has no concept
279+
of enabled / disabled tables.
280+
281+
:raises: :class:`ValueError <exceptions.ValueError>`
282+
if ``disable=True``.
283+
"""
284+
if disable:
285+
raise ValueError('The disable argument should not be used in '
286+
'delete_table(). Cloud Bigtable has no concept '
287+
'of enabled / disabled tables.')
288+
289+
name = self._table_name(name)
290+
_LowLevelTable(name, self._cluster).delete()
291+
267292
def enable_table(self, name):
268293
"""Enable the specified table.
269294

gcloud/bigtable/happybase/test_connection.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,37 @@ def test_tables_with_prefix(self):
311311
result = connection.tables()
312312
self.assertEqual(result, [unprefixed_table_name1])
313313

314+
def test_delete_table(self):
315+
from gcloud._testing import _Monkey
316+
from gcloud.bigtable.happybase import connection as MUT
317+
318+
cluster = _Cluster() # Avoid implicit environ check.
319+
connection = self._makeOne(autoconnect=False, cluster=cluster)
320+
321+
tables_created = []
322+
323+
def make_table(*args, **kwargs):
324+
result = _MockLowLevelTable(*args, **kwargs)
325+
tables_created.append(result)
326+
return result
327+
328+
name = 'table-name'
329+
with _Monkey(MUT, _LowLevelTable=make_table):
330+
connection.delete_table(name)
331+
332+
# Just one table would have been created.
333+
table_instance, = tables_created
334+
self.assertEqual(table_instance.args, (name, cluster))
335+
self.assertEqual(table_instance.kwargs, {})
336+
self.assertEqual(table_instance.delete_calls, 1)
337+
338+
def test_delete_table_disable(self):
339+
cluster = _Cluster() # Avoid implicit environ check.
340+
connection = self._makeOne(autoconnect=False, cluster=cluster)
341+
name = 'table-name'
342+
with self.assertRaises(ValueError):
343+
connection.delete_table(name, disable=True)
344+
314345
def test_enable_table(self):
315346
cluster = _Cluster() # Avoid implicit environ check.
316347
connection = self._makeOne(autoconnect=False, cluster=cluster)
@@ -385,3 +416,14 @@ def copy(self):
385416

386417
def list_tables(self):
387418
return self.list_tables_result
419+
420+
421+
class _MockLowLevelTable(object):
422+
423+
def __init__(self, *args, **kwargs):
424+
self.args = args
425+
self.kwargs = kwargs
426+
self.delete_calls = 0
427+
428+
def delete(self):
429+
self.delete_calls += 1

0 commit comments

Comments
 (0)