pgslice 0.1.4 → 0.1.5

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: 63369b304364c5cbc5b6b96e9303d3f46293d0b3
4
- data.tar.gz: 15e76add4582111650e46daad02bbe755b3e45e1
3
+ metadata.gz: 02a6779afe17f04bc97713d360ac5bbdcdbd68e0
4
+ data.tar.gz: e3aac8b61fed50af76c9bc7b4835b6f218a474d1
5
5
  SHA512:
6
- metadata.gz: 64d23629b73f8adf89ce6adc03c2cc951b9b67e7edc8e03eb596546e43b40f1415ea494ccdcab4efb460686ff967941ddf4ba66491525d3b4612490b017b4a0f
7
- data.tar.gz: 864e600e79f944667c11a151a23341366085b39d1cf81b10f1ef03408c9d6101690ad3697d36369f0151353baa90d7f7d0429b0507c6d738c33396ffb3cefa47
6
+ metadata.gz: dde7c9b7330f0edf78297d4a3a61e1d8d253c893f14fbe808083e3d55253fa4e4ffc3351f12ee34053c5fe4c999066b2eb7cdd8f801f594013ea7a477f1985db
7
+ data.tar.gz: 6a578d1b2663fe77eedb21dfbf121284ed4a6ed026f4cfc4c4c60b911d6513168b4072bf30ab3fb3cb519227903e57c9bb11aa34400e1e9abb870e527d8a62d7
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.1.5
2
+
3
+ - Removed `activesupport` dependency for speed
4
+ - Fixed `fill` for months
5
+
1
6
  ## 0.1.4
2
7
 
3
8
  - Added sequence ownership
data/README.md CHANGED
@@ -205,6 +205,7 @@ gem specific_install ankane/pgslice
205
205
 
206
206
  - Command to sync index changes with partitions
207
207
  - Disable indexing for faster `fill`
208
+ - ETA for `fill`
208
209
 
209
210
  ## Contributing
210
211
 
data/lib/pgslice.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require "pgslice/version"
2
2
  require "slop"
3
3
  require "pg"
4
- require "active_support/all"
5
4
 
6
5
  module PgSlice
7
6
  class Error < StandardError; end
@@ -117,21 +116,22 @@ CREATE TRIGGER #{trigger_name}
117
116
  # ensure table has trigger
118
117
  abort "No trigger on table: #{table}\nDid you mean to use --intermediate?" unless has_trigger?(trigger_name, table)
119
118
 
120
- period, field, name_format, inc, today = settings_from_table(original_table, table)
119
+ index_defs = execute("select pg_get_indexdef(indexrelid) from pg_index where indrelid = $1::regclass AND indisprimary = 'f'", [original_table]).map { |r| r["pg_get_indexdef"] }
120
+ primary_key = self.primary_key(table)
121
121
 
122
- days = range.map { |n| today + (n * inc) }
123
122
  queries = []
124
123
 
125
- index_defs = execute("select pg_get_indexdef(indexrelid) from pg_index where indrelid = $1::regclass AND indisprimary = 'f'", [original_table]).map { |r| r["pg_get_indexdef"] }
126
- primary_key = self.primary_key(table)
124
+ period, field = settings_from_table(original_table, table)
125
+ today = round_date(Date.today, period)
126
+ range.each do |n|
127
+ day = advance_date(today, period, n)
127
128
 
128
- days.each do |day|
129
- partition_name = "#{original_table}_#{day.strftime(name_format)}"
129
+ partition_name = "#{original_table}_#{day.strftime(name_format(period))}"
130
130
  next if table_exists?(partition_name)
131
131
 
132
132
  queries << <<-SQL
133
133
  CREATE TABLE #{partition_name}
134
- (CHECK (#{field} >= #{sql_date(day)} AND #{field} < #{sql_date(day + inc)}))
134
+ (CHECK (#{field} >= #{sql_date(day)} AND #{field} < #{sql_date(advance_date(day, period, 1))}))
135
135
  INHERITS (#{table});
136
136
  SQL
137
137
 
@@ -161,11 +161,12 @@ CREATE TABLE #{partition_name}
161
161
  abort "Table not found: #{source_table}" unless table_exists?(source_table)
162
162
  abort "Table not found: #{dest_table}" unless table_exists?(dest_table)
163
163
 
164
- period, field, name_format, inc, today = settings_from_table(table, dest_table)
164
+ period, field = settings_from_table(table, dest_table)
165
+ name_format = self.name_format(period)
165
166
 
166
167
  existing_tables = self.existing_tables(like: "#{table}_%").select { |t| /#{Regexp.escape("#{table}_")}(\d{4,6})/.match(t) }.sort
167
- starting_time = DateTime.strptime(existing_tables.first.last(8), name_format)
168
- ending_time = DateTime.strptime(existing_tables.last.last(8), name_format) + inc
168
+ starting_time = DateTime.strptime(existing_tables.first.split("_").last, name_format)
169
+ ending_time = advance_date(DateTime.strptime(existing_tables.last.split("_").last, name_format), period, 1)
169
170
 
170
171
  primary_key = self.primary_key(table)
171
172
  max_source_id = max_id(source_table, primary_key)
@@ -412,6 +413,35 @@ INSERT INTO #{dest_table} (#{fields})
412
413
  "'#{time.strftime("%Y-%m-%d")}'::date"
413
414
  end
414
415
 
416
+ def name_format(period)
417
+ case period.to_sym
418
+ when :day
419
+ "%Y%m%d"
420
+ else
421
+ "%Y%m"
422
+ end
423
+ end
424
+
425
+ def round_date(date, period)
426
+ date = date.to_date
427
+ case period.to_sym
428
+ when :day
429
+ date
430
+ else
431
+ Date.new(date.year, date.month)
432
+ end
433
+ end
434
+
435
+ def advance_date(date, period, count = 1)
436
+ date = date.to_date
437
+ case period.to_sym
438
+ when :day
439
+ date.next_day(count)
440
+ else
441
+ date.next_month(count)
442
+ end
443
+ end
444
+
415
445
  def settings_from_table(original_table, table)
416
446
  trigger_name = self.trigger_name(original_table)
417
447
  function_def = execute("select pg_get_functiondef(oid) from pg_proc where proname = $1", [trigger_name])[0]["pg_get_functiondef"]
@@ -419,20 +449,7 @@ INSERT INTO #{dest_table} (#{fields})
419
449
  abort "Could not read settings" unless sql_format
420
450
  period = sql_format[0]
421
451
  field = /to_char\(NEW\.(\w+),/.match(function_def)[1]
422
-
423
- today = Time.now
424
- case period
425
- when :day
426
- name_format = "%Y%m%d"
427
- inc = 1.day
428
- today = today.beginning_of_day
429
- else
430
- name_format = "%Y%m"
431
- inc = 1.month
432
- today = today.beginning_of_month
433
- end
434
-
435
- [period, field, name_format, inc, today]
452
+ [period, field]
436
453
  end
437
454
  end
438
455
  end
@@ -1,3 +1,3 @@
1
1
  module PgSlice
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
data/pgslice.gemspec CHANGED
@@ -19,7 +19,6 @@ Gem::Specification.new do |spec|
19
19
 
20
20
  spec.add_dependency "slop", ">= 4.2.0"
21
21
  spec.add_dependency "pg"
22
- spec.add_dependency "activesupport"
23
22
 
24
23
  spec.add_development_dependency "bundler"
25
24
  spec.add_development_dependency "rake"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgslice
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-25 00:00:00.000000000 Z
11
+ date: 2016-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slop
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: activesupport
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: bundler
57
43
  requirement: !ruby/object:Gem::Requirement