lib/pgslice.rb in pgslice-0.1.1 vs lib/pgslice.rb in pgslice-0.1.2

- old
+ new

@@ -57,12 +57,10 @@ abort "Table not found: #{table}" unless table_exists?(table) abort "Table already exists: #{intermediate_table}" if table_exists?(intermediate_table) abort "Column not found: #{column}" unless columns(table).include?(column) abort "Invalid period: #{period}" unless SQL_FORMAT[period.to_sym] - log "Creating #{intermediate_table} from #{table}" - queries = [] queries << <<-SQL CREATE TABLE #{intermediate_table} ( LIKE #{table} INCLUDING INDEXES INCLUDING DEFAULTS @@ -95,12 +93,10 @@ trigger_name = self.trigger_name(table) abort "Usage: pgslice unprep <table>" if arguments.length != 1 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}();" ] run_queries(queries) @@ -128,11 +124,10 @@ days.each do |day| partition_name = "#{original_table}_#{day.strftime(name_format)}" next if table_exists?(partition_name) - log "Creating #{partition_name} from #{table}" date_format = "%Y-%m-%d" queries << <<-SQL CREATE TABLE #{partition_name} ( LIKE #{table} INCLUDING INDEXES INCLUDING DEFAULTS, @@ -173,26 +168,28 @@ starting_id = max_dest_id + 1 fields = columns(source_table).join(", ") batch_size = options[:batch_size] - if starting_id < max_source_id - log "#{primary_key}: #{starting_id} -> #{max_source_id}" - log "time: #{starting_time.to_date} -> #{ending_time.to_date}" + log "Overview" + log "#{source_table} max #{primary_key}: #{max_source_id}" + log "#{dest_table} max #{primary_key}: #{max_dest_id}" + log "time period: #{starting_time.to_date} -> #{ending_time.to_date}" + log - while starting_id < max_source_id - log "#{starting_id}..#{[starting_id + batch_size - 1, max_source_id].min}" + log "Batches" + while starting_id <= max_source_id + log "#{starting_id}..#{[starting_id + batch_size - 1, max_source_id].min}" - query = "INSERT INTO #{dest_table} (#{fields}) SELECT #{fields} FROM #{source_table} WHERE #{primary_key} >= #{starting_id} AND #{primary_key} < #{starting_id + batch_size} AND #{field} >= '#{starting_time.strftime(date_format)}'::date AND #{field} < '#{ending_time.strftime(date_format)}'::date" - log query if options[:debug] - execute(query) + query = "INSERT INTO #{dest_table} (#{fields}) SELECT #{fields} FROM #{source_table} WHERE #{primary_key} >= #{starting_id} AND #{primary_key} < #{starting_id + batch_size} AND #{field} >= '#{starting_time.strftime(date_format)}'::date AND #{field} < '#{ending_time.strftime(date_format)}'::date" + log query if options[:debug] + execute(query) - starting_id += batch_size + starting_id += batch_size - if options[:sleep] && starting_id < max_source_id - sleep(options[:sleep]) - end + if options[:sleep] && starting_id <= max_source_id + sleep(options[:sleep]) end end end def swap @@ -203,13 +200,10 @@ abort "Usage: pgslice swap <table>" if arguments.length != 1 abort "Table not found: #{table}" unless table_exists?(table) abort "Table not found: #{intermediate_table}" unless table_exists?(intermediate_table) abort "Table already exists: #{retired_table}" if table_exists?(retired_table) - 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};" ] run_queries(queries) @@ -223,13 +217,10 @@ abort "Usage: pgslice unswap <table>" if arguments.length != 1 abort "Table not found: #{table}" unless table_exists?(table) abort "Table not found: #{retired_table}" unless table_exists?(retired_table) abort "Table already exists: #{intermediate_table}" if table_exists?(intermediate_table) - 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};" ] run_queries(queries) @@ -244,10 +235,11 @@ o.boolean "--debug" o.float "--sleep" o.integer "--future", default: 3 o.integer "--past", default: 3 o.integer "--batch-size", default: 10000 + o.boolean "--dry-run", default: false o.on "-v", "--version", "print the version" do log PgSlice::VERSION @exit = true end end @@ -261,10 +253,14 @@ def log(message = nil) $stderr.puts message end + def log_sql(message = nil) + $stdout.puts message + end + def abort(message) raise PgSlice::Error, message end # database connection @@ -292,16 +288,17 @@ end def run_queries(queries) connection.transaction do execute("SET client_min_messages TO warning") - log - log "============================== SQL ==============================" + log_sql "BEGIN;" + log_sql queries.each do |query| - log - log query - execute(query) + log_sql query + log_sql + execute(query) unless options[:dry_run] end + log_sql "COMMIT;" end end def existing_tables(like:) query = "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = $1 AND tablename LIKE $2"