Class: Geordi::DumpLoader
- Inherits:
-
Object
- Object
- Geordi::DumpLoader
- Defined in:
- lib/geordi/dump_loader.rb
Instance Method Summary collapse
- #development_database_config ⇒ Object (also: #config)
- #dump_file ⇒ Object
-
#initialize(file, database) ⇒ DumpLoader
constructor
A new instance of DumpLoader.
- #load ⇒ Object
- #mysql_command ⇒ Object (also: #mysql2_command)
- #postgresql_command ⇒ Object
Constructor Details
#initialize(file, database) ⇒ DumpLoader
Returns a new instance of DumpLoader.
9 10 11 12 |
# File 'lib/geordi/dump_loader.rb', line 9 def initialize(file, database) @dump_file = file @database = database end |
Instance Method Details
#development_database_config ⇒ Object Also known as: config
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/geordi/dump_loader.rb', line 14 def development_database_config return @config if @config require 'yaml' evaluated_config_file = ERB.new(File.read('config/database.yml')).result # Allow aliases and a special set of classes like symbols and time objects permitted_classes = [Symbol, Time] database_config = if Gem::Version.new(Psych::VERSION) >= Gem::Version.new('3.1.0') YAML.safe_load(evaluated_config_file, permitted_classes: permitted_classes, aliases: true) else YAML.safe_load(evaluated_config_file, permitted_classes, [], true) end development_config = database_config['development'] if development_config.values[0].is_a? Hash # Multi-db setup @config = if @database development_config[@database] || Interaction.fail(%(Unknown development database "#{@database}".)) elsif development_config.has_key? 'primary' development_config['primary'] else development_config.values[0] end else # Single-db setup if @database Interaction.fail %(Could not select "#{@database}" database in a single-db setup.) else @config = development_config end end @config end |
#dump_file ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/geordi/dump_loader.rb', line 79 def dump_file @dump_file ||= begin dumps_glob = File.join(File.('~'), 'dumps', '*.dump') available_dumps = Dir.glob(dumps_glob).sort HighLine.new.choose(*available_dumps) do || .hidden('') { Interaction.fail 'Abort.' } end end end |
#load ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/geordi/dump_loader.rb', line 90 def load adapter_command = "#{config['adapter']}_command" Interaction.fail "Unknown database adapter #{config['adapter'].inspect} in config/database.yml." unless respond_to? adapter_command Interaction.note 'Source file: ' + dump_file source_command = send(adapter_command) Util.run! source_command, fail_message: "An error occurred loading #{File.basename(dump_file)}" end |
#mysql_command ⇒ Object Also known as: mysql2_command
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/geordi/dump_loader.rb', line 51 def mysql_command command = 'mysql --silent' command << ' -p' << config['password'].to_s if config['password'] command << ' -u' << config['username'].to_s if config['username'] command << ' --port=' << config['port'].to_s if config['port'] command << ' --host=' << config['host'].to_s if config['host'] command << ' --default-character-set=utf8' command << ' ' << config['database'].to_s command << ' < ' << dump_file end |
#postgresql_command ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/geordi/dump_loader.rb', line 63 def postgresql_command shared_command_arguments = '' shared_command_arguments << ' --username=' << config['username'].to_s if config['username'] shared_command_arguments << ' --port=' << config['port'].to_s if config['port'] shared_command_arguments << ' --host=' << config['host'].to_s if config['host'] ENV['PGPASSWORD'] = config['password'] database_name = config['database'].to_s drop_command = "dropdb --if-exists#{shared_command_arguments} #{database_name}" create_command = "createdb#{shared_command_arguments} #{database_name}" restore_command = "pg_restore --no-owner --no-acl#{shared_command_arguments} --dbname=#{database_name} #{dump_file}" drop_command + ' && ' + create_command + ' && ' + restore_command end |