mysqlexport 0.1.0 → 0.2.0.1

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: 4066422b1ed4c327bd6f6e145a533caee962e9af743e72f0fc646c6d60f348d2
4
- data.tar.gz: 51a70e58bcbf27975018789571f3b2154966b84ed5058d21a8a383c1642d3612
3
+ metadata.gz: a608240d3dbd6a64d7cb6fb70ace1587da291af6097e6a49d3255b723e1e02fc
4
+ data.tar.gz: ce57658a089cc83c46a9f965b3de8c5d4c6d7877c094e8f5fecfdb89612328c1
5
5
  SHA512:
6
- metadata.gz: 62032c894ba2052a2b369f6056a0b4ee0663b69896727e7bc08171f828bb230ce075fc4447a23fa1eb50e22c30b4119b59ab56cb67af68ef9015e751ee4f88e2
7
- data.tar.gz: effd9497b52f606fe72963a32a29e02cdd713d179644e1f48c933ae242db370301e13f897ab7a78200c9ff8c66fd7728c4390c01f16460c39053626b3657c07a
6
+ metadata.gz: 23cbb97fedb5a0473f5796fe59471712b840bab6ebbdd015d794f9c556c6d1bedfd53af34ea17c3f1ec927d4984ddd5630ab5d2194d578c2ad254de6e33a4218
7
+ data.tar.gz: ea151bbc5c228f7d72eaf3af6e963de69af0f146f68409ed7a554d17e506a1ec04c025e7644788837d403d4040e1b0f847704a645c11cbc0154f077b5389cf3c
@@ -0,0 +1,17 @@
1
+ name: Rubocop
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ rubocop:
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: 2.7.1
14
+ - name: Rubocop
15
+ run: |
16
+ bundle install
17
+ bundle exec rake rubocop
@@ -0,0 +1,45 @@
1
+ name: Unit Tests
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ unit-tests:
7
+ runs-on: ${{ matrix.os }}
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ include:
12
+ - os: ubuntu-20.04
13
+ mysql-version: 8.0
14
+ - os: ubuntu-18.04
15
+ mysql-version: 8.0
16
+ - os: ubuntu-18.04
17
+ mysql-version: 5.7
18
+ - os: ubuntu-16.04
19
+ mysql-version: 8.0
20
+ - os: ubuntu-16.04
21
+ mysql-version: 5.7
22
+ - os: macos-10.15
23
+ mysql-version: 8.0
24
+ - os: macos-10.15
25
+ mysql-version: 5.7
26
+ - os: macos-10.15
27
+ mysql-version: 5.6
28
+ steps:
29
+ - uses: actions/checkout@v2
30
+ - name: Set up Ruby
31
+ uses: ruby/setup-ruby@v1
32
+ with:
33
+ ruby-version: 2.7.1
34
+ - name: Set up MySQL
35
+ uses: ankane/setup-mysql@v1
36
+ with:
37
+ mysql-version: ${{ matrix.mysql-version }}
38
+ - name: mysql config
39
+ run: |
40
+ mysqladmin -u root password root
41
+ bundle config --local build.mysql2 "--with-ldflags=-L/usr/local/opt/openssl/lib --with-cppflags=-I/usr/local/opt/openssl/include"
42
+ - name: Run Tests
43
+ run: |
44
+ bundle install
45
+ bundle exec rake unit_tests
data/.rubocop.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  AllCops:
2
- TargetRubyVersion: 2.4
2
+ TargetRubyVersion: 2.7
3
3
  SuggestExtensions: false
4
4
 
5
5
 
@@ -24,4 +24,13 @@ Metrics/BlockLength:
24
24
  Enabled: false
25
25
 
26
26
  Metrics/AbcSize:
27
+ Enabled: false
28
+
29
+ Metrics/MethodLength:
30
+ Enabled: false
31
+
32
+ Style/IfUnlessModifier:
33
+ Enabled: false
34
+
35
+ Layout/LineLength:
27
36
  Enabled: false
data/Gemfile.lock CHANGED
@@ -1,20 +1,21 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mysqlexport (0.1.0)
5
- activerecord (~> 6.1)
4
+ mysqlexport (0.2.0.1)
5
+ activerecord (>= 6.1.2.1)
6
6
  mixlib-cli (~> 2.1.8)
7
+ multi_json (~> 1.0)
7
8
  mysql2 (~> 0.5.3)
8
9
 
9
10
  GEM
10
11
  remote: https://rubygems.org/
11
12
  specs:
12
- activemodel (6.1.1)
13
- activesupport (= 6.1.1)
14
- activerecord (6.1.1)
15
- activemodel (= 6.1.1)
16
- activesupport (= 6.1.1)
17
- activesupport (6.1.1)
13
+ activemodel (6.1.3.2)
14
+ activesupport (= 6.1.3.2)
15
+ activerecord (6.1.3.2)
16
+ activemodel (= 6.1.3.2)
17
+ activesupport (= 6.1.3.2)
18
+ activesupport (6.1.3.2)
18
19
  concurrent-ruby (~> 1.0, >= 1.0.2)
19
20
  i18n (>= 1.6, < 2)
20
21
  minitest (>= 5.1)
@@ -22,20 +23,29 @@ GEM
22
23
  zeitwerk (~> 2.3)
23
24
  ast (2.4.2)
24
25
  benchmark (0.1.1)
26
+ coderay (1.1.3)
25
27
  concurrent-ruby (1.1.8)
26
28
  diff-lcs (1.4.4)
27
- i18n (1.8.8)
29
+ fakefs (1.3.2)
30
+ i18n (1.8.10)
28
31
  concurrent-ruby (~> 1.0)
29
- minitest (5.14.3)
32
+ method_source (0.9.2)
33
+ minitest (5.14.4)
30
34
  mixlib-cli (2.1.8)
35
+ multi_json (1.15.0)
31
36
  mysql2 (0.5.3)
32
37
  parallel (1.20.1)
33
- parser (3.0.0.0)
38
+ parser (3.0.1.1)
34
39
  ast (~> 2.4.1)
40
+ pry (0.12.2)
41
+ coderay (~> 1.1.0)
42
+ method_source (~> 0.9.0)
43
+ pry-nav (0.3.0)
44
+ pry (>= 0.9.10, < 0.13.0)
35
45
  rainbow (3.0.0)
36
46
  rake (13.0.3)
37
- regexp_parser (2.0.3)
38
- rexml (3.2.4)
47
+ regexp_parser (2.1.1)
48
+ rexml (3.2.5)
39
49
  rspec (3.10.0)
40
50
  rspec-core (~> 3.10.0)
41
51
  rspec-expectations (~> 3.10.0)
@@ -49,22 +59,22 @@ GEM
49
59
  diff-lcs (>= 1.2.0, < 2.0)
50
60
  rspec-support (~> 3.10.0)
51
61
  rspec-support (3.10.2)
52
- rubocop (1.9.1)
62
+ rubocop (1.14.0)
53
63
  parallel (~> 1.10)
54
64
  parser (>= 3.0.0.0)
55
65
  rainbow (>= 2.2.2, < 4.0)
56
66
  regexp_parser (>= 1.8, < 3.0)
57
67
  rexml
58
- rubocop-ast (>= 1.2.0, < 2.0)
68
+ rubocop-ast (>= 1.5.0, < 2.0)
59
69
  ruby-progressbar (~> 1.7)
60
70
  unicode-display_width (>= 1.4.0, < 3.0)
61
- rubocop-ast (1.4.1)
62
- parser (>= 2.7.1.5)
71
+ rubocop-ast (1.5.0)
72
+ parser (>= 3.0.1.1)
63
73
  ruby-progressbar (1.11.0)
64
74
  tzinfo (2.0.4)
65
75
  concurrent-ruby (~> 1.0)
66
76
  unicode-display_width (2.0.0)
67
- yaml (0.1.0)
77
+ yaml (0.1.1)
68
78
  zeitwerk (2.4.2)
69
79
 
70
80
  PLATFORMS
@@ -72,7 +82,9 @@ PLATFORMS
72
82
 
73
83
  DEPENDENCIES
74
84
  benchmark (~> 0.1)
85
+ fakefs (~> 1.3)
75
86
  mysqlexport!
87
+ pry-nav (~> 0.3)
76
88
  rake (~> 13.0)
77
89
  rspec (~> 3.0)
78
90
  rubocop (~> 1.7)
data/LICENSE.txt CHANGED
@@ -1,5 +1,7 @@
1
1
  The MIT License (MIT)
2
2
 
3
+ Copyright (c) 2013 Seamus Abshere
4
+
3
5
  Copyright (c) 2021 Iqbal Singh
4
6
 
5
7
  Permission is hereby granted, free of charge, to any person obtaining a copy
data/README.md CHANGED
@@ -1,9 +1,7 @@
1
1
  # Mysqlexport
2
-
3
- Gives you binary `mysqlexport` and ruby class `Mysqlexport::Csv` to export mysql tables into csv file.
2
+ Gives you binary `mysqlexport` and ruby classes `Mysqlexport::Csv` and `Mysqlexport::Json` to export mysql tables into csv or json file respectively.
4
3
 
5
4
  ## Installation
6
-
7
5
  Add this line to your application's Gemfile:
8
6
 
9
7
  ```ruby
@@ -20,48 +18,68 @@ Or install it yourself as:
20
18
  $ gem install mysqlexport
21
19
  ```
22
20
 
23
-
24
21
  ## Usage
25
-
26
22
  #### Binaries
27
23
  ```
28
- $ mysqlexport --user=root --password=root --database=mysqlexport_test --table=employees
24
+ $ mysqlexport --user=root --password=root --database=mysqlexport_test --table=employees --to=csv
29
25
  ```
30
26
 
31
27
  #### options
32
28
  ```
33
- $ mysqlexport --help
29
+ $ mysqlexport --help
34
30
 
35
- -c, --col-sep=, column separtor for csv, default is ","
36
- -d, --database=DATABASE Set MySQL database
37
- -e, --execute=EXECUTE The SQL statement to execute
38
- -f, --force-quotes=true force quotes to csv, default is false
39
- -h, --host=HOST Set MySQL host
40
- -o, --out=PATH output path, default is current directory
31
+ -u, --username=USERNAME Set MySQL username
41
32
  -p, --password=PASSWORD Set MySQL password
33
+ -h, --host=HOST Set MySQL host
42
34
  -P, --port=PORT Set MySQL port
43
- -r, --row-sep=\n row separator for csv, default is "\n"
35
+ -d, --database=DATABASE Set MySQL database
44
36
  -s, --socket=SOCKET Set MySQL socket
37
+ -T, --to=CSV Export Mysql table to CSV/JSON
38
+ -e, --execute=EXECUTE The SQL statement to execute
39
+ -o, --out=PATH output path, default is current directory
45
40
  -t, --table=TABLE MySQL table you want to export
46
- -u, --username=USERNAME Set MySQL username
47
- --help Show help
48
41
 
42
+ CSV Options
43
+ -f, --force-quotes=false force quotes to csv, default is false
44
+ -c, --col-sep=, column separtor for csv, default is ","
45
+ -r, --row-sep=\n row separator for csv, default is "\n"
46
+ -H, --csv-heading=true show csv heading, default is true
47
+
48
+ JSON Options
49
+ -y, --pretty=false display json pretty, default is false
50
+ -j, --json-engine=oj choose json engine
51
+ ```
52
+
53
+ ## Use in Ruby
54
+ #### Ruby classes
55
+ ```ruby
56
+ Mysqlexport::Csv.new(options) # create csv object
57
+ Mysqlexport::Json.new(options) # create json object
49
58
  ```
50
- #### Ruby Class
59
+ #### Methods Suported
51
60
  ```ruby
61
+ to_stdout # write it directly to $stdout
62
+ to_path(String) # write it to a file at this path
63
+ to_file(File) # write it to a file handle
64
+ ```
65
+ #### Usage
66
+ ```ruby
67
+ require 'mysqlexport'
52
68
  options = {
53
69
  username: "root",
54
70
  password: "root",
55
71
  database: "mysqlexport_test",
56
72
  execute: "select * from employees"
57
73
  }
58
- Mysqlexport::Csv.new(options).to_stdout # write it directly to $stdout
59
- Mysqlexport::Csv.new(options).to_path('/tmp/table.csv') # write it to a file at this path
60
- Mysqlexport::Csv.new(options).to_file(File.open('/tmp/table.csv', 'w')) # write it to a file handle
74
+ Mysqlexport::Csv.new(options).to_stdout
75
+ Mysqlexport::Csv.new(options).to_path('/tmp/table.csv')
76
+ Mysqlexport::Json.new(options).to_file(File.open('/tmp/table.json', 'w'))
61
77
  ```
78
+
62
79
  #### All available options
63
- ```ruby
64
- Mysqlexport::Csv.new(
80
+ ##### General options
81
+
82
+ ```py
65
83
  host: "127.0.0.1", # optional, default is 127.0.0.1
66
84
  port: "3306", # optional, default is 3306
67
85
  username: "root", # optional if using Active record
@@ -69,24 +87,121 @@ Mysqlexport::Csv.new(
69
87
  database: "mysqlexport_test", # optional if using Active record
70
88
  socket: "/path/to/mysql.sock", # optional
71
89
  execute: "select * from employees", # not required if table is given
72
- table: "employees", # not required if execute query is given
90
+ table: "employees" # not required if execute query is given
91
+ ```
92
+
93
+ ##### csv options (only works with `Mysqlexport::Csv` class)
94
+ ```py
73
95
  force_quotes: true, # optional, default is false
74
- col_sep: ",", # optional, default is ","
75
- row_sep: "", # optional, default is "\n"
96
+ col_sep: ",", # optional, default is ','
97
+ row_sep: "", # optional, default is '\n'
76
98
  output_path: "/tmp/employees.csv" # optional, default is current directory
77
- )
78
99
  ```
79
100
 
101
+ ##### json options (only works with `Mysqlexport::Json` class)
102
+ ```py
103
+ pretty: false # display json pretty, default is false
104
+ ```
80
105
 
81
- If you're running it inside a Rails application, it will default to the ActiveRecord connection config.
106
+ ## More Uses
107
+ #### ActiveRecord Support
108
+ If you're running it inside a Rails application, it will default to the `ActiveRecord` connection configurations.
82
109
 
83
110
  ```ruby
84
- csv = Mysqlexport::Csv.new execute: "select * from employees" # no need to specify username, password
85
- csv.to_stdout
111
+ csv = Mysqlexport::Csv.new execute: "select * from employees" # no need to specify username, password or database
112
+ csv.to_stdout
113
+
114
+ json = Mysqlexport::Json.new table: "employees" # no need to specify username, password or database
115
+ json.to_stdout
86
116
  ```
87
117
 
118
+ #### to_path method/out option
119
+ Supports both relative and absolute path
120
+ ##### ruby
121
+ ```ruby
122
+ Mysqlexport::Csv.new(options).to_path('/tmp/table.csv') # this will create a file with name table.csv at given path
123
+ Mysqlexport::Csv.new(options).to_path('table.csv') # this will create file with name table.csv at current directory
124
+ ```
125
+
126
+ ##### binary
127
+ create a file with name table.csv at given path
128
+ ```
129
+ $ mysqlexport --user=root --password=root --database=mysqlexport_test --table=employees --to=csv --out=/tmp/table.csv
130
+ ```
131
+ create file with name table.csv at current directory
132
+ ```
133
+ $ mysqlexport --user=root --password=root --database=mysqlexport_test --table=employees --to=csv --out=table.csv
134
+ ```
135
+ When directory is given in path. It will create a file inside the directory.
136
+ ##### ruby
137
+ ```ruby
138
+ Mysqlexport::Csv.new(options).to_path('/tmp/mydir') # this will create a file inside mydir
139
+ Mysqlexport::Csv.new(options).to_path # this will create a file in the current directory
140
+ ```
141
+
142
+ ##### binary
143
+ create a file inside a directory
144
+ ```
145
+ $ mysqlexport --user=root --password=root --database=mysqlexport_test --table=employees --to=csv --out=/tmp/mydir
146
+ ```
147
+ create a file in current directory
148
+ ```
149
+ $ mysqlexport --user=root --password=root --database=mysqlexport_test --table=employees --to=csv
150
+ ```
151
+
152
+ What would be the file name in above cases? It takes file name from table option. If table option is not provided it will generate a file with current timestamp.
153
+ ##### ruby
154
+ ```ruby
155
+ Mysqlexport::Json.new({ table: "employees" }).to_path('/tmp/mydir') # this will create a file with name employees.json
156
+ Mysqlexport::Json.new({ execute: "select * from employees" }).to_path('/tmp/mydir') # this will create a file with current timestamp.
157
+ Mysqlexport::Json.new({
158
+ table: "employees",
159
+ execute: "select * from employees limit 2"
160
+ }).to_path('/tmp/mydir') # this will create a file with name employees.json
161
+ ```
162
+ ##### binary
163
+ Create a file with name employees.json
164
+ ```
165
+ $ mysqlexport --user=root --password=root --database=mysqlexport_test --table=employees --to=json --out=/tmp/mydir
166
+ ```
167
+ This will create a file with current timestamp
168
+ ```
169
+ $ mysqlexport --user=root --password=root --database=mysqlexport_test --execute="select * from employees" --to=json --out=/tmp/mydir
170
+ ```
171
+ This will create a file with name employees.json
172
+ ```
173
+ $ mysqlexport --user=root --password=root --database=mysqlexport_test --table=emplyees --execute="select * from employees" --to=json --out=/tmp/mydir
174
+ ```
175
+
176
+ #### Json Engine
177
+ It uses [multi_json][multi_json] to convert to json.
178
+
179
+ #### Supported JSON Engines
180
+ * [Oj][oj] Optimized JSON by Peter Ohler
181
+ * [Yajl][yajl] Yet Another JSON Library by Brian Lopez
182
+ * [JSON][json-gem] The default JSON gem with C-extensions (ships with Ruby 1.9+)
183
+ * [JSON Pure][json-gem] A Ruby variant of the JSON gem
184
+ * [NSJSONSerialization][nsjson] Wrapper for Apple's NSJSONSerialization in the Cocoa Framework (MacRuby only)
185
+ * [gson.rb][gson] A Ruby wrapper for google-gson library (JRuby only)
186
+ * [JrJackson][jrjackson] JRuby wrapper for Jackson (JRuby only)
187
+ * [OkJson][okjson] A simple, vendorable JSON parser
188
+
189
+ #### Usecase
190
+ ##### ruby
191
+ In case of ruby classes to use a json engine, it should be already loaded.
192
+ ```ruby
193
+ require 'mysqlexport'
194
+ require 'oj'
195
+ Mysqlexport::Json.new(options).to_stdout
196
+ ```
197
+ ##### binary
198
+ In case of the binary, it will try to load the specified json engine in the option `json_engine`. Make sure you have the json engine already installed in order to use it. If it is unable to load json engine it will default to OkJson.
199
+ ```
200
+ $ mysqlexport --user=root --password=root --database=mysqlexport_test --table=employees --to=json --json-engine=oj
201
+ ```
88
202
 
89
203
  ## Development
204
+ #### Configurations
90
205
  Go to `spec/configuration.yml`, then set the mysql configurations and test database. see example configuration below.
91
206
  ```
92
207
  host: localhost
@@ -94,24 +209,26 @@ username: root
94
209
  password:
95
210
  database: mysqlexport_test
96
211
  ```
97
- Use `bundle install` to install the necessary development & testing gems and `bundle exec rake` for running the tests.
212
+
213
+ #### Unit Tests and Rubocop
214
+ Use `bundle install` to install the necessary development & testing then `bundle exec rake` for running both unit_tests and rubocop. Database named `mysqlexport_test` and table named `unit_tests` with some data will automatically be created.
98
215
  ```
99
216
  $ bundle install
100
217
  $ bundle exec rake
101
218
  ```
102
- Database named `mysqlexport_test` will automatically be created.
103
219
 
220
+ ##### Other rake tasks
221
+ `bundle exec rake unit_tests` to run only unit tests
222
+ `bundle exec rake rubocop` to run only rubocop
104
223
 
105
- ## Benchmarks
106
-
107
- #### Running benchmarks
224
+ #### Benchmarks
225
+ ##### Running benchmarks
108
226
  ```
109
- $ bundle exec rake benchmark:run
227
+ $ bundle exec rake benchmark:all:run
110
228
  ```
111
- It will insert 1 million rows in mysql and run the tests on it.
112
-
229
+ It will insert 1 million rows in mysql and runs the benchmark on it. Database named `mysqlexport_test` and table named `employees` will automatically be created.
113
230
 
114
- #### Latest Benchmark Results
231
+ ##### Latest Benchmark Results
115
232
  ```
116
233
  user system total real
117
234
  1000 rows: 0.043286 0.003848 0.047134 ( 0.047876)
@@ -123,11 +240,25 @@ It will insert 1 million rows in mysql and run the tests on it.
123
240
  1000000 rows: 27.928350 0.183935 28.112285 ( 29.992699)
124
241
  ```
125
242
 
126
- If you want to skip loading data into mysql
127
- ```
128
- $ bundle exec rake benchmark:skip_data_load
243
+ ##### All available rake tasks for benchmark
244
+ ```ruby
245
+ benchmark:all:run # load data into mysql, run benchmarks for csv and json
246
+ benchmark:all:skip_data_load # do not load data into mysql, run benchmarks for csv and json
247
+ benchmark:csv:run # load data into mysql, run benchmarks for csv
248
+ benchmark:csv:skip_data_load # do not load data into mysql, run benchmarks for csv
249
+ benchmark:json:run # load data into mysql, run benchmarks for only json
250
+ benchmark:json:skip_data_load # do not load data into mysql, run benchmarks for only json
129
251
  ```
130
252
 
131
253
  ## License
132
-
133
254
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
255
+
256
+ [gson]: https://github.com/avsej/gson.rb
257
+ [jrjackson]: https://github.com/guyboertje/jrjackson
258
+ [json-gem]: https://github.com/flori/json
259
+ [json-pure]: https://github.com/flori/json
260
+ [oj]: https://github.com/ohler55/oj
261
+ [okjson]: https://github.com/kr/okjson
262
+ [yajl]: https://github.com/brianmario/yajl-ruby
263
+ [multi_json]: https://github.com/intridea/multi_json
264
+ [nsjson]: https://developer.apple.com/documentation/foundation/jsonserialization
data/Rakefile CHANGED
@@ -15,12 +15,30 @@ task :unit_tests do
15
15
  end
16
16
 
17
17
  namespace :benchmark do
18
- task run: ["benchmark:load_data", "benchmark:skip_data_load"]
19
18
  task :load_data do
20
19
  Benchmark::RunBenchmark.new.load_data
21
20
  end
22
- task :skip_data_load do
23
- Benchmark::RunBenchmark.new.run
21
+
22
+ namespace :all do
23
+ task run: ["benchmark:load_data", "benchmark:all:skip_data_load"]
24
+ task :skip_data_load do
25
+ Benchmark::RunBenchmark.new.run_csv
26
+ Benchmark::RunBenchmark.new.run_json
27
+ end
28
+ end
29
+
30
+ namespace :csv do
31
+ task run: ["benchmark:load_data", "benchmark:csv:skip_data_load"]
32
+ task :skip_data_load do
33
+ Benchmark::RunBenchmark.new.run_csv
34
+ end
35
+ end
36
+
37
+ namespace :json do
38
+ task run: ["benchmark:load_data", "benchmark:json:skip_data_load"]
39
+ task :skip_data_load do
40
+ Benchmark::RunBenchmark.new.run_json
41
+ end
24
42
  end
25
43
  end
26
44
 
data/exe/mysqlexport CHANGED
@@ -1,11 +1,30 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "mysqlexport"
4
- require "mysqlexport/cli"
4
+ require "mysqlexport/cli/cli"
5
+ require "mysqlexport/cli/help_parser"
5
6
 
6
7
  cli = Mysqlexport::Cli.new
7
- cli.parse_options
8
8
 
9
+ cli.parse_options
9
10
  options = cli.config
10
11
 
11
- Mysqlexport::Csv.new(options).to_path
12
+ Mysqlexport::HelpParser.new.parse(cli.options) if options[:help] == true
13
+ Mysqlexport::HelpParser.new.info if options == {}
14
+
15
+ to = options[:to] || "csv"
16
+ to = to.to_s.downcase
17
+ case to
18
+ when "csv"
19
+ Mysqlexport::Csv.new(options).to_path
20
+ when "json"
21
+ begin
22
+ json_engine = options[:json_engine] || ""
23
+ require json_engine
24
+ rescue LoadError
25
+ puts "[WARNING]: Invalid or no json engine provided using default json engine"
26
+ end
27
+ Mysqlexport::Json.new(options).to_path
28
+ else
29
+ raise Mysqlexport::Error, "invalid option [--to] only csv/json is allowed"
30
+ end
data/lib/mysqlexport.rb CHANGED
@@ -8,8 +8,14 @@ require "mysql2"
8
8
 
9
9
  require "mysqlexport/version"
10
10
  require "mysqlexport/config"
11
- require "mysqlexport/csv"
11
+ require "mysqlexport/writer"
12
+ require "mysqlexport/writer/csv"
13
+ require "mysqlexport/writer/json"
12
14
 
13
15
  module Mysqlexport
14
- class Error < StandardError; end
16
+ class Error < StandardError
17
+ def initialize(msg = "Mysqlexport::Error")
18
+ super(msg)
19
+ end
20
+ end
15
21
  end
@@ -8,17 +8,23 @@ module Mysqlexport
8
8
  long: "--#{o.downcase}=#{o.upcase}",
9
9
  description: "Set MySQL #{o.downcase}" }
10
10
  end
11
+ option :to, { short: "-T TO",
12
+ long: "--to=CSV",
13
+ description: "Export Mysql table to CSV/JSON" }
11
14
  option :execute, { short: "-e EXECUTE",
12
15
  long: "--execute=EXECUTE",
13
16
  description: "The SQL statement to execute" }
14
17
  option :force_quotes, { short: "-f false",
15
- long: "--force-quotes=true",
18
+ long: "--force-quotes=false",
19
+ type: "csv",
16
20
  description: "force quotes to csv, default is false" }
17
21
  option :col_sep, { short: "-c ,",
18
22
  long: "--col-sep=,",
23
+ type: "csv",
19
24
  description: "column separtor for csv, default is \",\"" }
20
25
  option :row_sep, { short: "-r \\n",
21
26
  long: "--row-sep=\\n",
27
+ type: "csv",
22
28
  description: "row separator for csv, default is \"\\n\"" }
23
29
  option :output_path, { short: "-o n",
24
30
  long: "--out=PATH",
@@ -26,11 +32,21 @@ module Mysqlexport
26
32
  option :table, { short: "-t TABLE",
27
33
  long: "--table=TABLE",
28
34
  description: "MySQL table you want to export" }
35
+ option :pretty, { short: "-y false",
36
+ long: "--pretty=false",
37
+ type: "json",
38
+ description: "display json pretty, default is false" }
39
+ option :json_engine, { short: "-j oj",
40
+ long: "--json-engine=oj",
41
+ type: "json",
42
+ description: "choose json engine" }
43
+ option :csv_heading, { short: "-H true",
44
+ long: "--csv-heading=true",
45
+ type: "csv",
46
+ description: "show csv heading, default is true" }
29
47
  option :help, { long: "--help",
30
48
  description: "Show help",
31
49
  on: :tail,
32
- boolean: true,
33
- show_options: true,
34
- exit: 0 }
50
+ boolean: true }
35
51
  end
36
52
  end
@@ -0,0 +1,41 @@
1
+ require "optparse"
2
+ module Mysqlexport
3
+ class HelpParser
4
+ def parse(hsh)
5
+ # this only works if ARGV contains "--help"
6
+ OptionParser.new do |opts|
7
+ opts.banner = "Usage: mysqlexport [options]"
8
+ hsh.tap { |h| h.delete(:help) }
9
+ all = []
10
+ all.append([[], nil])
11
+ all.append([[], "\nCSV Options"])
12
+ all.append([[], "\nJSON Options"])
13
+ hsh.each do |o|
14
+ case o[1][:type]
15
+ when "csv"
16
+ all[1][0].append(o[1])
17
+ when "json"
18
+ all[2][0].append(o[1])
19
+ else
20
+ all[0][0].append(o[1])
21
+ end
22
+ end
23
+ all.map do |category|
24
+ opts.on category[1] unless category[1].nil?
25
+ category[0].each do |o|
26
+ short = o[:short].slice(0, 2)
27
+ long = o[:long]
28
+ desc = o[:description]
29
+ opts.on(short, long, desc)
30
+ end
31
+ end
32
+ end.parse!
33
+ end
34
+
35
+ def info
36
+ puts "[Mysqlexport v#{Mysqlexport::VERSION}]"
37
+ puts "run mysqlexport --help for options"
38
+ exit 0
39
+ end
40
+ end
41
+ end
@@ -6,6 +6,7 @@ module Mysqlexport
6
6
  @user_specified_options = user_specified_options.symbolize_keys
7
7
  end
8
8
 
9
+ # database options
9
10
  def host
10
11
  user_specified_options[:host] || active_record_config.try(:[], :host)
11
12
  end
@@ -38,6 +39,11 @@ module Mysqlexport
38
39
  user_specified_options[:table]
39
40
  end
40
41
 
42
+ def output_path
43
+ user_specified_options[:output_path]
44
+ end
45
+
46
+ # csv options
41
47
  def force_quotes
42
48
  user_specified_options[:force_quotes].to_s.downcase == "true" || nil
43
49
  end
@@ -50,10 +56,16 @@ module Mysqlexport
50
56
  user_specified_options[:row_sep]
51
57
  end
52
58
 
53
- def output_path
54
- user_specified_options[:output_path]
59
+ def csv_heading
60
+ user_specified_options[:csv_heading].to_s.downcase != "false"
61
+ end
62
+
63
+ # json options
64
+ def pretty
65
+ user_specified_options[:pretty].to_s.downcase == "true" || nil
55
66
  end
56
67
 
68
+ # client options
57
69
  def client
58
70
  return @client if @client.is_a? ::Mysql2::Client
59
71
 
@@ -1,3 +1,3 @@
1
1
  module Mysqlexport
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0.1".freeze
3
3
  end
@@ -1,21 +1,53 @@
1
- module Writer
2
- def to_stdout
3
- to_file $stdout
4
- nil
5
- end
1
+ module Mysqlexport
2
+ class Writer
3
+ attr_reader :client, :sql, :config
6
4
 
7
- def filter_path(path)
8
- path = ::Dir.pwd.to_s unless path.instance_of? ::String
9
- file_name = "/#{::Time.now.to_i}_mysqlexport.csv" if ::File.directory?(path)
10
- file_name = "/#{config.table}.csv" if ::File.directory?(path) && !config.table.nil?
11
- path += file_name.to_s
12
- path
13
- end
5
+ def initialize(options = {})
6
+ validate!(options)
7
+ @config = Mysqlexport::Config.new options
8
+ @client = @config.client
9
+ end
10
+
11
+ def to_stdout
12
+ to_file $stdout
13
+ nil
14
+ end
15
+
16
+ def filter_path(path)
17
+ path = ::Dir.pwd.to_s unless path.instance_of? ::String
18
+ file_name = "/#{::Time.now.to_i}_mysqlexport.#{extension}" if ::File.directory?(path)
19
+ file_name = "/#{config.table}.#{extension}" if ::File.directory?(path) && !config.table.nil?
20
+ path += file_name.to_s
21
+ path
22
+ end
23
+
24
+ def to_path(path = config.output_path)
25
+ f = ::File.open(filter_path(path), "w")
26
+ to_file f
27
+ f.close
28
+ nil
29
+ end
30
+
31
+ def to_s
32
+ s = ::StringIO.new
33
+ to_file s
34
+ s.rewind
35
+ s.read
36
+ end
37
+
38
+ private
39
+
40
+ def validate!(options)
41
+ raise Error, "[required option missing] execute or table required in options" if options[:table].nil? && options[:execute].nil?
42
+
43
+ true
44
+ end
45
+
46
+ def extension
47
+ # default extension is csv
48
+ return "json" if instance_of? Mysqlexport::Json
14
49
 
15
- def to_path(path = config.output_path)
16
- f = ::File.open(filter_path(path), "w")
17
- to_file f
18
- f.close
19
- nil
50
+ "csv"
51
+ end
20
52
  end
21
53
  end
@@ -3,22 +3,18 @@ if RUBY_VERSION >= "1.9"
3
3
  else
4
4
  require "fastercsv"
5
5
  end
6
- require "mysqlexport/writer"
7
6
  module Mysqlexport
8
- class Csv
9
- include Writer
10
- attr_reader :client, :sql, :config
11
-
7
+ class Csv < Writer
12
8
  def initialize(options = {})
13
- @config = Mysqlexport::Config.new options
14
- @client = @config.client
9
+ super(**options)
15
10
  end
16
11
 
17
12
  def to_file(file)
18
13
  result = client.query(config.execute, stream: true, cache_rows: false, as: :array)
19
- file.write result.fields.to_csv(**csv_options)
14
+ csv_o = csv_options
15
+ file.write result.fields.to_csv(**csv_o) if config.csv_heading
20
16
  result.each do |row|
21
- file.write row.to_csv(**csv_options)
17
+ file.write row.to_csv(**csv_o)
22
18
  end
23
19
  end
24
20
 
@@ -0,0 +1,35 @@
1
+ require "multi_json"
2
+
3
+ module Mysqlexport
4
+ class Json < Writer
5
+ def initialize(options = {})
6
+ super(**options)
7
+ end
8
+
9
+ def to_file(file)
10
+ first = true
11
+ json_o = json_options
12
+ file.write "["
13
+ client.query(config.execute, stream: true, cache_rows: false).each do |h|
14
+ next if h.nil?
15
+
16
+ if first
17
+ first = false
18
+ else
19
+ file.write ","
20
+ end
21
+ file.write ::MultiJson.dump(h, **json_o)
22
+ end
23
+ file.write "]"
24
+ nil
25
+ end
26
+
27
+ private
28
+
29
+ def json_options
30
+ options = {}
31
+ options[:pretty] = config.pretty unless config.pretty.nil?
32
+ options
33
+ end
34
+ end
35
+ end
data/mysqlexport.gemspec CHANGED
@@ -5,11 +5,11 @@ Gem::Specification.new do |spec|
5
5
  spec.version = Mysqlexport::VERSION
6
6
  spec.authors = ["Iqbal Singh"]
7
7
  spec.email = ["singhiqbal1007@gmail.com"]
8
- spec.summary = "Export mysql table to csv files"
9
- spec.description = "Gives you binary mysqlexport and Ruby classes to export mysql tables into csv files"
8
+ spec.summary = "Export mysql table to csv and json files"
9
+ spec.description = "Gives you binary mysqlexport and Ruby classes to export mysql tables into csv and json files"
10
10
  spec.homepage = "https://github.com/singhiqbal1007/mysqlexport"
11
11
  spec.license = "MIT"
12
- spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
13
13
  spec.metadata["homepage_uri"] = spec.homepage
14
14
  spec.metadata["source_code_uri"] = spec.homepage
15
15
 
@@ -21,12 +21,15 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ["lib"]
22
22
 
23
23
  spec.add_development_dependency "benchmark", "~> 0.1"
24
+ spec.add_development_dependency "fakefs", "~> 1.3"
25
+ spec.add_development_dependency "pry-nav", "~> 0.3"
24
26
  spec.add_development_dependency "rake", "~> 13.0"
25
27
  spec.add_development_dependency "rspec", "~> 3.0"
26
28
  spec.add_development_dependency "rubocop", "~> 1.7"
27
29
  spec.add_development_dependency "yaml", "~> 0.1"
28
30
 
29
- spec.add_dependency "activerecord", "~> 6.1"
31
+ spec.add_dependency "activerecord", ">= 6.1.2.1"
30
32
  spec.add_dependency "mixlib-cli", "~> 2.1.8"
33
+ spec.add_dependency "multi_json", "~> 1.0"
31
34
  spec.add_dependency "mysql2", "~> 0.5.3"
32
35
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mysqlexport
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Iqbal Singh
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-02-28 00:00:00.000000000 Z
11
+ date: 2021-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: benchmark
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fakefs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-nav
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.3'
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: rake
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -84,16 +112,16 @@ dependencies:
84
112
  name: activerecord
85
113
  requirement: !ruby/object:Gem::Requirement
86
114
  requirements:
87
- - - "~>"
115
+ - - ">="
88
116
  - !ruby/object:Gem::Version
89
- version: '6.1'
117
+ version: 6.1.2.1
90
118
  type: :runtime
91
119
  prerelease: false
92
120
  version_requirements: !ruby/object:Gem::Requirement
93
121
  requirements:
94
- - - "~>"
122
+ - - ">="
95
123
  - !ruby/object:Gem::Version
96
- version: '6.1'
124
+ version: 6.1.2.1
97
125
  - !ruby/object:Gem::Dependency
98
126
  name: mixlib-cli
99
127
  requirement: !ruby/object:Gem::Requirement
@@ -108,6 +136,20 @@ dependencies:
108
136
  - - "~>"
109
137
  - !ruby/object:Gem::Version
110
138
  version: 2.1.8
139
+ - !ruby/object:Gem::Dependency
140
+ name: multi_json
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '1.0'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '1.0'
111
153
  - !ruby/object:Gem::Dependency
112
154
  name: mysql2
113
155
  requirement: !ruby/object:Gem::Requirement
@@ -123,7 +165,7 @@ dependencies:
123
165
  - !ruby/object:Gem::Version
124
166
  version: 0.5.3
125
167
  description: Gives you binary mysqlexport and Ruby classes to export mysql tables
126
- into csv files
168
+ into csv and json files
127
169
  email:
128
170
  - singhiqbal1007@gmail.com
129
171
  executables:
@@ -131,7 +173,8 @@ executables:
131
173
  extensions: []
132
174
  extra_rdoc_files: []
133
175
  files:
134
- - ".github/workflows/main.yml"
176
+ - ".github/workflows/rubocop.yml"
177
+ - ".github/workflows/unit-tests.yml"
135
178
  - ".gitignore"
136
179
  - ".rspec"
137
180
  - ".rubocop.yml"
@@ -145,11 +188,13 @@ files:
145
188
  - bin/setup
146
189
  - exe/mysqlexport
147
190
  - lib/mysqlexport.rb
148
- - lib/mysqlexport/cli.rb
191
+ - lib/mysqlexport/cli/cli.rb
192
+ - lib/mysqlexport/cli/help_parser.rb
149
193
  - lib/mysqlexport/config.rb
150
- - lib/mysqlexport/csv.rb
151
194
  - lib/mysqlexport/version.rb
152
195
  - lib/mysqlexport/writer.rb
196
+ - lib/mysqlexport/writer/csv.rb
197
+ - lib/mysqlexport/writer/json.rb
153
198
  - mysqlexport.gemspec
154
199
  homepage: https://github.com/singhiqbal1007/mysqlexport
155
200
  licenses:
@@ -165,7 +210,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
165
210
  requirements:
166
211
  - - ">="
167
212
  - !ruby/object:Gem::Version
168
- version: 2.4.0
213
+ version: 2.7.0
169
214
  required_rubygems_version: !ruby/object:Gem::Requirement
170
215
  requirements:
171
216
  - - ">="
@@ -175,5 +220,5 @@ requirements: []
175
220
  rubygems_version: 3.1.2
176
221
  signing_key:
177
222
  specification_version: 4
178
- summary: Export mysql table to csv files
223
+ summary: Export mysql table to csv and json files
179
224
  test_files: []
@@ -1,32 +0,0 @@
1
- name: Unit Tests
2
-
3
- on: [push,pull_request]
4
-
5
- jobs:
6
- build:
7
- runs-on: ubuntu-latest
8
- services:
9
- mysql:
10
- image: mysql:8.0
11
- env:
12
- MYSQL_ROOT_PASSWORD: root
13
- ports:
14
- - 3306
15
- options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
16
- steps:
17
- - uses: actions/checkout@v2
18
- - name: Set up Ruby
19
- uses: ruby/setup-ruby@v1
20
- with:
21
- ruby-version: 2.7.1
22
- - name: Set up MySQL
23
- run: |
24
- sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev
25
- mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports[3306] }} -uroot -proot -e "SHOW GRANTS FOR 'root'@'localhost'"
26
- mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql --host 127.0.0.1 --port ${{ job.services.mysql.ports[3306] }} -uroot -proot mysql
27
- sudo service mysql start
28
- - name: Run tests
29
- run: |
30
- bundle install
31
- bundle exec rake
32
-