Write rake task to find and delete orphaned migration classes
When squashing migrations, ideally we should clean up all of the dependencies of those migrations. It is easy to automatically find and remove migrations and their respective rspec tests, however, finding other dependencies can be slightly more problematic.
Many times, custom classes are added to facilitate migrations, such as batched background migrations. We need a rake task to find all of these and git rm
them, such that a developer who is squashing migrations does not have to hand track and delete all of them, which is a tedious and error-prone process.
The suggested mechanism approach is the following:
- Assemble a list of folders containing migration dependencies (for now,
lib/gitlab/background_migration
is the foremost example) - For each folder, capture the class name. One approach is to use regular expressions, another is to break each file's code into a Ruby abstract syntax tree and find the class declaration
- For each migration folder, iterate over every ruby file therein and look for an invocation of any of the classes gathered in step 2. If it's found, mark it as "used".
- Any classes in the list from step 2, minus the "used" classes, equals the classes (and their specs) which we need to delete.