sliding_partition 0.5.0 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2bf2b1b4f2a76d444d0238530ae8eb2be574c566
4
- data.tar.gz: 97ae79b822ca5e4212d53e18e90976d78029e1aa
3
+ metadata.gz: f9916b72faca0f3568f5053076bff0f27629fc8c
4
+ data.tar.gz: dbe0c196c037d676770a56a9da7703f799f7db2a
5
5
  SHA512:
6
- metadata.gz: 6c076eee13cd511d8f74788e7462cf05ef7835ad92d943d0252d7ddb4b2d09f1cf8d3e63e118f958fa76461ed4e883873a2e56416d8e64b18fe52186e1bcf224
7
- data.tar.gz: 250ce476eed36ef530ef28ed3af7d0084ef3c2c95acfce42d7a87647caf8b623fb98ee2c7a238d668abf78fd936ea4687a1846340388b06372167559d77eb0c1
6
+ metadata.gz: 208f6b59ee67f39c3068327b2356834a845a66ec29c7ec5690b78d6a1bf10d635dfb3d7f0ebf097ea41084684d9938478db1739abf9be125cad932407d96a5e5
7
+ data.tar.gz: f61305d57f5dfc532d0c7b5b11d66c3c2c1284bc1f50c5b172513251f4a0e89c09ef59272d463a138d5c5ec8d9765ee14ddd5adbecf0f253a8cca002f94e281d
data/README.md CHANGED
@@ -16,7 +16,6 @@
16
16
  - [Setup](#setup)
17
17
  - [Usage](#usage)
18
18
  - [Rails](#rails)
19
- - [config/sliding_partitions.rb](#configsliding_partitionsrb)
20
19
  - [Tests](#tests)
21
20
  - [Versioning](#versioning)
22
21
  - [Code of Conduct](#code-of-conduct)
@@ -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
@@ -12,7 +12,7 @@ module SlidingPartition
12
12
  end
13
13
 
14
14
  def self.version
15
- "0.5.0"
15
+ "0.6.0"
16
16
  end
17
17
 
18
18
  def self.version_label
@@ -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("Migrating data") { migrate_data!(from: parent_table, to: new_table) }
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 #{parent_table} (
48
- SELECT * FROM #{retired_table} WHERE id NOT IN (SELECT id FROM #{parent_table})
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) unless partition_table_exists?(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 #{parent_table} INCLUDING ALL
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 #{parent_table} RENAME TO #{retired_table};
76
- ALTER TABLE #{new_table} RENAME TO #{parent_table};
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 #{inherited_table_name};
111
+ DROP TRIGGER IF EXISTS #{inherited_table_name}_trigger ON #{parent_table};
109
112
  CREATE TRIGGER #{inherited_table_name}_trigger
110
- BEFORE INSERT ON #{inherited_table_name}
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 measurement_insert_trigger() function!';
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 (#{inherited_table_name});
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
- parent_table + "_new"
182
+ inherited_table_name + "_new"
180
183
  end
181
184
 
182
185
  def retired_table
183
- parent_table + "_retired"
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.5.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-04-18 00:00:00.000000000 Z
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: '7.7'
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: '7.7'
102
+ version: '9.4'
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: pry
105
105
  requirement: !ruby/object:Gem::Requirement