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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9df5d37c2955058f654420ef2cdf85130477dc75446c3407e5d010e781997cbf
4
- data.tar.gz: 29f0f569604a5b0dbeccb89d484e0231e098e31329a70e6a0dee8a38c8e8e623
3
+ metadata.gz: 54c5bb0e7a737257f4270bb013a2e29140ede972ef5cf586431eb8bb0d23b09e
4
+ data.tar.gz: bee04b0a4b23e709c4f9211f89bac792efb7906248242ab8898a8cab18a35fe4
5
5
  SHA512:
6
- metadata.gz: 237e64c10ef093881093da17f1c96435fcd5b5559b0751778490570e8d61108e5b526f479cd04e137f5a59b95a96d23a2b578293032a98cc00cebbf862ebadf4
7
- data.tar.gz: c3135845cbf2ac3f2680821bd2c9ce902545b2e99986d31ae67980f7888cc22a0e41d99419a72b3e2371b68deb85a966ccd075f9956c8f78762f9e56af892a61
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
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source "https://rubygems.org"
2
4
 
3
5
  # Specify your gem's dependencies in sqldef.gemspec
4
6
  gemspec
5
7
 
6
- gem "rake", "~> 12.0"
8
+ gem "rake", "~> 13.0"
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2020 Takashi Kokubun
3
+ Copyright (c) 2021 Takashi Kokubun
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,39 +1,72 @@
1
- # Sqldef
1
+ # sqldef-ruby
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sqldef`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- And then execute:
11
+ ## Usage
12
+ ### Download
16
13
 
17
- $ bundle install
14
+ You can download `mysqldef`, `psqldef`, or `sqlite3def`.
18
15
 
19
- Or install it yourself as:
16
+ ```rb
17
+ Sqldef.bin = './bin'
18
+ Sqldef.download(:psqldef)
19
+ ```
20
20
 
21
- $ gem install sqldef
21
+ `download` is automatically executed by the following methods too.
22
22
 
23
- ## Usage
23
+ ### Export
24
24
 
25
- TODO: Write usage instructions here
25
+ You can export the database schema to a file.
26
26
 
27
- ## Development
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
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+ You can show DDLs to be executed.
30
42
 
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
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
- ## Contributing
55
+ ### Apply
34
56
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sqldef.
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
@@ -1,2 +1,4 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "bundler/gem_tasks"
2
- task :default => :spec
4
+ task default: %i[]
data/bin/console CHANGED
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
4
  require "bundler/setup"
4
5
  require "sqldef"
data/lib/sqldef.rb CHANGED
@@ -1,6 +1,128 @@
1
- require "sqldef/version"
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
- class Error < StandardError; end
5
- # Your code goes here...
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Sqldef
2
- VERSION = "0.0.0"
4
+ VERSION = '0.1.0'
3
5
  end
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 = "sqldef"
6
+ spec.name = 'sqldef'
5
7
  spec.version = Sqldef::VERSION
6
- spec.authors = ["Takashi Kokubun"]
7
- spec.email = ["takashikkbn@gmail.com"]
8
+ spec.authors = ['Takashi Kokubun']
9
+ spec.email = ['takashikkbn@gmail.com']
8
10
 
9
- spec.summary = %q{Placeholder for future sqldef}
10
- spec.description = %q{Placeholder for future sqldef}
11
- spec.homepage = "https://github.com/k0kubun/sqldef"
12
- spec.license = "MIT"
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["homepage_uri"] = spec.homepage
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 = Dir.chdir(File.expand_path('..', __FILE__)) do
19
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
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 = "exe"
22
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
- spec.require_paths = ["lib"]
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.0.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: 2020-10-07 00:00:00.000000000 Z
11
+ date: 2021-03-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Placeholder for future sqldef
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/k0kubun/sqldef
31
+ homepage: https://github.com/sqldef/sqldef-ruby
32
32
  licenses:
33
33
  - MIT
34
34
  metadata:
35
- homepage_uri: https://github.com/k0kubun/sqldef
36
- post_install_message:
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: '0'
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.1.2
52
- signing_key:
52
+ rubygems_version: 3.2.3
53
+ signing_key:
53
54
  specification_version: 4
54
- summary: Placeholder for future sqldef
55
+ summary: Idempotent MySQL/PostgreSQL schema management by SQL
55
56
  test_files: []
data/Gemfile.lock DELETED
@@ -1,19 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- sqldef (0.1.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- rake (12.3.3)
10
-
11
- PLATFORMS
12
- ruby
13
-
14
- DEPENDENCIES
15
- rake (~> 12.0)
16
- sqldef!
17
-
18
- BUNDLED WITH
19
- 2.1.2