partitioned 0.8.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.
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,1000 @@
1
+ #!/usr/bin/env ../spec/dummy/script/rails runner
2
+ # if you use linux, please change previous line to the
3
+ # "#! ../spec/dummy/script/rails runner"
4
+
5
+ # Before running this example you should execute "bundle install" and "rake db:create".
6
+ # To run this example you should open 'example' directory and execute example with one of the following flags:
7
+ # -C cleanup data in database and exit;
8
+ # -F cleanup data in database before creating new data;
9
+ #
10
+ # For example:
11
+ # ./created_at_referencing_awards.rb - F
12
+
13
+ # Initial data:
14
+ #
15
+ # Companies table is completed by four companies:
16
+ #
17
+ # create table companies (
18
+ # id serial not null primary key,
19
+ # created_at timestamp not null default now(),
20
+ # updated_at timestamp,
21
+ # name text null
22
+ # );
23
+ #
24
+ # insert into companies (created_at,id,name) values
25
+ # ('2012-03-13 13:26:52.184347',1,'Fluent Mobile, inc.'),
26
+ # ('2012-03-13 13:26:52.184347',2,'Fiksu, inc.'),
27
+ # ('2012-03-13 13:26:52.184347',3,'AppExchanger, inc.'),
28
+ # ('2012-03-13 13:26:52.184347',4,'FreeMyApps, inc.');
29
+ #
30
+ # id | created_at | updated_at | name
31
+ # ---+----------------------------+------------+---------------------
32
+ # 1 | 2012-03-11 13:26:52.184347 | | Fluent Mobile, inc.
33
+ # 2 | 2012-03-11 13:26:52.184347 | | Fiksu, inc.
34
+ # 3 | 2012-03-11 13:26:52.184347 | | AppExchanger, inc.
35
+ # 4 | 2012-03-11 13:26:52.184347 | | FreeMyApps, inc.
36
+ #
37
+ # Employees table is associated with companies table via key - id:
38
+ #
39
+ # create table employees (
40
+ # id serial not null primary key,
41
+ # created_at timestamp not null default now(),
42
+ # updated_at timestamp,
43
+ # name text not null,
44
+ # salary money not null,
45
+ # company_id integer not null
46
+ # );
47
+ #
48
+ # id | created_at | updated_at | name | salary | company_id
49
+ # ----+------------+------------+------+--------+------------
50
+ #
51
+ # Awards table is associated with employees table via key - employee_id:
52
+ #
53
+ # create table awards (
54
+ # id serial not null primary key,
55
+ # created_at timestamp not null default now(),
56
+ # employee_created_at timestamp not null,
57
+ # awarded_on date not null,
58
+ # employee_id integer not null,
59
+ # award_title text not null
60
+ # );
61
+ #
62
+ # id | created_at | employee_created_at | awarded_on | employee_id | award_title
63
+ # ----+------------+---------------------+------------+-------------+-------------
64
+ #
65
+ # Task:
66
+ #
67
+ # To increase the speed of requests to the database and to reduce the time
68
+ # of the request, need to split the Employees table and Awards table to the partition tables.
69
+ # Break criterion for Employees table is a created date(created_at).
70
+ # Break criterion for Awards table is a employee created date(employee_created_at).
71
+ #
72
+ # Implementation:
73
+ #
74
+ # Class Employee inherits from the abstract class ByCreatedAt,
75
+ # which supports partitioning.
76
+ #
77
+ # class Employee < Partitioned::ByCreatedAt
78
+ #
79
+ # Indicates a relationship to the companies table.
80
+ # belongs_to :company, :class_name => 'Company'
81
+ #
82
+ # Create a rules for each partition.
83
+ # Id is a unique index. Foreign key is company_id.
84
+ # This imposes a restriction on each of partition, that
85
+ # the column company_id associated with the table of companies
86
+ # and can not have values ​​that are not in the table companies.
87
+ # In this example, set up only 4 records in the table companies,
88
+ # so company_id can not be equal to 5 in any partition
89
+ # until it is an established company with id = 5.
90
+ #
91
+ # partitioned do |partition|
92
+ # partition.index :id, :unique => true
93
+ # partition.foreign_key :company_id
94
+ # end
95
+ # end
96
+ #
97
+ # Class Award inherits from the abstract class ByEmployeeCreatedAt,
98
+ # which supports partitioning.
99
+ #
100
+ # class Award < ByEmployeeCreatedAt
101
+ #
102
+ # Indicates a relationship to the employees table.
103
+ # belongs_to :employee, :class_name => 'Employee'
104
+ #
105
+ # partitioned do |partition|
106
+ # partition.foreign_key lambda {|model, *partition_key_values|
107
+ # return Configurator::Data::ForeignKey.
108
+ # new(:employee_id, Employee.partition_name(*partition_key_values), :id)
109
+ # }
110
+ # end
111
+ # end
112
+ #
113
+ # Create a schema employees_partitions, within which to store employees partitions:
114
+ #
115
+ # Employee.create_infrastructure
116
+ #
117
+ # Create the infrastructure for Awards table which includes the schema and partition tables:
118
+ #
119
+ # Award.create_infrastructure
120
+ #
121
+ # Create a partition tables with increments of one week:
122
+ #
123
+ # dates = Employee.partition_generate_range(START_DATE, END_DATE)
124
+ #
125
+ # Employee.create_new_partition_tables(dates)
126
+ # Each of employees partition has the same structure as that of the employees table:
127
+ #
128
+ # id | created_at | updated_at | name | salary | company_id
129
+ # ----+------------+------------+------+--------+------------
130
+ #
131
+ # CREATE TABLE "employees_partitions"."p20101227" (CHECK (created_at >= '2010-12-27' AND
132
+ # created_at < '2011-01-03')) INHERITS (employees);
133
+ # CREATE INDEX "p20101227_created_at_idx" ON "employees_partitions"."p20101227" ("created_at");
134
+ # CREATE UNIQUE INDEX "p20101227_id_udx" ON "employees_partitions"."p20101227" ("id");
135
+ # ALTER TABLE employees_partitions.p20101227 add foreign key (company_id) references companies(id);
136
+ #
137
+ # CREATE TABLE "employees_partitions"."p20110103" (CHECK (created_at >= '2011-01-03' AND
138
+ # created_at < '2011-01-10')) INHERITS (employees);
139
+ # CREATE INDEX "p20110103_created_at_idx" ON "employees_partitions"."p20110103" ("created_at");
140
+ # CREATE UNIQUE INDEX "p20110103_id_udx" ON "employees_partitions"."p20110103" ("id");
141
+ # ALTER TABLE employees_partitions.p20110103 add foreign key (company_id) references companies(id);
142
+ #
143
+ # CREATE TABLE "employees_partitions"."p20110110" (CHECK (created_at >= '2011-01-10' AND
144
+ # created_at < '2011-01-17')) INHERITS (employees);
145
+ # CREATE INDEX "p20110110_created_at_idx" ON "employees_partitions"."p20110110" ("created_at");
146
+ # CREATE UNIQUE INDEX "p20110110_id_udx" ON "employees_partitions"."p20110110" ("id");
147
+ # ALTER TABLE employees_partitions.p20110110 add foreign key (company_id) references companies(id);
148
+ #
149
+ # ...
150
+ #
151
+ # CREATE TABLE "employees_partitions"."p20111212" (CHECK (created_at >= '2011-12-12' AND
152
+ # created_at < '2011-12-19')) INHERITS (employees);
153
+ # CREATE INDEX "p20111212_created_at_idx" ON "employees_partitions"."p20111212" ("created_at");
154
+ # CREATE UNIQUE INDEX "p20111212_id_udx" ON "employees_partitions"."p20111212" ("id");
155
+ # ALTER TABLE employees_partitions.p20111212 add foreign key (company_id) references companies(id);
156
+ #
157
+ # CREATE TABLE "employees_partitions"."p20111219" (CHECK (created_at >= '2011-12-19' AND
158
+ # created_at < '2011-12-26')) INHERITS (employees);
159
+ # CREATE INDEX "p20111219_created_at_idx" ON "employees_partitions"."p20111219" ("created_at");
160
+ # CREATE UNIQUE INDEX "p20111219_id_udx" ON "employees_partitions"."p20111219" ("id");
161
+ # ALTER TABLE employees_partitions.p20111219 add foreign key (company_id) references companies(id);
162
+ #
163
+ # CREATE TABLE "employees_partitions"."p20111226" (CHECK (created_at >= '2011-12-26' AND
164
+ # created_at < '2012-01-02')) INHERITS (employees);
165
+ # CREATE INDEX "p20111226_created_at_idx" ON "employees_partitions"."p20111226" ("created_at");
166
+ # CREATE UNIQUE INDEX "p20111226_id_udx" ON "employees_partitions"."p20111226" ("id");
167
+ # ALTER TABLE employees_partitions.p20111226 add foreign key (company_id) references companies(id);
168
+ #
169
+ # You should have the following tables with increments of one week:
170
+ # employees_partitions.p20101227
171
+ # employees_partitions.p20110103
172
+ # employees_partitions.p20110110
173
+ # employees_partitions.p20110117
174
+ # employees_partitions.p20110124
175
+ # employees_partitions.p20110131
176
+ # employees_partitions.p20110207
177
+ # employees_partitions.p20110214
178
+ # employees_partitions.p20110221
179
+ # employees_partitions.p20110228
180
+ # employees_partitions.p20110307
181
+ # employees_partitions.p20110314
182
+ # employees_partitions.p20110321
183
+ # employees_partitions.p20110328
184
+ # employees_partitions.p20110404
185
+ # employees_partitions.p20110411
186
+ # employees_partitions.p20110418
187
+ # employees_partitions.p20110425
188
+ # employees_partitions.p20110502
189
+ # employees_partitions.p20110509
190
+ # employees_partitions.p20110516
191
+ # employees_partitions.p20110523
192
+ # employees_partitions.p20110530
193
+ # employees_partitions.p20110606
194
+ # employees_partitions.p20110613
195
+ # employees_partitions.p20110620
196
+ # employees_partitions.p20110627
197
+ # employees_partitions.p20110704
198
+ # employees_partitions.p20110711
199
+ # employees_partitions.p20110718
200
+ # employees_partitions.p20110725
201
+ # employees_partitions.p20110801
202
+ # employees_partitions.p20110808
203
+ # employees_partitions.p20110815
204
+ # employees_partitions.p20110822
205
+ # employees_partitions.p20110829
206
+ # employees_partitions.p20110905
207
+ # employees_partitions.p20110912
208
+ # employees_partitions.p20110919
209
+ # employees_partitions.p20110926
210
+ # employees_partitions.p20111003
211
+ # employees_partitions.p20111010
212
+ # employees_partitions.p20111017
213
+ # employees_partitions.p20111024
214
+ # employees_partitions.p20111031
215
+ # employees_partitions.p20111107
216
+ # employees_partitions.p20111114
217
+ # employees_partitions.p20111121
218
+ # employees_partitions.p20111128
219
+ # employees_partitions.p20111205
220
+ # employees_partitions.p20111212
221
+ # employees_partitions.p20111219
222
+ # employees_partitions.p20111226
223
+ #
224
+ # Each of partitions inherits from employees table,
225
+ # thus a new row will automatically be added to the employees table .
226
+ #
227
+ # To add data, we use the following constructions,
228
+ # in which employees and employee_data - a random data:
229
+ #
230
+ # create_many - allows you to add multiple records
231
+ # Employee.create_many(employees)
232
+ # create - allows you to add one record
233
+ # Employee.create(employee_data)
234
+ # new/save! - allows you to add one record without using "create" method
235
+ # employee = Employee.new(employee_data)
236
+ # employee.save!
237
+ #
238
+ # For update data, we use the following constructions,
239
+ # in which updates - a random data:
240
+ #
241
+ # update_many - allows you to update multiple records.
242
+ # :set_array - additional option, you may read the description
243
+ # of the method in the file update_many bulk_methods_mixin.rb about this option.
244
+ # Employee.update_many(updates, { :set_array => '"salary = #{table_name}.salary +
245
+ # datatable.salary, updated_at = now()"' })
246
+ #
247
+ # This construction using for update one record. You also may use update method.
248
+ # employee = Employee.from_partition(employee_record[:created_at]).find(employee_record[:id])
249
+ # employee.save
250
+ #
251
+ # The data get into the employees table ONLY through partition tables.
252
+ # You can not do an insert row into a table employees directly.
253
+ # For this purpose special restrictions are imposed on the table employees.
254
+ #
255
+ # Award.create_new_partition_tables(dates)
256
+ # Each of awards partition has the same structure as that of the awards table:
257
+ #
258
+ # id | created_at | employee_created_at | awarded_on | employee_id | award_title
259
+ # -----+------------+---------------------+------------+-------------+-------------
260
+ #
261
+ # CREATE TABLE "awards_partitions"."p20101227" (CHECK (employee_created_at >= '2010-12-27' AND
262
+ # employee_created_at < '2011-01-03')) INHERITS (awards);
263
+ # CREATE INDEX "p20101227_employee_created_at_idx" ON "awards_partitions"."p20101227" ("employee_created_at");
264
+ # ALTER TABLE awards_partitions.p20101227 add foreign key (employee_id) references employees_partitions.p20101227(id);
265
+ #
266
+ # CREATE TABLE "awards_partitions"."p20110103" (CHECK (employee_created_at >= '2011-01-03' AND
267
+ # employee_created_at < '2011-01-10')) INHERITS (awards);
268
+ # CREATE INDEX "p20110103_employee_created_at_idx" ON "awards_partitions"."p20110103" ("employee_created_at");
269
+ # ALTER TABLE awards_partitions.p20110103 add foreign key (employee_id) references employees_partitions.p20110103(id);
270
+ #
271
+ # CREATE TABLE "awards_partitions"."p20110110" (CHECK (employee_created_at >= '2011-01-10' AND
272
+ # employee_created_at < '2011-01-17')) INHERITS (awards);
273
+ # CREATE INDEX "p20110110_employee_created_at_idx" ON "awards_partitions"."p20110110" ("employee_created_at");
274
+ # ALTER TABLE awards_partitions.p20110110 add foreign key (employee_id) references employees_partitions.p20110110(id);
275
+ #
276
+ # ...
277
+ #
278
+ # CREATE TABLE "awards_partitions"."p20111212" (CHECK (employee_created_at >= '2011-12-12' AND
279
+ # employee_created_at < '2011-12-19')) INHERITS (awards);
280
+ # CREATE INDEX "p20111212_employee_created_at_idx" ON "awards_partitions"."p20111212" ("employee_created_at");
281
+ # ALTER TABLE awards_partitions.p20111212 add foreign key (employee_id) references employees_partitions.p20111212(id);
282
+ #
283
+ # CREATE TABLE "awards_partitions"."p20111219" (CHECK (employee_created_at >= '2011-12-19' AND
284
+ # employee_created_at < '2011-12-26')) INHERITS (awards);
285
+ # CREATE INDEX "p20111219_employee_created_at_idx" ON "awards_partitions"."p20111219" ("employee_created_at");
286
+ # ALTER TABLE awards_partitions.p20111219 add foreign key (employee_id) references employees_partitions.p20111219(id);
287
+ #
288
+ # CREATE TABLE "awards_partitions"."p20111226" (CHECK (employee_created_at >= '2011-12-26' AND
289
+ # employee_created_at < '2012-01-02')) INHERITS (awards);
290
+ # CREATE INDEX "p20111226_employee_created_at_idx" ON "awards_partitions"."p20111226" ("employee_created_at");
291
+ # ALTER TABLE awards_partitions.p20111226 add foreign key (employee_id) references employees_partitions.p20111226(id);
292
+ #
293
+ # You should have the following tables with increments of one week:
294
+ # awards_partitions.p20101227
295
+ # awards_partitions.p20110103
296
+ # awards_partitions.p20110110
297
+ # awards_partitions.p20110117
298
+ # awards_partitions.p20110124
299
+ # awards_partitions.p20110131
300
+ # awards_partitions.p20110207
301
+ # awards_partitions.p20110214
302
+ # awards_partitions.p20110221
303
+ # awards_partitions.p20110228
304
+ # awards_partitions.p20110307
305
+ # awards_partitions.p20110314
306
+ # awards_partitions.p20110321
307
+ # awards_partitions.p20110328
308
+ # awards_partitions.p20110404
309
+ # awards_partitions.p20110411
310
+ # awards_partitions.p20110418
311
+ # awards_partitions.p20110425
312
+ # awards_partitions.p20110502
313
+ # awards_partitions.p20110509
314
+ # awards_partitions.p20110516
315
+ # awards_partitions.p20110523
316
+ # awards_partitions.p20110530
317
+ # awards_partitions.p20110606
318
+ # awards_partitions.p20110613
319
+ # awards_partitions.p20110620
320
+ # awards_partitions.p20110627
321
+ # awards_partitions.p20110704
322
+ # awards_partitions.p20110711
323
+ # awards_partitions.p20110718
324
+ # awards_partitions.p20110725
325
+ # awards_partitions.p20110801
326
+ # awards_partitions.p20110808
327
+ # awards_partitions.p20110815
328
+ # awards_partitions.p20110822
329
+ # awards_partitions.p20110829
330
+ # awards_partitions.p20110905
331
+ # awards_partitions.p20110912
332
+ # awards_partitions.p20110919
333
+ # awards_partitions.p20110926
334
+ # awards_partitions.p20111003
335
+ # awards_partitions.p20111010
336
+ # awards_partitions.p20111017
337
+ # awards_partitions.p20111024
338
+ # awards_partitions.p20111031
339
+ # awards_partitions.p20111107
340
+ # awards_partitions.p20111114
341
+ # awards_partitions.p20111121
342
+ # awards_partitions.p20111128
343
+ # awards_partitions.p20111205
344
+ # awards_partitions.p20111212
345
+ # awards_partitions.p20111219
346
+ # awards_partitions.p20111226
347
+ #
348
+ # Each of partitions inherits from awards table,
349
+ # thus a new row will automatically be added to the awards table .
350
+ #
351
+ # To add data, we use the following constructions,
352
+ # in which awards and award_data - a random data:
353
+ #
354
+ # create_many - allows you to add multiple records
355
+ # Award.create_many(awards)
356
+ # create - allows you to add one record
357
+ # Award.create(award_data)
358
+ # new/save! - allows you to add one record without using "create" method
359
+ # award = Award.new(award_data)
360
+ # award.save!
361
+ #
362
+ # For update data, we use the following constructions,
363
+ # in which updates - a random data:
364
+ #
365
+ # update_many - allows you to update multiple records.
366
+ # :set_array - additional option, you may read the description
367
+ # of the method in the file update_many bulk_methods_mixin.rb about this option.
368
+ # Award.update_many(updates, { :set_array => '"award_title = datatable.award_title,
369
+ # awarded_on = now()"' })
370
+ #
371
+ # This construction using for update one record. You also may use update method.
372
+ # award = Award.from_partition(award_record[:employee_created_at]).find(award_record[:id])
373
+ # award.save
374
+ #
375
+ # The data get into the awards table ONLY through partition tables.
376
+ # You can not do an insert row into a table awards directly.
377
+ # For this purpose special restrictions are imposed on the table awards.
378
+ #
379
+ # Result:
380
+ #
381
+ # We have table companies:
382
+ #
383
+ # id | created_at | updated_at | name
384
+ # ---+----------------------------+------------+---------------------
385
+ # 1 | 2012-03-11 13:26:52.184347 | | Fluent Mobile, inc.
386
+ # 2 | 2012-03-11 13:26:52.184347 | | Fiksu, inc.
387
+ # 3 | 2012-03-11 13:26:52.184347 | | AppExchanger, inc.
388
+ # 4 | 2012-03-11 13:26:52.184347 | | FreeMyApps, inc.
389
+ #
390
+ # Table employees with random data from 1 to 2500:
391
+ #
392
+ # id | created_at | updated_at | name | salary | company_id
393
+ #------+---------------------+----------------------------+---------------------------------+-------------+------------
394
+ # 1 | 2011-02-27 09:30:12 | | Winston J. Sillypants, I | $89,653.00 | 2
395
+ # 2 | 2011-02-08 01:08:50 | | Winston J. Sillypants, II | $94,759.00 | 4
396
+ # 3 | 2011-12-08 19:54:21 | 2012-03-27 11:38:50.674123 | Winston J. Sillypants, III | $73,726.00 | 2
397
+ # ...
398
+ # 2498 | 2011-01-18 01:43:42 | 2012-03-27 10:38:36.029443 | Picholine Pimplenad, MMCDXCVIII | $61,107.00 | 3
399
+ # 2499 | 2011-11-03 21:07:05 | 2012-03-27 11:38:50.656815 | Picholine Pimplenad, MMCDXCIX | $62,638.00 | 4
400
+ # 2500 | 2011-12-26 04:03:57 | 2012-03-27 10:38:36.039419 | Picholine Pimplenad, MMD | $102,855.00 | 1
401
+ #
402
+ # Table awards with random data from 1 to 2500:
403
+ #
404
+ # id | created_at | employee_created_at | awarded_on | employee_id | award_title
405
+ #------+----------------------------+---------------------+------------+-------------+---------------------------------
406
+ # 1 | 2012-03-27 10:38:39.728924 | 2011-10-17 13:44:07 | 2012-07-02 | 440 | You're the best I
407
+ # 2 | 2012-03-27 10:38:39.728924 | 2011-06-15 20:00:02 | 2012-06-07 | 921 | You're the best II
408
+ # 3 | 2012-03-27 10:38:39.728924 | 2011-11-20 06:30:34 | 2012-10-16 | 349 | You're the best III
409
+ # ...
410
+ # 2498 | 2012-03-27 10:38:45.954887 | 2011-10-09 22:51:04 | 2012-07-10 | 1421 | You're the best MMCDXCVIII
411
+ # 2499 | 2012-03-27 10:38:45.959831 | 2011-01-07 02:25:00 | 2011-09-07 | 1717 | You're the best MMCDXCIX
412
+ # 2500 | 2012-03-27 10:38:45.964599 | 2011-08-23 08:02:50 | 2012-03-27 | 515 | You're the greatest DCCCXXXVIII
413
+ #
414
+ # Partition employees_partitions.p20101227 - partition where
415
+ # created_at >= '2010-12-27 00:00:00' AND created_at < '2011-01-03 00:00:00':
416
+ #
417
+ # id | created_at | updated_at | name | salary | company_id
418
+ #------+---------------------+----------------------------+---------------------------------+-------------+------------
419
+ # 641 | 2011-01-01 06:32:16 | | Winston J. Sillypants, DCXLI | $103,834.00 | 4
420
+ # 644 | 2011-01-02 08:00:35 | 2012-03-27 11:38:50.70092 | Winston J. Sillypants, DCXLIV | $98,756.00 | 4
421
+ # 766 | 2011-01-01 18:22:34 | 2012-03-27 11:38:50.70092 | Winston J. Sillypants, DCCLXVI | $133,384.00 | 1
422
+ # ...
423
+ # 2058 | 2011-01-01 09:07:52 | 2012-03-27 10:38:33.742273 | Picholine Pimplenad, MMLVIII | $130,422.00 | 3
424
+ # 2422 | 2011-01-01 07:44:38 | 2012-03-27 10:38:35.604894 | Picholine Pimplenad, MMCDXXII | $86,484.00 | 3
425
+ # 2482 | 2011-01-01 07:52:26 | 2012-03-27 10:38:35.890074 | Picholine Pimplenad, MMCDLXXXII | $113,265.00 | 2
426
+ #
427
+ # Partition employees_partitions.p20110103 - partition where
428
+ # created_at >= '2011-01-03 00:00:00' AND created_at < '2011-01-10 00:00:00':
429
+ #
430
+ # id | created_at | updated_at | name | salary | company_id
431
+ #------+---------------------+----------------------------+---------------------------------+-------------+------------
432
+ # 22 | 2011-01-09 03:20:24 | 2012-03-27 11:38:50.588609 | Winston J. Sillypants, XXII | $139,538.00 | 2
433
+ # 63 | 2011-01-09 04:51:41 | | Winston J. Sillypants, LXIII | $127,878.00 | 2
434
+ # 282 | 2011-01-03 09:19:16 | 2012-03-27 10:38:37.896505 | Winston J. Sillypants, CCLXXXII | $106,733.00 | 3
435
+ # ...
436
+ # 2366 | 2011-01-07 19:18:54 | 2012-03-27 11:38:50.588609 | Picholine Pimplenad, MMCCCLXVI | $112,775.00 | 4
437
+ # 2392 | 2011-01-08 21:19:03 | 2012-03-27 10:38:35.463625 | Picholine Pimplenad, MMCCCXCII | $106,327.00 | 4
438
+ # 2399 | 2011-01-03 06:04:39 | 2012-03-27 10:38:35.497058 | Picholine Pimplenad, MMCCCXCIX | $77,089.00 | 2
439
+ #
440
+ # Partition employees_partitions.p20110110 - partition where
441
+ # created_at >= '2011-01-10 00:00:00' AND created_at < '2011-01-17 00:00:00':
442
+ #
443
+ # id | created_at | updated_at | name | salary | company_id
444
+ #------+---------------------+----------------------------+---------------------------------+-------------+------------
445
+ # 30 | 2011-01-15 11:53:36 | | Winston J. Sillypants, XXX | $130,203.00 | 4
446
+ # 34 | 2011-01-16 20:01:25 | 2012-03-27 10:38:37.428899 | Winston J. Sillypants, XXXIV | $69,225.00 | 4
447
+ # 109 | 2011-01-12 08:17:06 | | Winston J. Sillypants, CIX | $127,294.00 | 4
448
+ # ...
449
+ # 2325 | 2011-01-13 04:17:58 | 2012-03-27 11:38:50.661251 | Picholine Pimplenad, MMCCCXXV | $98,912.00 | 2
450
+ # 2362 | 2011-01-14 06:48:52 | 2012-03-27 10:38:35.316907 | Picholine Pimplenad, MMCCCLXII | $116,023.00 | 2
451
+ # 2403 | 2011-01-12 00:28:05 | 2012-03-27 10:38:38.022877 | Picholine Pimplenad, MMCDIII | $106,252.00 | 3
452
+ #
453
+ # ...
454
+ #
455
+ # Partition employees_partitions.p20111212 - partition where
456
+ # created_at >= '2011-12-12 00:00:00' AND created_at < '2011-12-19 00:00:00':
457
+ #
458
+ # id | created_at | updated_at | name | salary | company_id
459
+ #------+---------------------+----------------------------+-----------------------------------+-------------+------------
460
+ # 85 | 2011-12-15 00:35:46 | | Winston J. Sillypants, LXXXV | $110,469.00 | 2
461
+ # 137 | 2011-12-12 10:58:14 | | Winston J. Sillypants, CXXXVII | $96,201.00 | 1
462
+ # 222 | 2011-12-14 07:13:52 | 2012-03-27 11:38:50.556991 | Winston J. Sillypants, CCXXII | $127,907.00 | 4
463
+ # ...
464
+ # 2351 | 2011-12-17 17:04:37 | 2012-03-27 10:38:35.26375 | Picholine Pimplenad, MMCCCLI | $82,418.00 | 3
465
+ # 2383 | 2011-12-12 22:01:11 | 2012-03-27 10:38:35.420221 | Picholine Pimplenad, MMCCCLXXXIII | $62,355.00 | 2
466
+ # 2391 | 2011-12-18 02:42:22 | 2012-03-27 10:38:35.458827 | Picholine Pimplenad, MMCCCXCI | $116,403.00 | 4
467
+ #
468
+ # Partition employees_partitions.p20111219 - partition where
469
+ # created_at >= '2011-12-19 00:00:00' AND created_at < '2011-12-26 00:00:00':
470
+ #
471
+ # id | created_at | updated_at | name | salary | company_id
472
+ #------+---------------------+----------------------------+----------------------------------+-------------+------------
473
+ # 7 | 2011-12-20 04:40:07 | 2012-03-27 11:38:50.492916 | Winston J. Sillypants, VII | $89,608.00 | 4
474
+ # 84 | 2011-12-25 14:45:16 | | Winston J. Sillypants, LXXXIV | $122,661.00 | 2
475
+ # 120 | 2011-12-19 21:18:09 | | Winston J. Sillypants, CXX | $116,477.00 | 3
476
+ # ...
477
+ # 2256 | 2011-12-22 22:28:13 | 2012-03-27 10:38:34.755228 | Picholine Pimplenad, MMCCLVI | $106,724.00 | 1
478
+ # 2384 | 2011-12-23 13:27:09 | 2012-03-27 10:38:35.425007 | Picholine Pimplenad, MMCCCLXXXIV | $107,816.00 | 2
479
+ # 2434 | 2011-12-24 05:43:52 | 2012-03-27 10:38:35.65791 | Picholine Pimplenad, MMCDXXXIV | $69,897.00 | 1
480
+ #
481
+ # Partition employees_partitions.p20111226 - partition where
482
+ # created_at >= '2011-12-26 00:00:00' AND created_at < '2012-01-02 00:00:00':
483
+ #
484
+ # id | created_at | updated_at | name | salary | company_id
485
+ #------+---------------------+----------------------------+-------------------------------+-------------+------------
486
+ # 4 | 2011-12-28 09:59:33 | | Winston J. Sillypants, IV | $75,740.00 | 3
487
+ # 33 | 2011-12-26 00:15:53 | | Winston J. Sillypants, XXXIII | $72,866.00 | 4
488
+ # 134 | 2011-12-30 22:29:57 | | Winston J. Sillypants, CXXXIV | $101,256.00 | 2
489
+ # ...
490
+ # 2417 | 2011-12-26 14:49:13 | 2012-03-27 10:38:35.584125 | Picholine Pimplenad, MMCDXVII | $111,609.00 | 1
491
+ # 2420 | 2011-12-30 22:15:29 | 2012-03-27 10:38:35.596825 | Picholine Pimplenad, MMCDXX | $105,180.00 | 2
492
+ # 2500 | 2011-12-26 04:03:57 | 2012-03-27 10:38:36.039419 | Picholine Pimplenad, MMD | $102,855.00 | 1
493
+ #
494
+ # Partition awards_partitions.p20101227 - partition where
495
+ # employee_created_at >= '2010-12-27 00:00:00' AND employee_created_at < '2011-01-03 00:00:00':
496
+ #
497
+ # id | created_at | employee_created_at | awarded_on | employee_id | award_title
498
+ #------+----------------------------+---------------------+------------+-------------+----------------------------
499
+ # 33 | 2012-03-27 10:38:39.728924 | 2011-01-02 05:58:21 | 2011-09-19 | 1811 | You're the best XXXIII
500
+ # 259 | 2012-03-27 10:38:39.728924 | 2011-01-02 10:54:32 | 2011-03-23 | 873 | You're the best CCLIX
501
+ # 261 | 2012-03-27 10:38:39.728924 | 2011-01-02 10:54:32 | 2011-12-10 | 873 | You're the best CCLXI
502
+ # ...
503
+ # 2269 | 2012-03-27 10:38:44.743054 | 2011-01-02 05:58:21 | 2012-03-27 | 1811 | You're the greatest CDXV
504
+ # 2408 | 2012-03-27 10:38:45.458391 | 2011-01-01 06:32:16 | 2011-06-25 | 641 | You're the best MMCDVIII
505
+ # 2484 | 2012-03-27 10:38:45.887678 | 2011-01-02 05:58:21 | 2011-08-22 | 1811 | You're the best MMCDLXXXIV
506
+ #
507
+ # Partition awards_partitions.p20110103 - partition where
508
+ # employee_created_at >= '2011-01-03 00:00:00' AND employee_created_at < '2011-01-10 00:00:00':
509
+ #
510
+ # id | created_at | employee_created_at | awarded_on | employee_id | award_title
511
+ #------+----------------------------+---------------------+------------+-------------+------------------------------
512
+ # 61 | 2012-03-27 10:38:39.728924 | 2011-01-07 19:18:54 | 2012-03-27 | 2366 | You're the greatest CCCXLIII
513
+ # 134 | 2012-03-27 10:38:39.728924 | 2011-01-07 19:33:11 | 2011-05-17 | 810 | You're the best CXXXIV
514
+ # 193 | 2012-03-27 10:38:39.728924 | 2011-01-04 06:58:09 | 2011-05-27 | 836 | You're the best CXCIII
515
+ # ...
516
+ # 2218 | 2012-03-27 10:38:44.49816 | 2011-01-06 21:05:51 | 2011-07-16 | 2190 | You're the best MMCCXVIII
517
+ # 2245 | 2012-03-27 10:38:44.630179 | 2011-01-08 11:44:32 | 2011-04-17 | 841 | You're the best MMCCXLV
518
+ # 2499 | 2012-03-27 10:38:45.959831 | 2011-01-07 02:25:00 | 2011-09-07 | 1717 | You're the best MMCDXCIX
519
+ #
520
+ # Partition employees_partitions.p20110110 - partition where
521
+ # employee_created_at >= '2011-01-10 00:00:00' AND employee_created_at < '2011-01-17 00:00:00':
522
+ #
523
+ # id | created_at | employee_created_at | awarded_on | employee_id | award_title
524
+ #------+----------------------------+---------------------+------------+-------------+---------------------------
525
+ # 36 | 2012-03-27 10:38:39.728924 | 2011-01-12 03:12:37 | 2011-06-23 | 1196 | You're the best XXXVI
526
+ # 44 | 2012-03-27 10:38:39.728924 | 2011-01-15 20:43:01 | 2011-10-09 | 1159 | You're the best XLIV
527
+ # 93 | 2012-03-27 10:38:39.728924 | 2011-01-16 14:45:36 | 2011-10-02 | 912 | You're the best XCIII
528
+ # ...
529
+ # 2476 | 2012-03-27 10:38:45.848691 | 2011-01-10 23:23:24 | 2011-11-05 | 1314 | You're the best MMCDLXXVI
530
+ # 2481 | 2012-03-27 10:38:45.873229 | 2011-01-11 05:12:47 | 2011-08-10 | 2240 | You're the best MMCDLXXXI
531
+ # 2491 | 2012-03-27 10:38:45.921378 | 2011-01-12 04:18:22 | 2011-08-13 | 1595 | You're the best MMCDXCI
532
+ #
533
+ # ...
534
+ #
535
+ # Partition awards_partitions.p20111212 - partition where
536
+ # employee_created_at >= '2011-12-12 00:00:00' AND employee_created_at < '2011-12-19 00:00:00':
537
+ #
538
+ # id | created_at | employee_created_at | awarded_on | employee_id | award_title
539
+ #------+----------------------------+---------------------+------------+-------------+--------------------------------
540
+ # 76 | 2012-03-27 10:38:39.728924 | 2011-12-17 11:12:41 | 2012-03-27 | 1548 | You're the greatest DXXIII
541
+ # 159 | 2012-03-27 10:38:39.728924 | 2011-12-17 17:04:37 | 2012-03-27 | 2351 | You're the greatest DCCCLXXXII
542
+ # 181 | 2012-03-27 10:38:39.728924 | 2011-12-18 14:25:29 | 2012-04-20 | 2031 | You're the best CLXXXI
543
+ # ...
544
+ # 2197 | 2012-03-27 10:38:44.39818 | 2011-12-13 00:14:00 | 2012-06-11 | 555 | You're the best MMCXCVII
545
+ # 2216 | 2012-03-27 10:38:44.488553 | 2011-12-13 17:08:24 | 2011-12-26 | 596 | You're the best MMCCXVI
546
+ # 2217 | 2012-03-27 10:38:44.493389 | 2011-12-16 18:12:58 | 2012-12-14 | 1860 | You're the best MMCCXVII
547
+ #
548
+ # Partition awards_partitions.p20111219 - partition where
549
+ # employee_created_at >= '2011-12-19 00:00:00' AND employee_created_at < '2011-12-26 00:00:00':
550
+ #
551
+ # id | created_at | employee_created_at | awarded_on | employee_id | award_title
552
+ #------+----------------------------+---------------------+------------+-------------+-----------------------------
553
+ # 16 | 2012-03-27 10:38:39.728924 | 2011-12-23 17:53:33 | 2012-05-06 | 1534 | You're the best XVI
554
+ # 132 | 2012-03-27 10:38:39.728924 | 2011-12-19 21:58:52 | 2012-08-28 | 979 | You're the best CXXXII
555
+ # 149 | 2012-03-27 10:38:39.728924 | 2011-12-24 06:28:33 | 2012-04-06 | 507 | You're the best CXLIX
556
+ # ...
557
+ # 2451 | 2012-03-27 10:38:45.726195 | 2011-12-23 22:40:28 | 2012-11-05 | 914 | You're the best MMCDLI
558
+ # 2483 | 2012-03-27 10:38:45.882936 | 2011-12-20 13:54:57 | 2012-05-09 | 1615 | You're the best MMCDLXXXIII
559
+ # 2485 | 2012-03-27 10:38:45.892318 | 2011-12-25 06:54:49 | 2012-03-27 | 2017 | You're the greatest CLIII
560
+ #
561
+ # Partition awards_partitions.p20111226 - partition where
562
+ # employee_created_at >= '2011-12-26 00:00:00' AND employee_created_at < '2012-01-02 00:00:00':
563
+ #
564
+ # id | created_at | employee_created_at | awarded_on | employee_id | award_title
565
+ #------+----------------------------+---------------------+------------+-------------+-----------------------------
566
+ # 15 | 2012-03-27 10:38:39.728924 | 2011-12-27 22:35:27 | 2012-03-27 | 1496 | You're the greatest CXXIII
567
+ # 62 | 2012-03-27 10:38:39.728924 | 2011-12-27 18:36:46 | 2012-03-27 | 1663 | You're the greatest CLXXXII
568
+ # 64 | 2012-03-27 10:38:39.728924 | 2011-12-26 14:49:13 | 2012-04-02 | 2417 | You're the best LXIV
569
+ # ...
570
+ # 2211 | 2012-03-27 10:38:44.464965 | 2011-12-27 10:11:18 | 2012-03-27 | 2388 | You're the greatest XXX
571
+ # 2291 | 2012-03-27 10:38:44.908378 | 2011-12-26 08:07:37 | 2012-03-27 | 242 | You're the greatest LXXXVI
572
+ # 2497 | 2012-03-27 10:38:45.949948 | 2011-12-27 16:51:56 | 2012-11-10 | 1902 | You're the best MMCDXCVII
573
+ #
574
+
575
+ require File.expand_path(File.dirname(__FILE__) + "/lib/command_line_tool_mixin")
576
+ require File.expand_path(File.dirname(__FILE__) + "/lib/get_options")
577
+
578
+ include CommandLineToolMixin
579
+
580
+ $cleanup = false
581
+ $force = false
582
+ $create_many = 1500
583
+ $create_individual = 500
584
+ $new_individual = 500
585
+ $update_many = 500
586
+ $update_individual = 500
587
+
588
+ @options = {
589
+ "--cleanup" => {
590
+ :short => "-C",
591
+ :argument => GetoptLong::NO_ARGUMENT,
592
+ :note => "cleanup data in database and exit"
593
+ },
594
+ "--force" => {
595
+ :short => "-F",
596
+ :argument => GetoptLong::NO_ARGUMENT,
597
+ :note => "cleanup data in database before creating new data"
598
+ },
599
+ "--create-many" => {
600
+ :short => "-m",
601
+ :argument => GetoptLong::REQUIRED_ARGUMENT,
602
+ :note => "how many objects to create via create_many",
603
+ :argument_note => "NUMBER"
604
+ },
605
+ "--create-individual" => {
606
+ :short => "-i",
607
+ :argument => GetoptLong::REQUIRED_ARGUMENT,
608
+ :note => "how many objects to create via create",
609
+ :argument_note => "NUMBER"
610
+ },
611
+ "--new-individual" => {
612
+ :short => "-I",
613
+ :argument => GetoptLong::REQUIRED_ARGUMENT,
614
+ :note => "how many objects to create via new/save",
615
+ :argument_note => "NUMBER"
616
+ },
617
+ "--update-individual" => {
618
+ :short => "-u",
619
+ :argument => GetoptLong::REQUIRED_ARGUMENT,
620
+ :note => "how many objects to update indivudually",
621
+ :argument_note => "NUMBER"
622
+ },
623
+ "--update-many" => {
624
+ :short => "-U",
625
+ :argument => GetoptLong::REQUIRED_ARGUMENT,
626
+ :note => "how many objects to update via update_many",
627
+ :argument_note => "NUMBER"
628
+ },
629
+ }
630
+
631
+ command_line_options(@options) do |option,argument|
632
+ if option == '--cleanup'
633
+ $cleanup = true
634
+ elsif option == '--force'
635
+ $force = true
636
+ elsif option == '--create-many'
637
+ $create_many = argument.to_i
638
+ elsif option == '--create-individual'
639
+ $create_individual = argument.to_i
640
+ elsif option == '--new-individual'
641
+ $new_individual = argument.to_i
642
+ elsif option == '--update-individual'
643
+ $update_individual = argument.to_i
644
+ elsif option == '--update-many'
645
+ $update_many = argument.to_i
646
+ end
647
+ end
648
+
649
+ if $cleanup || $force
650
+ ActiveRecord::Base.connection.drop_schema("awards_partitions", :cascade => true) rescue nil
651
+ ActiveRecord::Base.connection.drop_table("awards") rescue nil
652
+ ActiveRecord::Base.connection.drop_schema("employees_partitions", :cascade => true) rescue nil
653
+ ActiveRecord::Base.connection.drop_table("employees") rescue nil
654
+ ActiveRecord::Base.connection.drop_table("companies") rescue nil
655
+ exit(0) if $cleanup
656
+ end
657
+
658
+ $total_records = ($create_many + $create_individual + $new_individual) * 2
659
+
660
+ puts "total records: #{$total_records}"
661
+
662
+ START_DATE = Date.parse('2011-01-01')
663
+ END_DATE = Date.parse('2011-12-31')
664
+
665
+ # the ActiveRecord classes
666
+
667
+ require File.expand_path(File.dirname(__FILE__) + "/lib/company")
668
+
669
+ class Employee < Partitioned::ByCreatedAt
670
+ belongs_to :company, :class_name => 'Company'
671
+ attr_accessible :created_at, :salary, :name, :company_id
672
+
673
+ partitioned do |partition|
674
+ partition.index :id, :unique => true
675
+ partition.foreign_key :company_id
676
+ end
677
+
678
+ connection.execute <<-SQL
679
+ create table employees
680
+ (
681
+ id serial not null primary key,
682
+ created_at timestamp not null default now(),
683
+ updated_at timestamp,
684
+ name text not null,
685
+ salary money not null,
686
+ company_id integer not null
687
+ );
688
+ SQL
689
+ end
690
+
691
+ class ByEmployeeCreatedAt < Partitioned::ByWeeklyTimeField
692
+ self.abstract_class = true
693
+
694
+ def self.partition_time_field
695
+ return :employee_created_at
696
+ end
697
+ end
698
+
699
+ class Award < ByEmployeeCreatedAt
700
+ belongs_to :employee, :class_name => 'Employee'
701
+ attr_accessible :award_title, :employee_created_at, :employee_id, :awarded_on
702
+
703
+ partitioned do |partition|
704
+ partition.foreign_key lambda {|model, *partition_key_values|
705
+ return Configurator::Data::ForeignKey.new(:employee_id, Employee.partition_name(*partition_key_values), :id)
706
+ }
707
+ end
708
+
709
+ connection.execute <<-SQL
710
+ create table awards
711
+ (
712
+ id serial not null primary key,
713
+ created_at timestamp not null default now(),
714
+ employee_created_at timestamp not null,
715
+ awarded_on date not null,
716
+ employee_id integer not null,
717
+ award_title text not null
718
+ );
719
+ SQL
720
+ end
721
+
722
+ # You should have the following tables:
723
+ # public.companies
724
+ # public.employees
725
+
726
+ # create the infrastructure for EMPLOYEES table which includes the schema and partition tables
727
+
728
+ Employee.create_infrastructure
729
+
730
+ # create the infrastructure for Awards table which includes the schema and partition tables
731
+
732
+ Award.create_infrastructure
733
+
734
+ # You should have the following schema:
735
+ # employees_partitions
736
+ # awards_partitions
737
+
738
+ dates = Employee.partition_generate_range(START_DATE, END_DATE)
739
+
740
+ Employee.create_new_partition_tables(dates)
741
+
742
+ # You should have the following tables with increments of one week:
743
+ # employees_partitions.p20101227
744
+ # employees_partitions.p20110103
745
+ # employees_partitions.p20110110
746
+ # employees_partitions.p20110117
747
+ # employees_partitions.p20110124
748
+ # employees_partitions.p20110131
749
+ # employees_partitions.p20110207
750
+ # employees_partitions.p20110214
751
+ # employees_partitions.p20110221
752
+ # employees_partitions.p20110228
753
+ # employees_partitions.p20110307
754
+ # employees_partitions.p20110314
755
+ # employees_partitions.p20110321
756
+ # employees_partitions.p20110328
757
+ # employees_partitions.p20110404
758
+ # employees_partitions.p20110411
759
+ # employees_partitions.p20110418
760
+ # employees_partitions.p20110425
761
+ # employees_partitions.p20110502
762
+ # employees_partitions.p20110509
763
+ # employees_partitions.p20110516
764
+ # employees_partitions.p20110523
765
+ # employees_partitions.p20110530
766
+ # employees_partitions.p20110606
767
+ # employees_partitions.p20110613
768
+ # employees_partitions.p20110620
769
+ # employees_partitions.p20110627
770
+ # employees_partitions.p20110704
771
+ # employees_partitions.p20110711
772
+ # employees_partitions.p20110718
773
+ # employees_partitions.p20110725
774
+ # employees_partitions.p20110801
775
+ # employees_partitions.p20110808
776
+ # employees_partitions.p20110815
777
+ # employees_partitions.p20110822
778
+ # employees_partitions.p20110829
779
+ # employees_partitions.p20110905
780
+ # employees_partitions.p20110912
781
+ # employees_partitions.p20110919
782
+ # employees_partitions.p20110926
783
+ # employees_partitions.p20111003
784
+ # employees_partitions.p20111010
785
+ # employees_partitions.p20111017
786
+ # employees_partitions.p20111024
787
+ # employees_partitions.p20111031
788
+ # employees_partitions.p20111107
789
+ # employees_partitions.p20111114
790
+ # employees_partitions.p20111121
791
+ # employees_partitions.p20111128
792
+ # employees_partitions.p20111205
793
+ # employees_partitions.p20111212
794
+ # employees_partitions.p20111219
795
+ # employees_partitions.p20111226
796
+
797
+ Award.create_new_partition_tables(dates)
798
+
799
+ # You should have the following tables with increments of one week:
800
+ # awards_partitions.p20101227
801
+ # awards_partitions.p20110103
802
+ # awards_partitions.p20110110
803
+ # awards_partitions.p20110117
804
+ # awards_partitions.p20110124
805
+ # awards_partitions.p20110131
806
+ # awards_partitions.p20110207
807
+ # awards_partitions.p20110214
808
+ # awards_partitions.p20110221
809
+ # awards_partitions.p20110228
810
+ # awards_partitions.p20110307
811
+ # awards_partitions.p20110314
812
+ # awards_partitions.p20110321
813
+ # awards_partitions.p20110328
814
+ # awards_partitions.p20110404
815
+ # awards_partitions.p20110411
816
+ # awards_partitions.p20110418
817
+ # awards_partitions.p20110425
818
+ # awards_partitions.p20110502
819
+ # awards_partitions.p20110509
820
+ # awards_partitions.p20110516
821
+ # awards_partitions.p20110523
822
+ # awards_partitions.p20110530
823
+ # awards_partitions.p20110606
824
+ # awards_partitions.p20110613
825
+ # awards_partitions.p20110620
826
+ # awards_partitions.p20110627
827
+ # awards_partitions.p20110704
828
+ # awards_partitions.p20110711
829
+ # awards_partitions.p20110718
830
+ # awards_partitions.p20110725
831
+ # awards_partitions.p20110801
832
+ # awards_partitions.p20110808
833
+ # awards_partitions.p20110815
834
+ # awards_partitions.p20110822
835
+ # awards_partitions.p20110829
836
+ # awards_partitions.p20110905
837
+ # awards_partitions.p20110912
838
+ # awards_partitions.p20110919
839
+ # awards_partitions.p20110926
840
+ # awards_partitions.p20111003
841
+ # awards_partitions.p20111010
842
+ # awards_partitions.p20111017
843
+ # awards_partitions.p20111024
844
+ # awards_partitions.p20111031
845
+ # awards_partitions.p20111107
846
+ # awards_partitions.p20111114
847
+ # awards_partitions.p20111121
848
+ # awards_partitions.p20111128
849
+ # awards_partitions.p20111205
850
+ # awards_partitions.p20111212
851
+ # awards_partitions.p20111219
852
+ # awards_partitions.p20111226
853
+
854
+ # add some companies
855
+
856
+ Company.create_many(COMPANIES)
857
+ company_ids = Company.all.map(&:id)
858
+
859
+ # now add some employees across the year.
860
+
861
+ employees = []
862
+
863
+ require File.expand_path(File.dirname(__FILE__) + "/lib/roman")
864
+
865
+ # generates data for employees_partitions and employees tables
866
+
867
+ base = 0
868
+ (1..$create_many).each do |i|
869
+ employees << {
870
+ :name => "Winston J. Sillypants, #{to_roman(base+i)}",
871
+ :created_at => START_DATE + rand(END_DATE - START_DATE) + rand(1.day.seconds).seconds,
872
+ :salary => rand(80000) + 60000,
873
+ :company_id => company_ids[rand company_ids.length]
874
+ }
875
+ end
876
+
877
+ puts "creating many employees #{$create_many}"
878
+ Employee.create_many(employees)
879
+ base += $create_many
880
+
881
+ puts "creating individual employees #{$create_individual}"
882
+ (1..$create_individual).each do |i|
883
+ employee_data = {
884
+ :name => "Jonathan Crabapple, #{to_roman(base+i)}",
885
+ :created_at => START_DATE + rand(END_DATE - START_DATE) + rand(1.day.seconds).seconds,
886
+ :salary => rand(80000) + 60000,
887
+ :company_id => company_ids[rand company_ids.length]
888
+ }
889
+ employees << Employee.create(employee_data)
890
+ end
891
+ base += $create_individual
892
+
893
+ puts "new individual employees #{$new_individual}"
894
+ (1..$new_individual).each do |i|
895
+ employee_data = {
896
+ :name => "Picholine Pimplenad, #{to_roman(base+i)}",
897
+ :created_at => START_DATE + rand(END_DATE - START_DATE) + rand(1.day.seconds).seconds,
898
+ :salary => rand(80000) + 60000,
899
+ :company_id => company_ids[rand company_ids.length]
900
+ }
901
+ employee = Employee.new(employee_data)
902
+ employee.save
903
+ employees << employee
904
+ end
905
+ base += $new_individual
906
+
907
+ updates = {}
908
+ puts "update many employees #{$update_many}"
909
+ (1..$update_many).each do |i|
910
+ index = rand(employees.length)
911
+ employee_record = employees[index]
912
+ updates[{
913
+ :id => employee_record[:id],
914
+ :created_at => employee_record[:created_at]
915
+ }] = {
916
+ :salary => 100
917
+ }
918
+ end
919
+
920
+ Employee.update_many(updates, {:set_array => '"salary = #{table_name}.salary + datatable.salary, updated_at = now()"'})
921
+
922
+ puts "update individual employees #{$update_individual}"
923
+ (1..$update_individual).each do |i|
924
+ index = rand(employees.length)
925
+ employee_record = employees[index]
926
+ employee = Employee.from_partition(employee_record[:created_at]).find(employee_record[:id])
927
+ employee.salary += 1000
928
+ employee.save
929
+ end
930
+
931
+ # generates data for awards_partitions and awards tables
932
+
933
+ awards = []
934
+
935
+ base = 0
936
+ (1..$create_many).each do |i|
937
+ employee_record = employees[rand(employees.length)]
938
+ awards << {
939
+ :award_title => "You're the best #{to_roman(base+i)}",
940
+ :awarded_on => employee_record[:created_at] + rand(1.year.seconds).seconds,
941
+ :employee_created_at => employee_record[:created_at],
942
+ :employee_id => employee_record[:id]
943
+ }
944
+ end
945
+
946
+ puts "creating many awards #{$create_many}"
947
+ Award.create_many(awards)
948
+ base += $create_many
949
+
950
+ puts "creating individual awards #{$create_individual}"
951
+ (1..$create_individual).each do |i|
952
+ employee_record = employees[rand(employees.length)]
953
+ award_data = {
954
+ :award_title => "You're the best #{to_roman(base+i)}",
955
+ :awarded_on => employee_record[:created_at] + rand(1.year.seconds).seconds,
956
+ :employee_created_at => employee_record[:created_at],
957
+ :employee_id => employee_record[:id]
958
+ }
959
+ awards << Award.create(award_data)
960
+ end
961
+ base += $create_individual
962
+
963
+ puts "new individual awards #{$new_individual}"
964
+ (1..$new_individual).each do |i|
965
+ employee_record = employees[rand(employees.length)]
966
+ award_data = {
967
+ :award_title => "You're the best #{to_roman(base+i)}",
968
+ :awarded_on => employee_record[:created_at] + rand(1.year.seconds).seconds,
969
+ :employee_created_at => employee_record[:created_at],
970
+ :employee_id => employee_record[:id]
971
+ }
972
+ award = Award.new(award_data)
973
+ award.save
974
+ awards << award
975
+ end
976
+
977
+ base = 0
978
+ updates = {}
979
+ puts "update many awards #{$update_many}"
980
+ (1..$update_many).each do |i|
981
+ award_record = awards[rand(awards.length)]
982
+ updates[{
983
+ :id => award_record[:id],
984
+ :employee_created_at => award_record[:employee_created_at]
985
+ }] = {
986
+ :award_title => "You're the greatest #{to_roman(base+i)}"
987
+ }
988
+ end
989
+
990
+ Award.update_many(updates, {:set_array => '"award_title = datatable.award_title, awarded_on = now()"'})
991
+ base += $update_many
992
+
993
+ puts "update individual #{$update_individual}"
994
+ (1..$update_individual).each do |i|
995
+ award_record = awards[rand(awards.length)]
996
+ award = Award.from_partition(award_record[:employee_created_at]).find(award_record[:id])
997
+ award.awarded_on = Date.parse(Time.now.to_s)
998
+ award.award_title = "You're the greatest #{to_roman(base+i)}"
999
+ award.save
1000
+ end