Class: DbHelper
- Inherits:
-
Object
- Object
- DbHelper
- Defined in:
- lib/db_helper.rb
Class Method Summary collapse
- .dump!(options = {}) ⇒ Object
- .fork!(options = {}) ⇒ Object
- .get_configs ⇒ Object
- .load!(options = {}) ⇒ Object
- .save_db_config! ⇒ Object
Class Method Details
.dump!(options = {}) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/db_helper.rb', line 34 def self.dump!(={}) @db_config.keys.each do |env| if (([:branch]==:all) || env.to_s.start_with?([:branch].to_s)) param = @db_config[env] dumpname = [:file] dumpname = File.join([@dump_store, dumpname]) unless [:relative] p "dump <#{env}> to file: #{dumpname}" # pp param system " PGPASSWORD='#{param[:password]}' pg_dump -U #{param[:username]} -h #{param[:host]} #{param[:database]} > #{dumpname} " end end end |
.fork!(options = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/db_helper.rb', line 50 def self.fork!(={}) dumps = {} @db_config.keys.each do |env| if (([:branch]==:all) || env.to_s.start_with?([:branch].to_s)) param = @db_config[env] = Time.now.strftime('%Y%m%dT%H%M%S%z') dumps[env] = File.join([@dump_store, "#{param[:database]}_#{timestamp}.sql"]) p "dump <#{env}> to file: #{dumps[env]}" # pp param system " PGPASSWORD='#{param[:password]}' pg_dump -U #{param[:username]} -h #{param[:host]} #{param[:database]} > #{dumps[env]} " new_db_name = [:name] + param[:database].gsub(/.+?(_db)?(_test)?(_db)?/i, '\1\2\3') @db_config[env][:database] = new_db_name end end p "All backups created!" save_db_config! @db_config.keys.each do |env| if (([:branch]==:all) || env.to_s.start_with?([:branch].to_s)) param = @db_config[env] p "load <#{env}> from file: #{dumps[env]}" # pp param system "RAILS_ENV=#{env} bundle exec rake db:drop" system "RAILS_ENV=#{env} bundle exec rake db:create" system "RAILS_ENV=#{env} bundle exec rake db:migrate" system " PGPASSWORD='#{param[:password]}' psql -U #{param[:username]} -h #{param[:host]} #{param[:database]} < #{dumps[env]} " end end end |
.get_configs ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/db_helper.rb', line 9 def self.get_configs() @working_dir = Dir.pwd @sysdir = File.(File.join(File.dirname(__FILE__), '..')) unless File.exists?(File.join(@sysdir, 'config.yml')) p "No config file. Creating default one." FileUtils.cp(File.join(@sysdir, 'config.yml.sample'), File.join(@sysdir, 'config.yml')) p "Please correct your config file at:" p "#{File.join(@sysdir, 'config.yml')}" end @config = get_hash_file File.join(@sysdir, 'config.yml') @db_yml_file = @config[:db_yml_file] @db_sample_file = @config[:db_sample_file] @dump_store = @config[:dump_store] @db_config = get_hash_file File.join(@working_dir, @db_yml_file) # pp @config # pp @db_config end |
.load!(options = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/db_helper.rb', line 88 def self.load!(={}) @db_config.keys.each do |env| if (([:branch]==:all) || env.to_s.start_with?([:branch].to_s)) param = @db_config[env] dumpname = [:file] dumpname = File.join([@dump_store, dumpname]) unless [:relative] p "load <#{env}> from file: #{dumpname}" # pp param if [:clean] system "bundle exec rake db:drop" system "bundle exec rake db:create" system "bundle exec rake db:migrate" end system " PGPASSWORD='#{param[:password]}' psql -U #{param[:username]} -h #{param[:host]} #{param[:database]} < #{dumpname} " end end end |
.save_db_config! ⇒ Object
30 31 32 |
# File 'lib/db_helper.rb', line 30 def self.save_db_config!() put_hash_file File.join(@working_dir, @db_yml_file), @db_config end |