Module: FuryDumper

Defined in:
lib/fury_dumper.rb,
lib/fury_dumper/api.rb,
lib/fury_dumper/config.rb,
lib/fury_dumper/dumper.rb,
lib/fury_dumper/engine.rb,
lib/fury_dumper/version.rb,
lib/fury_dumper/encrypter.rb,
lib/fury_dumper/dumpers/model.rb,
lib/fury_dumper/dumpers/dump_state.rb,
lib/fury_dumper/dumpers/model_queue.rb,
lib/fury_dumper/dumpers/relation_items.rb,
lib/generators/fury_dumper/config_generator.rb,
app/controllers/fury_dumper/dump_process_controller.rb

Defined Under Namespace

Modules: Dumpers, Generators Classes: Api, Config, DumpProcessController, Dumper, Encrypter, Engine, Error

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.check_type(field, expected_type, field_name) ⇒ Object

Raises:

  • (ArgumentError)


90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/fury_dumper.rb', line 90

def self.check_type(field, expected_type, field_name)
  is_ok     = if expected_type.is_a?(Array)
                expected_type.any? do |type|
                  field.is_a?(type)
                end
              else
                field.is_a?(expected_type)
              end
  types_str = expected_type.is_a?(Array) ? expected_type.join(' or ') : expected_type

  raise ArgumentError, "Expected #{field_name} as #{types_str}, got: #{field.class}" unless is_ok
end

.configurationObject



21
22
23
# File 'lib/fury_dumper.rb', line 21

def self.configuration
  @configuration ||= Config.config
end

.dump(password:, host:, port:, user:, field_values:, database:, model_name: 'Lead', field_name: 'id', debug_mode: :none, ask: true) ⇒ Object

Start dumping

Examples:

FuryDumper.dump( password: ‘12345’,

host:         'localhost',
port:         '5432',
user:         'username',
model_name:   'User',
field_name:   'admin_token',
field_values: ['99999999-8888-4444-1212-111111111111'],
database:     'staging',
debug_mode:   :short)

Parameters:

  • password (String)
    • password for remote DB

  • host (String)
    • host for remote DB

  • port (String)
    • port for remote DB

  • user (String)
    • username for remote DB

  • model_name (String) (defaults to: 'Lead')
    • name of model for dump

  • field_name (String) (defaults to: 'id')
    • field name for model

  • field_values (Array|Range)
    • values of field_name

  • database (String)
    • DB remote name

  • debug_mode (Symbol) (defaults to: :none)
    • debug mode (full - all msgs, short - part of msgs, none - nothing)

  • ask (Boolean) (defaults to: true)
    • ask user for confirm different schema of target & remote DB



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fury_dumper.rb', line 47

def self.dump(password:,
              host:,
              port:,
              user:,
              field_values:, database:, model_name: 'Lead',
              field_name: 'id',
              debug_mode: :none,
              ask: true)

  check_type(model_name, String, 'model name')
  check_type(field_name, String, 'field name')
  check_type(field_values, [Array, Range], 'field values')

  states = []
  field_values.to_a.in_groups_of(FuryDumper::Config.batch_size) do |batch|
    relation_items = FuryDumper::Dumpers::RelationItems.new_with_key_value(item_key: field_name, item_values: batch)

    sync = Dumper.new \
      password: password,
      host: host,
      port: port,
      user: user,
      database: database,
      model: FuryDumper::Dumpers::Model.new(source_model: model_name, relation_items: relation_items),
      debug_mode: debug_mode

    if ask && !sync.equal_schemas?
      confirm = ask('Are you sure to continue? [Y/N] ') { |yn| yn.limit = 1, yn.validate = /[yn]/i }
      ask     = false
      return unless confirm.downcase == 'y' # rubocop:disable Lint/NonLocalExitFromIterator
    end

    sync.sync_models
    states << sync.dump_state
  end

  states.each_with_index do |state, index|
    p "Batch ##{index}:"
    state.print_statistic
  end
  true
end