mysqlexport 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4066422b1ed4c327bd6f6e145a533caee962e9af743e72f0fc646c6d60f348d2
4
+ data.tar.gz: 51a70e58bcbf27975018789571f3b2154966b84ed5058d21a8a383c1642d3612
5
+ SHA512:
6
+ metadata.gz: 62032c894ba2052a2b369f6056a0b4ee0663b69896727e7bc08171f828bb230ce075fc4447a23fa1eb50e22c30b4119b59ab56cb67af68ef9015e751ee4f88e2
7
+ data.tar.gz: effd9497b52f606fe72963a32a29e02cdd713d179644e1f48c933ae242db370301e13f897ab7a78200c9ff8c66fd7728c4390c01f16460c39053626b3657c07a
@@ -0,0 +1,32 @@
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
+
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /tmp/
8
+ /spec/benchmarks/results/csv/*
9
+ /spec/benchmarks/results/reports/*
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ /test.rb
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,27 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+ SuggestExtensions: false
4
+
5
+
6
+ Style/StringLiterals:
7
+ Enabled: true
8
+ EnforcedStyle: double_quotes
9
+
10
+ Style/StringLiteralsInInterpolation:
11
+ Enabled: true
12
+ EnforcedStyle: double_quotes
13
+
14
+ Layout/LineLength:
15
+ Max: 120
16
+
17
+ Documentation:
18
+ Enabled: false
19
+
20
+ Style/FrozenStringLiteralComment:
21
+ Enabled: false
22
+
23
+ Metrics/BlockLength:
24
+ Enabled: false
25
+
26
+ Metrics/AbcSize:
27
+ Enabled: false
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at singhiqbal1007@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,82 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mysqlexport (0.1.0)
5
+ activerecord (~> 6.1)
6
+ mixlib-cli (~> 2.1.8)
7
+ mysql2 (~> 0.5.3)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ 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)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (>= 1.6, < 2)
20
+ minitest (>= 5.1)
21
+ tzinfo (~> 2.0)
22
+ zeitwerk (~> 2.3)
23
+ ast (2.4.2)
24
+ benchmark (0.1.1)
25
+ concurrent-ruby (1.1.8)
26
+ diff-lcs (1.4.4)
27
+ i18n (1.8.8)
28
+ concurrent-ruby (~> 1.0)
29
+ minitest (5.14.3)
30
+ mixlib-cli (2.1.8)
31
+ mysql2 (0.5.3)
32
+ parallel (1.20.1)
33
+ parser (3.0.0.0)
34
+ ast (~> 2.4.1)
35
+ rainbow (3.0.0)
36
+ rake (13.0.3)
37
+ regexp_parser (2.0.3)
38
+ rexml (3.2.4)
39
+ rspec (3.10.0)
40
+ rspec-core (~> 3.10.0)
41
+ rspec-expectations (~> 3.10.0)
42
+ rspec-mocks (~> 3.10.0)
43
+ rspec-core (3.10.1)
44
+ rspec-support (~> 3.10.0)
45
+ rspec-expectations (3.10.1)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.10.0)
48
+ rspec-mocks (3.10.2)
49
+ diff-lcs (>= 1.2.0, < 2.0)
50
+ rspec-support (~> 3.10.0)
51
+ rspec-support (3.10.2)
52
+ rubocop (1.9.1)
53
+ parallel (~> 1.10)
54
+ parser (>= 3.0.0.0)
55
+ rainbow (>= 2.2.2, < 4.0)
56
+ regexp_parser (>= 1.8, < 3.0)
57
+ rexml
58
+ rubocop-ast (>= 1.2.0, < 2.0)
59
+ ruby-progressbar (~> 1.7)
60
+ unicode-display_width (>= 1.4.0, < 3.0)
61
+ rubocop-ast (1.4.1)
62
+ parser (>= 2.7.1.5)
63
+ ruby-progressbar (1.11.0)
64
+ tzinfo (2.0.4)
65
+ concurrent-ruby (~> 1.0)
66
+ unicode-display_width (2.0.0)
67
+ yaml (0.1.0)
68
+ zeitwerk (2.4.2)
69
+
70
+ PLATFORMS
71
+ x86_64-linux
72
+
73
+ DEPENDENCIES
74
+ benchmark (~> 0.1)
75
+ mysqlexport!
76
+ rake (~> 13.0)
77
+ rspec (~> 3.0)
78
+ rubocop (~> 1.7)
79
+ yaml (~> 0.1)
80
+
81
+ BUNDLED WITH
82
+ 2.2.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Iqbal Singh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # Mysqlexport
2
+
3
+ Gives you binary `mysqlexport` and ruby class `Mysqlexport::Csv` to export mysql tables into csv file.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mysqlexport'
11
+ ```
12
+
13
+ And then execute:
14
+ ```
15
+ $ bundle install
16
+ ```
17
+
18
+ Or install it yourself as:
19
+ ```
20
+ $ gem install mysqlexport
21
+ ```
22
+
23
+
24
+ ## Usage
25
+
26
+ #### Binaries
27
+ ```
28
+ $ mysqlexport --user=root --password=root --database=mysqlexport_test --table=employees
29
+ ```
30
+
31
+ #### options
32
+ ```
33
+ $ mysqlexport --help
34
+
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
41
+ -p, --password=PASSWORD Set MySQL password
42
+ -P, --port=PORT Set MySQL port
43
+ -r, --row-sep=\n row separator for csv, default is "\n"
44
+ -s, --socket=SOCKET Set MySQL socket
45
+ -t, --table=TABLE MySQL table you want to export
46
+ -u, --username=USERNAME Set MySQL username
47
+ --help Show help
48
+
49
+ ```
50
+ #### Ruby Class
51
+ ```ruby
52
+ options = {
53
+ username: "root",
54
+ password: "root",
55
+ database: "mysqlexport_test",
56
+ execute: "select * from employees"
57
+ }
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
61
+ ```
62
+ #### All available options
63
+ ```ruby
64
+ Mysqlexport::Csv.new(
65
+ host: "127.0.0.1", # optional, default is 127.0.0.1
66
+ port: "3306", # optional, default is 3306
67
+ username: "root", # optional if using Active record
68
+ password: "root", # optional if using Active record
69
+ database: "mysqlexport_test", # optional if using Active record
70
+ socket: "/path/to/mysql.sock", # optional
71
+ execute: "select * from employees", # not required if table is given
72
+ table: "employees", # not required if execute query is given
73
+ force_quotes: true, # optional, default is false
74
+ col_sep: ",", # optional, default is ","
75
+ row_sep: "", # optional, default is "\n"
76
+ output_path: "/tmp/employees.csv" # optional, default is current directory
77
+ )
78
+ ```
79
+
80
+
81
+ If you're running it inside a Rails application, it will default to the ActiveRecord connection config.
82
+
83
+ ```ruby
84
+ csv = Mysqlexport::Csv.new execute: "select * from employees" # no need to specify username, password
85
+ csv.to_stdout
86
+ ```
87
+
88
+
89
+ ## Development
90
+ Go to `spec/configuration.yml`, then set the mysql configurations and test database. see example configuration below.
91
+ ```
92
+ host: localhost
93
+ username: root
94
+ password:
95
+ database: mysqlexport_test
96
+ ```
97
+ Use `bundle install` to install the necessary development & testing gems and `bundle exec rake` for running the tests.
98
+ ```
99
+ $ bundle install
100
+ $ bundle exec rake
101
+ ```
102
+ Database named `mysqlexport_test` will automatically be created.
103
+
104
+
105
+ ## Benchmarks
106
+
107
+ #### Running benchmarks
108
+ ```
109
+ $ bundle exec rake benchmark:run
110
+ ```
111
+ It will insert 1 million rows in mysql and run the tests on it.
112
+
113
+
114
+ #### Latest Benchmark Results
115
+ ```
116
+ user system total real
117
+ 1000 rows: 0.043286 0.003848 0.047134 ( 0.047876)
118
+ 5000 rows: 0.142942 0.003945 0.146887 ( 0.147594)
119
+ 10000 rows: 0.287696 0.004216 0.291912 ( 0.310871)
120
+ 50000 rows: 1.417689 0.004253 1.421942 ( 1.484315)
121
+ 100000 rows: 2.786458 0.012036 2.798494 ( 2.839488)
122
+ 500000 rows: 13.706715 0.044224 13.750939 ( 15.014367)
123
+ 1000000 rows: 27.928350 0.183935 28.112285 ( 29.992699)
124
+ ```
125
+
126
+ If you want to skip loading data into mysql
127
+ ```
128
+ $ bundle exec rake benchmark:skip_data_load
129
+ ```
130
+
131
+ ## License
132
+
133
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
+ require_relative "spec/benchmarks/run_benchmark"
7
+
8
+ RuboCop::RakeTask.new
9
+
10
+ task :unit_tests do
11
+ RSpec::Core::RakeTask.new(:spec) do |t|
12
+ t.pattern = "spec/unit/**{,/*/**}/*_spec.rb"
13
+ end
14
+ Rake::Task["spec"].execute
15
+ end
16
+
17
+ namespace :benchmark do
18
+ task run: ["benchmark:load_data", "benchmark:skip_data_load"]
19
+ task :load_data do
20
+ Benchmark::RunBenchmark.new.load_data
21
+ end
22
+ task :skip_data_load do
23
+ Benchmark::RunBenchmark.new.run
24
+ end
25
+ end
26
+
27
+ task default: %i[unit_tests rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "mysqlexport"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/mysqlexport ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "mysqlexport"
4
+ require "mysqlexport/cli"
5
+
6
+ cli = Mysqlexport::Cli.new
7
+ cli.parse_options
8
+
9
+ options = cli.config
10
+
11
+ Mysqlexport::Csv.new(options).to_path
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/version"
5
+ require "active_support/core_ext" if ::ActiveSupport::VERSION::MAJOR >= 3
6
+
7
+ require "mysql2"
8
+
9
+ require "mysqlexport/version"
10
+ require "mysqlexport/config"
11
+ require "mysqlexport/csv"
12
+
13
+ module Mysqlexport
14
+ class Error < StandardError; end
15
+ end
@@ -0,0 +1,36 @@
1
+ require "mixlib/cli"
2
+
3
+ module Mysqlexport
4
+ class Cli
5
+ include ::Mixlib::CLI
6
+ %w[username password host Port database socket].each do |o|
7
+ option o.downcase.to_sym, { short: "-#{o[0, 1]} #{o.upcase}",
8
+ long: "--#{o.downcase}=#{o.upcase}",
9
+ description: "Set MySQL #{o.downcase}" }
10
+ end
11
+ option :execute, { short: "-e EXECUTE",
12
+ long: "--execute=EXECUTE",
13
+ description: "The SQL statement to execute" }
14
+ option :force_quotes, { short: "-f false",
15
+ long: "--force-quotes=true",
16
+ description: "force quotes to csv, default is false" }
17
+ option :col_sep, { short: "-c ,",
18
+ long: "--col-sep=,",
19
+ description: "column separtor for csv, default is \",\"" }
20
+ option :row_sep, { short: "-r \\n",
21
+ long: "--row-sep=\\n",
22
+ description: "row separator for csv, default is \"\\n\"" }
23
+ option :output_path, { short: "-o n",
24
+ long: "--out=PATH",
25
+ description: "output path, default is current directory" }
26
+ option :table, { short: "-t TABLE",
27
+ long: "--table=TABLE",
28
+ description: "MySQL table you want to export" }
29
+ option :help, { long: "--help",
30
+ description: "Show help",
31
+ on: :tail,
32
+ boolean: true,
33
+ show_options: true,
34
+ exit: 0 }
35
+ end
36
+ end
@@ -0,0 +1,76 @@
1
+ module Mysqlexport
2
+ class Config
3
+ attr_reader :user_specified_options
4
+
5
+ def initialize(user_specified_options = {})
6
+ @user_specified_options = user_specified_options.symbolize_keys
7
+ end
8
+
9
+ def host
10
+ user_specified_options[:host] || active_record_config.try(:[], :host)
11
+ end
12
+
13
+ def port
14
+ user_specified_options[:port] || active_record_config.try(:[], :port)
15
+ end
16
+
17
+ def username
18
+ user_specified_options[:username] || active_record_config.try(:[], :username)
19
+ end
20
+
21
+ def password
22
+ user_specified_options[:password] || active_record_config.try(:[], :password)
23
+ end
24
+
25
+ def database
26
+ user_specified_options[:database] || active_record_connection.try(:current_database)
27
+ end
28
+
29
+ def socket
30
+ user_specified_options[:socket] || active_record_config.try(:[], :socket)
31
+ end
32
+
33
+ def execute
34
+ user_specified_options[:execute] || "select * from #{table}"
35
+ end
36
+
37
+ def table
38
+ user_specified_options[:table]
39
+ end
40
+
41
+ def force_quotes
42
+ user_specified_options[:force_quotes].to_s.downcase == "true" || nil
43
+ end
44
+
45
+ def col_sep
46
+ user_specified_options[:col_sep]
47
+ end
48
+
49
+ def row_sep
50
+ user_specified_options[:row_sep]
51
+ end
52
+
53
+ def output_path
54
+ user_specified_options[:output_path]
55
+ end
56
+
57
+ def client
58
+ return @client if @client.is_a? ::Mysql2::Client
59
+
60
+ @client = ::Mysql2::Client.new(host: host, port: port, username: username,
61
+ password: password, database: database, socket: socket)
62
+ end
63
+
64
+ private
65
+
66
+ def active_record_connection
67
+ ::ActiveRecord::Base.connection if defined?(::ActiveRecord)
68
+ rescue StandardError
69
+ # oh well
70
+ end
71
+
72
+ def active_record_config
73
+ active_record_connection&.instance_variable_get :@config
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,35 @@
1
+ if RUBY_VERSION >= "1.9"
2
+ require "csv"
3
+ else
4
+ require "fastercsv"
5
+ end
6
+ require "mysqlexport/writer"
7
+ module Mysqlexport
8
+ class Csv
9
+ include Writer
10
+ attr_reader :client, :sql, :config
11
+
12
+ def initialize(options = {})
13
+ @config = Mysqlexport::Config.new options
14
+ @client = @config.client
15
+ end
16
+
17
+ def to_file(file)
18
+ result = client.query(config.execute, stream: true, cache_rows: false, as: :array)
19
+ file.write result.fields.to_csv(**csv_options)
20
+ result.each do |row|
21
+ file.write row.to_csv(**csv_options)
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def csv_options
28
+ options = {}
29
+ options[:force_quotes] = config.force_quotes unless config.force_quotes.nil?
30
+ options[:col_sep] = config.col_sep unless config.col_sep.nil?
31
+ options[:row_sep] = config.row_sep unless config.row_sep.nil?
32
+ options
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module Mysqlexport
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,21 @@
1
+ module Writer
2
+ def to_stdout
3
+ to_file $stdout
4
+ nil
5
+ end
6
+
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
14
+
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
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ require_relative "lib/mysqlexport/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "mysqlexport"
5
+ spec.version = Mysqlexport::VERSION
6
+ spec.authors = ["Iqbal Singh"]
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"
10
+ spec.homepage = "https://github.com/singhiqbal1007/mysqlexport"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
13
+ spec.metadata["homepage_uri"] = spec.homepage
14
+ spec.metadata["source_code_uri"] = spec.homepage
15
+
16
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "benchmark", "~> 0.1"
24
+ spec.add_development_dependency "rake", "~> 13.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ spec.add_development_dependency "rubocop", "~> 1.7"
27
+ spec.add_development_dependency "yaml", "~> 0.1"
28
+
29
+ spec.add_dependency "activerecord", "~> 6.1"
30
+ spec.add_dependency "mixlib-cli", "~> 2.1.8"
31
+ spec.add_dependency "mysql2", "~> 0.5.3"
32
+ end
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysqlexport
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Iqbal Singh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-02-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: benchmark
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: yaml
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '6.1'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '6.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: mixlib-cli
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 2.1.8
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 2.1.8
111
+ - !ruby/object:Gem::Dependency
112
+ name: mysql2
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 0.5.3
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 0.5.3
125
+ description: Gives you binary mysqlexport and Ruby classes to export mysql tables
126
+ into csv files
127
+ email:
128
+ - singhiqbal1007@gmail.com
129
+ executables:
130
+ - mysqlexport
131
+ extensions: []
132
+ extra_rdoc_files: []
133
+ files:
134
+ - ".github/workflows/main.yml"
135
+ - ".gitignore"
136
+ - ".rspec"
137
+ - ".rubocop.yml"
138
+ - CODE_OF_CONDUCT.md
139
+ - Gemfile
140
+ - Gemfile.lock
141
+ - LICENSE.txt
142
+ - README.md
143
+ - Rakefile
144
+ - bin/console
145
+ - bin/setup
146
+ - exe/mysqlexport
147
+ - lib/mysqlexport.rb
148
+ - lib/mysqlexport/cli.rb
149
+ - lib/mysqlexport/config.rb
150
+ - lib/mysqlexport/csv.rb
151
+ - lib/mysqlexport/version.rb
152
+ - lib/mysqlexport/writer.rb
153
+ - mysqlexport.gemspec
154
+ homepage: https://github.com/singhiqbal1007/mysqlexport
155
+ licenses:
156
+ - MIT
157
+ metadata:
158
+ homepage_uri: https://github.com/singhiqbal1007/mysqlexport
159
+ source_code_uri: https://github.com/singhiqbal1007/mysqlexport
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: 2.4.0
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubygems_version: 3.1.2
176
+ signing_key:
177
+ specification_version: 4
178
+ summary: Export mysql table to csv files
179
+ test_files: []