Class: SQLiteSweep::SourceStream

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

Overview

Reads database URIs from a shell command, one per line.

The command is run via IO.popen and its stdout is read line-by-line. Each non-empty line is parsed into a DatabaseURI. Blank lines are skipped.

The command can be anything: a simple cat, a ‘rails runner` invocation, a script that queries an API, etc. Whatever it is, it must output one database URI per line to stdout.

Examples:

stream = SourceStream.new("cat /tmp/uris.txt")
stream.each { |uri| puts uri.path }

with Rails

stream = SourceStream.new('rails runner "Tenant.find_each { |t| puts t.db_path }"')

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ SourceStream

Returns a new instance of SourceStream.



19
20
21
# File 'lib/sqlitesweep/source_stream.rb', line 19

def initialize(command)
  @command = command
end

Instance Method Details

#each {|uri| ... } ⇒ Object

Yields each DatabaseURI from the source command’s output. Returns an Enumerator if no block is given.

Yield Parameters:

Raises:

  • (SourceError)

    If the command exits with a non-zero status.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/sqlitesweep/source_stream.rb', line 28

def each(&block)
  return enum_for(:each) unless block_given?

  IO.popen(@command, "r") do |io|
    io.each_line do |line|
      line = line.strip
      next if line.empty?
      yield DatabaseURI.new(line)
    end
  end

  unless $?.success?
    raise SourceError, "Source command exited with status #{$?.exitstatus}: #{@command}"
  end
end