summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/sqlalchemy/dialects/firebird/base.py2
-rw-r--r--lib/sqlalchemy/dialects/postgresql/base.py24
-rw-r--r--lib/sqlalchemy/engine/ddl.py4
3 files changed, 20 insertions, 10 deletions
diff --git a/lib/sqlalchemy/dialects/firebird/base.py b/lib/sqlalchemy/dialects/firebird/base.py
index 4c2c7f913..232583c39 100644
--- a/lib/sqlalchemy/dialects/firebird/base.py
+++ b/lib/sqlalchemy/dialects/firebird/base.py
@@ -378,7 +378,7 @@ class FBDialect(default.DefaultDialect):
c = connection.execute(tblqry, [self.denormalize_name(table_name)])
return c.first() is not None
- def has_sequence(self, connection, sequence_name):
+ def has_sequence(self, connection, sequence_name, schema=None):
"""Return ``True`` if the given sequence (generator) exists."""
genqry = """
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py
index 0bc5f08b0..061c3b066 100644
--- a/lib/sqlalchemy/dialects/postgresql/base.py
+++ b/lib/sqlalchemy/dialects/postgresql/base.py
@@ -539,13 +539,23 @@ class PGDialect(default.DefaultDialect):
)
return bool(cursor.first())
- def has_sequence(self, connection, sequence_name):
- cursor = connection.execute(
- sql.text("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)",
- bindparams=[sql.bindparam('seqname', unicode(sequence_name), type_=sqltypes.Unicode)]
- ))
+ def has_sequence(self, connection, sequence_name, schema=None):
+ if schema is None:
+ cursor = connection.execute(
+ sql.text("SELECT relname FROM pg_class c join pg_namespace n on "
+ "n.oid=c.relnamespace where relkind='S' and n.nspname=current_schema() and lower(relname)=:name",
+ bindparams=[sql.bindparam('name', unicode(sequence_name.lower()), type_=sqltypes.Unicode)]
+ )
+ )
+ else:
+ cursor = connection.execute(
+ sql.text("SELECT relname FROM pg_class c join pg_namespace n on "
+ "n.oid=c.relnamespace where relkind='S' and n.nspname=:schema and lower(relname)=:name",
+ bindparams=[sql.bindparam('name', unicode(sequence_name.lower()), type_=sqltypes.Unicode),
+ sql.bindparam('schema', unicode(schema), type_=sqltypes.Unicode)]
+ )
+ )
+
return bool(cursor.first())
def table_names(self, connection, schema):
diff --git a/lib/sqlalchemy/engine/ddl.py b/lib/sqlalchemy/engine/ddl.py
index 6e7253e9a..f5cff1a20 100644
--- a/lib/sqlalchemy/engine/ddl.py
+++ b/lib/sqlalchemy/engine/ddl.py
@@ -66,7 +66,7 @@ class SchemaGenerator(DDLBase):
if ((not self.dialect.sequences_optional or
not sequence.optional) and
(not self.checkfirst or
- not self.dialect.has_sequence(self.connection, sequence.name))):
+ not self.dialect.has_sequence(self.connection, sequence.name, schema=sequence.schema))):
self.connection.execute(schema.CreateSequence(sequence))
def visit_index(self, index):
@@ -124,5 +124,5 @@ class SchemaDropper(DDLBase):
if ((not self.dialect.sequences_optional or
not sequence.optional) and
(not self.checkfirst or
- self.dialect.has_sequence(self.connection, sequence.name))):
+ self.dialect.has_sequence(self.connection, sequence.name, schema=sequence.schema))):
self.connection.execute(schema.DropSequence(sequence))