new orm class method table_ensure

This commit is contained in:
Noah Levitt 2017-03-01 11:40:12 -08:00
parent c14bae6050
commit 246dfb5b7e
2 changed files with 18 additions and 1 deletions

View File

@ -175,6 +175,22 @@ class Document(dict, object):
'''
rr.table_create(cls.table).run()
@classmethod
def table_ensure(cls, rr):
'''
Creates the table if it doesn't exist.
'''
dbs = rr.db_list().run()
if not rr.dbname in dbs:
logging.info('creating rethinkdb database %s', repr(rr.dbname))
rr.db_create(rr.dbname).run()
tables = rr.table_list().run()
if not cls.table in tables:
logging.info(
'creating rethinkdb table %s in database %s',
repr(cls.table), repr(rr.dbname))
cls.table_create(rr)
def __init__(self, rr, d={}):
dict.__setattr__(self, 'rr', rr)
self._pk = None

View File

@ -284,6 +284,7 @@ def test_orm(rr):
table = 'some_doc'
SomeDoc.table_create(rr)
SomeDoc.table_ensure(rr)
with pytest.raises(Exception):
SomeDoc.table_create(rr)
@ -467,7 +468,7 @@ def test_orm_pk(rr):
with pytest.raises(Exception):
NonstandardPrimaryKey.load(rr, 'no_such_thing')
NonstandardPrimaryKey.table_create(rr)
NonstandardPrimaryKey.table_ensure(rr)
# new empty doc
f = NonstandardPrimaryKey(rr, {})