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.
- data/Gemfile +17 -0
- data/LICENSE +30 -0
- data/PARTITIONING_EXPLAINED.txt +351 -0
- data/README +111 -0
- data/Rakefile +27 -0
- data/examples/README +23 -0
- data/examples/company_id.rb +417 -0
- data/examples/company_id_and_created_at.rb +689 -0
- data/examples/created_at.rb +590 -0
- data/examples/created_at_referencing_awards.rb +1000 -0
- data/examples/id.rb +475 -0
- data/examples/lib/by_company_id.rb +11 -0
- data/examples/lib/command_line_tool_mixin.rb +71 -0
- data/examples/lib/company.rb +29 -0
- data/examples/lib/get_options.rb +44 -0
- data/examples/lib/roman.rb +41 -0
- data/examples/start_date.rb +621 -0
- data/init.rb +1 -0
- data/lib/monkey_patch_activerecord.rb +92 -0
- data/lib/monkey_patch_postgres.rb +73 -0
- data/lib/partitioned.rb +26 -0
- data/lib/partitioned/active_record_overrides.rb +34 -0
- data/lib/partitioned/bulk_methods_mixin.rb +288 -0
- data/lib/partitioned/by_created_at.rb +13 -0
- data/lib/partitioned/by_foreign_key.rb +21 -0
- data/lib/partitioned/by_id.rb +35 -0
- data/lib/partitioned/by_integer_field.rb +32 -0
- data/lib/partitioned/by_monthly_time_field.rb +23 -0
- data/lib/partitioned/by_time_field.rb +65 -0
- data/lib/partitioned/by_weekly_time_field.rb +30 -0
- data/lib/partitioned/multi_level.rb +20 -0
- data/lib/partitioned/multi_level/configurator/data.rb +14 -0
- data/lib/partitioned/multi_level/configurator/dsl.rb +32 -0
- data/lib/partitioned/multi_level/configurator/reader.rb +162 -0
- data/lib/partitioned/multi_level/partition_manager.rb +47 -0
- data/lib/partitioned/partitioned_base.rb +354 -0
- data/lib/partitioned/partitioned_base/configurator.rb +6 -0
- data/lib/partitioned/partitioned_base/configurator/data.rb +62 -0
- data/lib/partitioned/partitioned_base/configurator/dsl.rb +628 -0
- data/lib/partitioned/partitioned_base/configurator/reader.rb +209 -0
- data/lib/partitioned/partitioned_base/partition_manager.rb +138 -0
- data/lib/partitioned/partitioned_base/sql_adapter.rb +286 -0
- data/lib/partitioned/version.rb +3 -0
- data/lib/tasks/desirable_tasks.rake +4 -0
- data/partitioned.gemspec +21 -0
- data/spec/dummy/.rspec +1 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/javascripts/application.js +9 -0
- data/spec/dummy/app/assets/stylesheets/application.css +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +51 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +32 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +30 -0
- data/spec/dummy/config/environments/production.rb +60 -0
- data/spec/dummy/config/environments/test.rb +39 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +58 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/spec/spec_helper.rb +27 -0
- data/spec/monkey_patch_posgres_spec.rb +176 -0
- data/spec/partitioned/bulk_methods_mixin_spec.rb +512 -0
- data/spec/partitioned/by_created_at_spec.rb +62 -0
- data/spec/partitioned/by_foreign_key_spec.rb +95 -0
- data/spec/partitioned/by_id_spec.rb +97 -0
- data/spec/partitioned/by_integer_field_spec.rb +143 -0
- data/spec/partitioned/by_monthly_time_field_spec.rb +100 -0
- data/spec/partitioned/by_time_field_spec.rb +182 -0
- data/spec/partitioned/by_weekly_time_field_spec.rb +100 -0
- data/spec/partitioned/multi_level/configurator/dsl_spec.rb +88 -0
- data/spec/partitioned/multi_level/configurator/reader_spec.rb +147 -0
- data/spec/partitioned/partitioned_base/configurator/dsl_spec.rb +459 -0
- data/spec/partitioned/partitioned_base/configurator/reader_spec.rb +513 -0
- data/spec/partitioned/partitioned_base/sql_adapter_spec.rb +204 -0
- data/spec/partitioned/partitioned_base_spec.rb +173 -0
- data/spec/spec_helper.rb +32 -0
- data/spec/support/shared_example_spec_helper_for_integer_key.rb +137 -0
- data/spec/support/shared_example_spec_helper_for_time_key.rb +147 -0
- data/spec/support/tables_spec_helper.rb +47 -0
- metadata +250 -0
|
@@ -0,0 +1,689 @@
|
|
|
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
|
+
# ./company_id_and_created_at.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
|
+
# Task:
|
|
52
|
+
#
|
|
53
|
+
# To increase the speed of requests to the database and to reduce the time
|
|
54
|
+
# of the request, need to split the Employees table to the partition tables.
|
|
55
|
+
# Break criterion are company(company_id) and created date(created_at).
|
|
56
|
+
#
|
|
57
|
+
# Implementation:
|
|
58
|
+
#
|
|
59
|
+
# Class Employee inherits from the abstract class MultiLevel,
|
|
60
|
+
# which supports multilevel partitioning.
|
|
61
|
+
#
|
|
62
|
+
# class Employee < Partitioned::MultiLevel
|
|
63
|
+
#
|
|
64
|
+
# Indicates a relationship to the companies table.
|
|
65
|
+
# belongs_to :company, :class_name => 'Company'
|
|
66
|
+
#
|
|
67
|
+
# Create a rules for each partition.
|
|
68
|
+
# Will be created on the partition for each company,
|
|
69
|
+
# And will be created on the partition for each company in increments of one week.
|
|
70
|
+
#
|
|
71
|
+
# partitioned do |partition|
|
|
72
|
+
# partition.using_classes ByCompanyId, Partitioned::ByCreatedAt
|
|
73
|
+
# end
|
|
74
|
+
# end
|
|
75
|
+
#
|
|
76
|
+
# Create a schema employees_partitions, within which to store all of our partitions:
|
|
77
|
+
#
|
|
78
|
+
# Employee.create_infrastructure
|
|
79
|
+
#
|
|
80
|
+
# Create a partition for each of company and each of company across the week:
|
|
81
|
+
#
|
|
82
|
+
# Where partition_key_values is array of pairs company_id and date.
|
|
83
|
+
# Employee.create_new_partition_tables(partition_key_values)
|
|
84
|
+
#
|
|
85
|
+
# Each of partition has the same structure as that of the employees table:
|
|
86
|
+
#
|
|
87
|
+
# id | created_at | updated_at | name | salary | company_id
|
|
88
|
+
# ----+------------+------------+------+--------+------------
|
|
89
|
+
#
|
|
90
|
+
# CREATE TABLE "employees_partitions"."p1" (CHECK (( company_id = 1 ))) INHERITS (employees);
|
|
91
|
+
# CREATE TABLE "employees_partitions"."p1_20101227" (CHECK (created_at >= '2010-12-27'
|
|
92
|
+
# AND created_at < '2011-01-03')) INHERITS (employees_partitions.p1);
|
|
93
|
+
# CREATE TABLE "employees_partitions"."p1_20110103" (CHECK (created_at >= '2011-01-03'
|
|
94
|
+
# AND created_at < '2011-01-10')) INHERITS (employees_partitions.p1);
|
|
95
|
+
# CREATE TABLE "employees_partitions"."p1_20110110" (CHECK (created_at >= '2011-01-10'
|
|
96
|
+
# AND created_at < '2011-01-17')) INHERITS (employees_partitions.p1);
|
|
97
|
+
# ...
|
|
98
|
+
# CREATE TABLE "employees_partitions"."p1_20111212" (CHECK (created_at >= '2011-12-12'
|
|
99
|
+
# AND created_at < '2011-12-19')) INHERITS (employees_partitions.p1);
|
|
100
|
+
# CREATE TABLE "employees_partitions"."p1_20111219" (CHECK (created_at >= '2011-12-19'
|
|
101
|
+
# AND created_at < '2011-12-26')) INHERITS (employees_partitions.p1);
|
|
102
|
+
# CREATE TABLE "employees_partitions"."p1_20111226" (CHECK (created_at >= '2011-12-26'
|
|
103
|
+
# AND created_at < '2012-01-02')) INHERITS (employees_partitions.p1);
|
|
104
|
+
#
|
|
105
|
+
# CREATE TABLE "employees_partitions"."p2" (CHECK (( company_id = 2 ))) INHERITS (employees);
|
|
106
|
+
# CREATE TABLE "employees_partitions"."p2_20101227" (CHECK (created_at >= '2010-12-27'
|
|
107
|
+
# AND created_at < '2011-01-03')) INHERITS (employees_partitions.p2);
|
|
108
|
+
# CREATE TABLE "employees_partitions"."p2_20110103" (CHECK (created_at >= '2011-01-03'
|
|
109
|
+
# AND created_at < '2011-01-10')) INHERITS (employees_partitions.p2);
|
|
110
|
+
# CREATE TABLE "employees_partitions"."p2_20110110" (CHECK (created_at >= '2011-01-10'
|
|
111
|
+
# AND created_at < '2011-01-17')) INHERITS (employees_partitions.p2);
|
|
112
|
+
# ...
|
|
113
|
+
# CREATE TABLE "employees_partitions"."p2_20111212" (CHECK (created_at >= '2011-12-12'
|
|
114
|
+
# AND created_at < '2011-12-19')) INHERITS (employees_partitions.p2);
|
|
115
|
+
# CREATE TABLE "employees_partitions"."p2_20111219" (CHECK (created_at >= '2011-12-19'
|
|
116
|
+
# AND created_at < '2011-12-26')) INHERITS (employees_partitions.p2);
|
|
117
|
+
# CREATE TABLE "employees_partitions"."p2_20111226" (CHECK (created_at >= '2011-12-26'
|
|
118
|
+
# AND created_at < '2012-01-02')) INHERITS (employees_partitions.p2);
|
|
119
|
+
#
|
|
120
|
+
# CREATE TABLE "employees_partitions"."p3" (CHECK (( company_id = 3 ))) INHERITS (employees);
|
|
121
|
+
# CREATE TABLE "employees_partitions"."p3_20101227" (CHECK (created_at >= '2010-12-27'
|
|
122
|
+
# AND created_at < '2011-01-03')) INHERITS (employees_partitions.p3);
|
|
123
|
+
# CREATE TABLE "employees_partitions"."p3_20110103" (CHECK (created_at >= '2011-01-03'
|
|
124
|
+
# AND created_at < '2011-01-10')) INHERITS (employees_partitions.p3);
|
|
125
|
+
# CREATE TABLE "employees_partitions"."p3_20110110" (CHECK (created_at >= '2011-01-10'
|
|
126
|
+
# AND created_at < '2011-01-17')) INHERITS (employees_partitions.p3);
|
|
127
|
+
# ...
|
|
128
|
+
# CREATE TABLE "employees_partitions"."p3_20111212" (CHECK (created_at >= '2011-12-12'
|
|
129
|
+
# AND created_at < '2011-12-19')) INHERITS (employees_partitions.p3);
|
|
130
|
+
# CREATE TABLE "employees_partitions"."p3_20111219" (CHECK (created_at >= '2011-12-19'
|
|
131
|
+
# AND created_at < '2011-12-26')) INHERITS (employees_partitions.p3);
|
|
132
|
+
# CREATE TABLE "employees_partitions"."p3_20111226" (CHECK (created_at >= '2011-12-26'
|
|
133
|
+
# AND created_at < '2012-01-02')) INHERITS (employees_partitions.p3);
|
|
134
|
+
#
|
|
135
|
+
# CREATE TABLE "employees_partitions"."p4" (CHECK (( company_id = 4 ))) INHERITS (employees);
|
|
136
|
+
# CREATE TABLE "employees_partitions"."p4_20101227" (CHECK (created_at >= '2010-12-27'
|
|
137
|
+
# AND created_at < '2011-01-03')) INHERITS (employees_partitions.p4);
|
|
138
|
+
# CREATE TABLE "employees_partitions"."p4_20110103" (CHECK (created_at >= '2011-01-03'
|
|
139
|
+
# AND created_at < '2011-01-10')) INHERITS (employees_partitions.p4);
|
|
140
|
+
# CREATE TABLE "employees_partitions"."p4_20110110" (CHECK (created_at >= '2011-01-10'
|
|
141
|
+
# AND created_at < '2011-01-17')) INHERITS (employees_partitions.p4);
|
|
142
|
+
# ...
|
|
143
|
+
# CREATE TABLE "employees_partitions"."p4_20111212" (CHECK (created_at >= '2011-12-12'
|
|
144
|
+
# AND created_at < '2011-12-19')) INHERITS (employees_partitions.p4);
|
|
145
|
+
# CREATE TABLE "employees_partitions"."p4_20111219" (CHECK (created_at >= '2011-12-19'
|
|
146
|
+
# AND created_at < '2011-12-26')) INHERITS (employees_partitions.p4);
|
|
147
|
+
# CREATE TABLE "employees_partitions"."p4_20111226" (CHECK (created_at >= '2011-12-26'
|
|
148
|
+
# AND created_at < '2012-01-02')) INHERITS (employees_partitions.p4);
|
|
149
|
+
#
|
|
150
|
+
# You should have the following tables:
|
|
151
|
+
# employees_partitions.p1
|
|
152
|
+
# employees_partitions.p2
|
|
153
|
+
# employees_partitions.p3
|
|
154
|
+
# employees_partitions.p4
|
|
155
|
+
# employees_partitions.p1_20101227
|
|
156
|
+
# employees_partitions.p1_20110103
|
|
157
|
+
# employees_partitions.p1_20110110
|
|
158
|
+
# employees_partitions.p1_20110117
|
|
159
|
+
# employees_partitions.p1_20110124
|
|
160
|
+
# employees_partitions.p1_20110131
|
|
161
|
+
# employees_partitions.p1_20110207
|
|
162
|
+
# employees_partitions.p1_20110214
|
|
163
|
+
# employees_partitions.p1_20110221
|
|
164
|
+
# employees_partitions.p1_20110228
|
|
165
|
+
# employees_partitions.p1_20110307
|
|
166
|
+
# employees_partitions.p1_20110314
|
|
167
|
+
# employees_partitions.p1_20110321
|
|
168
|
+
# employees_partitions.p1_20110328
|
|
169
|
+
# employees_partitions.p1_20110404
|
|
170
|
+
# employees_partitions.p1_20110411
|
|
171
|
+
# employees_partitions.p1_20110418
|
|
172
|
+
# employees_partitions.p1_20110425
|
|
173
|
+
# employees_partitions.p1_20110502
|
|
174
|
+
# employees_partitions.p1_20110509
|
|
175
|
+
# employees_partitions.p1_20110516
|
|
176
|
+
# employees_partitions.p1_20110523
|
|
177
|
+
# employees_partitions.p1_20110530
|
|
178
|
+
# employees_partitions.p1_20110606
|
|
179
|
+
# employees_partitions.p1_20110613
|
|
180
|
+
# employees_partitions.p1_20110620
|
|
181
|
+
# employees_partitions.p1_20110627
|
|
182
|
+
# employees_partitions.p1_20110704
|
|
183
|
+
# employees_partitions.p1_20110711
|
|
184
|
+
# employees_partitions.p1_20110718
|
|
185
|
+
# employees_partitions.p1_20110725
|
|
186
|
+
# employees_partitions.p1_20110801
|
|
187
|
+
# employees_partitions.p1_20110808
|
|
188
|
+
# employees_partitions.p1_20110815
|
|
189
|
+
# employees_partitions.p1_20110822
|
|
190
|
+
# employees_partitions.p1_20110829
|
|
191
|
+
# employees_partitions.p1_20110905
|
|
192
|
+
# employees_partitions.p1_20110912
|
|
193
|
+
# employees_partitions.p1_20110919
|
|
194
|
+
# employees_partitions.p1_20110926
|
|
195
|
+
# employees_partitions.p1_20111003
|
|
196
|
+
# employees_partitions.p1_20111010
|
|
197
|
+
# employees_partitions.p1_20111017
|
|
198
|
+
# employees_partitions.p1_20111024
|
|
199
|
+
# employees_partitions.p1_20111031
|
|
200
|
+
# employees_partitions.p1_20111107
|
|
201
|
+
# employees_partitions.p1_20111114
|
|
202
|
+
# employees_partitions.p1_20111121
|
|
203
|
+
# employees_partitions.p1_20111128
|
|
204
|
+
# employees_partitions.p1_20111205
|
|
205
|
+
# employees_partitions.p1_20111212
|
|
206
|
+
# employees_partitions.p1_20111219
|
|
207
|
+
# employees_partitions.p1_20111226
|
|
208
|
+
# For the next three lines the similar partitions are generated.
|
|
209
|
+
# Difference only in company_id prefix.
|
|
210
|
+
# employees_partitions.p2_20101227 - employees_partitions.p2_20111226
|
|
211
|
+
# employees_partitions.p3_20101227 - employees_partitions.p3_20111226
|
|
212
|
+
# employees_partitions.p4_20101227 - employees_partitions.p4_20111226
|
|
213
|
+
#
|
|
214
|
+
# Each of partitions inherits from employees table,
|
|
215
|
+
# thus a new row will automatically be added to the employees table .
|
|
216
|
+
#
|
|
217
|
+
# To add data, we use the following constructions,
|
|
218
|
+
# in which employees and employee_data - a random data:
|
|
219
|
+
#
|
|
220
|
+
# create_many - allows you to add multiple records
|
|
221
|
+
# Employee.create_many(employees)
|
|
222
|
+
# create - allows you to add one record
|
|
223
|
+
# Employee.create(employee_data)
|
|
224
|
+
# new/save! - allows you to add one record without using "create" method
|
|
225
|
+
# employee = Employee.new(employee_data)
|
|
226
|
+
# employee.save!
|
|
227
|
+
#
|
|
228
|
+
# For update data, we use the following constructions,
|
|
229
|
+
# in which updates - a random data:
|
|
230
|
+
#
|
|
231
|
+
# update_many - allows you to update multiple records.
|
|
232
|
+
# :set_array - additional option, you may read the description
|
|
233
|
+
# of the method in the file update_many bulk_methods_mixin.rb about this option.
|
|
234
|
+
# Employee.update_many(updates, { :set_array => '"salary = #{table_name}.salary +
|
|
235
|
+
# datatable.salary, updated_at = now()"' })
|
|
236
|
+
#
|
|
237
|
+
# This construction using for update one record. You also may use update method.
|
|
238
|
+
# employee = Employee.from_partition(employee_record[:company_id],
|
|
239
|
+
# employee_record[:created_at]).find(employee_record[:id])
|
|
240
|
+
# employee.save
|
|
241
|
+
#
|
|
242
|
+
# The data get into the employees table ONLY through partition tables.
|
|
243
|
+
# You can not do an insert row into a table employees directly.
|
|
244
|
+
# For this purpose special restrictions are imposed on the table employees.
|
|
245
|
+
#
|
|
246
|
+
# Result:
|
|
247
|
+
#
|
|
248
|
+
# We have table companies:
|
|
249
|
+
#
|
|
250
|
+
# id | created_at | updated_at | name
|
|
251
|
+
# ---+----------------------------+------------+---------------------
|
|
252
|
+
# 1 | 2012-03-11 13:26:52.184347 | | Fluent Mobile, inc.
|
|
253
|
+
# 2 | 2012-03-11 13:26:52.184347 | | Fiksu, inc.
|
|
254
|
+
# 3 | 2012-03-11 13:26:52.184347 | | AppExchanger, inc.
|
|
255
|
+
# 4 | 2012-03-11 13:26:52.184347 | | FreeMyApps, inc.
|
|
256
|
+
#
|
|
257
|
+
# Table employees with random data from 1 to 5000:
|
|
258
|
+
#
|
|
259
|
+
# id | created_at | updated_at | name | salary | company_id
|
|
260
|
+
#------+---------------------+----------------------------+-----------------------------------+-------------+------------
|
|
261
|
+
# 1 | 2011-03-06 21:06:59 | 2012-03-26 12:41:40.32776 | Winston J. Sillypants, I | $125,499.00 | 3
|
|
262
|
+
# 2 | 2011-11-19 21:03:15 | | Winston J. Sillypants, II | $84,881.00 | 2
|
|
263
|
+
# 3 | 2011-10-04 10:06:14 | 2012-03-26 12:41:40.478717 | Winston J. Sillypants, III | $124,067.00 | 3
|
|
264
|
+
# ...
|
|
265
|
+
# 4998 | 2011-09-23 06:10:23 | 2012-03-26 11:41:30.218432 | Picholine Pimplenad, MMMMCMXCVIII | $121,474.00 | 3
|
|
266
|
+
# 4999 | 2011-08-26 18:24:12 | 2012-03-26 11:41:30.222835 | Picholine Pimplenad, MMMMCMXCIX | $134,549.00 | 4
|
|
267
|
+
# 5000 | 2011-10-01 18:29:33 | 2012-03-26 12:41:40.544125 | Picholine Pimplenad, _V | $135,786.00 | 1
|
|
268
|
+
#
|
|
269
|
+
#
|
|
270
|
+
# Partition employees_partitions.p1 - partition where company_id = 1:
|
|
271
|
+
#
|
|
272
|
+
# id | created_at | updated_at | name | salary | company_id
|
|
273
|
+
#------+---------------------+----------------------------+----------------------------------+-------------+------------
|
|
274
|
+
# 5 | 2011-05-04 02:18:47 | | Winston J. Sillypants, V | $80,891.00 | 1
|
|
275
|
+
# 7 | 2011-08-08 03:59:22 | | Winston J. Sillypants, VII | $139,737.00 | 1
|
|
276
|
+
# 12 | 2011-08-15 11:06:28 | | Winston J. Sillypants, XII | $73,080.00 | 1
|
|
277
|
+
# ...
|
|
278
|
+
# 4994 | 2011-09-10 19:57:12 | 2012-03-26 11:41:30.200199 | Picholine Pimplenad, MMMMCMXCIV | $130,988.00 | 1
|
|
279
|
+
# 4997 | 2011-02-12 01:19:43 | 2012-03-26 11:41:30.213843 | Picholine Pimplenad, MMMMCMXCVII | $74,378.00 | 1
|
|
280
|
+
# 5000 | 2011-10-01 18:29:33 | 2012-03-26 12:41:40.544125 | Picholine Pimplenad, _V | $135,786.00 | 1
|
|
281
|
+
#
|
|
282
|
+
# Partition employees_partitions.p2 - partition where company_id = 2:
|
|
283
|
+
#
|
|
284
|
+
# id | created_at | updated_at | name | salary | company_id
|
|
285
|
+
#------+---------------------+----------------------------+----------------------------------+-------------+------------
|
|
286
|
+
# 2 | 2011-11-19 21:03:15 | | Winston J. Sillypants, II | $84,881.00 | 2
|
|
287
|
+
# 4 | 2011-05-10 17:00:58 | | Winston J. Sillypants, IV | $75,230.00 | 2
|
|
288
|
+
# 8 | 2011-03-30 01:00:17 | | Winston J. Sillypants, VIII | $69,076.00 | 2
|
|
289
|
+
# ...
|
|
290
|
+
# 4991 | 2011-09-07 11:18:00 | 2012-03-26 11:41:30.186616 | Picholine Pimplenad, MMMMCMXCI | $116,773.00 | 2
|
|
291
|
+
# 4992 | 2011-01-08 13:01:50 | 2012-03-26 11:41:31.962307 | Picholine Pimplenad, MMMMCMXCII | $127,687.00 | 2
|
|
292
|
+
# 4996 | 2011-10-16 03:51:57 | 2012-03-26 11:41:36.227312 | Picholine Pimplenad, MMMMCMXCVI | $74,418.00 | 2
|
|
293
|
+
#
|
|
294
|
+
# Partition employees_partitions.p3 - partition where company_id = 3:
|
|
295
|
+
#
|
|
296
|
+
# id | created_at | updated_at | name | salary | company_id
|
|
297
|
+
#------+---------------------+----------------------------+----------------------------------+-------------+------------
|
|
298
|
+
# 1 | 2011-03-06 21:06:59 | 2012-03-26 12:41:40.32776 | Winston J. Sillypants, I | $125,499.00 | 3
|
|
299
|
+
# 3 | 2011-10-04 10:06:14 | 2012-03-26 12:41:40.478717 | Winston J. Sillypants, III | $124,067.00 | 3
|
|
300
|
+
# 10 | 2011-05-29 07:14:12 | | Winston J. Sillypants, X | $63,104.00 | 3
|
|
301
|
+
# ...
|
|
302
|
+
# 4990 | 2011-02-22 06:41:15 | 2012-03-26 11:41:30.182068 | Picholine Pimplenad, MMMMCMXC | $113,361.00 | 3
|
|
303
|
+
# 4995 | 2011-12-03 05:07:19 | 2012-03-26 12:41:40.202759 | Picholine Pimplenad, MMMMCMXCV | $114,038.00 | 3
|
|
304
|
+
# 4998 | 2011-09-23 06:10:23 | 2012-03-26 11:41:30.218432 | Picholine Pimplenad, MMMMCMXCVIII| $121,474.00 | 3
|
|
305
|
+
#
|
|
306
|
+
# Partition employees_partitions.p4 - partition where company_id = 4:
|
|
307
|
+
#
|
|
308
|
+
# id | created_at | updated_at | name | salary | company_id
|
|
309
|
+
#------+---------------------+----------------------------+----------------------------------+-------------+------------
|
|
310
|
+
# 6 | 2011-08-04 08:24:38 | 2012-03-26 12:41:40.176512 | Winston J. Sillypants, VI | $77,371.00 | 4
|
|
311
|
+
# 13 | 2011-06-23 01:39:10 | | Winston J. Sillypants, XIII | $64,291.00 | 4
|
|
312
|
+
# 14 | 2011-02-23 00:58:10 | | Winston J. Sillypants, XIV | $131,059.00 | 4
|
|
313
|
+
# ...
|
|
314
|
+
# 4982 | 2011-09-19 08:05:20 | 2012-03-26 11:41:30.145857 | Picholine Pimplenad, MMMMCMLXXXII| $66,049.00 | 4
|
|
315
|
+
# 4989 | 2011-02-25 11:44:56 | 2012-03-26 11:41:30.17755 | Picholine Pimplenad, MMMMCMLXXXIX| $121,402.00 | 4
|
|
316
|
+
# 4999 | 2011-08-26 18:24:12 | 2012-03-26 11:41:30.222835 | Picholine Pimplenad, MMMMCMXCIX | $134,549.00 | 4
|
|
317
|
+
#
|
|
318
|
+
# Partition employees_partitions.p1_20101227 - partition where company_id = 1
|
|
319
|
+
# and created_at >= '2010-12-27 00:00:00' AND created_at < '2011-01-03 00:00:00':
|
|
320
|
+
#
|
|
321
|
+
# id | created_at | updated_at | name | salary | company_id
|
|
322
|
+
#------+---------------------+----------------------------+----------------------------------+-------------+------------
|
|
323
|
+
# 941 | 2011-01-01 20:26:25 | | Winston J. Sillypants, CMXLI | $89,025.00 | 1
|
|
324
|
+
# 1095 | 2011-01-02 22:35:39 | | Winston J. Sillypants, MXCV | $87,774.00 | 1
|
|
325
|
+
# 1215 | 2011-01-02 08:11:15 | | Winston J. Sillypants, MCCXV | $114,288.00 | 1
|
|
326
|
+
# 3882 | 2011-01-02 16:06:25 | 2012-03-26 11:41:24.637678 | Jonathan Crabapple, MMMDCCCLXXXII| $77,973.00 | 1
|
|
327
|
+
#
|
|
328
|
+
# Partition employees_partitions.p1_20110103 - partition where company_id = 1
|
|
329
|
+
# and created_at >= '2011-01-03 00:00:00' AND created_at < '2011-01-10 00:00:00':
|
|
330
|
+
#
|
|
331
|
+
# id | created_at | updated_at | name | salary | company_id
|
|
332
|
+
#------+---------------------+----------------------------+-------------------------------------+-------------+------------
|
|
333
|
+
# 103 | 2011-01-07 21:16:49 | | Winston J. Sillypants, CIII | $120,113.00 | 1
|
|
334
|
+
# 357 | 2011-01-04 05:38:30 | 2012-03-26 12:41:39.982053 | Winston J. Sillypants, CCCLVII | $106,232.00 | 1
|
|
335
|
+
# 659 | 2011-01-09 11:03:37 | | Winston J. Sillypants, DCLIX | $123,277.00 | 1
|
|
336
|
+
# 772 | 2011-01-07 19:38:57 | | Winston J. Sillypants, DCCLXXII | $101,343.00 | 1
|
|
337
|
+
# 954 | 2011-01-03 03:54:49 | 2012-03-26 11:41:32.366725 | Winston J. Sillypants, CMLIV | $86,807.00 | 1
|
|
338
|
+
# 1392 | 2011-01-05 05:41:35 | 2012-03-26 12:41:39.982053 | Winston J. Sillypants, MCCCXCII | $88,342.00 | 1
|
|
339
|
+
# 1531 | 2011-01-07 00:27:07 | | Winston J. Sillypants, MDXXXI | $75,970.00 | 1
|
|
340
|
+
# 1744 | 2011-01-05 00:33:42 | | Winston J. Sillypants, MDCCXLIV | $133,429.00 | 1
|
|
341
|
+
# 1848 | 2011-01-03 11:26:23 | 2012-03-26 12:41:39.982053 | Winston J. Sillypants, MDCCCXLVIII | $109,972.00 | 1
|
|
342
|
+
# 2244 | 2011-01-08 22:40:11 | 2012-03-26 12:41:39.982053 | Winston J. Sillypants, MMCCXLIV | $139,564.00 | 1
|
|
343
|
+
# 2582 | 2011-01-04 15:41:31 | | Winston J. Sillypants, MMDLXXXII | $77,274.00 | 1
|
|
344
|
+
# 2631 | 2011-01-09 05:36:52 | | Winston J. Sillypants, MMDCXXXI | $82,723.00 | 1
|
|
345
|
+
# 2695 | 2011-01-05 10:42:01 | 2012-03-26 11:41:33.452627 | Winston J. Sillypants, MMDCXCV | $122,804.00 | 1
|
|
346
|
+
# 3011 | 2011-01-06 09:39:40 | 2012-03-26 11:41:20.006112 | Jonathan Crabapple, MMMXI | $110,734.00 | 1
|
|
347
|
+
# 3012 | 2011-01-09 15:53:01 | 2012-03-26 11:41:34.50332 | Jonathan Crabapple, MMMXII | $81,894.00 | 1
|
|
348
|
+
# 3271 | 2011-01-08 07:13:09 | 2012-03-26 12:41:39.982053 | Jonathan Crabapple, MMMCCLXXI | $124,440.00 | 1
|
|
349
|
+
# 3426 | 2011-01-06 19:18:33 | 2012-03-26 11:41:22.246979 | Jonathan Crabapple, MMMCDXXVI | $133,000.00 | 1
|
|
350
|
+
# 3516 | 2011-01-09 17:37:28 | 2012-03-26 11:41:22.736326 | Jonathan Crabapple, MMMDXVI | $133,313.00 | 1
|
|
351
|
+
# 3659 | 2011-01-07 09:01:39 | 2012-03-26 11:41:23.459928 | Jonathan Crabapple, MMMDCLIX | $126,074.00 | 1
|
|
352
|
+
# 3694 | 2011-01-03 21:27:01 | 2012-03-26 11:41:23.637282 | Jonathan Crabapple, MMMDCXCIV | $77,741.00 | 1
|
|
353
|
+
# 4089 | 2011-01-04 00:44:02 | 2012-03-26 12:41:39.982053 | Picholine Pimplenad, MMMMLXXXIX | $108,649.00 | 1
|
|
354
|
+
# 4288 | 2011-01-06 19:15:58 | 2012-03-26 11:41:26.689476 | Picholine Pimplenad, MMMMCCLXXXVIII | $119,212.00 | 1
|
|
355
|
+
# 4602 | 2011-01-09 09:25:28 | 2012-03-26 11:41:28.240448 | Picholine Pimplenad, MMMMDCII | $107,141.00 | 1
|
|
356
|
+
# 4701 | 2011-01-04 17:08:40 | 2012-03-26 11:41:28.753281 | Picholine Pimplenad, MMMMDCCI | $108,088.00 | 1
|
|
357
|
+
# 4806 | 2011-01-09 15:07:05 | 2012-03-26 12:41:39.982053 | Picholine Pimplenad, MMMMDCCCVI | $102,352.00 | 1
|
|
358
|
+
#
|
|
359
|
+
# ...
|
|
360
|
+
#
|
|
361
|
+
# Partition employees_partitions.p1_20111219 - partition where company_id = 1
|
|
362
|
+
# and created_at >= '2011-12-19 00:00:00' AND created_at < '2011-12-26 00:00:00':
|
|
363
|
+
#
|
|
364
|
+
# id | created_at | updated_at | name | salary | company_id
|
|
365
|
+
#------+---------------------+----------------------------+------------------------------------+-------------+------------
|
|
366
|
+
# 426 | 2011-12-21 08:39:22 | 2012-03-26 12:41:40.484584 | Winston J. Sillypants, CDXXVI | $72,017.00 | 1
|
|
367
|
+
# 668 | 2011-12-22 10:15:37 | 2012-03-26 12:41:40.484584 | Winston J. Sillypants, DCLXVIII | $131,558.00 | 1
|
|
368
|
+
# 1230 | 2011-12-21 03:35:53 | | Winston J. Sillypants, MCCXXX | $69,077.00 | 1
|
|
369
|
+
# 1397 | 2011-12-22 22:00:53 | | Winston J. Sillypants, MCCCXCVII | $86,444.00 | 1
|
|
370
|
+
# 1481 | 2011-12-19 12:14:14 | | Winston J. Sillypants, MCDLXXXI | $119,621.00 | 1
|
|
371
|
+
# 1623 | 2011-12-22 05:27:10 | | Winston J. Sillypants, MDCXXIII | $60,037.00 | 1
|
|
372
|
+
# 1779 | 2011-12-20 23:13:24 | | Winston J. Sillypants, MDCCLXXIX | $111,578.00 | 1
|
|
373
|
+
# 2180 | 2011-12-22 01:23:11 | | Winston J. Sillypants, MMCLXXX | $73,307.00 | 1
|
|
374
|
+
# 2192 | 2011-12-19 15:01:57 | | Winston J. Sillypants, MMCXCII | $89,245.00 | 1
|
|
375
|
+
# 2330 | 2011-12-20 16:17:34 | | Winston J. Sillypants, MMCCCXXX | $81,206.00 | 1
|
|
376
|
+
# 2434 | 2011-12-19 14:37:29 | 2012-03-26 12:41:40.484584 | Winston J. Sillypants, MMCDXXXIV | $97,325.00 | 1
|
|
377
|
+
# 2899 | 2011-12-20 12:52:28 | | Winston J. Sillypants, MMDCCCXCIX | $113,013.00 | 1
|
|
378
|
+
# 3239 | 2011-12-25 04:16:59 | 2012-03-26 11:41:21.274165 | Jonathan Crabapple, MMMCCXXXIX | $113,709.00 | 1
|
|
379
|
+
# 3368 | 2011-12-25 09:06:53 | 2012-03-26 11:41:21.958145 | Jonathan Crabapple, MMMCCCLXVIII | $103,363.00 | 1
|
|
380
|
+
# 3692 | 2011-12-24 04:26:47 | 2012-03-26 11:41:37.466462 | Jonathan Crabapple, MMMDCXCII | $135,681.00 | 1
|
|
381
|
+
# 3762 | 2011-12-25 05:55:07 | 2012-03-26 11:41:24.021495 | Jonathan Crabapple, MMMDCCLXII | $99,464.00 | 1
|
|
382
|
+
# 3895 | 2011-12-19 01:27:20 | 2012-03-26 11:41:24.702233 | Jonathan Crabapple, MMMDCCCXCV | $84,213.00 | 1
|
|
383
|
+
# 4092 | 2011-12-19 12:11:48 | 2012-03-26 11:41:33.112306 | Picholine Pimplenad, MMMMXCII | $70,843.00 | 1
|
|
384
|
+
# 4104 | 2011-12-19 22:34:49 | 2012-03-26 11:41:25.726682 | Picholine Pimplenad, MMMMCIV | $61,785.00 | 1
|
|
385
|
+
# 4570 | 2011-12-19 18:19:31 | 2012-03-26 11:41:28.091344 | Picholine Pimplenad, MMMMDLXX | $110,348.00 | 1
|
|
386
|
+
# 4773 | 2011-12-21 20:13:59 | 2012-03-26 11:41:29.08274 | Picholine Pimplenad, MMMMDCCLXXIII | $132,882.00 | 1
|
|
387
|
+
# 4981 | 2011-12-22 15:54:12 | 2012-03-26 11:41:33.279564 | Picholine Pimplenad, MMMMCMLXXXI | $98,870.00 | 1
|
|
388
|
+
#
|
|
389
|
+
# Partition employees_partitions.p1_20111226 - partition where company_id = 1
|
|
390
|
+
# and created_at >= '2011-12-26 00:00:00' AND created_at < '2012-01-02 00:00:00':
|
|
391
|
+
#
|
|
392
|
+
# id | created_at | updated_at | name | salary | company_id
|
|
393
|
+
#------+---------------------+----------------------------+------------------------------------+-------------+------------
|
|
394
|
+
# 472 | 2011-12-26 02:03:03 | | Winston J. Sillypants, CDLXXII | $135,441.00 | 1
|
|
395
|
+
# 628 | 2011-12-27 07:51:26 | | Winston J. Sillypants, DCXXVIII | $110,916.00 | 1
|
|
396
|
+
# 752 | 2011-12-26 17:17:41 | | Winston J. Sillypants, DCCLII | $65,324.00 | 1
|
|
397
|
+
# 1322 | 2011-12-30 17:36:06 | | Winston J. Sillypants, MCCCXXII | $129,800.00 | 1
|
|
398
|
+
# 1551 | 2011-12-30 13:15:13 | | Winston J. Sillypants, MDLI | $121,257.00 | 1
|
|
399
|
+
# 1677 | 2011-12-27 00:19:32 | 2012-03-26 12:41:40.519792 | Winston J. Sillypants, MDCLXXVII | $91,205.00 | 1
|
|
400
|
+
# 1801 | 2011-12-26 23:24:22 | 2012-03-26 11:41:37.824209 | Winston J. Sillypants, MDCCCI | $78,008.00 | 1
|
|
401
|
+
# 1903 | 2011-12-27 15:09:50 | 2012-03-26 11:41:36.071137 | Winston J. Sillypants, MCMIII | $72,188.00 | 1
|
|
402
|
+
# 2207 | 2011-12-26 13:14:00 | 2012-03-26 11:41:32.551838 | Winston J. Sillypants, MMCCVII | $85,783.00 | 1
|
|
403
|
+
# 2443 | 2011-12-26 12:57:37 | 2012-03-26 11:41:37.584218 | Winston J. Sillypants, MMCDXLIII | $69,366.00 | 1
|
|
404
|
+
# 2719 | 2011-12-26 13:26:59 | | Winston J. Sillypants, MMDCCXIX | $115,327.00 | 1
|
|
405
|
+
# 2914 | 2011-12-28 05:10:10 | | Winston J. Sillypants, MMCMXIV | $98,958.00 | 1
|
|
406
|
+
# 3212 | 2011-12-27 04:46:51 | 2012-03-26 11:41:21.093833 | Jonathan Crabapple, MMMCCXII | $75,632.00 | 1
|
|
407
|
+
# 3667 | 2011-12-30 18:36:50 | 2012-03-26 11:41:34.97244 | Jonathan Crabapple, MMMDCLXVII | $75,279.00 | 1
|
|
408
|
+
# 3697 | 2011-12-26 03:14:06 | 2012-03-26 11:41:23.651512 | Jonathan Crabapple, MMMDCXCVII | $104,874.00 | 1
|
|
409
|
+
# 3994 | 2011-12-30 08:12:20 | 2012-03-26 11:41:25.219097 | Jonathan Crabapple, MMMCMXCIV | $72,999.00 | 1
|
|
410
|
+
# 4191 | 2011-12-30 16:10:08 | 2012-03-26 11:41:26.183611 | Picholine Pimplenad, MMMMCXCI | $84,720.00 | 1
|
|
411
|
+
# 4677 | 2011-12-28 00:49:02 | 2012-03-26 11:41:28.643459 | Picholine Pimplenad, MMMMDCLXXVII | $61,137.00 | 1
|
|
412
|
+
# 4897 | 2011-12-27 03:26:12 | 2012-03-26 11:41:32.167034 | Picholine Pimplenad, MMMMDCCCXCVII | $104,534.00 | 1
|
|
413
|
+
#
|
|
414
|
+
# A similar behavior for all other partitions where company_id = 2, 3, 4.
|
|
415
|
+
|
|
416
|
+
require File.expand_path(File.dirname(__FILE__) + "/lib/command_line_tool_mixin")
|
|
417
|
+
require File.expand_path(File.dirname(__FILE__) + "/lib/get_options")
|
|
418
|
+
|
|
419
|
+
include CommandLineToolMixin
|
|
420
|
+
|
|
421
|
+
$cleanup = false
|
|
422
|
+
$force = false
|
|
423
|
+
$create_many = 3000
|
|
424
|
+
$create_individual = 1000
|
|
425
|
+
$new_individual = 1000
|
|
426
|
+
$update_many = 1000
|
|
427
|
+
$update_individual = 1000
|
|
428
|
+
|
|
429
|
+
@options = {
|
|
430
|
+
"--cleanup" => {
|
|
431
|
+
:short => "-C",
|
|
432
|
+
:argument => GetoptLong::NO_ARGUMENT,
|
|
433
|
+
:note => "cleanup data in database and exit"
|
|
434
|
+
},
|
|
435
|
+
"--force" => {
|
|
436
|
+
:short => "-F",
|
|
437
|
+
:argument => GetoptLong::NO_ARGUMENT,
|
|
438
|
+
:note => "cleanup data in database before creating new data"
|
|
439
|
+
},
|
|
440
|
+
"--create-many" => {
|
|
441
|
+
:short => "-m",
|
|
442
|
+
:argument => GetoptLong::REQUIRED_ARGUMENT,
|
|
443
|
+
:note => "how many objects to create via create_many",
|
|
444
|
+
:argument_note => "NUMBER"
|
|
445
|
+
},
|
|
446
|
+
"--create-individual" => {
|
|
447
|
+
:short => "-i",
|
|
448
|
+
:argument => GetoptLong::REQUIRED_ARGUMENT,
|
|
449
|
+
:note => "how many objects to create via create",
|
|
450
|
+
:argument_note => "NUMBER"
|
|
451
|
+
},
|
|
452
|
+
"--new-individual" => {
|
|
453
|
+
:short => "-I",
|
|
454
|
+
:argument => GetoptLong::REQUIRED_ARGUMENT,
|
|
455
|
+
:note => "how many objects to create via new/save",
|
|
456
|
+
:argument_note => "NUMBER"
|
|
457
|
+
},
|
|
458
|
+
"--update-individual" => {
|
|
459
|
+
:short => "-u",
|
|
460
|
+
:argument => GetoptLong::REQUIRED_ARGUMENT,
|
|
461
|
+
:note => "how many objects to update individually",
|
|
462
|
+
:argument_note => "NUMBER"
|
|
463
|
+
},
|
|
464
|
+
"--update-many" => {
|
|
465
|
+
:short => "-U",
|
|
466
|
+
:argument => GetoptLong::REQUIRED_ARGUMENT,
|
|
467
|
+
:note => "how many objects to update via update_many",
|
|
468
|
+
:argument_note => "NUMBER"
|
|
469
|
+
},
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
command_line_options(@options) do |option,argument|
|
|
473
|
+
if option == '--cleanup'
|
|
474
|
+
$cleanup = true
|
|
475
|
+
elsif option == '--force'
|
|
476
|
+
$force = true
|
|
477
|
+
elsif option == '--create-many'
|
|
478
|
+
$create_many = argument.to_i
|
|
479
|
+
elsif option == '--create-individual'
|
|
480
|
+
$create_individual = argument.to_i
|
|
481
|
+
elsif option == '--new-individual'
|
|
482
|
+
$new_individual = argument.to_i
|
|
483
|
+
elsif option == '--update-individual'
|
|
484
|
+
$update_individual = argument.to_i
|
|
485
|
+
elsif option == '--update-many'
|
|
486
|
+
$update_many = argument.to_i
|
|
487
|
+
end
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
if $cleanup || $force
|
|
491
|
+
ActiveRecord::Base.connection.drop_schema("employees_partitions", :cascade => true) rescue nil
|
|
492
|
+
ActiveRecord::Base.connection.drop_table("employees") rescue nil
|
|
493
|
+
ActiveRecord::Base.connection.drop_table("companies") rescue nil
|
|
494
|
+
exit(0) if $cleanup
|
|
495
|
+
end
|
|
496
|
+
|
|
497
|
+
$total_records = $create_many + $create_individual + $new_individual
|
|
498
|
+
|
|
499
|
+
puts "total records: #{$total_records}"
|
|
500
|
+
|
|
501
|
+
START_DATE = Date.parse('2011-01-01')
|
|
502
|
+
END_DATE = Date.parse('2011-12-31')
|
|
503
|
+
|
|
504
|
+
# the ActiveRecord classes
|
|
505
|
+
require File.expand_path(File.dirname(__FILE__) + "/lib/company")
|
|
506
|
+
require File.expand_path(File.dirname(__FILE__) + "/lib/by_company_id")
|
|
507
|
+
|
|
508
|
+
class Employee < Partitioned::MultiLevel
|
|
509
|
+
belongs_to :company, :class_name => 'Company'
|
|
510
|
+
attr_accessible :created_at, :salary, :company_id, :name
|
|
511
|
+
|
|
512
|
+
partitioned do |partition|
|
|
513
|
+
partition.using_classes ByCompanyId, Partitioned::ByCreatedAt
|
|
514
|
+
end
|
|
515
|
+
|
|
516
|
+
connection.execute <<-SQL
|
|
517
|
+
create table employees
|
|
518
|
+
(
|
|
519
|
+
id serial not null primary key,
|
|
520
|
+
created_at timestamp not null default now(),
|
|
521
|
+
updated_at timestamp,
|
|
522
|
+
name text not null,
|
|
523
|
+
salary money not null,
|
|
524
|
+
company_id integer not null
|
|
525
|
+
);
|
|
526
|
+
SQL
|
|
527
|
+
end
|
|
528
|
+
|
|
529
|
+
# You should have the following tables:
|
|
530
|
+
# public.companies
|
|
531
|
+
# public.employees
|
|
532
|
+
|
|
533
|
+
# add some companies
|
|
534
|
+
|
|
535
|
+
Company.create_many(COMPANIES)
|
|
536
|
+
company_ids = Company.all.map(&:id)
|
|
537
|
+
dates = Partitioned::ByCreatedAt.partition_generate_range(START_DATE, END_DATE)
|
|
538
|
+
|
|
539
|
+
partition_key_values = []
|
|
540
|
+
company_ids.each do |company_id|
|
|
541
|
+
partition_key_values << company_id
|
|
542
|
+
dates.each do |date|
|
|
543
|
+
partition_key_values << [company_id, date]
|
|
544
|
+
end
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
# create the infrastructure for EMPLOYEES table which includes the schema
|
|
548
|
+
Employee.create_infrastructure
|
|
549
|
+
|
|
550
|
+
# You should have the following schema:
|
|
551
|
+
# employees_partitions
|
|
552
|
+
|
|
553
|
+
# create the employees partition tables
|
|
554
|
+
Employee.create_new_partition_tables(partition_key_values)
|
|
555
|
+
|
|
556
|
+
# You should have the following tables:
|
|
557
|
+
# employees_partitions.p1
|
|
558
|
+
# employees_partitions.p2
|
|
559
|
+
# employees_partitions.p3
|
|
560
|
+
# employees_partitions.p4
|
|
561
|
+
# employees_partitions.p1_20101227
|
|
562
|
+
# employees_partitions.p1_20110103
|
|
563
|
+
# employees_partitions.p1_20110110
|
|
564
|
+
# employees_partitions.p1_20110117
|
|
565
|
+
# employees_partitions.p1_20110124
|
|
566
|
+
# employees_partitions.p1_20110131
|
|
567
|
+
# employees_partitions.p1_20110207
|
|
568
|
+
# employees_partitions.p1_20110214
|
|
569
|
+
# employees_partitions.p1_20110221
|
|
570
|
+
# employees_partitions.p1_20110228
|
|
571
|
+
# employees_partitions.p1_20110307
|
|
572
|
+
# employees_partitions.p1_20110314
|
|
573
|
+
# employees_partitions.p1_20110321
|
|
574
|
+
# employees_partitions.p1_20110328
|
|
575
|
+
# employees_partitions.p1_20110404
|
|
576
|
+
# employees_partitions.p1_20110411
|
|
577
|
+
# employees_partitions.p1_20110418
|
|
578
|
+
# employees_partitions.p1_20110425
|
|
579
|
+
# employees_partitions.p1_20110502
|
|
580
|
+
# employees_partitions.p1_20110509
|
|
581
|
+
# employees_partitions.p1_20110516
|
|
582
|
+
# employees_partitions.p1_20110523
|
|
583
|
+
# employees_partitions.p1_20110530
|
|
584
|
+
# employees_partitions.p1_20110606
|
|
585
|
+
# employees_partitions.p1_20110613
|
|
586
|
+
# employees_partitions.p1_20110620
|
|
587
|
+
# employees_partitions.p1_20110627
|
|
588
|
+
# employees_partitions.p1_20110704
|
|
589
|
+
# employees_partitions.p1_20110711
|
|
590
|
+
# employees_partitions.p1_20110718
|
|
591
|
+
# employees_partitions.p1_20110725
|
|
592
|
+
# employees_partitions.p1_20110801
|
|
593
|
+
# employees_partitions.p1_20110808
|
|
594
|
+
# employees_partitions.p1_20110815
|
|
595
|
+
# employees_partitions.p1_20110822
|
|
596
|
+
# employees_partitions.p1_20110829
|
|
597
|
+
# employees_partitions.p1_20110905
|
|
598
|
+
# employees_partitions.p1_20110912
|
|
599
|
+
# employees_partitions.p1_20110919
|
|
600
|
+
# employees_partitions.p1_20110926
|
|
601
|
+
# employees_partitions.p1_20111003
|
|
602
|
+
# employees_partitions.p1_20111010
|
|
603
|
+
# employees_partitions.p1_20111017
|
|
604
|
+
# employees_partitions.p1_20111024
|
|
605
|
+
# employees_partitions.p1_20111031
|
|
606
|
+
# employees_partitions.p1_20111107
|
|
607
|
+
# employees_partitions.p1_20111114
|
|
608
|
+
# employees_partitions.p1_20111121
|
|
609
|
+
# employees_partitions.p1_20111128
|
|
610
|
+
# employees_partitions.p1_20111205
|
|
611
|
+
# employees_partitions.p1_20111212
|
|
612
|
+
# employees_partitions.p1_20111219
|
|
613
|
+
# employees_partitions.p1_20111226
|
|
614
|
+
# For the next three lines the similar partitions are generated.
|
|
615
|
+
# Difference only in company_id prefix.
|
|
616
|
+
# employees_partitions.p2_20101227 - employees_partitions.p2_20111226
|
|
617
|
+
# employees_partitions.p3_20101227 - employees_partitions.p3_20111226
|
|
618
|
+
# employees_partitions.p4_20101227 - employees_partitions.p4_20111226
|
|
619
|
+
|
|
620
|
+
employees = []
|
|
621
|
+
|
|
622
|
+
require File.expand_path(File.dirname(__FILE__) + "/lib/roman")
|
|
623
|
+
|
|
624
|
+
# generates data for employees_partitions and employees tables
|
|
625
|
+
|
|
626
|
+
|
|
627
|
+
base = 0
|
|
628
|
+
(1..$create_many).each do |i|
|
|
629
|
+
employees << {
|
|
630
|
+
:name => "Winston J. Sillypants, #{to_roman(base+i)}",
|
|
631
|
+
:created_at => START_DATE + rand(END_DATE - START_DATE) + rand(1.day.seconds).seconds,
|
|
632
|
+
:salary => rand(80000) + 60000,
|
|
633
|
+
:company_id => company_ids[rand company_ids.length]
|
|
634
|
+
}
|
|
635
|
+
end
|
|
636
|
+
|
|
637
|
+
puts "creating many #{$create_many}"
|
|
638
|
+
Employee.create_many(employees)
|
|
639
|
+
base += $create_many
|
|
640
|
+
|
|
641
|
+
puts "creating individual #{$create_individual}"
|
|
642
|
+
(1..$create_individual).each do |i|
|
|
643
|
+
employee_data = {
|
|
644
|
+
:name => "Jonathan Crabapple, #{to_roman(base+i)}",
|
|
645
|
+
:created_at => START_DATE + rand(END_DATE - START_DATE) + rand(1.day.seconds).seconds,
|
|
646
|
+
:salary => rand(80000) + 60000,
|
|
647
|
+
:company_id => company_ids[rand company_ids.length]
|
|
648
|
+
}
|
|
649
|
+
employees << Employee.create(employee_data)
|
|
650
|
+
end
|
|
651
|
+
base += $create_individual
|
|
652
|
+
|
|
653
|
+
puts "new individual #{$new_individual}"
|
|
654
|
+
(1..$new_individual).each do |i|
|
|
655
|
+
employee_data = {
|
|
656
|
+
:name => "Picholine Pimplenad, #{to_roman(base+i)}",
|
|
657
|
+
:created_at => START_DATE + rand(END_DATE - START_DATE) + rand(1.day.seconds).seconds,
|
|
658
|
+
:salary => rand(80000) + 60000,
|
|
659
|
+
:company_id => company_ids[rand company_ids.length]
|
|
660
|
+
}
|
|
661
|
+
employee = Employee.new(employee_data)
|
|
662
|
+
employee.save!
|
|
663
|
+
employees << employee
|
|
664
|
+
end
|
|
665
|
+
base += $new_individual
|
|
666
|
+
|
|
667
|
+
updates = {}
|
|
668
|
+
puts "update many #{$update_many}"
|
|
669
|
+
(1..$update_many).each do |i|
|
|
670
|
+
employee_record = employees[rand(employees.length)]
|
|
671
|
+
updates[{
|
|
672
|
+
:id => employee_record[:id],
|
|
673
|
+
:company_id => employee_record[:company_id],
|
|
674
|
+
:created_at => employee_record[:created_at]
|
|
675
|
+
}] = {
|
|
676
|
+
:salary => 100
|
|
677
|
+
}
|
|
678
|
+
end
|
|
679
|
+
|
|
680
|
+
Employee.update_many(updates, {:set_array => '"salary = #{table_name}.salary + datatable.salary, updated_at = now()"'})
|
|
681
|
+
|
|
682
|
+
puts "update individual #{$update_individual}"
|
|
683
|
+
(1..$update_individual).each do |i|
|
|
684
|
+
employee_record = employees[rand(employees.length)]
|
|
685
|
+
employee = Employee.from_partition(employee_record[:company_id], employee_record[:created_at]).find(employee_record[:id])
|
|
686
|
+
employee.salary += 1000
|
|
687
|
+
employee.save
|
|
688
|
+
end
|
|
689
|
+
|