Module: Sqldef

Defined in:
lib/sqldef.rb,
lib/sqldef/version.rb

Constant Summary collapse

VERSION =
'0.4.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.binObject

Returns the value of attribute bin.



38
39
40
# File 'lib/sqldef.rb', line 38

def bin
  @bin
end

Class Method Details

.apply(command:, path:, host:, port: nil, user:, password: nil, database:) ⇒ Object

Parameters:

  • command (String, Symbol)
    • mysqldef, psqldef, sqllite3def
  • path (String)
    • Schema file path


72
73
74
75
76
77
78
79
80
81
# File 'lib/sqldef.rb', line 72

def apply(command:, path:, host:, port: nil, user:, password: nil, database:)
  sqldef = download(command)
  execute(
    sqldef,
    "--user=#{user}", *(["--password=#{password}"] if password),
    "--host=#{host}", *(["--port=#{port}"] if port),
    database,
    in: path,
  )
end

.download(command) ⇒ Object

Returns String - command path.

Parameters:

  • command (String, Symbol)
    • mysqldef, psqldef, sqllite3def

Returns:

  • String - command path



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/sqldef.rb', line 85

def download(command)
  path = File.join(bin, command = command.to_s)
  return path if File.executable?(path)

  print("Downloading '#{command}' under '#{bin}'... ")
  url = build_url(command)
  resp = get(url, code: 200, max_retries: 4)

  if url.end_with?('.zip')
    Zip::File.open_buffer(resp.body) do |zip|
      unless entry = zip.find_entry(command)
        raise "'#{command}' was not found in the archive"
      end
      File.binwrite(path, zip.read(entry))
    end
  else
    gzip = Zlib::GzipReader.new(StringIO.new(resp.body))
    Gem::Package::TarReader.new(gzip) do |tar|
      unless file = tar.find { |f| f.full_name == command }
        raise "'#{command}' was not found in the archive"
      end
      File.binwrite(path, file.read)
    end
  end

  FileUtils.chmod('+x', path)
  puts 'done.'
  path
end

.dry_run(command:, path:, host:, port: nil, user:, password: nil, database:) ⇒ Object

Parameters:

  • command (String, Symbol)
    • mysqldef, psqldef, sqllite3def
  • path (String)
    • Schema file path


59
60
61
62
63
64
65
66
67
68
# File 'lib/sqldef.rb', line 59

def dry_run(command:, path:, host:, port: nil, user:, password: nil, database:)
  sqldef = download(command)
  execute(
    sqldef,
    "--user=#{user}", *(["--password=#{password}"] if password),
    "--host=#{host}", *(["--port=#{port}"] if port),
    '--dry-run', database,
    in: path,
  )
end

.export(command:, path:, host:, port: nil, user:, password: nil, database:) ⇒ Object

Parameters:

  • command (String, Symbol)
    • mysqldef, psqldef, sqllite3def
  • path (String)
    • Schema export path


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sqldef.rb', line 42

def export(command:, path:, host:, port: nil, user:, password: nil, database:)
  sqldef = download(command)
  schema = IO.popen(
    [
      sqldef,
      "--user=#{user}", *(["--password=#{password}"] if password),
      "--host=#{host}", *(["--port=#{port}"] if port),
      '--export', database,
    ],
    &:read
  )
  raise "Failed to execute '#{sqldef}'" unless $?.success?
  File.write(path, schema)
end