-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsqldef.rb
150 lines (133 loc) · 4.32 KB
/
sqldef.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# frozen_string_literal: true
require 'etc'
require 'fileutils'
require 'net/http'
require 'rubygems/package'
require 'stringio'
require 'uri'
require 'zlib'
require 'zip'
require_relative 'sqldef/version'
module Sqldef
GOARCH = {
'x86_64' => 'amd64',
'aarch64' => 'arm64',
# TODO: add arm and 386
}
private_constant :GOARCH
COMMANDS = %w[
mysqldef
psqldef
sqlite3def
]
private_constant :COMMANDS
OS_ARCHIVE = {
'linux' => 'tar.gz',
'windows' => 'zip',
'darwin' => 'zip',
}
private_constant :OS_ARCHIVE
@bin = Dir.pwd
class << self
attr_accessor :bin
# @param [String,Symbol] command - mysqldef, psqldef, sqllite3def
# @param [String] path - Schema export path
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
# @param [String,Symbol] command - mysqldef, psqldef, sqllite3def
# @param [String] path - Schema file path
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
# @param [String,Symbol] command - mysqldef, psqldef, sqllite3def
# @param [String] path - Schema file path
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
# @param [String,Symbol] command - mysqldef, psqldef, sqllite3def
# @return String - command path
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
private
def execute(*cmd, **opts)
unless system(*cmd, **opts)
raise "Failed to execute '#{cmd.first.is_a?(Hash) ? cmd[1] : cmd.first}'"
end
end
def build_url(command)
unless COMMANDS.include?(command)
raise "Unexpected sqldef command: #{command}"
end
os = Etc.uname.fetch(:sysname).downcase
archive = OS_ARCHIVE.fetch(os)
arch = Etc.uname.fetch(:machine)
goarch = GOARCH.fetch(arch, arch)
"https://github.com/sqldef/sqldef/releases/latest/download/#{command}_#{os}_#{goarch}.#{archive}"
end
# TODO: Retry transient errors
def get(url, code: nil, max_retries:)
uri = URI.parse(url)
resp = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
http.get("#{uri.path}?#{uri.query}")
end
if resp.is_a?(Net::HTTPRedirection) && max_retries > 0
# Follow redirects that lead to the current repository (if sqldef/sqldef is moved),
# Latest, vX.Y.Z, and to the binary
return get(resp['location'], code: code, max_retries: max_retries - 1)
elsif code && resp.code != code.to_s
raise "Expected '#{url}' to return #{code}, but got #{resp.code}: #{resp.body}"
end
resp
end
end
end