From 698725a1a9bdd7abdc09c57beb727abf9f37fa78 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Wed, 16 Aug 2006 19:48:24 +0000 Subject: [ticket:277] check if pg/oracle sequence exists. checks in all cases before CREATE SEQUENCE/ DROP SEQUENCE --- lib/sqlalchemy/databases/postgres.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'lib/sqlalchemy/databases/postgres.py') diff --git a/lib/sqlalchemy/databases/postgres.py b/lib/sqlalchemy/databases/postgres.py index 4efe0e162..9adc42d02 100644 --- a/lib/sqlalchemy/databases/postgres.py +++ b/lib/sqlalchemy/databases/postgres.py @@ -279,9 +279,14 @@ class PGDialect(ansisql.ANSIDialect): return self.module def has_table(self, connection, table_name): + # TODO: why are we case folding here ? cursor = connection.execute("""select relname from pg_class where lower(relname) = %(name)s""", {'name':table_name.lower()}) return bool( not not cursor.rowcount ) + def has_sequence(self, connection, sequence_name): + cursor = connection.execute('''SELECT relname FROM pg_class WHERE relkind = 'S' AND relnamespace IN ( SELECT oid FROM pg_namespace WHERE nspname NOT LIKE 'pg_%%' AND nspname != 'information_schema' AND relname = %(seqname)s);''', {'seqname': sequence_name}) + return bool(not not cursor.rowcount) + def reflecttable(self, connection, table): if self.version == 2: ischema_names = pg2_ischema_names @@ -347,13 +352,13 @@ class PGSchemaGenerator(ansisql.ANSISchemaGenerator): return colspec def visit_sequence(self, sequence): - if not sequence.optional: + if not sequence.optional and not self.engine.dialect.has_sequence(self.connection, sequence.name): self.append("CREATE SEQUENCE %s" % sequence.name) self.execute() class PGSchemaDropper(ansisql.ANSISchemaDropper): def visit_sequence(self, sequence): - if not sequence.optional: + if not sequence.optional and self.engine.dialect.has_sequence(self.connection, sequence.name): self.append("DROP SEQUENCE %s" % sequence.name) self.execute() -- cgit v1.2.1