summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/sqlalchemy/orm/mapper.py2
-rw-r--r--lib/sqlalchemy/schema.py15
-rw-r--r--lib/sqlalchemy/sql/compiler.py14
-rw-r--r--lib/sqlalchemy/sql/util.py8
4 files changed, 26 insertions, 13 deletions
diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py
index 3e5f418fa..d1713e6f9 100644
--- a/lib/sqlalchemy/orm/mapper.py
+++ b/lib/sqlalchemy/orm/mapper.py
@@ -1314,7 +1314,7 @@ class Mapper(object):
for t in mapper.tables:
table_to_mapper[t] = mapper
- for table in sqlutil.sort_tables(table_to_mapper.keys(), reverse=True):
+ for table in reversed(sqlutil.sort_tables(table_to_mapper.keys())):
delete = {}
for state, mapper, connection in tups:
if table not in mapper._pks_by_table:
diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py
index f6e55581f..bb97943eb 100644
--- a/lib/sqlalchemy/schema.py
+++ b/lib/sqlalchemy/schema.py
@@ -1534,14 +1534,23 @@ class MetaData(SchemaItem):
# TODO: scan all other tables and remove FK _column
del self.tables[table.key]
+ @util.deprecated('Deprecated. Use ``metadata.sorted_tables``')
def table_iterator(self, reverse=True, tables=None):
from sqlalchemy.sql.util import sort_tables
if tables is None:
tables = self.tables.values()
else:
tables = set(tables).intersection(self.tables.values())
- return iter(sort_tables(tables, reverse=reverse))
-
+ ret = sort_tables(tables)
+ if reverse:
+ ret = reversed(ret)
+ return iter(ret)
+
+ @property
+ def sorted_tables(self):
+ from sqlalchemy.sql.util import sort_tables
+ return sort_tables(self.tables.values())
+
def reflect(self, bind=None, schema=None, only=None):
"""Load all available table definitions from the database.
@@ -1660,8 +1669,8 @@ class MetaData(SchemaItem):
checkfirst
Defaults to True, don't issue CREATEs for tables already present
in the target database.
+
"""
-
if bind is None:
bind = _bind_or_error(self)
for listener in self.ddl_listeners['before-create']:
diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py
index 4a47df941..eacbe59e1 100644
--- a/lib/sqlalchemy/sql/compiler.py
+++ b/lib/sqlalchemy/sql/compiler.py
@@ -20,7 +20,7 @@ is otherwise internal to SQLAlchemy.
import string, re
from sqlalchemy import schema, engine, util, exc
-from sqlalchemy.sql import operators, functions
+from sqlalchemy.sql import operators, functions, util as sql_util
from sqlalchemy.sql import expression as sql
RESERVED_WORDS = set([
@@ -787,7 +787,11 @@ class SchemaGenerator(DDLBase):
return not self.checkfirst or not self.dialect.has_table(self.connection, table.name, schema=table.schema)
def visit_metadata(self, metadata):
- collection = [t for t in metadata.table_iterator(reverse=False, tables=self.tables) if self._can_create(t)]
+ if self.tables:
+ tables = self.tables
+ else:
+ tables = metadata.tables.values()
+ collection = [t for t in sql_util.sort_tables(tables) if self._can_create(t)]
for table in collection:
self.traverse_single(table)
if self.dialect.supports_alter:
@@ -950,7 +954,11 @@ class SchemaDropper(DDLBase):
self.dialect = dialect
def visit_metadata(self, metadata):
- collection = [t for t in metadata.table_iterator(reverse=True, tables=self.tables) if self._can_drop(t)]
+ if self.tables:
+ tables = self.tables
+ else:
+ tables = metadata.tables.values()
+ collection = [t for t in reversed(sql_util.sort_tables(tables)) if self._can_drop(t)]
if self.dialect.supports_alter:
for alterable in self.find_alterables(collection):
self.drop_foreignkey(alterable)
diff --git a/lib/sqlalchemy/sql/util.py b/lib/sqlalchemy/sql/util.py
index ba6b5a605..e1636ccf9 100644
--- a/lib/sqlalchemy/sql/util.py
+++ b/lib/sqlalchemy/sql/util.py
@@ -4,7 +4,7 @@ from itertools import chain
"""Utility functions that build upon SQL and Schema constructs."""
-def sort_tables(tables, reverse=False):
+def sort_tables(tables):
"""sort a collection of Table objects in order of their foreign-key dependency."""
tuples = []
@@ -18,11 +18,7 @@ def sort_tables(tables, reverse=False):
for table in tables:
visitors.traverse(table, {'schema_visitor':True}, {'foreign_key':visit_foreign_key})
- sequence = topological.sort(tuples, tables)
- if reverse:
- return reversed(sequence)
- else:
- return sequence
+ return topological.sort(tuples, tables)
def search(clause, target):
if not clause: