bulkseed_mysql 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 0d9e569b787184c4da8b195dd018d5f8e4a0ce08e935b58e6e29a3026677a8d1
4
+ data.tar.gz: e6797a02c534d791cba415af098c3f5eb2e67008b581472fcee1b0de1f404460
5
+ SHA512:
6
+ metadata.gz: 5312f0338c9f2403628da76ec256bed392514a759895c189d1f37e3acce65227c787bb7bd227408daa5fdbd69c4ff272d896edc294292a354957795f35cd6aad
7
+ data.tar.gz: da2c3658e0ed40892f811fa8e98e0a1ee35a3f12dfd34da2dcf5ae479e5a13bb41a0e83875006c16963a7be17a73c8ee4d5a6ed4a2c96e789dc7d549df818bad
@@ -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.5
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ .env
@@ -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 yuma.fuu05@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,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in bulkseed_mysql.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "test-unit", "~> 3.0"
11
+
12
+ gem "terminal-table"
data/Gemfile.lock ADDED
@@ -0,0 +1,27 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bulkseed_mysql (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ power_assert (2.0.0)
10
+ rake (13.0.6)
11
+ terminal-table (3.0.1)
12
+ unicode-display_width (>= 1.1.1, < 3)
13
+ test-unit (3.4.4)
14
+ power_assert
15
+ unicode-display_width (2.0.0)
16
+
17
+ PLATFORMS
18
+ arm64-darwin-20
19
+
20
+ DEPENDENCIES
21
+ bulkseed_mysql!
22
+ rake (~> 13.0)
23
+ terminal-table
24
+ test-unit (~> 3.0)
25
+
26
+ BUNDLED WITH
27
+ 2.2.3
data/README.md ADDED
@@ -0,0 +1,76 @@
1
+ # BulkseedMysql
2
+
3
+ ## Installation
4
+
5
+ ```ruby
6
+ gem 'bulkseed_mysql'
7
+ ```
8
+
9
+ Or
10
+
11
+ ```bash
12
+ $ gem install bulkseed_mysql
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Use REPLACE Statement. [see](https://dev.mysql.com/doc/refman/5.6/en/replace.html)<br>
18
+ If call without primary key, mysql will do DELETE and INSERT<br>
19
+ Be Carefull!!
20
+
21
+ ```ruby
22
+ require "bulkseed_mysql"
23
+
24
+ now = Time.now.to_s.split(" ").take(2).join(" ")
25
+ # => "2021-07-09 22:14:59"
26
+
27
+ seed = BulkseedMysql.new
28
+
29
+ seed.prepare "users" do |s|
30
+ s.columns = [:id, :name, :sex, :created_at, :updated_at]
31
+ s.data = [
32
+ [1, "hoge", 23, :male, now, now],
33
+ [2, "fuga", 25, :female, now, now],
34
+ [3, "piyo", 18, :other, now, now],
35
+ ]
36
+ end
37
+
38
+ seed.prepare do |s|
39
+ # you can also specify here
40
+ s.table = "admins"
41
+
42
+ # you can also set Array of Hash to data
43
+ # keys are to be columns
44
+ s.data = [
45
+ {
46
+ id: 1,
47
+ name: "foo",
48
+ email: "admin1@sample.com",
49
+ created_at: now,
50
+ updated_at: now,
51
+ },
52
+ {
53
+ id: 2,
54
+ name: "bar",
55
+ email: "admin2@sample.com",
56
+ created_at: now,
57
+ updated_at: now,
58
+ },
59
+ ]
60
+ end
61
+
62
+ # insert all prepared tables
63
+ seed.call
64
+ ```
65
+
66
+ ```ruby
67
+ require "bulkseed_mysql"
68
+
69
+ # You can call without prepare
70
+ seed = BulkseedMysql.call "users" do |s|
71
+ s.data = [
72
+ ...
73
+ ]
74
+ end
75
+
76
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/*_test.rb"]
10
+ end
11
+
12
+ task default: :test
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 "bulkseed_mysql"
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
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/bulkseed_mysql/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "bulkseed_mysql"
7
+ spec.version = BulkseedMysql::VERSION
8
+ spec.authors = ["Yuma Ishikawa"]
9
+ spec.email = ["yuma.fuu05@gmail.com"]
10
+
11
+ spec.summary = "Bulkseed for mysql"
12
+ spec.description = "Bulkseed for mysql"
13
+ spec.homepage = "https://github.com/YumaFuu/bulkseed_mysql"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
15
+
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/YumaFuu/bulkseed_mysql"
19
+ spec.metadata["changelog_uri"] = "https://github.com/YumaFuu/bulkseed_mysql"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ # spec.add_dependency "example-gem", "~> 1.0"
32
+
33
+ # For more information and examples about making a new gem, checkout our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+ require "pry"
3
+ require "json"
4
+ require 'terminal-table'
5
+
6
+ class BulkseedMysql
7
+ class Seed
8
+ def inspect
9
+ pretty_table = Terminal::Table.new do |t|
10
+ t << @columns
11
+ t.add_separator
12
+
13
+ @values.each do |v|
14
+ t << v
15
+ end
16
+ end
17
+
18
+ <<~TXT
19
+ ======= #{@table} =========
20
+ #{ pretty_table }
21
+ TXT
22
+ end
23
+
24
+ def initialize(table)
25
+ @table = table
26
+ @values = []
27
+ end
28
+
29
+ def data= data
30
+ data.each do |item|
31
+ case item
32
+ when Array
33
+ @values << item
34
+ when Hash
35
+ @columns ||= item.keys
36
+
37
+ if @columns != item.keys
38
+ raise <<~TXT
39
+ Keys are not same to before
40
+ table: #{@table}
41
+ row: #{JSON.pretty_generate(item)}
42
+ TXT
43
+ end
44
+
45
+ @values << item.values
46
+ end
47
+ end
48
+ end
49
+
50
+ def columns= columns
51
+ @columns = columns
52
+ end
53
+
54
+ def table= table
55
+ @table = table
56
+ end
57
+
58
+ def to_cmd
59
+ values = @values.map do |rows|
60
+ vals = rows.map do |v|
61
+ # escape quotes
62
+ s = %Q(#{v}).gsub('"', '\"')
63
+ v.nil? ? "NULL" : "\"#{s}\""
64
+ end
65
+
66
+ "(#{ vals * "," })"
67
+ end
68
+
69
+ <<~SQL
70
+ REPLACE INTO #{@table}
71
+ (#{ @columns.map{ |a| %(`#{a}`) } * ',' })
72
+ VALUES #{ values * "," }
73
+ SQL
74
+ end
75
+ end
76
+
77
+ def self.call(name = nil, conn = nil, &block)
78
+ seed = new conn
79
+ seed.prepare name, &block
80
+ seed.call
81
+ end
82
+
83
+ attr_reader :seeds
84
+ attr_accessor :conn
85
+
86
+ def initialize(conn = nil)
87
+ @conn = conn
88
+ @conn ||= Mysql2::Client.new(
89
+ host: ENV["DB_HOST"],
90
+ username: ENV["DB_USER"],
91
+ password: ENV["DB_PASSWORD"],
92
+ database: ENV["DB_NAME"],
93
+ )
94
+
95
+ @seeds = []
96
+ end
97
+
98
+ def prepare(name = nil, &block)
99
+ seed = Seed.new name
100
+ yield seed
101
+
102
+ @seeds << seed
103
+ end
104
+
105
+ def call
106
+ @seeds.each do |s|
107
+ puts s.inspect
108
+ @conn.query s.to_cmd
109
+ end
110
+ end
111
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BulkseedMysql
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,109 @@
1
+ require "./lib/bulkseed_mysql.rb"
2
+ require "bundler/inline"
3
+
4
+ gemfile do
5
+ gem "activerecord"
6
+ gem "mysql2"
7
+ end
8
+
9
+ require "active_record"
10
+
11
+ db = "bulkseed_mysql_sample"
12
+ ActiveRecord::Base.establish_connection(
13
+ adapter: "mysql2",
14
+ host: ENV["DB_HOST"],
15
+ username: ENV["DB_USER"],
16
+ password: ENV["DB_PASSWORD"],
17
+ )
18
+
19
+ con = ActiveRecord::Base.connection
20
+ con.execute "DROP DATABASE IF EXISTS #{db}"
21
+ con.execute "CREATE DATABASE IF NOT EXISTS #{db}"
22
+
23
+ ActiveRecord::Base.establish_connection(
24
+ adapter: "mysql2",
25
+ host: ENV["DB_HOST"],
26
+ username: ENV["DB_USER"],
27
+ password: ENV["DB_PASSWORD"],
28
+ database: db,
29
+ )
30
+
31
+ class CreateSample < ActiveRecord::Migration[4.2]
32
+ def change
33
+ create_table :users do |t|
34
+ t.string :name
35
+ t.string :age
36
+ t.integer :sex
37
+ t.timestamps
38
+ end
39
+
40
+ create_table :posts do |t|
41
+ t.string :title
42
+ t.string :body
43
+ t.integer :user_id
44
+ t.index :user_id
45
+ t.timestamps
46
+ end
47
+ end
48
+ end
49
+
50
+ CreateSample.migrate :up
51
+
52
+ class User < ActiveRecord::Base
53
+ enum sex: [:male, :female, :other]
54
+ end
55
+
56
+ class Post < ActiveRecord::Base
57
+ end
58
+
59
+ # -----------------------------------
60
+ # Use prepare
61
+
62
+ seed = BulkseedMysql.new
63
+ seed.conn = ActiveRecord::Base.connection
64
+
65
+ now = Time.now.to_s
66
+ binding.pry
67
+ seed.prepare do |s|
68
+ s.table = User.table_name
69
+ s.columns = User.column_names
70
+ s.data = [
71
+ [1, "aaa", 23, User.sexes[:male], now, now],
72
+ [2, "bbb", 18, User.sexes[:female], now, now],
73
+ [3, "ccc", 18, User.sexes[:other], now, now],
74
+ ]
75
+ end
76
+
77
+ seed.call
78
+
79
+ # -----------------------------------
80
+ # Without prepare
81
+
82
+ BulkseedMysql.call "posts" do |s|
83
+ s.data = [
84
+ {
85
+ id: 1,
86
+ user_id: 1,
87
+ title: "user1-1",
88
+ body: "body1",
89
+ },
90
+ {
91
+ id: 2,
92
+ user_id: 1,
93
+ title: "user1-2",
94
+ body: "body2",
95
+ },
96
+ {
97
+ id: 3,
98
+ user_id: 2,
99
+ title: "user2-1",
100
+ body: "body3",
101
+ },
102
+ {
103
+ id: 4,
104
+ user_id: 3,
105
+ title: "user3-1",
106
+ body: "body4",
107
+ },
108
+ ]
109
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bulkseed_mysql
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Yuma Ishikawa
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-07-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Bulkseed for mysql
14
+ email:
15
+ - yuma.fuu05@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".github/workflows/main.yml"
21
+ - ".gitignore"
22
+ - CODE_OF_CONDUCT.md
23
+ - Gemfile
24
+ - Gemfile.lock
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - bulkseed_mysql.gemspec
30
+ - lib/bulkseed_mysql.rb
31
+ - lib/bulkseed_mysql/version.rb
32
+ - sample/activerecord.rb
33
+ homepage: https://github.com/YumaFuu/bulkseed_mysql
34
+ licenses: []
35
+ metadata:
36
+ homepage_uri: https://github.com/YumaFuu/bulkseed_mysql
37
+ source_code_uri: https://github.com/YumaFuu/bulkseed_mysql
38
+ changelog_uri: https://github.com/YumaFuu/bulkseed_mysql
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 2.4.0
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.2.3
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Bulkseed for mysql
58
+ test_files: []