Class: PgSlice::Table
Instance Attribute Summary
Attributes inherited from GenericTable
#table
Instance Method Summary
collapse
#columns, #exists?, #foreign_keys, #index_defs, #initialize, #primary_key, #sequences, #to_s
Instance Method Details
#column_cast(column) ⇒ Object
15
16
17
18
|
# File 'lib/pgslice/table.rb', line 15
def column_cast(column)
data_type = execute("SELECT data_type FROM information_schema.columns WHERE table_schema || '.' || table_name = $1 AND column_name = $2", [table, column])[0]["data_type"]
data_type == "timestamp with time zone" ? "timestamptz" : "date"
end
|
#existing_partitions(period = nil) ⇒ Object
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/pgslice/table.rb', line 38
def existing_partitions(period = nil)
count =
case period
when "day"
8
when "month"
6
when "year"
4
else
"6,8"
end
existing_tables(like: "#{table}_%").select { |t| /\A#{Regexp.escape("#{table}_")}\d{#{count}}\z/.match(t) }
end
|
54
55
56
|
# File 'lib/pgslice/table.rb', line 54
def
execute("SELECT obj_description(#{regclass(table)}) AS comment")[0]
end
|
#fetch_trigger(trigger_name) ⇒ Object
58
59
60
|
# File 'lib/pgslice/table.rb', line 58
def fetch_trigger(trigger_name)
execute("SELECT obj_description(oid, 'pg_trigger') AS comment FROM pg_trigger WHERE tgname = $1 AND tgrelid = #{regclass(table)}", [trigger_name])[0]
end
|
3
4
5
|
# File 'lib/pgslice/table.rb', line 3
def intermediate_table
self.class.new("#{table}_intermediate")
end
|
#max_id(primary_key, below: nil, where: nil) ⇒ Object
20
21
22
23
24
25
26
27
|
# File 'lib/pgslice/table.rb', line 20
def max_id(primary_key, below: nil, where: nil)
query = "SELECT MAX(#{quote_ident(primary_key)}) FROM #{quote_table(table)}"
conditions = []
conditions << "#{quote_ident(primary_key)} <= #{below}" if below
conditions << where if where
query << " WHERE #{conditions.join(" AND ")}" if conditions.any?
execute(query)[0]["max"].to_i
end
|
#min_id(primary_key, column, cast, starting_time, where) ⇒ Object
29
30
31
32
33
34
35
36
|
# File 'lib/pgslice/table.rb', line 29
def min_id(primary_key, column, cast, starting_time, where)
query = "SELECT MIN(#{quote_ident(primary_key)}) FROM #{quote_table(table)}"
conditions = []
conditions << "#{quote_ident(column)} >= #{sql_date(starting_time, cast)}" if starting_time
conditions << where if where
query << " WHERE #{conditions.join(" AND ")}" if conditions.any?
(execute(query)[0]["min"] || 1).to_i
end
|
#retired_table ⇒ Object
7
8
9
|
# File 'lib/pgslice/table.rb', line 7
def retired_table
self.class.new("#{table}_retired")
end
|
#trigger_name ⇒ Object
11
12
13
|
# File 'lib/pgslice/table.rb', line 11
def trigger_name
"#{table.split(".")[-1]}_insert_trigger"
end
|