Class: SQLiteSweep::DatabaseURI
- Inherits:
-
Object
- Object
- SQLiteSweep::DatabaseURI
- 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.
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #hash ⇒ Object
-
#host_key ⇒ Object
Returns a key that uniquely identifies the remote host (user@host).
-
#initialize(uri_string) ⇒ DatabaseURI
constructor
A new instance of DatabaseURI.
-
#local? ⇒ Boolean
Returns true if this database is on the local filesystem.
-
#remote? ⇒ Boolean
Returns true if this database is on a remote host (accessed via SSH).
-
#ssh_destination ⇒ Object
Returns the SSH destination string (e.g. “deploy@web1” or “web1”).
- #to_s ⇒ Object
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
#host ⇒ Object (readonly)
Returns the value of attribute host.
24 25 26 |
# File 'lib/sqlitesweep/database_uri.rb', line 24 def host @host end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
24 25 26 |
# File 'lib/sqlitesweep/database_uri.rb', line 24 def path @path end |
#user ⇒ Object (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 |
#hash ⇒ Object
83 84 85 |
# File 'lib/sqlitesweep/database_uri.rb', line 83 def hash [@user, @host, @path].hash end |
#host_key ⇒ Object
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.
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).
48 49 50 |
# File 'lib/sqlitesweep/database_uri.rb', line 48 def remote? !@host.nil? end |
#ssh_destination ⇒ Object
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_s ⇒ Object
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 |