Module: Litestream::Commands

Defined in:
lib/litestream/commands.rb

Defined Under Namespace

Modules: Output

Constant Summary collapse

DEFAULT_DIR =
File.expand_path(File.join(__dir__, "..", "..", "exe"))
GEM_NAME =
"litestream"
UnsupportedPlatformException =

raised when the host platform is not supported by upstream litestream’s binary releases

Class.new(StandardError)
ExecutableNotFoundException =

raised when the litestream executable could not be found where we expected it to be

Class.new(StandardError)
DirectoryNotFoundException =

raised when LITESTREAM_INSTALL_DIR does not exist

Class.new(StandardError)
DatabaseRequiredException =

raised when a litestream command requires a database argument but it isn’t provided

Class.new(StandardError)
CommandFailedException =

raised when a litestream command fails

Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.databases(**argv) ⇒ Object



113
114
115
# File 'lib/litestream/commands.rb', line 113

def databases(**argv)
  execute("databases", argv)
end

.executable(exe_path: DEFAULT_DIR) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/litestream/commands.rb', line 46

def executable(exe_path: DEFAULT_DIR)
  litestream_install_dir = ENV["LITESTREAM_INSTALL_DIR"]
  if litestream_install_dir
    if File.directory?(litestream_install_dir)
      warn "NOTE: using LITESTREAM_INSTALL_DIR to find litestream executable: #{litestream_install_dir}"
      exe_path = litestream_install_dir
      exe_file = File.expand_path(File.join(litestream_install_dir, "litestream"))
    else
      raise DirectoryNotFoundException, <<~MESSAGE
        LITESTREAM_INSTALL_DIR is set to #{litestream_install_dir}, but that directory does not exist.
      MESSAGE
    end
  else
    if Litestream::Upstream::NATIVE_PLATFORMS.keys.none? { |p| Gem::Platform.match_gem?(Gem::Platform.new(p), GEM_NAME) }
      raise UnsupportedPlatformException, <<~MESSAGE
        litestream-ruby does not support the #{platform} platform
        Please install litestream following instructions at https://litestream.io/install
      MESSAGE
    end

    exe_file = Dir.glob(File.expand_path(File.join(exe_path, "*", "litestream"))).find do |f|
      Gem::Platform.match_gem?(Gem::Platform.new(File.basename(File.dirname(f))), GEM_NAME)
    end
  end

  if exe_file.nil? || !File.exist?(exe_file)
    raise ExecutableNotFoundException, <<~MESSAGE
      Cannot find the litestream executable for #{platform} in #{exe_path}

      If you're using bundler, please make sure you're on the latest bundler version:

          gem install bundler
          bundle update --bundler

      Then make sure your lock file includes this platform by running:

          bundle lock --add-platform #{platform}
          bundle install

      See `bundle lock --help` output for details.

      If you're still seeing this message after taking those steps, try running
      `bundle config` and ensure `force_ruby_platform` isn't set to `true`. See
      https://github.com/fractaledmind/litestream-ruby#check-bundle_force_ruby_platform
      for more details.
    MESSAGE
  end

  exe_file
end

.generations(database, **argv) ⇒ Object



117
118
119
120
121
# File 'lib/litestream/commands.rb', line 117

def generations(database, **argv)
  raise DatabaseRequiredException, "database argument is required for generations command, e.g. litestream:generations -- --database=path/to/database.sqlite" if database.nil?

  execute("generations", argv, database)
end

.platformObject



42
43
44
# File 'lib/litestream/commands.rb', line 42

def platform
  [:cpu, :os].map { |m| Gem::Platform.local.send(m) }.join("-")
end

.replicate(async: false, **argv) ⇒ Object

Replicate can be run either as a fork or in the same process, depending on the context. Puma will start replication as a forked process, while running replication from a rake tasks won’t.



100
101
102
103
104
105
# File 'lib/litestream/commands.rb', line 100

def replicate(async: false, **argv)
  cmd = prepare("replicate", argv)
  run_replicate(cmd, async: async)
rescue
  raise CommandFailedException, "Failed to execute `#{cmd.join(" ")}`"
end

.restore(database, **argv) ⇒ Object



107
108
109
110
111
# File 'lib/litestream/commands.rb', line 107

def restore(database, **argv)
  raise DatabaseRequiredException, "database argument is required for restore command, e.g. litestream:restore -- --database=path/to/database.sqlite" if database.nil?

  execute("restore", argv, database, tabled_output: false)
end

.snapshots(database, **argv) ⇒ Object



123
124
125
126
127
# File 'lib/litestream/commands.rb', line 123

def snapshots(database, **argv)
  raise DatabaseRequiredException, "database argument is required for snapshots command, e.g. litestream:snapshots -- --database=path/to/database.sqlite" if database.nil?

  execute("snapshots", argv, database)
end

.wal(database, **argv) ⇒ Object



129
130
131
132
133
# File 'lib/litestream/commands.rb', line 129

def wal(database, **argv)
  raise DatabaseRequiredException, "database argument is required for wal command, e.g. litestream:wal -- --database=path/to/database.sqlite" if database.nil?

  execute("wal", argv, database)
end