sqldef 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +18 -0
- data/Gemfile +3 -1
- data/LICENSE.txt +1 -1
- data/README.md +50 -17
- data/Rakefile +3 -1
- data/bin/console +1 -0
- data/lib/sqldef.rb +125 -3
- data/lib/sqldef/version.rb +3 -1
- data/sqldef.gemspec +17 -13
- metadata +13 -12
- data/Gemfile.lock +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 54c5bb0e7a737257f4270bb013a2e29140ede972ef5cf586431eb8bb0d23b09e
|
4
|
+
data.tar.gz: bee04b0a4b23e709c4f9211f89bac792efb7906248242ab8898a8cab18a35fe4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c5e34db8bf2ed57bc4f82cfcc6b6165dcdd3185b7a222e1b4b5cf768b8f937bb8398005a1e5bf12b8891bfef27b5ec0e930893a343a15749976386a625c12ea
|
7
|
+
data.tar.gz: 10bc6159532d7bd5a7d18bb14d1e4f453c833a5a9a52f479c04dc85fc24823a864367bfba49fb96908a356ed69bcd74d4024726dc0a7e836f35d3a2c3638ab6e
|
@@ -0,0 +1,18 @@
|
|
1
|
+
name: Ruby
|
2
|
+
|
3
|
+
on: [push,pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
build:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
steps:
|
9
|
+
- uses: actions/checkout@v2
|
10
|
+
- name: Set up Ruby
|
11
|
+
uses: ruby/setup-ruby@v1
|
12
|
+
with:
|
13
|
+
ruby-version: 3.0.0
|
14
|
+
- name: Run the default task
|
15
|
+
run: |
|
16
|
+
gem install bundler -v 2.2.3
|
17
|
+
bundle install
|
18
|
+
bundle exec rake
|
data/Gemfile
CHANGED
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,39 +1,72 @@
|
|
1
|
-
#
|
1
|
+
# sqldef-ruby
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Ruby interface to call [sqldef](https://github.com/k0kubun/sqldef).
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
9
|
-
Add this line to your application's Gemfile:
|
10
|
-
|
11
7
|
```ruby
|
12
8
|
gem 'sqldef'
|
13
9
|
```
|
14
10
|
|
15
|
-
|
11
|
+
## Usage
|
12
|
+
### Download
|
16
13
|
|
17
|
-
|
14
|
+
You can download `mysqldef`, `psqldef`, or `sqlite3def`.
|
18
15
|
|
19
|
-
|
16
|
+
```rb
|
17
|
+
Sqldef.bin = './bin'
|
18
|
+
Sqldef.download(:psqldef)
|
19
|
+
```
|
20
20
|
|
21
|
-
|
21
|
+
`download` is automatically executed by the following methods too.
|
22
22
|
|
23
|
-
|
23
|
+
### Export
|
24
24
|
|
25
|
-
|
25
|
+
You can export the database schema to a file.
|
26
26
|
|
27
|
-
|
27
|
+
```rb
|
28
|
+
Sqldef.export(
|
29
|
+
command: :psqldef,
|
30
|
+
host: host,
|
31
|
+
port: port,
|
32
|
+
user: user,
|
33
|
+
password: password,
|
34
|
+
database: database,
|
35
|
+
path: 'db/schema.sql',
|
36
|
+
)
|
37
|
+
```
|
38
|
+
|
39
|
+
### Dry Run
|
28
40
|
|
29
|
-
|
41
|
+
You can show DDLs to be executed.
|
30
42
|
|
31
|
-
|
43
|
+
```rb
|
44
|
+
Sqldef.dry_run(
|
45
|
+
command: :psqldef,
|
46
|
+
host: host,
|
47
|
+
port: port,
|
48
|
+
user: user,
|
49
|
+
password: password,
|
50
|
+
database: database,
|
51
|
+
path: 'db/schema.sql',
|
52
|
+
)
|
53
|
+
```
|
32
54
|
|
33
|
-
|
55
|
+
### Apply
|
34
56
|
|
35
|
-
|
57
|
+
You can run DDLs to match the schema.
|
36
58
|
|
59
|
+
```rb
|
60
|
+
Sqldef.apply(
|
61
|
+
command: :psqldef,
|
62
|
+
host: host,
|
63
|
+
port: port,
|
64
|
+
user: user,
|
65
|
+
password: password,
|
66
|
+
database: database,
|
67
|
+
path: 'db/schema.sql',
|
68
|
+
)
|
69
|
+
```
|
37
70
|
|
38
71
|
## License
|
39
72
|
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
data/lib/sqldef.rb
CHANGED
@@ -1,6 +1,128 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'etc'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'net/http'
|
6
|
+
require 'rubygems/package'
|
7
|
+
require 'stringio'
|
8
|
+
require 'uri'
|
9
|
+
require 'zlib'
|
10
|
+
require_relative 'sqldef/version'
|
2
11
|
|
3
12
|
module Sqldef
|
4
|
-
|
5
|
-
|
13
|
+
GOARCH = {
|
14
|
+
'x86_64' => 'amd64',
|
15
|
+
'aarch64' => 'arm64',
|
16
|
+
# TODO: add arm and 386
|
17
|
+
}
|
18
|
+
private_constant :GOARCH
|
19
|
+
|
20
|
+
COMMANDS = %w[
|
21
|
+
mysqldef
|
22
|
+
psqldef
|
23
|
+
sqlite3def
|
24
|
+
]
|
25
|
+
private_constant :COMMANDS
|
26
|
+
|
27
|
+
@bin = Dir.pwd
|
28
|
+
|
29
|
+
class << self
|
30
|
+
attr_accessor :bin
|
31
|
+
|
32
|
+
# @param [String,Symbol] command - mysqldef, psqldef, sqllite3def
|
33
|
+
# @param [String] path - Schema export path
|
34
|
+
def export(command:, path:, host:, port: nil, user:, password: nil, database:)
|
35
|
+
sqldef = download(command)
|
36
|
+
schema = IO.popen(
|
37
|
+
[
|
38
|
+
sqldef,
|
39
|
+
"--user=#{user}", *(["--password=#{password}"] if password),
|
40
|
+
"--host=#{host}", *(["--port=#{port}"] if port),
|
41
|
+
'--export', database,
|
42
|
+
],
|
43
|
+
&:read
|
44
|
+
)
|
45
|
+
raise "Failed to execute '#{sqldef}'" unless $?.success?
|
46
|
+
File.write(path, schema)
|
47
|
+
end
|
48
|
+
|
49
|
+
# @param [String,Symbol] command - mysqldef, psqldef, sqllite3def
|
50
|
+
# @param [String] path - Schema file path
|
51
|
+
def dry_run(command:, path:, host:, port: nil, user:, password: nil, database:)
|
52
|
+
sqldef = download(command)
|
53
|
+
execute(
|
54
|
+
sqldef,
|
55
|
+
"--user=#{user}", *(["--password=#{password}"] if password),
|
56
|
+
"--host=#{host}", *(["--port=#{port}"] if port),
|
57
|
+
'--dry-run', database,
|
58
|
+
in: path,
|
59
|
+
)
|
60
|
+
end
|
61
|
+
|
62
|
+
# @param [String,Symbol] command - mysqldef, psqldef, sqllite3def
|
63
|
+
# @param [String] path - Schema file path
|
64
|
+
def apply(command:, path:, host:, port: nil, user:, password: nil, database:)
|
65
|
+
sqldef = download(command)
|
66
|
+
execute(
|
67
|
+
sqldef,
|
68
|
+
"--user=#{user}", *(["--password=#{password}"] if password),
|
69
|
+
"--host=#{host}", *(["--port=#{port}"] if port),
|
70
|
+
database,
|
71
|
+
in: path,
|
72
|
+
)
|
73
|
+
end
|
74
|
+
|
75
|
+
# @param [String,Symbol] command - mysqldef, psqldef, sqllite3def
|
76
|
+
# @return String - command path
|
77
|
+
def download(command)
|
78
|
+
path = File.join(bin, command = command.to_s)
|
79
|
+
return path if File.executable?(path)
|
80
|
+
|
81
|
+
print("Downloading '#{command}' under '#{bin}'... ")
|
82
|
+
resp = get(build_url(command), code: 302) # Latest
|
83
|
+
resp = get(resp['location'], code: 302) # vX.Y.Z
|
84
|
+
resp = get(resp['location'], code: 200) # Binary
|
85
|
+
|
86
|
+
gzip = Zlib::GzipReader.new(StringIO.new(resp.body))
|
87
|
+
Gem::Package::TarReader.new(gzip) do |tar|
|
88
|
+
unless file = tar.find { |f| f.full_name == command }
|
89
|
+
raise "'#{command}' was not found in the archive"
|
90
|
+
end
|
91
|
+
File.binwrite(path, file.read)
|
92
|
+
end
|
93
|
+
|
94
|
+
FileUtils.chmod('+x', path)
|
95
|
+
puts 'done.'
|
96
|
+
path
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
def execute(*cmd, **opts)
|
102
|
+
unless system(*cmd, **opts)
|
103
|
+
raise "Failed to execute '#{cmd.first}'"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def build_url(command)
|
108
|
+
unless COMMANDS.include?(command)
|
109
|
+
raise "Unexpected sqldef command: #{command}"
|
110
|
+
end
|
111
|
+
os = Etc.uname.fetch(:sysname).downcase
|
112
|
+
arch = GOARCH.fetch(Etc.uname.fetch(:machine))
|
113
|
+
"https://github.com/k0kubun/sqldef/releases/latest/download/#{command}_#{os}_#{arch}.tar.gz"
|
114
|
+
end
|
115
|
+
|
116
|
+
# TODO: Retry transient errors
|
117
|
+
def get(url, code: nil)
|
118
|
+
uri = URI.parse(url)
|
119
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
|
120
|
+
http.get("#{uri.path}?#{uri.query}")
|
121
|
+
end.tap do |resp|
|
122
|
+
if code && resp.code != code.to_s
|
123
|
+
raise "Expected '#{url}' to return #{code}, but got #{resp.code}: #{resp.body}"
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
6
128
|
end
|
data/lib/sqldef/version.rb
CHANGED
data/sqldef.gemspec
CHANGED
@@ -1,24 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require_relative 'lib/sqldef/version'
|
2
4
|
|
3
5
|
Gem::Specification.new do |spec|
|
4
|
-
spec.name =
|
6
|
+
spec.name = 'sqldef'
|
5
7
|
spec.version = Sqldef::VERSION
|
6
|
-
spec.authors = [
|
7
|
-
spec.email = [
|
8
|
+
spec.authors = ['Takashi Kokubun']
|
9
|
+
spec.email = ['takashikkbn@gmail.com']
|
8
10
|
|
9
|
-
spec.summary =
|
10
|
-
spec.description =
|
11
|
-
spec.homepage =
|
12
|
-
spec.license =
|
11
|
+
spec.summary = 'Idempotent MySQL/PostgreSQL schema management by SQL'
|
12
|
+
spec.description = 'Idempotent MySQL/PostgreSQL schema management by SQL'
|
13
|
+
spec.homepage = 'https://github.com/sqldef/sqldef-ruby'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
13
16
|
|
14
|
-
spec.metadata[
|
17
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
15
19
|
|
16
20
|
# Specify which files should be added to the gem when it is released.
|
17
21
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
-
spec.files
|
19
|
-
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{
|
22
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
20
24
|
end
|
21
|
-
spec.bindir =
|
22
|
-
spec.executables = spec.files.grep(%r{
|
23
|
-
spec.require_paths = [
|
25
|
+
spec.bindir = 'exe'
|
26
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ['lib']
|
24
28
|
end
|
metadata
CHANGED
@@ -1,25 +1,25 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sqldef
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Takashi Kokubun
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-13 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description:
|
13
|
+
description: Idempotent MySQL/PostgreSQL schema management by SQL
|
14
14
|
email:
|
15
15
|
- takashikkbn@gmail.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- ".github/workflows/main.yml"
|
20
21
|
- ".gitignore"
|
21
22
|
- Gemfile
|
22
|
-
- Gemfile.lock
|
23
23
|
- LICENSE.txt
|
24
24
|
- README.md
|
25
25
|
- Rakefile
|
@@ -28,12 +28,13 @@ files:
|
|
28
28
|
- lib/sqldef.rb
|
29
29
|
- lib/sqldef/version.rb
|
30
30
|
- sqldef.gemspec
|
31
|
-
homepage: https://github.com/
|
31
|
+
homepage: https://github.com/sqldef/sqldef-ruby
|
32
32
|
licenses:
|
33
33
|
- MIT
|
34
34
|
metadata:
|
35
|
-
homepage_uri: https://github.com/
|
36
|
-
|
35
|
+
homepage_uri: https://github.com/sqldef/sqldef-ruby
|
36
|
+
source_code_uri: https://github.com/sqldef/sqldef-ruby
|
37
|
+
post_install_message:
|
37
38
|
rdoc_options: []
|
38
39
|
require_paths:
|
39
40
|
- lib
|
@@ -41,15 +42,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
41
42
|
requirements:
|
42
43
|
- - ">="
|
43
44
|
- !ruby/object:Gem::Version
|
44
|
-
version:
|
45
|
+
version: 2.3.0
|
45
46
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
46
47
|
requirements:
|
47
48
|
- - ">="
|
48
49
|
- !ruby/object:Gem::Version
|
49
50
|
version: '0'
|
50
51
|
requirements: []
|
51
|
-
rubygems_version: 3.
|
52
|
-
signing_key:
|
52
|
+
rubygems_version: 3.2.3
|
53
|
+
signing_key:
|
53
54
|
specification_version: 4
|
54
|
-
summary:
|
55
|
+
summary: Idempotent MySQL/PostgreSQL schema management by SQL
|
55
56
|
test_files: []
|