Class: Hanami::Model::Migrator::Connection Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/model/migrator/connection.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Sequel connection wrapper

Normalize external adapters interfaces

Since:

  • 0.5.0

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Connection

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Connection.

Since:

  • 0.5.0


15
16
17
# File 'lib/hanami/model/migrator/connection.rb', line 15

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#databaseObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns DB name from conenction

Even when adapter doesn't provide it explicitly it tries to parse

Since:

  • 0.5.0


58
59
60
# File 'lib/hanami/model/migrator/connection.rb', line 58

def database
  @database ||= parsed_uri.path[1..-1]
end

#database_typeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns DB type

Examples:

connection.database_type
  # => 'postgres'

Since:

  • 0.5.0


70
71
72
73
74
75
76
77
78
79
# File 'lib/hanami/model/migrator/connection.rb', line 70

def database_type
  case uri
  when /sqlite/
    :sqlite
  when /postgres/
    :postgres
  when /mysql/
    :mysql
  end
end

#global_uriObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns DB connection wihout specifying database name

Since:

  • 0.5.0


113
114
115
# File 'lib/hanami/model/migrator/connection.rb', line 113

def global_uri
  uri.sub(parsed_uri.select(:path).first, '')
end

#hostObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns DB connection host

Even when adapter doesn't provide it explicitly it tries to parse

Since:

  • 0.5.0


38
39
40
# File 'lib/hanami/model/migrator/connection.rb', line 38

def host
  @host ||= parsed_uri.host || parsed_opt('host')
end

#jdbc?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a boolean telling if a DB connection is from JDBC or not

Returns:

  • (Boolean)

Since:

  • 0.5.0


121
122
123
# File 'lib/hanami/model/migrator/connection.rb', line 121

def jdbc?
  !uri.scan('jdbc:').empty?
end

#parsed_uriObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns database connection URI instance without JDBC namespace

Since:

  • 0.5.0


129
130
131
# File 'lib/hanami/model/migrator/connection.rb', line 129

def parsed_uri
  @parsed_uri ||= URI.parse(uri.sub('jdbc:', ''))
end

#passwordObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns user from DB connection

Even when adapter doesn't provide it explicitly it tries to parse

Since:

  • 0.5.0


97
98
99
# File 'lib/hanami/model/migrator/connection.rb', line 97

def password
  @password ||= parsed_opt('password') || parsed_uri.password
end

#portObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns DB connection port

Even when adapter doesn't provide it explicitly it tries to parse

Since:

  • 0.5.0


48
49
50
# File 'lib/hanami/model/migrator/connection.rb', line 48

def port
  @port ||= parsed_uri.port || parsed_opt('port').to_i.nonzero?
end

#rawObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.7.0


21
22
23
24
25
26
27
28
29
30
# File 'lib/hanami/model/migrator/connection.rb', line 21

def raw
  @raw ||= begin
             Sequel.connect(
               configuration.url,
               loggers: [configuration.migrations_logger]
             )
           rescue Sequel::AdapterNotFound
             raise MigrationError.new("Current adapter (#{configuration.adapter.type}) doesn't support SQL database operations.")
           end
end

#schemaObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.5.0


134
135
136
# File 'lib/hanami/model/migrator/connection.rb', line 134

def schema
  configuration.schema
end

#table(name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return the database table for the given name

Since:

  • 0.7.0


142
143
144
# File 'lib/hanami/model/migrator/connection.rb', line 142

def table(name)
  raw[name] if raw.tables.include?(name)
end

#uriObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns DB connection URI directly from adapter

Since:

  • 0.5.0


105
106
107
# File 'lib/hanami/model/migrator/connection.rb', line 105

def uri
  @configuration.url
end

#userObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns user from DB connection

Even when adapter doesn't provide it explicitly it tries to parse

Since:

  • 0.5.0


87
88
89
# File 'lib/hanami/model/migrator/connection.rb', line 87

def user
  @user ||= parsed_opt('user') || parsed_uri.user
end