bulk_insert 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74b626a311a11e7f61ebe3fa74b0a974d2be0450
4
- data.tar.gz: ec3319157ee2067572762405ee7c402bd5195f47
3
+ metadata.gz: da3b8a0be5e2ad8c7dcf5bbf878cc31383d1e63e
4
+ data.tar.gz: 0c8f3ce86746632b0cdf34fec6c9c8021574aa8b
5
5
  SHA512:
6
- metadata.gz: c743e409a275b4c5ee2f9d5921cf0d5c670968b806ac73978286775886437e97049c8db99204af09b1c6ebc1210f5c9da7de97eda8e2fe7bacb4699a9ca76c9c
7
- data.tar.gz: dc6aa25046f0fd2fc907ae7614b1efd3552d85bf9fe019e144af487175734914f9b875ecfea263366f58590d113824923251a4b348677b8305c2a7b75b08768a
6
+ metadata.gz: b6c9b2dce1635af1c59f34dffd49c1d276d68478222ab0ca8f1941ac2333e7196f2bc7e1ce9425b9c5ed4f1b43828ea8d3c3f51cda45498a7b744d15cfbfc26e
7
+ data.tar.gz: ef556ebbf06358c534f3f0292ff5a4d2f53102a2c5faf1f22362964c8361f224ed932d54a27d24df397766de7f68c66eec6bbfa1014de505623b9199111e554f
@@ -1,7 +1,7 @@
1
1
  module BulkInsert
2
2
  MAJOR = 1
3
- MINOR = 2
4
- TINY = 1
3
+ MINOR = 3
4
+ TINY = 0
5
5
 
6
6
  VERSION = [MAJOR, MINOR, TINY].join(".")
7
7
  end
@@ -2,6 +2,7 @@ module BulkInsert
2
2
  class Worker
3
3
  attr_reader :connection
4
4
  attr_accessor :set_size
5
+ attr_accessor :after_save_callback
5
6
 
6
7
  def initialize(connection, table_name, column_names, set_size=500)
7
8
  @connection = connection
@@ -14,6 +15,8 @@ module BulkInsert
14
15
  @table_name = connection.quote_table_name(table_name)
15
16
  @column_names = column_names.map { |name| connection.quote_column_name(name) }.join(",")
16
17
 
18
+ @after_save_callback = nil
19
+
17
20
  @set = []
18
21
  end
19
22
 
@@ -53,6 +56,10 @@ module BulkInsert
53
56
  self
54
57
  end
55
58
 
59
+ def after_save(&block)
60
+ @after_save_callback = block
61
+ end
62
+
56
63
  def save!
57
64
  if pending?
58
65
  sql = "INSERT INTO #{@table_name} (#{@column_names}) VALUES "
@@ -77,6 +84,8 @@ module BulkInsert
77
84
  sql << rows.join(",")
78
85
  @connection.execute(sql)
79
86
 
87
+ @after_save_callback.() if @after_save_callback
88
+
80
89
  @set.clear
81
90
  end
82
91
 
@@ -120,5 +120,35 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase
120
120
  assert_equal 25, hello.age
121
121
  assert_equal true, hello.happy?
122
122
  end
123
+
124
+ test "save! calls the after_save handler" do
125
+ x = 41
126
+
127
+ @insert.after_save do
128
+ x += 1
129
+ end
130
+
131
+ @insert.add ["Yo", 15, false, @now, @now]
132
+ @insert.add ["Hello", 25, true, @now, @now]
133
+ @insert.save!
134
+
135
+ assert_equal 42, x
136
+ end
137
+
138
+ test "after_save stores a block as a proc" do
139
+ @insert.after_save do
140
+ "hello"
141
+ end
142
+
143
+ assert_equal "hello", @insert.after_save_callback.()
144
+ end
145
+
146
+ test "after_save_callback can be set as a proc" do
147
+ @insert.after_save_callback = -> do
148
+ "hello"
149
+ end
150
+
151
+ assert_equal "hello", @insert.after_save_callback.()
152
+ end
123
153
  end
124
154
 
@@ -2965,3 +2965,146 @@ BulkInsertWorkerTest: test_add_should_allow_values_given_as_Hash
2965
2965
   (0.2ms) INSERT INTO "testings" ("greeting","age","happy","created_at","updated_at","color") VALUES ('Yo',20,'f','2016-08-11 04:58:10.687048','2016-08-11 04:58:10.687048','chartreuse')
2966
2966
  Testing Load (0.1ms) SELECT "testings".* FROM "testings" ORDER BY "testings"."id" ASC LIMIT 1
2967
2967
   (0.2ms) rollback transaction
2968
+ ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations"
2969
+  (0.1ms) begin transaction
2970
+ ---------------------------------------------------------------------
2971
+ BulkInsertTest: test_bulk_insert_with_block_should_save_automatically
2972
+ ---------------------------------------------------------------------
2973
+  (0.3ms) SELECT COUNT(*) FROM "testings"
2974
+  (0.0ms) SAVEPOINT active_record_1
2975
+  (0.6ms) INSERT INTO "testings" ("greeting","age","happy","created_at","updated_at","color") VALUES ('Hello',NULL,NULL,'2016-11-01 17:59:20.883249','2016-11-01 17:59:20.883249','chartreuse')
2976
+  (0.0ms) RELEASE SAVEPOINT active_record_1
2977
+  (0.1ms) SELECT COUNT(*) FROM "testings"
2978
+  (0.3ms) rollback transaction
2979
+  (0.0ms) begin transaction
2980
+ ---------------------------------------------------------------
2981
+ BulkInsertTest: test_bulk_insert_with_block_should_yield_worker
2982
+ ---------------------------------------------------------------
2983
+  (0.0ms) SAVEPOINT active_record_1
2984
+  (0.0ms) RELEASE SAVEPOINT active_record_1
2985
+  (0.0ms) rollback transaction
2986
+  (0.0ms) begin transaction
2987
+ ------------------------------------------------------------------------------
2988
+ BulkInsertTest: test_default_bulk_columns_should_return_all_columns_without_id
2989
+ ------------------------------------------------------------------------------
2990
+  (0.0ms) rollback transaction
2991
+  (0.0ms) begin transaction
2992
+ -------------------------------------------------------------------
2993
+ BulkInsertTest: test_bulk_insert_without_block_should_return_worker
2994
+ -------------------------------------------------------------------
2995
+  (0.0ms) rollback transaction
2996
+  (0.1ms) begin transaction
2997
+ -----------------------------------------------------------------------------
2998
+ BulkInsertTest: test_bulk_insert_with_array_should_save_the_array_immediately
2999
+ -----------------------------------------------------------------------------
3000
+  (0.0ms) SELECT COUNT(*) FROM "testings"
3001
+  (0.0ms) SAVEPOINT active_record_1
3002
+  (0.2ms) INSERT INTO "testings" ("greeting","age","happy","created_at","updated_at","color") VALUES ('Hello',15,'t','green','2016-11-01 17:59:20.890598','chartreuse'),('Hey',20,'f','2016-11-01 17:59:20.890598','2016-11-01 17:59:20.890598','chartreuse')
3003
+  (0.0ms) RELEASE SAVEPOINT active_record_1
3004
+  (0.0ms) SELECT COUNT(*) FROM "testings"
3005
+  (0.3ms) rollback transaction
3006
+  (0.0ms) begin transaction
3007
+ ----------------------------------------------------------------
3008
+ BulkInsertWorkerTest: test_explicit_nil_should_override_defaults
3009
+ ----------------------------------------------------------------
3010
+  (0.2ms) INSERT INTO "testings" ("greeting","age","happy","created_at","updated_at","color") VALUES ('Hello',20,'f','2016-11-01 17:59:20.892761','2016-11-01 17:59:20.892761',NULL)
3011
+ Testing Load (0.1ms) SELECT "testings".* FROM "testings" ORDER BY "testings"."id" ASC LIMIT 1
3012
+  (0.3ms) rollback transaction
3013
+  (0.0ms) begin transaction
3014
+ -------------------------------------------------------------------------------
3015
+ BulkInsertWorkerTest: test_add_should_default_timestamp_columns_to_current_time
3016
+ -------------------------------------------------------------------------------
3017
+  (0.2ms) INSERT INTO "testings" ("greeting","age","happy","created_at","updated_at","color") VALUES ('Hello',15,'t','2016-11-01 17:59:20.898576','2016-11-01 17:59:20.898576','chartreuse')
3018
+ Testing Load (0.1ms) SELECT "testings".* FROM "testings" ORDER BY "testings"."id" ASC LIMIT 1
3019
+  (0.3ms) rollback transaction
3020
+  (0.0ms) begin transaction
3021
+ ----------------------------------------------------------------------------------
3022
+ BulkInsertWorkerTest: test_add_should_save_automatically_when_overflowing_set_size
3023
+ ----------------------------------------------------------------------------------
3024
+  (0.2ms) INSERT INTO "testings" ("greeting","age","happy","created_at","updated_at","color") VALUES ('Hello',15,'t','2016-11-01 17:59:20.900257','2016-11-01 17:59:20.900257','chartreuse')
3025
+  (0.1ms) SELECT COUNT(*) FROM "testings"
3026
+ Testing Load (0.1ms) SELECT "testings".* FROM "testings" ORDER BY "testings"."id" ASC LIMIT 1
3027
+  (0.3ms) rollback transaction
3028
+  (0.0ms) begin transaction
3029
+ --------------------------------------------------------
3030
+ BulkInsertWorkerTest: test_save!_inserts_pending_records
3031
+ --------------------------------------------------------
3032
+  (0.2ms) INSERT INTO "testings" ("greeting","age","happy","created_at","updated_at","color") VALUES ('Yo',15,'f','2016-11-01 17:59:20.902554','2016-11-01 17:59:20.902554','chartreuse'),('Hello',25,'t','2016-11-01 17:59:20.902554','2016-11-01 17:59:20.902554','chartreuse')
3033
+ Testing Load (0.1ms) SELECT "testings".* FROM "testings" WHERE "testings"."greeting" = ? LIMIT 1 [["greeting", "Yo"]]
3034
+ Testing Load (0.0ms) SELECT "testings".* FROM "testings" WHERE "testings"."greeting" = ? LIMIT 1 [["greeting", "Hello"]]
3035
+  (0.3ms) rollback transaction
3036
+  (0.0ms) begin transaction
3037
+ --------------------------------------------------------------
3038
+ BulkInsertWorkerTest: test_after_save_stores_a_block_as_a_proc
3039
+ --------------------------------------------------------------
3040
+  (0.0ms) rollback transaction
3041
+  (0.1ms) begin transaction
3042
+ -------------------------------------------------------------------
3043
+ BulkInsertWorkerTest: test_after_save_callback_can_be_set_as_a_proc
3044
+ -------------------------------------------------------------------
3045
+  (0.0ms) rollback transaction
3046
+  (0.1ms) begin transaction
3047
+ ---------------------------------------------------------
3048
+ BulkInsertWorkerTest: test_save!_makes_insert_not_pending
3049
+ ---------------------------------------------------------
3050
+  (0.2ms) INSERT INTO "testings" ("greeting","age","happy","created_at","updated_at","color") VALUES ('Hello',15,'t','2016-11-01 17:59:20.909065','2016-11-01 17:59:20.909065','chartreuse')
3051
+  (0.3ms) rollback transaction
3052
+  (0.0ms) begin transaction
3053
+ --------------------------------------------------------------------
3054
+ BulkInsertWorkerTest: test_adding_row_to_insert_makes_insert_pending
3055
+ --------------------------------------------------------------------
3056
+  (0.0ms) rollback transaction
3057
+  (0.1ms) begin transaction
3058
+ ----------------------------------------------------------------
3059
+ BulkInsertWorkerTest: test_add_should_allow_values_given_as_Hash
3060
+ ----------------------------------------------------------------
3061
+  (0.2ms) INSERT INTO "testings" ("greeting","age","happy","created_at","updated_at","color") VALUES ('Yo',20,'f','2016-11-01 17:59:20.910787','2016-11-01 17:59:20.910787','chartreuse')
3062
+ Testing Load (0.1ms) SELECT "testings".* FROM "testings" ORDER BY "testings"."id" ASC LIMIT 1
3063
+  (0.3ms) rollback transaction
3064
+  (0.0ms) begin transaction
3065
+ ------------------------------------------------------
3066
+ BulkInsertWorkerTest: test_empty_insert_is_not_pending
3067
+ ------------------------------------------------------
3068
+  (0.0ms) rollback transaction
3069
+  (0.0ms) begin transaction
3070
+ ----------------------------------------------------------------------------------------------
3071
+ BulkInsertWorkerTest: test_default_timestamp_columns_should_be_equivalent_for_the_entire_batch
3072
+ ----------------------------------------------------------------------------------------------
3073
+  (0.2ms) INSERT INTO "testings" ("greeting","age","happy","created_at","updated_at","color") VALUES ('Hello',15,'t','2016-11-01 17:59:20.912730','2016-11-01 17:59:20.912730','chartreuse'),('Howdy',20,'f','2016-11-01 17:59:20.912730','2016-11-01 17:59:20.912730','chartreuse')
3074
+ Testing Load (0.1ms) SELECT "testings".* FROM "testings"
3075
+  (0.3ms) rollback transaction
3076
+  (0.1ms) begin transaction
3077
+ ---------------------------------------------------------------------
3078
+ BulkInsertWorkerTest: test_add_all_should_append_all_items_to_the_set
3079
+ ---------------------------------------------------------------------
3080
+  (0.0ms) rollback transaction
3081
+  (0.0ms) begin transaction
3082
+ -------------------------------------------
3083
+ BulkInsertWorkerTest: test_default_set_size
3084
+ -------------------------------------------
3085
+  (0.0ms) rollback transaction
3086
+  (0.0ms) begin transaction
3087
+ ----------------------------------------------------------------------------
3088
+ BulkInsertWorkerTest: test_pending_count_should_describe_size_of_pending_set
3089
+ ----------------------------------------------------------------------------
3090
+  (0.0ms) rollback transaction
3091
+  (0.0ms) begin transaction
3092
+ -------------------------------------------------------------
3093
+ BulkInsertWorkerTest: test_save!_calls_the_after_save_handler
3094
+ -------------------------------------------------------------
3095
+  (0.2ms) INSERT INTO "testings" ("greeting","age","happy","created_at","updated_at","color") VALUES ('Yo',15,'f','2016-11-01 17:59:20.915894','2016-11-01 17:59:20.915894','chartreuse'),('Hello',25,'t','2016-11-01 17:59:20.915894','2016-11-01 17:59:20.915894','chartreuse')
3096
+  (0.2ms) rollback transaction
3097
+  (0.0ms) begin transaction
3098
+ -------------------------------------------------------------------
3099
+ BulkInsertWorkerTest: test_save!_when_not_pending_should_do_nothing
3100
+ -------------------------------------------------------------------
3101
+  (0.0ms) SELECT COUNT(*) FROM "testings"
3102
+  (0.0ms) SELECT COUNT(*) FROM "testings"
3103
+  (0.0ms) rollback transaction
3104
+  (0.0ms) begin transaction
3105
+ ------------------------------------------------------------------------------
3106
+ BulkInsertWorkerTest: test_add_should_use_database_default_values_when_present
3107
+ ------------------------------------------------------------------------------
3108
+  (0.1ms) INSERT INTO "testings" ("greeting","age","happy","created_at","updated_at","color") VALUES ('Hello',20,'f','2016-11-01 17:59:20.918117','2016-11-01 17:59:20.918117','chartreuse')
3109
+ Testing Load (0.1ms) SELECT "testings".* FROM "testings" ORDER BY "testings"."id" ASC LIMIT 1
3110
+  (0.2ms) rollback transaction
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bulk_insert
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamis Buck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-11 00:00:00.000000000 Z
11
+ date: 2016-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord