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 +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +1 -0
- data/lib/pgslice.rb +42 -25
- data/lib/pgslice/version.rb +1 -1
- data/pgslice.gemspec +0 -1
- metadata +2 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 02a6779afe17f04bc97713d360ac5bbdcdbd68e0
|
4
|
+
data.tar.gz: e3aac8b61fed50af76c9bc7b4835b6f218a474d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dde7c9b7330f0edf78297d4a3a61e1d8d253c893f14fbe808083e3d55253fa4e4ffc3351f12ee34053c5fe4c999066b2eb7cdd8f801f594013ea7a477f1985db
|
7
|
+
data.tar.gz: 6a578d1b2663fe77eedb21dfbf121284ed4a6ed026f4cfc4c4c60b911d6513168b4072bf30ab3fb3cb519227903e57c9bb11aa34400e1e9abb870e527d8a62d7
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
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
|
-
|
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
|
-
|
126
|
-
|
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
|
-
|
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
|
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
|
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.
|
168
|
-
ending_time = DateTime.strptime(existing_tables.last.
|
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
|
data/lib/pgslice/version.rb
CHANGED
data/pgslice.gemspec
CHANGED
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
|
+
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-
|
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
|