sliding_partition 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9916b72faca0f3568f5053076bff0f27629fc8c
|
4
|
+
data.tar.gz: dbe0c196c037d676770a56a9da7703f799f7db2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 208f6b59ee67f39c3068327b2356834a845a66ec29c7ec5690b78d6a1bf10d635dfb3d7f0ebf097ea41084684d9938478db1739abf9be125cad932407d96a5e5
|
7
|
+
data.tar.gz: f61305d57f5dfc532d0c7b5b11d66c3c2c1284bc1f50c5b172513251f4a0e89c09ef59272d463a138d5c5ec8d9765ee14ddd5adbecf0f253a8cca002f94e281d
|
data/README.md
CHANGED
@@ -8,10 +8,12 @@ module SlidingPartition
|
|
8
8
|
attr_reader :model
|
9
9
|
|
10
10
|
attr_accessor :inherited_table_name, :time_column, :suffix,
|
11
|
-
:partition_interval, :retention_interval
|
11
|
+
:partition_interval, :retention_interval,
|
12
|
+
:lead_time
|
12
13
|
|
13
14
|
def initialize(model)
|
14
15
|
@model = model
|
16
|
+
set_defaults
|
15
17
|
yield self if block_given?
|
16
18
|
end
|
17
19
|
|
@@ -39,5 +41,11 @@ module SlidingPartition
|
|
39
41
|
@inherited_table_name ||= model.table_name
|
40
42
|
end
|
41
43
|
|
44
|
+
private
|
45
|
+
|
46
|
+
def set_defaults
|
47
|
+
@lead_time = 0
|
48
|
+
end
|
49
|
+
|
42
50
|
end
|
43
51
|
end
|
@@ -35,24 +35,27 @@ module SlidingPartition
|
|
35
35
|
|
36
36
|
def migrate!
|
37
37
|
connection.transaction do
|
38
|
+
@new_table_name = new_table
|
38
39
|
say_with_time("Cloning table") { clone_new_table! }
|
39
40
|
say_with_time("Creating #{partitions.tables.size} partition tables") { create_tables! }
|
40
|
-
say_with_time("
|
41
|
+
say_with_time("Updating trigger function") { update_trigger_function! }
|
42
|
+
say_with_time("Creating trigger") { create_trigger! }
|
43
|
+
say_with_time("Migrating data") { migrate_data!(from: inherited_table_name, to: new_table) }
|
41
44
|
say_with_time("Swapping retired & new tables") { swap_tables! }
|
42
45
|
end
|
43
46
|
end
|
44
47
|
|
45
48
|
def final_copy!
|
46
49
|
connection.execute(<<-SQL)
|
47
|
-
INSERT INTO #{
|
48
|
-
SELECT * FROM #{retired_table} WHERE id NOT IN (SELECT id FROM #{
|
50
|
+
INSERT INTO #{inherited_table_name} (
|
51
|
+
SELECT * FROM #{retired_table} WHERE id NOT IN (SELECT id FROM #{inherited_table_name})
|
49
52
|
)
|
50
53
|
SQL
|
51
54
|
end
|
52
55
|
|
53
56
|
def create_tables!
|
54
57
|
partitions.each do |partition|
|
55
|
-
create_partition_table(partition)
|
58
|
+
create_partition_table(partition)
|
56
59
|
end
|
57
60
|
end
|
58
61
|
|
@@ -65,18 +68,18 @@ module SlidingPartition
|
|
65
68
|
def clone_new_table!
|
66
69
|
connection.execute <<-SQL
|
67
70
|
CREATE TABLE #{new_table} (
|
68
|
-
LIKE #{
|
71
|
+
LIKE #{inherited_table_name} INCLUDING ALL
|
69
72
|
);
|
70
73
|
SQL
|
71
74
|
end
|
72
75
|
|
73
76
|
def swap_tables!
|
74
77
|
connection.execute <<-SQL
|
75
|
-
ALTER TABLE #{
|
76
|
-
ALTER TABLE #{new_table} RENAME TO #{
|
78
|
+
ALTER TABLE #{inherited_table_name} RENAME TO #{retired_table};
|
79
|
+
ALTER TABLE #{new_table} RENAME TO #{inherited_table_name};
|
77
80
|
SQL
|
78
|
-
update_trigger_function!
|
79
|
-
create_trigger!
|
81
|
+
# update_trigger_function!
|
82
|
+
# create_trigger!
|
80
83
|
end
|
81
84
|
|
82
85
|
def migrate_data!(from:, to:)
|
@@ -105,9 +108,9 @@ module SlidingPartition
|
|
105
108
|
|
106
109
|
def create_trigger!
|
107
110
|
connection.execute(<<-SQL)
|
108
|
-
DROP TRIGGER IF EXISTS #{inherited_table_name}_trigger ON #{
|
111
|
+
DROP TRIGGER IF EXISTS #{inherited_table_name}_trigger ON #{parent_table};
|
109
112
|
CREATE TRIGGER #{inherited_table_name}_trigger
|
110
|
-
BEFORE INSERT ON #{
|
113
|
+
BEFORE INSERT ON #{parent_table}
|
111
114
|
FOR EACH ROW EXECUTE PROCEDURE #{inherited_table_name}_insert_trigger();
|
112
115
|
SQL
|
113
116
|
|
@@ -130,7 +133,7 @@ module SlidingPartition
|
|
130
133
|
ELSIF (NEW.#{time_column} < TIMESTAMP '#{partitions.first_partition_timestamp.to_s(:db)}')
|
131
134
|
THEN RETURN NULL; -- Just discard pre-historic rows
|
132
135
|
ELSE
|
133
|
-
RAISE EXCEPTION 'Date out of range. Fix the
|
136
|
+
RAISE EXCEPTION 'Date out of range. Fix the #{inherited_table_name}_insert_trigger() function!';
|
134
137
|
END IF;
|
135
138
|
RETURN NULL;
|
136
139
|
END;
|
@@ -143,7 +146,7 @@ module SlidingPartition
|
|
143
146
|
<<-SQL
|
144
147
|
CREATE TABLE #{partition.table_name} (
|
145
148
|
LIKE #{inherited_table_name} INCLUDING ALL
|
146
|
-
) INHERITS (#{
|
149
|
+
) INHERITS (#{parent_table});
|
147
150
|
|
148
151
|
ALTER TABLE #{partition.table_name}
|
149
152
|
ADD CHECK (
|
@@ -176,11 +179,11 @@ module SlidingPartition
|
|
176
179
|
end
|
177
180
|
|
178
181
|
def new_table
|
179
|
-
|
182
|
+
inherited_table_name + "_new"
|
180
183
|
end
|
181
184
|
|
182
185
|
def retired_table
|
183
|
-
|
186
|
+
inherited_table_name + "_retired"
|
184
187
|
end
|
185
188
|
|
186
189
|
end
|
@@ -9,7 +9,7 @@ module SlidingPartition
|
|
9
9
|
attr_reader :definition, :time
|
10
10
|
|
11
11
|
delegate %i[ inherited_table_name time_column suffix
|
12
|
-
partition_interval retention_interval ] => :definition
|
12
|
+
partition_interval retention_interval lead_time ] => :definition
|
13
13
|
|
14
14
|
def initialize(definition:, at_time:)
|
15
15
|
@definition, @time = definition, at_time
|
@@ -36,7 +36,7 @@ module SlidingPartition
|
|
36
36
|
end
|
37
37
|
|
38
38
|
def last_timestamp
|
39
|
-
time + partition_interval
|
39
|
+
time + lead_time + partition_interval
|
40
40
|
end
|
41
41
|
|
42
42
|
def first_partition_timestamp
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sliding_partition
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Sadauskas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-05-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -92,14 +92,14 @@ dependencies:
|
|
92
92
|
requirements:
|
93
93
|
- - "~>"
|
94
94
|
- !ruby/object:Gem::Version
|
95
|
-
version: '
|
95
|
+
version: '9.4'
|
96
96
|
type: :development
|
97
97
|
prerelease: false
|
98
98
|
version_requirements: !ruby/object:Gem::Requirement
|
99
99
|
requirements:
|
100
100
|
- - "~>"
|
101
101
|
- !ruby/object:Gem::Version
|
102
|
-
version: '
|
102
|
+
version: '9.4'
|
103
103
|
- !ruby/object:Gem::Dependency
|
104
104
|
name: pry
|
105
105
|
requirement: !ruby/object:Gem::Requirement
|