Guest User

Untitled

a guest
Mar 9th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. namespace :db do
  2. desc "Migrate all the databases found in sitesdb through scripts in db/migrate. Target specific version with VERSION=x"
  3. task :migrate_all => :environment do
  4.  
  5. adapter = "mysql"
  6. host = "mysql_host"
  7. username = "mysql_user"
  8. password = "mysql_pass"
  9. sitesdb = "projects"
  10. prefix = "project_"
  11.  
  12. # there is a #{sitesdb}.sites table with string column database
  13. class Site < ActiveRecord::Base
  14. end
  15.  
  16. ActiveRecord::Base.establish_connection :adapter => adapter,
  17. :host => host,
  18. :username => username,
  19. :password => password,
  20. :database => sitesdb
  21.  
  22. sites = Site.find(:all)
  23. sites.each do |site|
  24. ActiveRecord::Base.establish_connection :adapter => adapter,
  25. :host => host,
  26. :username => username,
  27. :password => password,
  28. :database => "#{prefix}#{site.database}"
  29. ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
  30. Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
  31. end
  32. end
  33. end
Add Comment
Please, Sign In to add comment