lib/hanami/model/migrator/connection.rb in hanami-model-0.7.0 vs lib/hanami/model/migrator/connection.rb in hanami-model-1.0.0.beta1
- old
+ new
@@ -6,48 +6,57 @@
# Normalize external adapters interfaces
#
# @since 0.5.0
# @api private
class Connection
- # @since 0.7.0
+ # @since 0.5.0
# @api private
- attr_reader :raw
+ def initialize(configuration)
+ @configuration = configuration
+ end
- # @since 0.5.0
+ # @since 0.7.0
# @api private
- def initialize(raw)
- @raw = raw
+ 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
# Returns DB connection host
#
# Even when adapter doesn't provide it explicitly it tries to parse
#
# @since 0.5.0
# @api private
def host
- @host ||= opts.fetch(:host, parsed_uri.host)
+ @host ||= parsed_uri.host
end
# Returns DB connection port
#
# Even when adapter doesn't provide it explicitly it tries to parse
#
# @since 0.5.0
# @api private
def port
- @port ||= opts.fetch(:port, parsed_uri.port)
+ @port ||= parsed_uri.port
end
# Returns DB name from conenction
#
# Even when adapter doesn't provide it explicitly it tries to parse
#
# @since 0.5.0
# @api private
def database
- @database ||= opts.fetch(:database, parsed_uri.path[1..-1])
+ @database ||= parsed_uri.path[1..-1]
end
# Returns DB type
#
# @example
@@ -55,90 +64,97 @@
# # => 'postgres'
#
# @since 0.5.0
# @api private
def database_type
- raw.database_type
+ case uri
+ when /sqlite/
+ :sqlite
+ when /postgres/
+ :postgres
+ when /mysql/
+ :mysql
+ end
end
# Returns user from DB connection
#
# Even when adapter doesn't provide it explicitly it tries to parse
#
# @since 0.5.0
# @api private
def user
- @user ||= opts.fetch(:user, parsed_opt('user'))
+ @user ||= parsed_opt('user')
end
# Returns user from DB connection
#
# Even when adapter doesn't provide it explicitly it tries to parse
#
# @since 0.5.0
# @api private
def password
- @password ||= opts.fetch(:password, parsed_opt('password'))
+ @password ||= parsed_opt('password')
end
# Returns DB connection URI directly from adapter
#
# @since 0.5.0
# @api private
def uri
- raw.uri
+ @configuration.url
end
# Returns DB connection wihout specifying database name
#
# @since 0.5.0
# @api private
def global_uri
- raw.uri.sub(parsed_uri.select(:path).first, '')
+ uri.sub(parsed_uri.select(:path).first, '')
end
# Returns a boolean telling if a DB connection is from JDBC or not
#
# @since 0.5.0
# @api private
def jdbc?
- !raw.uri.scan('jdbc:').empty?
+ !uri.scan('jdbc:').empty?
end
# Returns database connection URI instance without JDBC namespace
#
# @since 0.5.0
# @api private
def parsed_uri
- @uri ||= URI.parse(raw.uri.sub('jdbc:', ''))
+ @uri ||= URI.parse(uri.sub('jdbc:', ''))
end
+ def schema
+ configuration.schema
+ end
+
# Return the database table for the given name
#
# @since 0.7.0
# @api private
def table(name)
raw[name] if raw.tables.include?(name)
end
private
+ # @since 1.0.0.beta1
+ # @api private
+ attr_reader :configuration
+
# Returns a value of a given query string param
#
# @param option [String] which option from database connection will be extracted from URI
#
# @since 0.5.0
# @api private
def parsed_opt(option)
parsed_uri.to_s.match(/[\?|\&]#{ option }=(\w+)\&?/).to_a.last
- end
-
- # Fetch connection options from adapter
- #
- # @since 0.5.0
- # @api private
- def opts
- raw.opts
end
end
end
end
end