Advertisement
Guest User

Untitled

a guest
Feb 13th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.36 KB | None | 0 0
  1. Pry::Commands.block_command('connect_heroku_db', 'Connect to remote Heroku DB.') do |uri, wrap_in_transaction|
  2.   puts "Disconnecting current DB connection...".yellow
  3.   ActiveRecord::Base.remove_connection
  4.   adapter = uri.split("://")[0]
  5.   host = uri.split("@")[1].split(":")[0]
  6.   port = uri.split(":")[3].split("/")[0]
  7.   username = uri.split("://")[1].split(":")[0]
  8.   password = uri.split("://")[1].split(":")[1].split("@")[0]
  9.   database = uri.split("/")[3]
  10.   db_settings = {
  11.     adapter: "postgresql",
  12.     host: host,
  13.     port: port,
  14.     username: username,
  15.     password: password,
  16.     database: database
  17.   }
  18.   puts "Connecting to supplied host DB...".yellow
  19.   ActiveRecord::Base.establish_connection(db_settings)
  20.   puts "Checking if successfully connected...".yellow
  21.   begin
  22.     ActiveRecord::Base.connection.tables
  23.     puts "Successfully connected!".green
  24.     $old_pry_prompt = _pry_.prompt
  25.     _pry_.prompt = [
  26.       proc { |obj, nest_level, _| "pry-HEROKU(#{obj}):#{nest_level}> ".red },
  27.       proc { |obj, nest_level, _| "pry-HEROKU(#{obj}):#{nest_level}* ".red }
  28.     ]
  29.     # puts wrap_in_transaction
  30.     # if wrap_in_transaction == "true"
  31.     #   ActiveRecord::Base.connection.execute("BEGIN")
  32.     #   # ActiveRecord::Base.connection.execute("BEGIN TRANSACTION")
  33.     #   # ActiveRecord::Base.connection.execute("SAVEPOINT heroku_connection_sp")
  34.     #   $remote_connection = true
  35.     #   _pry_.hooks.add_hook :after_session, 'tparreira_pry:reset_remote_connection' do
  36.     #     puts "Exiting server: pry after_session hook resetting connection!".red
  37.     #     _pry_.run_command("reset_connection") if $remote_connection == true
  38.     #   end
  39.     # end
  40.   rescue ActiveRecord::ConnectionNotEstablished => e
  41.     puts "Coudn't connect: \n#{e}\n".red
  42.   rescue => e
  43.     puts "Unexpected exception:\n#{e}\n".red
  44.   end
  45. end
  46.  
  47. Pry::Commands.block_command('reset_connection', 'Reset DB connection to development mode.') do
  48.   if $old_pry_prompt != nil
  49.     # ActiveRecord::Base.connection.execute("ROLLBACK")
  50.     # NOTE: @tparreira - WHY DOES THIS SAY THERE'S NO TRANSACTION BLOCK?!
  51.     # ActiveRecord::Base.connection.execute("ROLLBACK TO SAVEPOINT heroku_connection_sp")
  52.     _pry_.prompt = $old_pry_prompt
  53.     $old_pry_prompt = nil
  54.   end
  55.   ActiveRecord::Base.remove_connection
  56.   ActiveRecord::Base.establish_connection
  57.   puts "Restored original development DB!".green
  58. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement