Returns a dictionary of database => [migrations] representing all
    migrations contained in ``path``.

def get_all_migrations(path, databases=None):
    """
    Returns a dictionary of database => [migrations] representing all
    migrations contained in ``path``.
    """
    # database: [(number, full_path)]
    possible_migrations = defaultdict(list)
    
    try:
        in_directory = sorted(get_file_list(path))
    except OSError:
        import traceback
        print "An error occurred while reading migrations from %r:" % path
        traceback.print_exc()
        return {}
    
    # Iterate through our results and discover which migrations are
    # actually runnable
    for full_path in in_directory:
        child_path, script = os.path.split(full_path)
        name, ext = os.path.splitext(script)
        
        # the database component is default if this is in the root directory
        # is <directory> if in a subdirectory
        if path == child_path:
            db = DEFAULT_DB_ALIAS
        else:
            db = os.path.split(child_path)[-1]
        
        # filter by database if set
        if databases and db not in databases:
            continue
        
        match = MIGRATION_NAME_RE.match(name)
        if match is None:
            raise MigrationError("Invalid migration file prefix %r "
                                 "(must begin with a number)" % name)
        
        number = int(match.group(1))
        if ext in [".sql", ".py"]:
            possible_migrations[db].append((number, full_path))
    
    return possible_migrations