lib/pgslice.rb in pgslice-0.1.0 vs lib/pgslice.rb in pgslice-0.1.1
- old
+ new
@@ -62,30 +62,30 @@
log "Creating #{intermediate_table} from #{table}"
queries = []
queries << <<-SQL
- CREATE TABLE #{intermediate_table} (
- LIKE #{table} INCLUDING INDEXES INCLUDING DEFAULTS
- )
+CREATE TABLE #{intermediate_table} (
+ LIKE #{table} INCLUDING INDEXES INCLUDING DEFAULTS
+);
SQL
sql_format = SQL_FORMAT[period.to_sym]
queries << <<-SQL
- CREATE FUNCTION #{trigger_name}()
- RETURNS trigger AS $$
- BEGIN
- EXECUTE 'INSERT INTO public.#{table}_' || to_char(NEW.#{column}, '#{sql_format}') || ' VALUES ($1.*)' USING NEW;
- RETURN NULL;
- END;
- $$ LANGUAGE plpgsql
+CREATE FUNCTION #{trigger_name}()
+RETURNS trigger AS $$
+BEGIN
+ EXECUTE 'INSERT INTO public.#{table}_' || to_char(NEW.#{column}, '#{sql_format}') || ' VALUES ($1.*)' USING NEW;
+ RETURN NULL;
+END;
+$$ LANGUAGE plpgsql;
SQL
queries << <<-SQL
- CREATE TRIGGER #{trigger_name}
- BEFORE INSERT ON #{intermediate_table}
- FOR EACH ROW EXECUTE PROCEDURE #{trigger_name}()
+CREATE TRIGGER #{trigger_name}
+BEFORE INSERT ON #{intermediate_table}
+FOR EACH ROW EXECUTE PROCEDURE #{trigger_name}();
SQL
run_queries(queries)
end
@@ -98,12 +98,12 @@
abort "Table not found: #{intermediate_table}" unless table_exists?(intermediate_table)
log "Dropping #{intermediate_table}"
queries = [
- "DROP TABLE #{intermediate_table} CASCADE",
- "DROP FUNCTION #{trigger_name}()"
+ "DROP TABLE #{intermediate_table} CASCADE;",
+ "DROP FUNCTION #{trigger_name}();"
]
run_queries(queries)
end
def add_partitions
@@ -132,14 +132,14 @@
log "Creating #{partition_name} from #{table}"
date_format = "%Y-%m-%d"
queries << <<-SQL
- CREATE TABLE #{partition_name} (
- LIKE #{table} INCLUDING INDEXES INCLUDING DEFAULTS,
- CHECK (#{field} >= '#{day.strftime(date_format)}'::date AND #{field} < '#{(day + inc).strftime(date_format)}'::date)
- ) INHERITS (#{table})
+CREATE TABLE #{partition_name} (
+ LIKE #{table} INCLUDING INDEXES INCLUDING DEFAULTS,
+ CHECK (#{field} >= '#{day.strftime(date_format)}'::date AND #{field} < '#{(day + inc).strftime(date_format)}'::date)
+) INHERITS (#{table});
SQL
end
run_queries(queries) if queries.any?
end
@@ -207,12 +207,12 @@
log "Renaming #{table} to #{retired_table}"
log "Renaming #{intermediate_table} to #{table}"
queries = [
- "ALTER TABLE #{table} RENAME TO #{retired_table}",
- "ALTER TABLE #{intermediate_table} RENAME TO #{table}"
+ "ALTER TABLE #{table} RENAME TO #{retired_table};",
+ "ALTER TABLE #{intermediate_table} RENAME TO #{table};"
]
run_queries(queries)
end
def unswap
@@ -227,12 +227,12 @@
log "Renaming #{table} to #{intermediate_table}"
log "Renaming #{retired_table} to #{table}"
queries = [
- "ALTER TABLE #{table} RENAME TO #{intermediate_table}",
- "ALTER TABLE #{retired_table} RENAME TO #{table}"
+ "ALTER TABLE #{table} RENAME TO #{intermediate_table};",
+ "ALTER TABLE #{retired_table} RENAME TO #{table};"
]
run_queries(queries)
end
# arguments
@@ -257,11 +257,11 @@
abort e.message
end
# output
- def log(message)
+ def log(message = nil)
$stderr.puts message
end
def abort(message)
raise PgSlice::Error, message
@@ -292,10 +292,14 @@
end
def run_queries(queries)
connection.transaction do
execute("SET client_min_messages TO warning")
- queries.map(&:squish).each do |query|
+ log
+ log "============================== SQL =============================="
+ queries.each do |query|
+ log
+ log query
execute(query)
end
end
end