diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-07-19 22:20:49 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2006-07-19 22:20:49 +0000 |
commit | 934ac330ea812395b66f82e13afc034303bafc9c (patch) | |
tree | 0d664563ba0bac2aa78f880c5b16dc6a3e313d28 | |
parent | 8b50115acc7d249129aa8046839eefa2c611c453 (diff) | |
download | sqlalchemy-934ac330ea812395b66f82e13afc034303bafc9c.tar.gz |
added table.exists()
-rw-r--r-- | CHANGES | 3 | ||||
-rw-r--r-- | lib/sqlalchemy/schema.py | 10 | ||||
-rw-r--r-- | test/engine/reflection.py | 3 |
3 files changed, 15 insertions, 1 deletions
@@ -44,7 +44,8 @@ aware of their "inherited" status if so. relationships to an inheriting mapper (which is also self-referential) - reduced bind param size in query._get to appease the picky oracle [ticket:244] -- added 'checkfirst' argument to table.create()/table.drop() +- added 'checkfirst' argument to table.create()/table.drop(), as +well as table.exists() [ticket:234] 0.2.5 - fixed endless loop bug in select_by(), if the traversal hit diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 8fce1665d..6b44cda29 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -236,6 +236,16 @@ class Table(SchemaItem, sql.TableClause): this does not issue a SQL DROP statement.""" key = _get_table_key(self.name, self.schema) del self.metadata.tables[key] + + def exists(self, engine=None): + if engine is None and self.metadata.is_bound(): + engine = self.engine + + def do(conn): + e = conn.engine + return e.dialect.has_table(conn, self.name) + return engine.run_callable(do) + def create(self, connectable=None, checkfirst=False): if connectable is not None: connectable.create(self, checkfirst=checkfirst) diff --git a/test/engine/reflection.py b/test/engine/reflection.py index 64a645d0f..582920b30 100644 --- a/test/engine/reflection.py +++ b/test/engine/reflection.py @@ -162,10 +162,13 @@ class ReflectionTest(PersistTest): Column('col1', Integer, primary_key=True), Column('col2', String(40))) try: + assert not table.exists() table.create() + assert table.exists() table.create(checkfirst=True) table.drop() table.drop(checkfirst=True) + assert not table.exists() table.create(checkfirst=True) table.drop() finally: |