Guest User

Untitled

a guest
Jun 17th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. require "active_record"
  2. require 'logger'
  3.  
  4. namespace :db do
  5. desc "Migrate the database ('VERSION=YYYMMDDHHIISS' migrates to specified Version)"
  6. task(:migrate => :environment) do
  7. log = Logger.new(STDOUT)
  8. log.level = Logger::INFO
  9. ActiveRecord::Base.logger = log
  10. ActiveRecord::Migration.verbose = true
  11. ActiveRecord::Migrator.migrate("db/migrate", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
  12. Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
  13. end
  14.  
  15. namespace :schema do
  16. desc "Create a db/schema.rb file that can be portably used against any DB supported by AR (with 'SCHEMA=filename' the schema-file can be specified)"
  17. task :dump => :environment do
  18. require 'active_record/schema_dumper'
  19. File.open(ENV['SCHEMA'] || "db/schema.rb", "w") do |file|
  20. ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
  21. end
  22. Rake::Task["db:schema:dump"].reenable
  23. end
  24.  
  25. desc "Load a schema.rb file into the database (with 'SCHEMA=filename' the schema-file can be specified)"
  26. task :load => :environment do
  27. file = ENV['SCHEMA'] || "db/schema.rb"
  28. if File.exists?(file)
  29. load(file)
  30. else
  31. abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again.}
  32. end
  33. end
  34. end
  35. end
  36.  
  37. task :environment do
  38. config_file = File.read("pfad/zur/config-datei")
  39. ActiveRecord::Base.establish_connection(
  40. :adapter => "mysql",
  41. :host => config_file.match(%r(.*\$GLOBAL_CONFIG\[.db.\]\[.host.\]\s*=\s*.(.*).;\s*\n))[1],
  42. :username => config_file.match(%r(.*\$GLOBAL_CONFIG\[.db.\]\[.user.\]\s*=\s*.(.*).;\s*\n))[1],
  43. :password => config_file.match(%r(.*\$GLOBAL_CONFIG\[.db.\]\[.pass.\]\s*=\s*.(.*).;\s*\n))[1],
  44. :database => config_file.match(%r(.*\$GLOBAL_CONFIG\[.db.\]\[.name.\]\s*=\s*.(.*).;\s*\n))[1]
  45. )
  46. end
Add Comment
Please, Sign In to add comment