Class: SampleDataDumpPostgresDataStore::Gateway

Inherits:
Object
  • Object
show all
Defined in:
lib/sample_data_dump_postgres_data_store/gateway.rb

Defined Under Namespace

Classes: TableConfigurationValidator

Instance Method Summary collapse

Constructor Details

#initialize(postgresql_adapter, settings) ⇒ Gateway

Returns a new instance of Gateway.


14
15
16
17
# File 'lib/sample_data_dump_postgres_data_store/gateway.rb', line 14

def initialize(postgresql_adapter, settings)
  @postgresql_adapter = postgresql_adapter
  @settings = settings
end

Instance Method Details

#dump_to_local_file(table_configuration) ⇒ Object


19
20
21
22
23
24
25
26
27
# File 'lib/sample_data_dump_postgres_data_store/gateway.rb', line 19

def dump_to_local_file(table_configuration)
  dump_file = SampleDataDump::Helpers::DumpFile.new(table_configuration, @settings)
  uncompressed_file_path = dump_file.local_dump_file_path

  sql = extraction_sql(table_configuration)
  results = @postgresql_adapter.execute(sql)
  export_results_to_sql(results, table_configuration, uncompressed_file_path)
  Dry::Monads::Success(uncompressed_file_path)
end

#load_dump_file(table_configuration) ⇒ Object


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sample_data_dump_postgres_data_store/gateway.rb', line 29

def load_dump_file(table_configuration)
  raise 'DO NOT LOAD OBFUSCATED DUMPS IN PRODUCTION!' if Rails.env.production?

  dump_file = SampleDataDump::Helpers::DumpFile.new(table_configuration, @settings)
  source_file_path = dump_file.local_dump_file_path
  unless File.exist?(source_file_path)
    return Dry::Monads::Failure("File #{source_file_path} does not exist for loading!")
  end

  sql = File.read(source_file_path)
  @postgresql_adapter.execute(sql)
  Dry::Monads::Success(true)
end

#reset_sequence(table_configuration) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/sample_data_dump_postgres_data_store/gateway.rb', line 43

def reset_sequence(table_configuration)
  table_name = table_configuration.qualified_table_name
  get_sequence_name_sql = "SELECT PG_GET_SERIAL_SEQUENCE('#{table_name}', 'id') AS name"
  sequence_name = @postgresql_adapter.execute(get_sequence_name_sql).first['name']
  if sequence_name.present?
    sql = "SELECT setval('#{sequence_name}', coalesce((SELECT MAX(id) FROM #{table_name}),1))"
    @postgresql_adapter.execute(sql)
  end
  Dry::Monads::Success(true)
rescue ActiveRecord::StatementInvalid # means sequence or column does not exist
  Dry::Monads::Success(true)
end

#valid?(table_configuration) ⇒ Boolean

Returns:

  • (Boolean)

56
57
58
# File 'lib/sample_data_dump_postgres_data_store/gateway.rb', line 56

def valid?(table_configuration)
  TableConfigurationValidator.new(table_configuration, @postgresql_adapter).validation_result
end

#wipe_table(table_configuration) ⇒ Object


60
61
62
63
64
# File 'lib/sample_data_dump_postgres_data_store/gateway.rb', line 60

def wipe_table(table_configuration)
  qualified_table_name = table_configuration.qualified_table_name
  @postgresql_adapter.execute "DELETE FROM #{qualified_table_name} CASCADE"
  Dry::Monads::Success(true)
end