Skip to content

Instantly share code, notes, and snippets.

@eamonn-webster
Last active December 18, 2015 04:19
Show Gist options
  • Save eamonn-webster/5724995 to your computer and use it in GitHub Desktop.
Save eamonn-webster/5724995 to your computer and use it in GitHub Desktop.
find and execute trove migrations
# load all migrations
Dir[Rails.root.join('db/migrate/*.rb')].each { |f| require f }
#find the trove migrations classes
# and the corresponding file
# sort by file
# instantiate
# and invoke change
# rescuing from any exception.
trove_migrations = Trove::BaseMigration.subclasses.collect { |c|
[c] + Dir[Rails.root.join("db/migrate/*#{c.name.tableize.singularize.gsub('trofe','trove')}*.rb")]
}.sort{ |a, b| a[1] <=> b[1] }.collect{ |pair| pair[0] }
trove_migrations.each { |c|
begin
mig = c.new
if mig.respond_to? :change
mig.change
else
mig.up
end
rescue
end
}
# TODO dump schema to trove_schema.rb
@terrafied
Copy link

  1. Do we have a file named trofe?
  2. Some of the migrations call up, not change.
  3. I'd make pretty sure I don't rescue from non-specific exceptions, otherwise you'd never know that the migration calls up instead of change, or that the migration failed because you already have the table, etc.
  4. Couldn't you just load only the trove migrations?

@eamonn-webster
Copy link
Author

  1. trofe is the singular of trove!
  2. yes I spotted that
  3. true but I'm lazy...
  4. we need to load them all to find which are subclasses of Trove::BaseMigration. However we could adopt the rule that all and only all trove migrations started (or ended) with trove.

@eamonn-webster
Copy link
Author

Next step would be to dump the schema,
then at start of tests create the trove schema from trove_schema.rb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment