Class: MySQLDBTool::Config::ConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/mysql_db_tool/config/config_loader.rb

Constant Summary collapse

DEFAULT_CONFIG =
{
  db_info: {
    host: "localhost",
    user: "root",
    password: "",
    database: ["mysql"],
    port: 3306
  },
  data_tables: [
    # { name: "large_table1", where: "updated_at" }
  ],
  ignore_tables: [
  ]
}

Class Method Summary collapse

Class Method Details

.load(environment) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mysql_db_tool/config/config_loader.rb', line 21

def self.load(environment)
  file_path = File.join(Dir.pwd, "config-#{environment}.json")
  if File.exist?(file_path)
    file_contents = File.read(file_path)
    json_data = JSON.parse(file_contents)
    {
      db_info: symbolize_keys(json_data['dbInfo']),
      data_tables: json_data['dataTables'].map { |table| symbolize_keys(table) },
      ignore_tables: json_data['ignoreTables']
    }
  else
    puts "Warning: config-#{environment}.json not found in the current directory. Using default configuration."
    DEFAULT_CONFIG
  end
rescue JSON::ParserError => e
  puts "Error parsing config-#{environment}.json: #{e.message}. Using default configuration."
  DEFAULT_CONFIG
end