Class: Osmosis::Command
- Inherits:
-
Object
- Object
- Osmosis::Command
- Defined in:
- lib/osmosis/command.rb
Class Method Summary collapse
Class Method Details
.connect ⇒ Object
46 47 48 49 50 51 |
# File 'lib/osmosis/command.rb', line 46 def self.connect() cmd = "PGPASSWORD=#{password} psql -U #{username} -h #{host} -d #{database}" return [ OpenStruct.new(type: :pty, command: cmd) ] end |
.export ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/osmosis/command.rb', line 29 def self.export() print "Filename: (db.dump): " filename = gets.chomp if filename == "" filename = "db.dump" end host = ActiveRecord::Base.connection_db_config.host database = ActiveRecord::Base.connection_db_config.database username = ActiveRecord::Base.connection_db_config.configuration_hash[:username] password = ActiveRecord::Base.connection_db_config.configuration_hash[:password] cmd = "PGPASSWORD=#{password} pg_dump -Fc --no-acl --no-owner -h #{host} -U #{username} -w #{database} > #{filename}" return [ OpenStruct.new(type: :cmd, command: cmd), OpenStruct.new(type: :print, command: "Exported database '#{database}'"), ] end |
.import ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/osmosis/command.rb', line 7 def self.import() print "Filename: (db.dump): " filename = gets.chomp if filename == "" filename = "db.dump" end if !File.exist?(filename) return [OpenStruct.new(type: :print, command: "File #{filename} does not exist")] end host = ActiveRecord::Base.connection_db_config.host database = ActiveRecord::Base.connection_db_config.database username = ActiveRecord::Base.connection_db_config.configuration_hash[:username] password = ActiveRecord::Base.connection_db_config.configuration_hash[:password] cmd = "PGPASSWORD=#{password} pg_restore -U #{username} -h #{host} -d #{database} -cO --if-exists -w < #{filename}" return [ OpenStruct.new(type: :rake, command: 'db:drop'), OpenStruct.new(type: :rake, command: 'db:create'), OpenStruct.new(type: :cmd, command: cmd), OpenStruct.new(type: :print, command: "Imported database '#{database}'"), ] end |
.run(commands) ⇒ Object
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 |
# File 'lib/osmosis/command.rb', line 53 def self.run(commands) commands.each do |cmd| if cmd.type == :rake Rake::Task[cmd.command].invoke elsif cmd.type == :cmd system cmd.command elsif cmd.type == :print puts cmd.command elsif cmd.type == :pty PTY.spawn(cmd.command) do |stdout, stdin, pid| trap("INT") { Process.kill("INT", pid) } begin loop do # Read output from psql stdout.each { |line| puts line} # Check if there's any input from the user if IO.select([STDIN], nil, nil, 0.1) user_input = STDIN.gets.chomp stdin.puts user_input end end rescue Errno::EIO puts "psql session ended" end end else raise StandardError, "Unknown command type #{cmd.type}" end end end |