partitioned 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. data/Gemfile +17 -0
  2. data/LICENSE +30 -0
  3. data/PARTITIONING_EXPLAINED.txt +351 -0
  4. data/README +111 -0
  5. data/Rakefile +27 -0
  6. data/examples/README +23 -0
  7. data/examples/company_id.rb +417 -0
  8. data/examples/company_id_and_created_at.rb +689 -0
  9. data/examples/created_at.rb +590 -0
  10. data/examples/created_at_referencing_awards.rb +1000 -0
  11. data/examples/id.rb +475 -0
  12. data/examples/lib/by_company_id.rb +11 -0
  13. data/examples/lib/command_line_tool_mixin.rb +71 -0
  14. data/examples/lib/company.rb +29 -0
  15. data/examples/lib/get_options.rb +44 -0
  16. data/examples/lib/roman.rb +41 -0
  17. data/examples/start_date.rb +621 -0
  18. data/init.rb +1 -0
  19. data/lib/monkey_patch_activerecord.rb +92 -0
  20. data/lib/monkey_patch_postgres.rb +73 -0
  21. data/lib/partitioned.rb +26 -0
  22. data/lib/partitioned/active_record_overrides.rb +34 -0
  23. data/lib/partitioned/bulk_methods_mixin.rb +288 -0
  24. data/lib/partitioned/by_created_at.rb +13 -0
  25. data/lib/partitioned/by_foreign_key.rb +21 -0
  26. data/lib/partitioned/by_id.rb +35 -0
  27. data/lib/partitioned/by_integer_field.rb +32 -0
  28. data/lib/partitioned/by_monthly_time_field.rb +23 -0
  29. data/lib/partitioned/by_time_field.rb +65 -0
  30. data/lib/partitioned/by_weekly_time_field.rb +30 -0
  31. data/lib/partitioned/multi_level.rb +20 -0
  32. data/lib/partitioned/multi_level/configurator/data.rb +14 -0
  33. data/lib/partitioned/multi_level/configurator/dsl.rb +32 -0
  34. data/lib/partitioned/multi_level/configurator/reader.rb +162 -0
  35. data/lib/partitioned/multi_level/partition_manager.rb +47 -0
  36. data/lib/partitioned/partitioned_base.rb +354 -0
  37. data/lib/partitioned/partitioned_base/configurator.rb +6 -0
  38. data/lib/partitioned/partitioned_base/configurator/data.rb +62 -0
  39. data/lib/partitioned/partitioned_base/configurator/dsl.rb +628 -0
  40. data/lib/partitioned/partitioned_base/configurator/reader.rb +209 -0
  41. data/lib/partitioned/partitioned_base/partition_manager.rb +138 -0
  42. data/lib/partitioned/partitioned_base/sql_adapter.rb +286 -0
  43. data/lib/partitioned/version.rb +3 -0
  44. data/lib/tasks/desirable_tasks.rake +4 -0
  45. data/partitioned.gemspec +21 -0
  46. data/spec/dummy/.rspec +1 -0
  47. data/spec/dummy/README.rdoc +261 -0
  48. data/spec/dummy/Rakefile +7 -0
  49. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  50. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  51. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  52. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  53. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  54. data/spec/dummy/config.ru +4 -0
  55. data/spec/dummy/config/application.rb +51 -0
  56. data/spec/dummy/config/boot.rb +10 -0
  57. data/spec/dummy/config/database.yml +32 -0
  58. data/spec/dummy/config/environment.rb +5 -0
  59. data/spec/dummy/config/environments/development.rb +30 -0
  60. data/spec/dummy/config/environments/production.rb +60 -0
  61. data/spec/dummy/config/environments/test.rb +39 -0
  62. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  63. data/spec/dummy/config/initializers/inflections.rb +10 -0
  64. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  65. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  66. data/spec/dummy/config/initializers/session_store.rb +8 -0
  67. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  68. data/spec/dummy/config/locales/en.yml +5 -0
  69. data/spec/dummy/config/routes.rb +58 -0
  70. data/spec/dummy/public/404.html +26 -0
  71. data/spec/dummy/public/422.html +26 -0
  72. data/spec/dummy/public/500.html +26 -0
  73. data/spec/dummy/public/favicon.ico +0 -0
  74. data/spec/dummy/script/rails +6 -0
  75. data/spec/dummy/spec/spec_helper.rb +27 -0
  76. data/spec/monkey_patch_posgres_spec.rb +176 -0
  77. data/spec/partitioned/bulk_methods_mixin_spec.rb +512 -0
  78. data/spec/partitioned/by_created_at_spec.rb +62 -0
  79. data/spec/partitioned/by_foreign_key_spec.rb +95 -0
  80. data/spec/partitioned/by_id_spec.rb +97 -0
  81. data/spec/partitioned/by_integer_field_spec.rb +143 -0
  82. data/spec/partitioned/by_monthly_time_field_spec.rb +100 -0
  83. data/spec/partitioned/by_time_field_spec.rb +182 -0
  84. data/spec/partitioned/by_weekly_time_field_spec.rb +100 -0
  85. data/spec/partitioned/multi_level/configurator/dsl_spec.rb +88 -0
  86. data/spec/partitioned/multi_level/configurator/reader_spec.rb +147 -0
  87. data/spec/partitioned/partitioned_base/configurator/dsl_spec.rb +459 -0
  88. data/spec/partitioned/partitioned_base/configurator/reader_spec.rb +513 -0
  89. data/spec/partitioned/partitioned_base/sql_adapter_spec.rb +204 -0
  90. data/spec/partitioned/partitioned_base_spec.rb +173 -0
  91. data/spec/spec_helper.rb +32 -0
  92. data/spec/support/shared_example_spec_helper_for_integer_key.rb +137 -0
  93. data/spec/support/shared_example_spec_helper_for_time_key.rb +147 -0
  94. data/spec/support/tables_spec_helper.rb +47 -0
  95. metadata +250 -0
@@ -0,0 +1,459 @@
1
+ require 'spec_helper'
2
+
3
+ module Partitioned
4
+ class PartitionedBase
5
+ module Configurator
6
+ describe Dsl do
7
+
8
+ before(:all) do
9
+ class Employee
10
+
11
+ end
12
+ end
13
+
14
+ after(:all) do
15
+ Partitioned::PartitionedBase::Configurator.send(:remove_const, :Employee)
16
+ end
17
+
18
+ let!(:dsl) { Partitioned::PartitionedBase::Configurator::Dsl.new(Employee) }
19
+
20
+ describe "initialize" do
21
+
22
+ let!(:data_stubs) do
23
+ {
24
+ "on_field" => nil,
25
+ "indexes" => [],
26
+ "foreign_keys" => [],
27
+ "last_partitions_order_by_clause" => nil,
28
+ "schema_name" => nil,
29
+ "name_prefix" => nil,
30
+ "base_name" => nil,
31
+ "part_name" => nil,
32
+ "table_name" => nil,
33
+ "parent_table_schema_name" => nil,
34
+ "parent_table_name" => nil,
35
+ "check_constraint" => nil,
36
+ "encoded_name" => nil
37
+ }
38
+ end
39
+
40
+ context "when try to create the new object" do
41
+
42
+ context "check the model name" do
43
+
44
+ it "returns Employer" do
45
+ dsl.model.should == Employee
46
+ end
47
+
48
+ end # check the model name
49
+
50
+ context "check the object data" do
51
+
52
+ it "returns data" do
53
+ dsl.data.instance_values.should == data_stubs
54
+ end
55
+
56
+ end # check the object data
57
+
58
+ end # when try to create a new object
59
+
60
+ end # initialize
61
+
62
+ describe "on" do
63
+
64
+ context "when try to set the field which used to partition child tables" do
65
+
66
+ it "returns data.on value" do
67
+ dsl.on(:company_id)
68
+ dsl.data.on_field.should == :company_id
69
+ end
70
+
71
+ end # when try to set the field which used to partition child tables
72
+
73
+ context "when try to set the field represented as a string to be interpolated naming the field to partition child tables" do
74
+
75
+ it "returns data.on value" do
76
+ dsl.on('#{model.partition_field}')
77
+ dsl.data.on_field.should == '#{model.partition_field}'
78
+ end
79
+
80
+ end # when try to set the field represented as a string to be interpolated naming the field to partition child tables
81
+
82
+ context "when try to set the field(with proc) which used to partition child tables" do
83
+
84
+ let(:lmd) do
85
+ lambda { |model| model.partition_field }
86
+ end
87
+
88
+ it "returns proc" do
89
+ dsl.on lmd
90
+ dsl.data.on_field.should == lmd
91
+ end
92
+
93
+ end # when try to set the field which(with proc) used to partition child tables
94
+
95
+ end # on
96
+
97
+ describe "index" do
98
+
99
+ context "when try to set the index to be created on all child tables" do
100
+
101
+ it "returns index" do
102
+ dsl.index(:id, { :unique => true })
103
+ dsl.data.indexes.first.field.should == :id
104
+ dsl.data.indexes.first.options.should == { :unique => true }
105
+ end
106
+
107
+ end # when try to set the index to be created on all child tables
108
+
109
+ context "when try to set the index(with proc) to be created on all child tables" do
110
+
111
+ let(:lmd) do
112
+ lambda { |model, *partition_key_values|
113
+ return Partitioned::PartitionedBase::Configurator::Data::Index.new(model.partition_field, {})
114
+ }
115
+ end
116
+
117
+ it "returns proc" do
118
+ dsl.index lmd
119
+ dsl.data.indexes.first.should == lmd
120
+ end
121
+
122
+ end # when try to set the index(with proc) to be created on all child tables
123
+
124
+ end # index
125
+
126
+ describe "foreign_key" do
127
+
128
+ context "when try to set the foreign key on a child table" do
129
+
130
+ it "returns foreign_keys" do
131
+ dsl.foreign_key(:company_id)
132
+ dsl.data.foreign_keys.first.referencing_field.should == :company_id
133
+ dsl.data.foreign_keys.first.referenced_table.should == "companies"
134
+ dsl.data.foreign_keys.first.referenced_field.should == :id
135
+ end
136
+
137
+ end # when try to set the foreign key on a child table
138
+
139
+ context "when try to set the foreign key(with proc) on a child table" do
140
+
141
+ let(:lmd) do
142
+ lambda { |model, *partition_key_values|
143
+ return Partitioned::PartitionedBase::Configurator::Data::ForeignKey.new(model.foreign_key_field)
144
+ }
145
+ end
146
+
147
+ it "returns proc" do
148
+ dsl.index lmd
149
+ dsl.data.indexes.first.should == lmd
150
+ end
151
+
152
+ end # when try to set the foreign key(with proc) on a child table
153
+
154
+ end # foreign_key
155
+
156
+ describe "check_constraint" do
157
+
158
+ context "when try to set the check constraint for a given child table" do
159
+
160
+ it "returns check_constraint" do
161
+ dsl.check_constraint('company_id = #{field_value}')
162
+ dsl.data.check_constraint.should == 'company_id = #{field_value}'
163
+ end
164
+
165
+ end # when try to set the check constraint for a given child table
166
+
167
+ context "when try to set the check constraint(with proc) for a given child table" do
168
+
169
+ let(:lmd) do
170
+ lambda {|model, value|
171
+ return "#{model.field_to_partition} = #{value}"
172
+ }
173
+ end
174
+
175
+ it "returns proc" do
176
+ dsl.check_constraint lmd
177
+ dsl.data.check_constraint.should == lmd
178
+ end
179
+
180
+ end # when try to set the check constraint(with proc) for a given child table
181
+
182
+ end # check_constraint
183
+
184
+ describe "order" do
185
+
186
+ context "when try to set the check constraint for a given child table" do
187
+
188
+ it "returns check_constraint" do
189
+ dsl.order('tablename desc')
190
+ dsl.data.last_partitions_order_by_clause.should == 'tablename desc'
191
+ end
192
+
193
+ end # when try to set the check constraint for a given child table
194
+
195
+ end # order
196
+
197
+ describe "schema_name" do
198
+
199
+ context "when try to set the name of the schema that will contain all child tables" do
200
+
201
+ it "returns schema_name" do
202
+ dsl.schema_name("employees_partitions")
203
+ dsl.data.schema_name.should == "employees_partitions"
204
+ end
205
+
206
+ end # when try to set the name of the schema that will contain all child tables
207
+
208
+ context "when try to set the schema name represented as a string to be interpolated at run time" do
209
+
210
+ it "returns schema_name" do
211
+ dsl.schema_name('#{model.table_name}_partitions')
212
+ dsl.data.schema_name.should == '#{model.table_name}_partitions'
213
+ end
214
+
215
+ end # when try to set the schema name represented as a string to be interpolated at run time
216
+
217
+ context "when try to set the name of the schema(with proc) that will contain all child tables" do
218
+
219
+ let(:lmd) do
220
+ lambda {|model, *value|
221
+ return "#{model.table_name}_partitions"
222
+ }
223
+ end
224
+
225
+ it "returns proc" do
226
+ dsl.schema_name lmd
227
+ dsl.data.schema_name.should == lmd
228
+ end
229
+
230
+ end # when try to set the name of the schema(with proc) that will contain all child tables
231
+
232
+ end # schema_name
233
+
234
+ describe "name_prefix" do
235
+
236
+ context "when try to set the name prefix for the child table's name" do
237
+
238
+ it "returns name_prefix" do
239
+ dsl.name_prefix("p")
240
+ dsl.data.name_prefix.should == "p"
241
+ end
242
+
243
+ end # when try to set the name prefix for the child table's name
244
+
245
+ context "when try to set the name prefix represented as a string to be interpolated at run time" do
246
+
247
+ it "returns name_prefix" do
248
+ dsl.name_prefix('#{model.table_name}_child_')
249
+ dsl.data.name_prefix.should == '#{model.table_name}_child_'
250
+ end
251
+
252
+ end # when try to set the name prefix represented as a string to be interpolated at run time
253
+
254
+ context "when try to set the name prefix(with proc) for the child table's name" do
255
+
256
+ let(:lmd) do
257
+ lambda {|model, *value|
258
+ return "#{model.table_name}_child_"
259
+ }
260
+ end
261
+
262
+ it "returns proc" do
263
+ dsl.name_prefix lmd
264
+ dsl.data.name_prefix.should == lmd
265
+ end
266
+
267
+ end # when try to set the name prefix(with proc) for the child table's name
268
+
269
+ end # name_prefix
270
+
271
+ describe "base_name" do
272
+
273
+ context "when try to set the name of the child table without the schema name or name prefix" do
274
+
275
+ it "returns base_name" do
276
+ dsl.base_name("25")
277
+ dsl.data.base_name.should == "25"
278
+ end
279
+
280
+ end # when try to set the name of the child table without the schema name or name prefix
281
+
282
+ context "when try to set the name of the child table represented as a string to be interpolated at run time" do
283
+
284
+ it "returns base_name" do
285
+ dsl.base_name('#{model.partition_normalize_key_value(field_value)}')
286
+ dsl.data.base_name.should == '#{model.partition_normalize_key_value(field_value)}'
287
+ end
288
+
289
+ end # when try to set the name of the child table represented as a string to be interpolated at run time
290
+
291
+ context "when try to set the name of the child table(with proc) without the schema name or name prefix" do
292
+
293
+ let(:lmd) do
294
+ lambda {|model, *partition_key_values|
295
+ return model.partition_normalize_key_value(*partition_key_values).to_s
296
+ }
297
+ end
298
+
299
+ it "returns proc" do
300
+ dsl.base_name lmd
301
+ dsl.data.base_name.should == lmd
302
+ end
303
+
304
+ end # when try to set the name of the child table(with proc) without the schema name or name prefix
305
+
306
+ end # base_name
307
+
308
+ describe "part_name" do
309
+
310
+ context "when try to set the part name of the child table without the schema name" do
311
+
312
+ it "returns part_name" do
313
+ dsl.part_name("p42")
314
+ dsl.data.part_name.should == "p42"
315
+ end
316
+
317
+ end # when try to set the part name of the child table without the schema name
318
+
319
+ context "when try to set the part name of the child table represented as a string to be interpolated at run time" do
320
+
321
+ it "returns part_name" do
322
+ dsl.part_name('#{model.table_name}_child_#{model.partition_normalize_key_value(field_value)}')
323
+ dsl.data.part_name.should == '#{model.table_name}_child_#{model.partition_normalize_key_value(field_value)}'
324
+ end
325
+
326
+ end # when try to set the part name of the child table represented as a string to be interpolated at run time
327
+
328
+ context "when try to set the part name(with proc) of the child table without the schema name" do
329
+
330
+ let(:lmd) do
331
+ lambda {|model, *partition_key_values|
332
+ return "#{model.table_name}_child_#{model.partition_normalize_key_value(field_value)}"
333
+ }
334
+ end
335
+
336
+ it "returns proc" do
337
+ dsl.part_name lmd
338
+ dsl.data.part_name.should == lmd
339
+ end
340
+
341
+ end # when try to set the part name(with proc) of the child table without the schema name
342
+
343
+ end # part_name
344
+
345
+ describe "table_name" do
346
+
347
+ context "when try to set the full name of a child table" do
348
+
349
+ it "returns table_name" do
350
+ dsl.table_name("foos_partitions.p42")
351
+ dsl.data.table_name.should == "foos_partitions.p42"
352
+ end
353
+
354
+ end # when try to set the full name of a child table
355
+
356
+ context "when try to set the table name of the child table represented as a string to be interpolated at run time" do
357
+
358
+ it "returns table_name" do
359
+ dsl.table_name('#{model.table_name}_partitions.#{model.table_name}_child_#{model.partition_normalize_key_value(field_value)}')
360
+ dsl.data.table_name.should == '#{model.table_name}_partitions.#{model.table_name}_child_#{model.partition_normalize_key_value(field_value)}'
361
+ end
362
+
363
+ end # when try to set the table name of the child table represented as a string to be interpolated at run time
364
+
365
+ context "when try to set the full name(with proc) of a child table" do
366
+
367
+ let(:lmd) do
368
+ lambda {|model, *partition_key_values|
369
+ return "#{model.table_name}_partitions.#{model.table_name}_child_#{model.partition_normalize_key_value(partition_key_values.first)}"
370
+ }
371
+ end
372
+
373
+ it "returns proc" do
374
+ dsl.table_name lmd
375
+ dsl.data.table_name.should == lmd
376
+ end
377
+
378
+ end # when try to set the full name(with proc) of a child table
379
+
380
+ end # table_name
381
+
382
+ describe "parent_table_name" do
383
+
384
+ context "when try to set the table name who is the direct ancestor of a child table" do
385
+
386
+ it "returns parent_table_name" do
387
+ dsl.parent_table_name("employees")
388
+ dsl.data.parent_table_name.should == "employees"
389
+ end
390
+
391
+ end # when try to set the table name who is the direct ancestor of a child table
392
+
393
+ context "when try to set the parent table name represented as a string to be interpolated at run time" do
394
+
395
+ it "returns parent_table_name" do
396
+ dsl.parent_table_name('#{model.table_name}')
397
+ dsl.data.parent_table_name.should == '#{model.table_name}'
398
+ end
399
+
400
+ end # when try to set the parent table name represented as a string to be interpolated at run time
401
+
402
+ context "when try to set the table name(with proc) who is the direct ancestor of a child table" do
403
+
404
+ let(:lmd) do
405
+ lambda {|model, *partition_key_values|
406
+ return "#{model.table_name}"
407
+ }
408
+ end
409
+
410
+ it "returns proc" do
411
+ dsl.parent_table_name lmd
412
+ dsl.data.parent_table_name.should == lmd
413
+ end
414
+
415
+ end # when try to set the table name(with proc) who is the direct ancestor of a child table
416
+
417
+ end # parent_table_name
418
+
419
+ describe "parent_table_schema_name" do
420
+
421
+ context "when try to set the schema name of the table who is the direct ancestor of a child table" do
422
+
423
+ it "returns parent_table_schema_name" do
424
+ dsl.parent_table_schema_name("public")
425
+ dsl.data.parent_table_schema_name.should == "public"
426
+ end
427
+
428
+ end # when try to set the schema name of the table who is the direct ancestor of a child table
429
+
430
+ context "when try to set the schema name represented as a string to be interpolated at run time" do
431
+
432
+ it "returns parent_table_schema_name" do
433
+ dsl.parent_table_schema_name('#{model.table_name}')
434
+ dsl.data.parent_table_schema_name.should == '#{model.table_name}'
435
+ end
436
+
437
+ end # when try to set the schema name represented as a string to be interpolated at run time
438
+
439
+ context "when try to set the schema name(with proc) of the table who is the direct ancestor of a child table" do
440
+
441
+ let(:lmd) do
442
+ lambda {|model, *partition_key_values|
443
+ return "#{model.table_name}"
444
+ }
445
+ end
446
+
447
+ it "returns proc" do
448
+ dsl.parent_table_schema_name lmd
449
+ dsl.data.parent_table_schema_name.should == lmd
450
+ end
451
+
452
+ end # when try to set the schema name(with proc) of the table who is the direct ancestor of a child table
453
+
454
+ end # parent_table_schema_name
455
+
456
+ end # Dsl
457
+ end # Configurator
458
+ end # PartitionedBase
459
+ end # Partitioned