class Pasqual::CLI
Public Class Methods
exit_on_failure?()
click to toggle source
# File lib/pasqual/cli.rb, line 7 def self.exit_on_failure? true end
Public Instance Methods
create(url = nil)
click to toggle source
# File lib/pasqual/cli.rb, line 12 def create(url = nil) puts "Creating #{pasqual(url).name}" pasqual(url).createdb rescue Pasqual::Createdb::AlreadyExists puts "#{pasqual(url).name} already exists, skipped creation" end
drop(url = nil)
click to toggle source
# File lib/pasqual/cli.rb, line 20 def drop(url = nil) puts "Dropping #{pasqual(url).name}" pasqual(url).dropdb rescue Pasqual::Dropdb::Failed puts "Could not drop #{pasqual(url).name}, skipped" end
file(path, url = nil)
click to toggle source
# File lib/pasqual/cli.rb, line 73 def file(path, url = nil) puts "Structuring #{pasqual(url).name}" pasqual(url).pipe_sql path rescue Errno::EPIPE puts "#{pasqual(url).name} doesn't exist, skipped structure" end
poll(url = nil)
click to toggle source
# File lib/pasqual/cli.rb, line 28 def poll(url = nil) say "Waiting for Postgres to come available" if attempt_connect(url) say "Connected!" exit 0 else say "Failed to connect" exit 1 end end
psql(*cmd_args)
click to toggle source
# File lib/pasqual/cli.rb, line 47 def psql(*cmd_args) url = nil sql_args = [] cmd_args.each do |arg| if arg =~ /postgres:/ url = arg else sql_args << arg end end db = pasqual(url) args = Pasqual::Arglist. args(db.username, db.password, db.host, db.port, db.name) if sql_args.any? args << '-c' args += sql_args.map(&:inspect) puts "Executing in #{db.name}: #{sql_args.inspect}" else cmd = "psql #{args.join(' ')}" end puts "Running #{cmd}" Kernel.exec({ 'PGPASSWORD' => db.password }, "psql #{args.join(' ')}") end
reset(path = nil, url = nil)
click to toggle source
# File lib/pasqual/cli.rb, line 40 def reset(path = nil, url = nil) drop(url) create(url) file(path, url) if path end
Private Instance Methods
attempt_connect(url)
click to toggle source
# File lib/pasqual/cli.rb, line 97 def attempt_connect(url) require 'pg' timeout = Time.now.to_i + (60 * 5) # Don't connect to a specific database because we may be polling the # the server to know whether we can _create_ a database yet. conn = pg_connection(url, no_db: true) status = conn.connect_poll while(Time.now.to_i < timeout && status != PG::PGRES_POLLING_OK) do sleep 2 status = conn.connect_poll conn = pg_connection(url, no_db: true) if status == PG::PGRES_POLLING_FAILED puts "Status: #{status.inspect}, Timeout: #{Time.now.to_i - timeout}" if ENV['DEBUG'] end conn.finish status == PG::PGRES_POLLING_OK end
pasqual(url)
click to toggle source
# File lib/pasqual/cli.rb, line 82 def pasqual(url) @pasqual ||= Pasqual.for(url || ENV['DATABASE_URL']) end
pg_connection(url, no_db: false)
click to toggle source
# File lib/pasqual/cli.rb, line 86 def pg_connection(url, no_db: false) db = pasqual(url) conn = PG::Connection.connect_start( host: db.host, port: db.port, dbname: no_db ? nil : db.name, user: db.username, password: db.password, connect_timeout: (60 * 5)) end