mass_insert 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.rvmrc +14 -5
- data/Gemfile +1 -0
- data/README.md +7 -7
- data/lib/mass_insert/base.rb +31 -1
- data/lib/mass_insert/builder/adapters/adapter.rb +1 -14
- data/lib/mass_insert/builder/adapters/helpers/abstract_query.rb +1 -1
- data/lib/mass_insert/builder/adapters/helpers/column_value.rb +17 -17
- data/lib/mass_insert/builder/adapters/mysql2_adapter.rb +1 -9
- data/lib/mass_insert/builder/adapters/sqlserver_adapter.rb +0 -6
- data/lib/mass_insert/builder/base.rb +10 -10
- data/lib/mass_insert/process.rb +13 -4
- data/lib/mass_insert/version.rb +1 -1
- data/spec/adapters/column_types/date_spec.rb +14 -0
- data/spec/adapters/column_types/datetime_spec.rb +14 -0
- data/spec/adapters/column_types/time_spec.rb +14 -0
- data/spec/dummy/app/models/user.rb +1 -1
- data/spec/dummy/db/migrate/20130412154541_create_users.rb +2 -0
- data/spec/dummy/db/schema.rb +2 -0
- data/spec/lib/mass_insert/builder/adapters/adapter_spec.rb +3 -16
- data/spec/lib/mass_insert/builder/adapters/helpers/column_value_spec.rb +74 -41
- data/spec/lib/mass_insert/builder/adapters/mysql_adapter_spec.rb +0 -6
- data/spec/lib/mass_insert/builder/adapters/sqlserver_adapter_spec.rb +0 -6
- data/spec/lib/mass_insert/process_spec.rb +47 -33
- data/spec/spec_helper.rb +4 -0
- metadata +11 -17
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
Zjc0YmM2NTg0MTRmYjg2NWQyNTcxMWQzZjdhY2VmMzVjOGE1ODFlNg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZTE4MGExN2IwY2NmOTBkYTUwNmVmYzNjZDZkM2IxZTNhMmY1MmYyNw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NzM2ZjNiODg1ODFkYjk0ZmJlM2Y1NGQ2MmI3Zjg1N2ZkYzU2OTBjMDM5MTUx
|
10
|
+
OTQxZDI4MTEyZGI2MTFjZWI5NzJiMWE2YzQxMTJiZTliZWYyMzIyOTEzNTkx
|
11
|
+
ZWE0MTZkYjIxODE2ZDMyYTJkMDE0Mzg4NTRhZDQ2MmVlNDNiZDM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
M2Q1MjFlYmVhNzc2ZDRlNDY4MTM0MTk3NjA0ODg4NDNkYmMwZDU4YjJkNWZm
|
14
|
+
N2IxZTZhMTI3MGFiNjI3Y2RjZGM1ZWUyYzNlODlkOTEzMDliMzM3ZjA2NzAz
|
15
|
+
MTA0ZmVjMmExNjNmNWYwNDE3MTRiNmJkYzMxYTEzMjI1OGMzYmU=
|
data/.rvmrc
CHANGED
@@ -6,11 +6,11 @@
|
|
6
6
|
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
|
7
7
|
# Only full ruby name is supported here, for short names use:
|
8
8
|
# echo "rvm use 1.9.3" > .rvmrc
|
9
|
-
environment_id="ruby-1.9.3-
|
9
|
+
environment_id="ruby-1.9.3-p484@mass_insert"
|
10
10
|
|
11
11
|
# Uncomment the following lines if you want to verify rvm version per project
|
12
|
-
# rvmrc_rvm_version="1.
|
13
|
-
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} |
|
12
|
+
# rvmrc_rvm_version="1.25.14 (stable)" # 1.10.1 seems like a safe start
|
13
|
+
# eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | __rvm_awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
|
14
14
|
# echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
|
15
15
|
# return 1
|
16
16
|
# }
|
@@ -30,6 +30,15 @@ then
|
|
30
30
|
fi
|
31
31
|
done
|
32
32
|
unset __hook
|
33
|
+
if (( ${rvm_use_flag:=1} >= 2 )) # display only when forced
|
34
|
+
then
|
35
|
+
if [[ $- == *i* ]] # check for interactive shells
|
36
|
+
then printf "%b" "Using: $(tput setaf 2 2>/dev/null)$GEM_HOME$(tput sgr0 2>/dev/null)
|
37
|
+
" # show the user the ruby and gemset they are using in green
|
38
|
+
else printf "%b" "Using: $GEM_HOME
|
39
|
+
" # don't use colors in non-interactive shells
|
40
|
+
fi
|
41
|
+
fi
|
33
42
|
else
|
34
43
|
# If the environment file has not yet been created, use the RVM CLI to select.
|
35
44
|
rvm --create "$environment_id" || {
|
@@ -41,7 +50,7 @@ fi
|
|
41
50
|
# If you use bundler, this might be useful to you:
|
42
51
|
# if [[ -s Gemfile ]] && {
|
43
52
|
# ! builtin command -v bundle >/dev/null ||
|
44
|
-
# builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
|
53
|
+
# builtin command -v bundle | GREP_OPTIONS="" \grep $rvm_path/bin/bundle >/dev/null
|
45
54
|
# }
|
46
55
|
# then
|
47
56
|
# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
|
@@ -49,5 +58,5 @@ fi
|
|
49
58
|
# fi
|
50
59
|
# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
|
51
60
|
# then
|
52
|
-
# bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
|
61
|
+
# bundle install | GREP_OPTIONS="" \grep -vE '^Using|Your bundle is complete'
|
53
62
|
# fi
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
# MassInsert [![Build Status](https://travis-ci.org/alejandrogutierrez/mass_insert.png?branch=master)](https://travis-ci.org/alejandrogutierrez/mass_insert)
|
1
|
+
# MassInsert [![Build Status](https://travis-ci.org/alejandrogutierrez/mass_insert.png?branch=master)](https://travis-ci.org/alejandrogutierrez/mass_insert) [![Coverage Status](https://coveralls.io/repos/alejandrogutierrez/mass_insert/badge.png)](https://coveralls.io/r/alejandrogutierrez/mass_insert)
|
2
2
|
|
3
3
|
This gem aims to provide an easy and faster way to do single database insertions in Rails.
|
4
|
-
Support Mysql, PostgreSQL and SQLite3 adapters.
|
4
|
+
Support Mysql, PostgreSQL and SQLite3 adapters. It depends on ActiveRecord.
|
5
5
|
|
6
6
|
## Installation
|
7
7
|
|
@@ -25,7 +25,7 @@ Faster. It's depending of the computer but these are some results...
|
|
25
25
|
|
26
26
|
## Attention
|
27
27
|
|
28
|
-
Since this is a single database insertion your model
|
28
|
+
Since this is a single database insertion your model validations will be ignored, then if you use this gem you need to be sure that information is OK to be persisted.
|
29
29
|
|
30
30
|
## Basic Usage
|
31
31
|
|
@@ -53,7 +53,7 @@ And call mass_insert method from your model:
|
|
53
53
|
|
54
54
|
## Results
|
55
55
|
|
56
|
-
Sometimes after MassInsert process you need to see
|
56
|
+
Sometimes after MassInsert process you need to see information about the process. MassInsert provides a simple way to do it. Just call the next methods from your model after MassInsert execution.
|
57
57
|
|
58
58
|
User.mass_insert_results.records # => 120000
|
59
59
|
|
@@ -67,7 +67,7 @@ Some result options are...
|
|
67
67
|
|
68
68
|
## Options
|
69
69
|
|
70
|
-
MassInsert accepts options hash by second param when you call `mass_insert` from your model.
|
70
|
+
MassInsert accepts options hash by second param when you call `mass_insert` from your model. These options allow you to configure the way that records will be persisted. Example...
|
71
71
|
|
72
72
|
options = {
|
73
73
|
:some_option => some_value,
|
@@ -84,13 +84,13 @@ Some options you can include are...
|
|
84
84
|
|
85
85
|
**Primary key**
|
86
86
|
|
87
|
-
By default primary key is
|
87
|
+
By default primary key is generated automatically. If you wish to set primary key manually you need to pass the `primary_key` option on true. Example...
|
88
88
|
|
89
89
|
User.mass_insert(values, :primary_key => true)
|
90
90
|
|
91
91
|
**Each slice**
|
92
92
|
|
93
|
-
Due you can get a database timeout error you can specify that the insertion will be in batches.
|
93
|
+
Due you can get a database timeout error you can specify that the insertion will be in batches. Just pass the `each_slice` option with the records per batch. Example...
|
94
94
|
|
95
95
|
User.mass_insert(values, :each_slice => 10000)
|
96
96
|
|
data/lib/mass_insert/base.rb
CHANGED
@@ -26,7 +26,7 @@ module MassInsert
|
|
26
26
|
# By default primary key is ignored. If you wish primary key doesn't
|
27
27
|
# be ignored you need to pass the primary_key option on true.
|
28
28
|
#
|
29
|
-
#
|
29
|
+
# User.mass_insert(values, :primary_key => true)
|
30
30
|
#
|
31
31
|
# === Each slice
|
32
32
|
#
|
@@ -50,6 +50,36 @@ module MassInsert
|
|
50
50
|
|
51
51
|
module ClassMethods
|
52
52
|
|
53
|
+
# == Results
|
54
|
+
#
|
55
|
+
# Sometimes after MassInsert process you need to see information about
|
56
|
+
# the process. MassInsert provides a simple way to do it. Just call the
|
57
|
+
# next methods from your model after MassInsert execution:
|
58
|
+
#
|
59
|
+
# === Records
|
60
|
+
#
|
61
|
+
# Returns the amount of records that were persisted.
|
62
|
+
#
|
63
|
+
# User.mass_insert_results.records # => 120000
|
64
|
+
#
|
65
|
+
# === Time
|
66
|
+
#
|
67
|
+
# Returns the time that took to do all the MassInsert process.
|
68
|
+
#
|
69
|
+
# User.mass_insert_results.time # => 0.67
|
70
|
+
#
|
71
|
+
# === Building time
|
72
|
+
#
|
73
|
+
# Returns the time that took to create the query string that was persisted.
|
74
|
+
#
|
75
|
+
# User.mass_insert_results.building_time # => 0.58
|
76
|
+
#
|
77
|
+
# === Execution time
|
78
|
+
#
|
79
|
+
# Returns the time that took to execute the query string that was persisted.
|
80
|
+
#
|
81
|
+
# User.mass_insert_results.execution_time # => 0.09
|
82
|
+
#
|
53
83
|
def mass_insert_results
|
54
84
|
Result.new(@mass_insert_process)
|
55
85
|
end
|
@@ -34,23 +34,10 @@ module MassInsert
|
|
34
34
|
columns.include?(:created_at) && columns.include?(:updated_at)
|
35
35
|
end
|
36
36
|
|
37
|
-
# Returns timestamp format according to the database adapter. This
|
38
|
-
# function can be overwrite in database adapters classes.
|
39
|
-
def timestamp_format
|
40
|
-
"%Y-%m-%d %H:%M:%S.%6N"
|
41
|
-
end
|
42
|
-
|
43
|
-
# Returns the timestamp value according to the correct timestamp
|
44
|
-
# format to that database engine.
|
45
|
-
def timestamp
|
46
|
-
Time.now.strftime(timestamp_format)
|
47
|
-
end
|
48
|
-
|
49
37
|
# Returns the timestamp hash to be merge into row values that will
|
50
38
|
# be saved in the database.
|
51
39
|
def timestamp_hash
|
52
|
-
|
53
|
-
{:created_at => timestamp_value, :updated_at => timestamp_value}
|
40
|
+
{:created_at => Time.now, :updated_at => Time.now}
|
54
41
|
end
|
55
42
|
|
56
43
|
# Returns the amount of records to each query. Tries to take the
|
@@ -48,11 +48,19 @@ module MassInsert
|
|
48
48
|
end
|
49
49
|
alias :column_value_text :column_value_string
|
50
50
|
alias :column_value_date :column_value_string
|
51
|
-
alias :column_value_time :column_value_string
|
52
|
-
alias :column_value_datetime :column_value_string
|
53
|
-
alias :column_value_timestamp :column_value_string
|
54
51
|
alias :column_value_binary :column_value_string
|
55
52
|
|
53
|
+
def column_value_time
|
54
|
+
case Utilities.adapter
|
55
|
+
when 'mysql2' then format = "%Y-%m-%d %H:%M:%S"
|
56
|
+
when 'sqlserver' then format = "%Y-%m-%d %H:%M:%S.%3N"
|
57
|
+
else format = "%Y-%m-%d %H:%M:%S.%6N"
|
58
|
+
end
|
59
|
+
"'#{column_value.strftime(format)}'"
|
60
|
+
end
|
61
|
+
alias :column_value_timestamp :column_value_time
|
62
|
+
alias :column_value_datetime :column_value_time
|
63
|
+
|
56
64
|
# Returns the correct value to integer column.
|
57
65
|
def column_value_integer
|
58
66
|
column_value.to_i.to_s
|
@@ -67,21 +75,13 @@ module MassInsert
|
|
67
75
|
# Returns the correct value to boolean column. This column calls
|
68
76
|
# the correct method according to the database adapter.
|
69
77
|
def column_value_boolean
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
column_value ? "true" : "false"
|
77
|
-
end
|
78
|
-
alias :postgresql_column_value_boolean :mysql2_column_value_boolean
|
79
|
-
|
80
|
-
# Returns the column value to boolean column to sqlite database.
|
81
|
-
def sqlite3_column_value_boolean
|
82
|
-
column_value ? "1" : "0"
|
78
|
+
case Utilities.adapter
|
79
|
+
when 'mysql2', 'postgresql'
|
80
|
+
column_value ? "true" : "false"
|
81
|
+
when 'sqlite3', 'sqlserver'
|
82
|
+
column_value ? "1" : "0"
|
83
|
+
end
|
83
84
|
end
|
84
|
-
alias :sqlserver_column_value_boolean :sqlite3_column_value_boolean
|
85
85
|
|
86
86
|
end
|
87
87
|
end
|
@@ -1,15 +1,7 @@
|
|
1
1
|
module MassInsert
|
2
2
|
module Builder
|
3
3
|
module Adapters
|
4
|
-
class Mysql2Adapter < Adapter
|
5
|
-
|
6
|
-
# The method is overwrited because the timestamp format to mysql
|
7
|
-
# adapter does not need accuracy with nanoseconds.
|
8
|
-
def timestamp_format
|
9
|
-
"%Y-%m-%d %H:%M:%S"
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
4
|
+
class Mysql2Adapter < Adapter; end
|
13
5
|
end
|
14
6
|
end
|
15
7
|
end
|
@@ -14,12 +14,6 @@ module MassInsert
|
|
14
14
|
each_slice || 1000
|
15
15
|
end
|
16
16
|
|
17
|
-
# The method is overwrited because the timestamp format to SQLServer
|
18
|
-
# adapter needs accuracy with three nanoseconds.
|
19
|
-
def timestamp_format
|
20
|
-
"%Y-%m-%d %H:%M:%S.%3N"
|
21
|
-
end
|
22
|
-
|
23
17
|
end
|
24
18
|
end
|
25
19
|
end
|
@@ -2,6 +2,15 @@ module MassInsert
|
|
2
2
|
module Builder
|
3
3
|
class Base
|
4
4
|
|
5
|
+
# Supported adapters hash. Key is the adapter name and value
|
6
|
+
# is its adapter class.
|
7
|
+
ADAPTERS = {
|
8
|
+
'mysql2' => Adapters::Mysql2Adapter,
|
9
|
+
'postgresql' => Adapters::PostgreSQLAdapter,
|
10
|
+
'sqlite3' => Adapters::SQLite3Adapter,
|
11
|
+
'sqlserver' => Adapters::SQLServerAdapter
|
12
|
+
}
|
13
|
+
|
5
14
|
# This function gets the correct adapter class and returns the
|
6
15
|
# sql string ready to be executed.
|
7
16
|
def build values, options
|
@@ -11,16 +20,7 @@ module MassInsert
|
|
11
20
|
# Returns the class of the correct database adapter according to the
|
12
21
|
# database engine that is used in Rails project.
|
13
22
|
def adapter_class
|
14
|
-
|
15
|
-
when "mysql2"
|
16
|
-
Adapters::Mysql2Adapter
|
17
|
-
when "postgresql"
|
18
|
-
Adapters::PostgreSQLAdapter
|
19
|
-
when "sqlite3"
|
20
|
-
Adapters::SQLite3Adapter
|
21
|
-
when "sqlserver"
|
22
|
-
Adapters::SQLServerAdapter
|
23
|
-
end
|
23
|
+
ADAPTERS[Utilities.adapter]
|
24
24
|
end
|
25
25
|
|
26
26
|
end
|
data/lib/mass_insert/process.rb
CHANGED
@@ -8,17 +8,26 @@ module MassInsert
|
|
8
8
|
@options = options
|
9
9
|
end
|
10
10
|
|
11
|
-
# Calls the necessary methods to complete the mass insertion process
|
12
|
-
# and the time that each method takes being executed.
|
13
11
|
def start
|
12
|
+
# MassInsert process is completed by two actions. The first one
|
13
|
+
# gets queries that will be persisted.
|
14
14
|
@building_time = Benchmark.measure do
|
15
|
-
@queries =
|
15
|
+
@queries = builder.build(@values, @options)
|
16
16
|
end
|
17
17
|
|
18
|
+
# The second one executes queries into the database using an
|
19
|
+
# ActiveRecord connection.
|
18
20
|
@execution_time = Benchmark.measure do
|
19
|
-
|
21
|
+
executer.execute(@queries)
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
25
|
+
def builder
|
26
|
+
@builder ||= Builder::Base.new
|
27
|
+
end
|
28
|
+
|
29
|
+
def executer
|
30
|
+
@executer ||= Executer.new
|
31
|
+
end
|
23
32
|
end
|
24
33
|
end
|
data/lib/mass_insert/version.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Date" do
|
4
|
+
let!(:values){ [{}] }
|
5
|
+
let!(:options){ Hash.new }
|
6
|
+
|
7
|
+
context "when contains date value" do
|
8
|
+
it "saves the date value correctly" do
|
9
|
+
values.first.merge!(birthday: Date.today)
|
10
|
+
User.mass_insert(values, options)
|
11
|
+
expect(User.last.birthday).to eq(Date.today)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Datetime" do
|
4
|
+
let!(:values){ [{}] }
|
5
|
+
let!(:options){ Hash.new }
|
6
|
+
|
7
|
+
context "when contains datetime value" do
|
8
|
+
it "saves the datetime value correctly" do
|
9
|
+
values.first.merge!(created_at: DateTime.now)
|
10
|
+
User.mass_insert(values, options)
|
11
|
+
expect(User.last.created_at.strftime("%Y-%m-%d %H:%M")).to eq(DateTime.now.strftime("%Y-%m-%d %H:%M"))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Time" do
|
4
|
+
let!(:values){ [{}] }
|
5
|
+
let!(:options){ Hash.new }
|
6
|
+
|
7
|
+
context "when contains time value" do
|
8
|
+
it "saves the time value correctly" do
|
9
|
+
values.first.merge!(birthtime: Time.now)
|
10
|
+
User.mass_insert(values, options)
|
11
|
+
expect(User.last.birthtime.strftime("%H:%M")).to eq(Time.now.strftime("%H:%M"))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -20,6 +20,8 @@ ActiveRecord::Schema.define(:version => 20130412154541) do
|
|
20
20
|
t.decimal "money", :precision => 10, :scale => 4
|
21
21
|
t.boolean "active"
|
22
22
|
t.binary "checked"
|
23
|
+
t.date "birthday"
|
24
|
+
t.time "birthtime"
|
23
25
|
t.datetime "created_at", :null => false
|
24
26
|
t.datetime "updated_at", :null => false
|
25
27
|
end
|
@@ -89,24 +89,11 @@ describe MassInsert::Builder::Adapters::Adapter do
|
|
89
89
|
end
|
90
90
|
end
|
91
91
|
|
92
|
-
describe "#timestamp_format" do
|
93
|
-
it "returns default timestamp format" do
|
94
|
-
expect(subject.timestamp_format).to eq("%Y-%m-%d %H:%M:%S.%6N")
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
describe "#timestamp" do
|
99
|
-
it "returns default timestamp value with correct format" do
|
100
|
-
subject.stub(:timestamp_format).and_return("%Y-%m-%d %H:%M:%S")
|
101
|
-
expect(subject.timestamp).to eq(Time.now.strftime("%Y-%m-%d %H:%M:%S"))
|
102
|
-
end
|
103
|
-
end
|
104
|
-
|
105
92
|
describe "#timestamp_hash" do
|
106
93
|
it "returns a timestamp hash" do
|
107
|
-
|
108
|
-
|
109
|
-
expect(subject.timestamp_hash).to eq({:created_at =>
|
94
|
+
time = Time.now
|
95
|
+
Time.stub(:now).and_return(time)
|
96
|
+
expect(subject.timestamp_hash).to eq({:created_at => Time.now, :updated_at => Time.now})
|
110
97
|
end
|
111
98
|
end
|
112
99
|
|
@@ -82,9 +82,6 @@ describe MassInsert::Builder::Adapters::Helpers::ColumnValue do
|
|
82
82
|
:string,
|
83
83
|
:text,
|
84
84
|
:date,
|
85
|
-
:time,
|
86
|
-
:datetime,
|
87
|
-
:timestamp,
|
88
85
|
:binary
|
89
86
|
].each do |column_type|
|
90
87
|
method = :"column_value_#{column_type}"
|
@@ -97,6 +94,50 @@ describe MassInsert::Builder::Adapters::Helpers::ColumnValue do
|
|
97
94
|
end
|
98
95
|
end
|
99
96
|
|
97
|
+
[
|
98
|
+
:time,
|
99
|
+
:datetime,
|
100
|
+
:timestamp,
|
101
|
+
].each do |column_type|
|
102
|
+
method = :"column_value_#{column_type}"
|
103
|
+
|
104
|
+
describe "##{method.to_s}" do
|
105
|
+
before :each do
|
106
|
+
time = Time.now
|
107
|
+
Time.stub(:now).and_return(time)
|
108
|
+
subject.stub(:column_value).and_return(Time.now)
|
109
|
+
end
|
110
|
+
|
111
|
+
context "when adapter is mysql2" do
|
112
|
+
it "returns date with the correct format" do
|
113
|
+
MassInsert::Builder::Utilities.stub(:adapter).and_return('mysql2')
|
114
|
+
expect(subject.send(method)).to eq("'#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}'")
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context "when adapter is sqlserver" do
|
119
|
+
it "returns date with the correct format" do
|
120
|
+
MassInsert::Builder::Utilities.stub(:adapter).and_return('sqlserver')
|
121
|
+
expect(subject.send(method)).to eq("'#{Time.now.strftime("%Y-%m-%d %H:%M:%S.%3N")}'")
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context "when adapter is sqlite3" do
|
126
|
+
it "returns date with the correct format" do
|
127
|
+
MassInsert::Builder::Utilities.stub(:adapter).and_return('sqlite3')
|
128
|
+
expect(subject.send(method)).to eq("'#{Time.now.strftime("%Y-%m-%d %H:%M:%S.%6N")}'")
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context "when adapter is postgresql" do
|
133
|
+
it "returns date with the correct format" do
|
134
|
+
MassInsert::Builder::Utilities.stub(:adapter).and_return('postgresql')
|
135
|
+
expect(subject.send(method)).to eq("'#{Time.now.strftime("%Y-%m-%d %H:%M:%S.%6N")}'")
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
100
141
|
describe "#column_value_integer" do
|
101
142
|
context "when is a integer value" do
|
102
143
|
it "returns the same integer value" do
|
@@ -134,54 +175,46 @@ describe MassInsert::Builder::Adapters::Helpers::ColumnValue do
|
|
134
175
|
end
|
135
176
|
|
136
177
|
describe "#column_value_boolean" do
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
end
|
143
|
-
|
144
|
-
[
|
145
|
-
:mysql2,
|
146
|
-
:postgresql,
|
147
|
-
].each do |adapter|
|
148
|
-
method = :"#{adapter}_column_value_boolean"
|
178
|
+
[:mysql2, :postgresql].each do |adapter|
|
179
|
+
context "when the adapter is #{adapter}" do
|
180
|
+
before :each do
|
181
|
+
MassInsert::Builder::Utilities.stub(:adapter).and_return(adapter.to_s)
|
182
|
+
end
|
149
183
|
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
184
|
+
context "when the column value evals to true" do
|
185
|
+
it "returns true" do
|
186
|
+
subject.stub(:column_value).and_return(true)
|
187
|
+
expect(subject.column_value_boolean).to eq("true")
|
188
|
+
end
|
155
189
|
end
|
156
|
-
end
|
157
190
|
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
191
|
+
context "when the column value evals to false" do
|
192
|
+
it "returns false" do
|
193
|
+
subject.stub(:column_value).and_return(false)
|
194
|
+
expect(subject.column_value_boolean).to eq("false")
|
195
|
+
end
|
162
196
|
end
|
163
197
|
end
|
164
198
|
end
|
165
|
-
end
|
166
199
|
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
200
|
+
[:sqlite3, :sqlserver].each do |adapter|
|
201
|
+
context "when the adapter is #{adapter}" do
|
202
|
+
before :each do
|
203
|
+
MassInsert::Builder::Utilities.stub(:adapter).and_return(adapter.to_s)
|
204
|
+
end
|
172
205
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
206
|
+
context "when the column value evals to true" do
|
207
|
+
it "returns true" do
|
208
|
+
subject.stub(:column_value).and_return(true)
|
209
|
+
expect(subject.column_value_boolean).to eq("1")
|
210
|
+
end
|
178
211
|
end
|
179
|
-
end
|
180
212
|
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
213
|
+
context "when the column value evals to false" do
|
214
|
+
it "returns false" do
|
215
|
+
subject.stub(:column_value).and_return(false)
|
216
|
+
expect(subject.column_value_boolean).to eq("0")
|
217
|
+
end
|
185
218
|
end
|
186
219
|
end
|
187
220
|
end
|
@@ -6,10 +6,4 @@ describe MassInsert::Builder::Adapters::Mysql2Adapter do
|
|
6
6
|
it "inherits from Adapter class" do
|
7
7
|
expect(described_class < MassInsert::Builder::Adapters::Adapter).to be_true
|
8
8
|
end
|
9
|
-
|
10
|
-
describe "#timestamp_format" do
|
11
|
-
it "returns format string" do
|
12
|
-
expect(subject.timestamp_format).to eq("%Y-%m-%d %H:%M:%S")
|
13
|
-
end
|
14
|
-
end
|
15
9
|
end
|
@@ -1,44 +1,58 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
describe "#start" do
|
17
|
-
before :each do
|
18
|
-
MassInsert::Builder::Base.any_instance.stub(:build).and_return("queries")
|
19
|
-
MassInsert::Executer.any_instance.stub(:execute)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "calls method to build the queries" do
|
23
|
-
MassInsert::Builder::Base.any_instance.should_receive(:build).with("values", "options")
|
24
|
-
subject.start
|
3
|
+
module MassInsert
|
4
|
+
describe Process do
|
5
|
+
let!(:subject){ described_class.new("values", "options") }
|
6
|
+
|
7
|
+
describe "#initialize" do
|
8
|
+
it "sets instance values variable" do
|
9
|
+
expect(subject.instance_variable_get(:@values)).to eq("values")
|
10
|
+
end
|
11
|
+
|
12
|
+
it "sets instance options variable" do
|
13
|
+
expect(subject.instance_variable_get(:@options)).to eq("options")
|
14
|
+
end
|
25
15
|
end
|
26
16
|
|
27
|
-
|
28
|
-
|
29
|
-
|
17
|
+
describe "#start" do
|
18
|
+
before :each do
|
19
|
+
Builder::Base.any_instance.stub(:build).and_return("queries")
|
20
|
+
Executer.any_instance.stub(:execute)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "calls method to build the queries" do
|
24
|
+
Builder::Base.any_instance.should_receive(:build).with("values", "options")
|
25
|
+
subject.start
|
26
|
+
end
|
27
|
+
|
28
|
+
it "calls method to execute the queries" do
|
29
|
+
Executer.any_instance.should_receive(:execute).with("queries")
|
30
|
+
subject.start
|
31
|
+
end
|
32
|
+
|
33
|
+
it "sets instance building_time variable" do
|
34
|
+
subject.start
|
35
|
+
building_time = subject.instance_variable_get(:@building_time)
|
36
|
+
expect(building_time).to be_an_instance_of(Benchmark::Tms)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "sets instance execution_time variable" do
|
40
|
+
subject.start
|
41
|
+
execution_time = subject.instance_variable_get(:@execution_time)
|
42
|
+
expect(execution_time).to be_an_instance_of(Benchmark::Tms)
|
43
|
+
end
|
30
44
|
end
|
31
45
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
46
|
+
describe '#builder' do
|
47
|
+
it 'returns new Builder::Base instace' do
|
48
|
+
expect(subject.builder).to be_an_instance_of(Builder::Base)
|
49
|
+
end
|
36
50
|
end
|
37
51
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
52
|
+
describe '#executer' do
|
53
|
+
it 'returns new Executer instace' do
|
54
|
+
expect(subject.executer).to be_an_instance_of(Executer)
|
55
|
+
end
|
42
56
|
end
|
43
57
|
end
|
44
58
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mass_insert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.3
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Alejandro Gutiérrez
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-04-30 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ! '>='
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ! '>='
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -78,9 +73,12 @@ files:
|
|
78
73
|
- mass_insert.gemspec
|
79
74
|
- spec/adapters/column_types/binary_spec.rb
|
80
75
|
- spec/adapters/column_types/boolean_spec.rb
|
76
|
+
- spec/adapters/column_types/date_spec.rb
|
77
|
+
- spec/adapters/column_types/datetime_spec.rb
|
81
78
|
- spec/adapters/column_types/decimal_spec.rb
|
82
79
|
- spec/adapters/column_types/integer_spec.rb
|
83
80
|
- spec/adapters/column_types/string_spec.rb
|
81
|
+
- spec/adapters/column_types/time_spec.rb
|
84
82
|
- spec/adapters/model_spec.rb
|
85
83
|
- spec/dummy/.gitignore
|
86
84
|
- spec/dummy/Gemfile
|
@@ -150,40 +148,36 @@ files:
|
|
150
148
|
homepage: https://github.com/alejandrogutierrez/mass_insert
|
151
149
|
licenses:
|
152
150
|
- MIT
|
151
|
+
metadata: {}
|
153
152
|
post_install_message:
|
154
153
|
rdoc_options: []
|
155
154
|
require_paths:
|
156
155
|
- lib
|
157
156
|
required_ruby_version: !ruby/object:Gem::Requirement
|
158
|
-
none: false
|
159
157
|
requirements:
|
160
158
|
- - ! '>='
|
161
159
|
- !ruby/object:Gem::Version
|
162
160
|
version: '0'
|
163
|
-
segments:
|
164
|
-
- 0
|
165
|
-
hash: -2559163002047617734
|
166
161
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
-
none: false
|
168
162
|
requirements:
|
169
163
|
- - ! '>='
|
170
164
|
- !ruby/object:Gem::Version
|
171
165
|
version: '0'
|
172
|
-
segments:
|
173
|
-
- 0
|
174
|
-
hash: -2559163002047617734
|
175
166
|
requirements: []
|
176
167
|
rubyforge_project:
|
177
|
-
rubygems_version:
|
168
|
+
rubygems_version: 2.2.2
|
178
169
|
signing_key:
|
179
|
-
specification_version:
|
170
|
+
specification_version: 4
|
180
171
|
summary: Mass database insertion in Rails
|
181
172
|
test_files:
|
182
173
|
- spec/adapters/column_types/binary_spec.rb
|
183
174
|
- spec/adapters/column_types/boolean_spec.rb
|
175
|
+
- spec/adapters/column_types/date_spec.rb
|
176
|
+
- spec/adapters/column_types/datetime_spec.rb
|
184
177
|
- spec/adapters/column_types/decimal_spec.rb
|
185
178
|
- spec/adapters/column_types/integer_spec.rb
|
186
179
|
- spec/adapters/column_types/string_spec.rb
|
180
|
+
- spec/adapters/column_types/time_spec.rb
|
187
181
|
- spec/adapters/model_spec.rb
|
188
182
|
- spec/dummy/.gitignore
|
189
183
|
- spec/dummy/Gemfile
|