Module: SequelRails::Storage

Defined in:
lib/sequel_rails/storage.rb,
lib/sequel_rails/storage/jdbc.rb,
lib/sequel_rails/storage/mysql.rb,
lib/sequel_rails/storage/mysql2.rb,
lib/sequel_rails/storage/sqlite.rb,
lib/sequel_rails/storage/abstract.rb,
lib/sequel_rails/storage/postgres.rb

Defined Under Namespace

Classes: Abstract, Jdbc, Mysql, Mysql2, Postgres, Sqlite

Class Method Summary collapse

Class Method Details

.adapter_for(config_or_env) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/sequel_rails/storage.rb', line 45

def self.adapter_for(config_or_env)
  config = if config_or_env.is_a? Hash
             config_or_env
           else
             ::SequelRails.configuration.environments[config_or_env.to_s]
           end
  lookup_class(config['adapter']).new config
end

.close_all_connectionsObject



37
38
39
# File 'lib/sequel_rails/storage.rb', line 37

def self.close_all_connections
  with_all_repositories { |config| close_connections_environment(config) }
end

.close_connections_environment(config_or_env) ⇒ Object



41
42
43
# File 'lib/sequel_rails/storage.rb', line 41

def self.close_connections_environment(config_or_env)
  adapter_for(config_or_env).close_connections
end

.create_allObject



11
12
13
# File 'lib/sequel_rails/storage.rb', line 11

def self.create_all
  with_local_repositories { |config| create_environment(config) }
end

.create_environment(config_or_env) ⇒ Object



19
20
21
# File 'lib/sequel_rails/storage.rb', line 19

def self.create_environment(config_or_env)
  adapter_for(config_or_env).create
end

.drop_allObject



15
16
17
# File 'lib/sequel_rails/storage.rb', line 15

def self.drop_all
  with_local_repositories { |config| drop_environment(config) }
end

.drop_environment(config_or_env) ⇒ Object



23
24
25
26
27
# File 'lib/sequel_rails/storage.rb', line 23

def self.drop_environment(config_or_env)
  adapter = adapter_for(config_or_env)
  adapter.close_connections
  adapter.drop
end

.dump_environment(config_or_env, filename) ⇒ Object



29
30
31
# File 'lib/sequel_rails/storage.rb', line 29

def self.dump_environment(config_or_env, filename)
  adapter_for(config_or_env).dump(filename)
end

.extract_adapter(config, uri) ⇒ Object



66
67
68
69
70
# File 'lib/sequel_rails/storage.rb', line 66

def self.extract_adapter(config, uri)
  adapter = config['adapter']
  adapter ||= uri.scheme if uri
  adapter
end

.extract_database(config, uri) ⇒ Object



60
61
62
63
64
# File 'lib/sequel_rails/storage.rb', line 60

def self.extract_database(config, uri)
  database = config['database']
  database ||= uri.path[1..-1] if uri
  database
end

.extract_host(config, uri) ⇒ Object



72
73
74
75
76
# File 'lib/sequel_rails/storage.rb', line 72

def self.extract_host(config, uri)
  host = config['host']
  host ||= uri.host if uri
  host
end

.load_environment(config_or_env, filename) ⇒ Object



33
34
35
# File 'lib/sequel_rails/storage.rb', line 33

def self.load_environment(config_or_env, filename)
  adapter_for(config_or_env).load(filename)
end

.lookup_class(adapter) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sequel_rails/storage.rb', line 106

def self.lookup_class(adapter)
  raise 'Adapter not specified in config, please set the :adapter key.' unless adapter
  return Jdbc if adapter =~ /jdbc/

  klass_name = adapter.camelize.to_sym
  unless self.const_defined?(klass_name)
    raise "Adapter #{adapter} not supported (#{klass_name.inspect})"
  end

  const_get klass_name
end

.parse_url(value) ⇒ Object



54
55
56
57
58
# File 'lib/sequel_rails/storage.rb', line 54

def self.parse_url(value)
  URI(value) if value.present?
rescue ArgumentError
  warn "config url could not be parsed, value was: #{value.inspect}"
end

.with_all_repositoriesObject



95
96
97
98
99
100
101
102
103
104
# File 'lib/sequel_rails/storage.rb', line 95

def self.with_all_repositories
  ::SequelRails.configuration.environments.each_value do |config|
    uri = parse_url(config['url'])
    database = extract_database(config, uri)
    adapter = extract_adapter(config, uri)

    next if database.blank? || adapter.blank?
    yield config
  end
end

.with_local_repositoriesObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sequel_rails/storage.rb', line 78

def self.with_local_repositories
  ::SequelRails.configuration.environments.each_value do |config|
    uri = parse_url(config['url'])

    database = extract_database(config, uri)
    adapter = extract_adapter(config, uri)
    host = extract_host(config, uri)

    next if database.blank? || adapter.blank?
    if host.blank? || %w( 127.0.0.1 localhost ).include?(host)
      yield config
    else
      warn "This task only modifies local databases. #{database} is on a remote host."
    end
  end
end