Class: SQLiteSweep::DatabaseURI

Inherits:
Object
  • Object
show all
Defined in:
lib/sqlitesweep/database_uri.rb

Overview

Parses and represents a database location. Supports three URI formats:

- Local path:  /data/tenants/acme.sqlite3
- File URI:    file:///data/tenants/acme.sqlite3
- SSH URI:     ssh://[email protected]/data/tenants/acme.sqlite3

For SSH URIs, the user portion is optional. The host and path are extracted and used to build SSH commands.

Examples:

uri = DatabaseURI.new("ssh://deploy@web1/data/db.sqlite3")
uri.remote?          # => true
uri.ssh_destination  # => "deploy@web1"
uri.path             # => "/data/db.sqlite3"

uri = DatabaseURI.new("/tmp/local.sqlite3")
uri.local?           # => true
uri.path             # => "/tmp/local.sqlite3"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri_string) ⇒ DatabaseURI

Returns a new instance of DatabaseURI.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/sqlitesweep/database_uri.rb', line 26

def initialize(uri_string)
  uri_string = uri_string.strip
  if uri_string.start_with?("ssh://")
    parsed = URI.parse(uri_string)
    @user = parsed.user
    @host = parsed.host
    @path = parsed.path
    raise ConfigError, "SSH URI missing host: #{uri_string}" if @host.nil? || @host.empty?
    raise ConfigError, "SSH URI missing path: #{uri_string}" if @path.nil? || @path.empty?
  elsif uri_string.start_with?("file://")
    parsed = URI.parse(uri_string)
    @user = nil
    @host = nil
    @path = parsed.path
  else
    @user = nil
    @host = nil
    @path = uri_string
  end
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



24
25
26
# File 'lib/sqlitesweep/database_uri.rb', line 24

def host
  @host
end

#pathObject (readonly)

Returns the value of attribute path.



24
25
26
# File 'lib/sqlitesweep/database_uri.rb', line 24

def path
  @path
end

#userObject (readonly)

Returns the value of attribute user.



24
25
26
# File 'lib/sqlitesweep/database_uri.rb', line 24

def user
  @user
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



79
80
81
# File 'lib/sqlitesweep/database_uri.rb', line 79

def ==(other)
  other.is_a?(DatabaseURI) && @user == other.user && @host == other.host && @path == other.path
end

#hashObject



83
84
85
# File 'lib/sqlitesweep/database_uri.rb', line 83

def hash
  [@user, @host, @path].hash
end

#host_keyObject

Returns a key that uniquely identifies the remote host (user@host). Used by HostBatcher to group databases on the same host into batches. Returns nil for local URIs.



67
68
69
# File 'lib/sqlitesweep/database_uri.rb', line 67

def host_key
  ssh_destination
end

#local?Boolean

Returns true if this database is on the local filesystem.

Returns:

  • (Boolean)


53
54
55
# File 'lib/sqlitesweep/database_uri.rb', line 53

def local?
  @host.nil?
end

#remote?Boolean

Returns true if this database is on a remote host (accessed via SSH).

Returns:

  • (Boolean)


48
49
50
# File 'lib/sqlitesweep/database_uri.rb', line 48

def remote?
  !@host.nil?
end

#ssh_destinationObject

Returns the SSH destination string (e.g. “deploy@web1” or “web1”). Used as the target for ssh commands. Returns nil for local URIs.



59
60
61
62
# File 'lib/sqlitesweep/database_uri.rb', line 59

def ssh_destination
  return nil unless remote?
  @user ? "#{@user}@#{@host}" : @host
end

#to_sObject



71
72
73
74
75
76
77
# File 'lib/sqlitesweep/database_uri.rb', line 71

def to_s
  if remote?
    "ssh://#{ssh_destination}#{@path}"
  else
    @path
  end
end