lib/pgslice/cli/add_partitions.rb in pgslice-0.4.6 vs lib/pgslice/cli/add_partitions.rb in pgslice-0.4.7
- old
+ new
@@ -2,19 +2,21 @@
class CLI
desc "add_partitions TABLE", "Add partitions"
option :intermediate, type: :boolean, default: false, desc: "Add to intermediate table"
option :past, type: :numeric, default: 0, desc: "Number of past partitions to add"
option :future, type: :numeric, default: 0, desc: "Number of future partitions to add"
+ option :tablespace, type: :string, default: "", desc: "Tablespace to use"
def add_partitions(table)
original_table = create_table(table)
table = options[:intermediate] ? original_table.intermediate_table : original_table
trigger_name = original_table.trigger_name
assert_table(table)
future = options[:future]
past = options[:past]
+ tablespace = options[:tablespace]
range = (-1 * past)..future
period, field, cast, needs_comment, declarative, version = table.fetch_settings(original_table.trigger_name)
unless period
message = "No settings found: #{table}"
@@ -48,10 +50,11 @@
index_defs = []
fk_defs = []
end
primary_key = schema_table.primary_key
+ tablespace_str = tablespace.empty? ? "" : " TABLESPACE #{quote_ident(tablespace)}"
added_partitions = []
range.each do |n|
day = advance_date(today, period, n)
@@ -59,16 +62,16 @@
next if partition.exists?
added_partitions << partition
if declarative
queries << <<-SQL
-CREATE TABLE #{quote_table(partition)} PARTITION OF #{quote_table(table)} FOR VALUES FROM (#{sql_date(day, cast, false)}) TO (#{sql_date(advance_date(day, period, 1), cast, false)});
+CREATE TABLE #{quote_table(partition)} PARTITION OF #{quote_table(table)} FOR VALUES FROM (#{sql_date(day, cast, false)}) TO (#{sql_date(advance_date(day, period, 1), cast, false)})#{tablespace_str};
SQL
else
queries << <<-SQL
CREATE TABLE #{quote_table(partition)}
(CHECK (#{quote_ident(field)} >= #{sql_date(day, cast)} AND #{quote_ident(field)} < #{sql_date(advance_date(day, period, 1), cast)}))
- INHERITS (#{quote_table(table)});
+ INHERITS (#{quote_table(table)})#{tablespace_str};
SQL
end
queries << "ALTER TABLE #{quote_table(partition)} ADD PRIMARY KEY (#{primary_key.map { |k| quote_ident(k) }.join(", ")});" if primary_key.any?