require 'flydata/source/generate_source_dump' require 'flydata/preference/data_entry_preference' require 'flydata/source_postgresql/postgresql_component' require 'flydata/source_postgresql/query_based_sync/diff_query_generator' require 'flydata/source_postgresql/table_meta' require 'flydata-core/postgresql/source_pos' require 'flydata-core/postgresql/pg_client' require 'msgpack' module Flydata module SourcePostgresql class GenerateSourceDump < Source::GenerateSourceDump include PostgresqlComponent def run_compatibility_check(dump_dir, backup_dir) %w(host username database).each do |k| if de_prefs[k].to_s.empty? raise "'#{k}' is required. Set the value in the conf file " + "-> #{Flydata::Preference::DataEntryPreference.conf_path(de)}" end end end def confirmation_items items = { "host" => de_prefs['host'], "port" => de_prefs['port'], "username" => de_prefs['username'], "database" => de_prefs['database'], "schema" => de_prefs['schema'], } items end DUMP_SIZE_QUERY = < column_name, "format_type" => columndef_hash["data_type"]} end source_table_hash = { "table_name" => tabledef.table_name, "columns" => columns } io.write(source_table_hash.to_msgpack) end def dump_table_chunk(table, schema, source_pos, num_rows, tabledef, pk_columns, last_pks, io, cli) query = Flydata::SourcePostgresql::QueryBasedSync::DiffQueryGenerator.new( table, schema, columns: tabledef.columns, to_sid: source_pos.snapshot, pk_columns: pk_columns, last_pks: last_pks, limit: num_rows).build_query column_names = tabledef.columns.inject([]) do |ary, h| ary << h[:column] end res = cli.query(query) count = 0 last_row = nil res.each do |row| count += 1 last_row = row dump_row(row, column_names, io) end last_pks = last_row ? pk_columns.collect{|col| last_row[col]} : nil [count, last_pks] end def dump_row(row, columns, io) data = columns.collect{|col| row[col] }.to_msgpack io.write(data) end end end end