Skip to content

Commit

Permalink
Added Database.sorted_classes property to return ORMs in the correc…
Browse files Browse the repository at this point in the history
…t order based on foreign key constraints
  • Loading branch information
mgilligan committed Mar 18, 2014
1 parent e8efc58 commit cf7550f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 16 additions & 0 deletions gtfsdb/model/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
class Database(object):

def __init__(self, **kwargs):
'''
keyword arguments:
url: SQLAlchemy database url
schema: database schema name
is_geospatial: if database supports geo functions
'''
self.url = kwargs.get('url', config.DEFAULT_DATABASE_URL)
self.schema = kwargs.get('schema', config.DEFAULT_SCHEMA)
self.is_geospatial = kwargs.get('is_geospatial',
Expand Down Expand Up @@ -60,6 +66,16 @@ def schema(self, val):
for cls in self.classes:
cls.__table__.schema = val

@property
def sorted_classes(self):
classes = []
for t in self.metadata.sorted_tables:
cls = next((c for c in self.classes
if c.__table__ == t), None)
if cls:
classes.append(cls)
return classes

@property
def url(self):
return self._url
Expand Down
2 changes: 1 addition & 1 deletion gtfsdb/model/gtfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def load(self, db, **kwargs):
batch_size=kwargs.get('batch_size', config.DEFAULT_BATCH_SIZE),
gtfs_directory=gtfs_directory,
)
for cls in db.classes:
for cls in db.sorted_classes:
cls.load(db, **load_kwargs)
shutil.rmtree(gtfs_directory)

Expand Down

0 comments on commit cf7550f

Please sign in to comment.