Try breaking this function into smaller ones. This actually seems to be a good candidate for using pipes:

```elixir
File.ls(directory)
|> filter_migrations
|> attach_versions
|> ensure_no_duplication
|> remove_already_up(repo.adapter.migrated_versions(repo))
|> run_migrations
```