Module: CapTaffy::Db

Extended by:
Db
Included in:
Db
Defined in:
lib/cap-taffy/db.rb

Defined Under Namespace

Classes: InvalidURL

Instance Method Summary collapse

Instance Method Details

#default_server_portObject

The default server port the Taps server is started on.



37
38
39
# File 'lib/cap-taffy/db.rb', line 37

def default_server_port
  5000
end

#local_database_url(env) ⇒ Object

Detects the local database url for env.

Looks for config/database.yml.



19
20
21
22
23
24
# File 'lib/cap-taffy/db.rb', line 19

def local_database_url(env)
  return "" unless File.exists?(Dir.pwd + '/config/database.yml')
  db_config = YAML.load(File.read(Dir.pwd + '/config/database.yml'))

  CapTaffy::Parse.database_url(db_config, env)
end

#remote_database_url(instance, env) ⇒ Object

Detects the remote database url for env and the current Capistrano instance.

Looks for config/database.yml in the current_path.



29
30
31
32
33
34
# File 'lib/cap-taffy/db.rb', line 29

def remote_database_url(instance, env)
  db_yml = instance.capture "cat #{instance.current_path}/config/database.yml"
  db_config = YAML::load(db_yml)

  CapTaffy::Parse.database_url(db_config, env)
end

#remote_url(options = {}) ⇒ Object

Generates the remote url used by Taps push/pull.

Parameters

  • :login, :password, :host, :port - See #run.

Examples

 = fetch(:user)
password = tmp_pass()                                               # returns asdkf239udjhdaks (for example)
remote_url(:login => , :password => password, :host => 'load-test') # returns http://henry:asdkf239udjhdaks@load-test:5000


52
53
54
55
56
57
58
59
# File 'lib/cap-taffy/db.rb', line 52

def remote_url(options={})
  host = options[:host]
  port = options[:port] || default_server_port
  host += ":#{port}"
  url = CapTaffy::Parse.new.uri_hash_to_url('username' => options[:login], 'password' => options[:password], 'host' => host, 'scheme' => 'http', 'path' => '')

  url.sub(/\/$/, '')
end

#run(instance, options = {}, &blk) ⇒ Object

The meat of the operation. Runs operations after setting up the Taps server.

  1. Runs the taps taps command to start the Taps server (assuming Sinatra is running on Thin)

  2. Wait until the server is ready

  3. Execute block on Taps client

  4. Close the connection(s) and bid farewell.

Parameters

  • :remote_database_url - Refers to local database url in the options for the Taps server command (see Taps Options).

  • :login - The login for host. Usually what’s in set :user, "the user" in deploy.rb

  • :password - The temporary password for the Taps server.

  • :port - The port the Taps server is on. If not given, defaults to #default_server_port.

  • :local_database_url - Refers to the local database url in the options for Taps client commands (see Taps Options).

Taps Options

taps

server <local_database_url> <login> <password> [--port=N]        Start a taps database import/export server
pull <local_database_url> <remote_url> [--chunksize=N]           Pull a database from a taps server
push <local_database_url> <remote_url> [--chunksize=N]           Push a database to a taps server

Examples

task :push do
   = fetch(:user)
  password = Time.now.to_s
  CapTaffy.Db.run(self, { :login => , :password => password, :remote_database_url => "sqlite://test_production", :local_database_url => "sqlite://test_development" }) do |client|
    client.cmd_send
  end
end


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cap-taffy/db.rb', line 123

def run(instance, options = {} , &blk) # :yields: client
  options[:port] ||= default_server_port
  remote_database_url, , password, port, local_database_url = options[:remote_database_url], options[:login], options[:password], options[:port], options[:local_database_url]
  force_local = options.delete(:local)

  data_so_far = ""
  instance.run CapTaffy::Db.server_command(options) do |channel, stream, data|
    data_so_far << data
    if data_so_far.include? ">> Listening on 0.0.0.0:#{port}, CTRL+C to stop"
      host = force_local ? '127.0.0.1' : channel[:host]
      remote_url = CapTaffy::Db.remote_url(options.merge(:host => host))

      CapTaffy::Db.taps_client(local_database_url, remote_url) do |client|
        yield client
      end

      data_so_far = ""
      channel.close
      channel[:status] = 0
    end
  end
end

#server_command(options = {}) ⇒ Object

Generates the server command used to start a Taps server

Parameters

  • :remote_database_url, :login, :password - See #run.

  • :port - The port the Taps server is on. If given and different from #default_server_port, appends --port=[port] to command.



85
86
87
88
89
90
91
# File 'lib/cap-taffy/db.rb', line 85

def server_command(options={})
  remote_database_url, , password, port = options[:remote_database_url], options[:login], options[:password], options[:port]
  port_argument = ''
  port_argument = " --port=#{port}" if port && port != default_server_port

  "taps server #{remote_database_url} #{} #{password}#{port_argument}"
end

#taps_client(local_database_url, remote_url, &blk) ⇒ Object

A quick start for a Taps client.

local_database_url and remote_url refer to the options for the Taps gem (see #run).



69
70
71
72
73
74
75
76
77
78
# File 'lib/cap-taffy/db.rb', line 69

def taps_client(local_database_url, remote_url, &blk) # :yields: client
  Taps::Config.chunksize = 1000
  Taps::Config.database_url = local_database_url
  Taps::Config.remote_url = remote_url
  Taps::Config.verify_database_url

  Taps::ClientSession.quickstart do |client|
    yield client
  end
end

#tmp_pass(user) ⇒ Object

Generates a temporary password to be used for the Taps server command.



62
63
64
# File 'lib/cap-taffy/db.rb', line 62

def tmp_pass(user)
  Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{user}--")
end