class Contentful::Exporter::Database::Export

Attributes

config[R]
logger[R]
mapping[R]
tables[R]

Public Class Methods

new(settings) click to toggle source
# File lib/database/export.rb, line 27
def initialize(settings)
  @config = settings
  @mapping = mapping_structure
  @tables = load_tables
  @logger = Logger.new(STDOUT)
end

Public Instance Methods

create_data_relations() click to toggle source
# File lib/database/export.rb, line 50
def create_data_relations
  relations_from_mapping.each do |model_name, relations|
    generate_relations_helper_indexes(relations)
    map_relations_to_links(model_name, relations)
  end
end
load_tables() click to toggle source
# File lib/database/export.rb, line 62
def load_tables
  fail ArgumentError, 'Before importing data from tables, define their names. Check README!' unless config.config['mapped'] && config.config['mapped']['tables']
  config.config['mapped']['tables']
end
mapping_structure() click to toggle source
# File lib/database/export.rb, line 57
def mapping_structure
  fail ArgumentError, 'Set PATH to contentful structure JSON file. Check README' unless config.config['mapping_dir']
  JSON.parse(File.read(config.config['mapping_dir']), symbolize_names: true).with_indifferent_access
end
missing_model_structure?(model_name) click to toggle source
# File lib/database/export.rb, line 67
def missing_model_structure?(model_name)
  mapping[model_name].nil?
end
save_data_as_json() click to toggle source
# File lib/database/export.rb, line 40
def save_data_as_json
  tables.each do |table|
    logger.info "Extracting data from #{"#{table} table"}..."
    model_name = table.to_s.camelize
    fail ArgumentError, "Missing model name in your mapping.json file. To extract data from #{table}, define structure for this model in mapping.json file or remove #{table} from settings.yml - mapped tables! View README." if missing_model_structure?(model_name)
    content_type_name = mapping[model_name][:content_type]
    save_object_to_file(table, content_type_name, model_name, asset?(model_name) ? config.assets_dir : config.entries_dir)
  end
end
tables_name() click to toggle source
# File lib/database/export.rb, line 34
def tables_name
  create_directory(config.data_dir)
  write_json_to_file("#{config.data_dir}/table_names.json", config.db.tables)
  logger.info "File with name of tables saved to #{"#{config.data_dir}/table_names.json"}"
end