class ArJdbc::Tasks::JdbcDatabaseTasks

Sharing task related code between AR 3.x and 4.x

@note this class needs to conform to the API available since AR 4.0 mostly to be usable with ActiveRecord::Tasks::DatabaseTasks module

Attributes

config[R]
configuration[R]

Public Class Methods

new(configuration) click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 12
def initialize(configuration)
  @configuration = configuration
end

Public Instance Methods

charset() click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 57
def charset
  establish_connection(config)
  if connection.respond_to?(:charset)
    puts connection.charset
  elsif connection.respond_to?(:encoding)
    puts connection.encoding
  else
    raise "AR-JDBC adapter '#{adapter_with_spec}' does not support charset/encoding"
  end
end
collation() click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 68
def collation
  establish_connection(config)
  if connection.respond_to?(:collation)
    puts connection.collation
  else
    raise "AR-JDBC adapter '#{adapter_with_spec}' does not support collation"
  end
end
create() click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 18
def create
  begin
    establish_connection(config)
    ActiveRecord::Base.connection
    if defined? ActiveRecord::Tasks::DatabaseAlreadyExists
      raise ActiveRecord::Tasks::DatabaseAlreadyExists # AR-4.x
    end # silence on AR < 4.0
  rescue #=> error # database does not exists :
    url = config['url']
    url = $1 if url && url =~ /^(.*(?<!\/)\/)(?=\w)/

    establish_connection(config.merge('database' => nil, 'url' => url))

    unless connection.respond_to?(:create_database)
      raise "AR-JDBC adapter '#{adapter_with_spec}' does not support create_database"
    end
    connection.create_database(resolve_database(config), config)

    establish_connection(config)
  end
end
drop() click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 40
def drop
  establish_connection(config)
  unless ActiveRecord::Base.connection.respond_to?(:drop_database)
    raise "AR-JDBC adapter '#{adapter_with_spec}' does not support drop_database"
  end
  connection.drop_database resolve_database(config)
end
purge() click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 48
def purge
  establish_connection(config) # :test
  unless ActiveRecord::Base.connection.respond_to?(:recreate_database)
    raise "AR-JDBC adapter '#{adapter_with_spec}' does not support recreate_database (purge)"
  end
  db_name = ActiveRecord::Base.connection.database_name
  ActiveRecord::Base.connection.recreate_database(db_name, config)
end
structure_dump(filename) click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 77
def structure_dump(filename)
  establish_connection(config)
  if connection.respond_to?(:structure_dump)
    File.open(filename, "w:utf-8") { |f| f << connection.structure_dump }
  else
    raise "AR-JDBC adapter '#{adapter_with_spec}' does not support structure_dump"
  end
end
structure_load(filename) click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 86
def structure_load(filename)
  establish_connection(config)
  if connection.respond_to?(:structure_load)
    connection.structure_load IO.read(filename)
  else
    #IO.read(filename).split(/;\n*/m).each do |ddl|
    #  connection.execute(ddl)
    #end
    raise "AR-JDBC adapter '#{adapter_with_spec}' does not support structure_load"
  end
end

Protected Instance Methods

expand_path(path) click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 100
def expand_path(path)
  require 'pathname'
  path = Pathname.new path
  return path.to_s if path.absolute?
  rails_root ? File.join(rails_root, path) : File.expand_path(path)
end
resolve_database(config, file_paths = false) click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 107
def resolve_database(config, file_paths = false)
  config['database'] || resolve_database_from_url(config['url'] || '', file_paths)
end
resolve_database_from_url(url, file_paths = false) click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 111
def resolve_database_from_url(url, file_paths = false)
  ( config = config_from_url(url, file_paths) ) ? config['database'] : nil
end

Private Instance Methods

adapter_with_spec() click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 158
def adapter_with_spec
  adapter, spec = config['adapter'], config['adapter_spec']
  spec ? "#{adapter} (#{spec})" : adapter
end
config_from_url(url, file_paths = false) click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 117
def config_from_url(url, file_paths = false)
  match = url.match %r{
    ^ jdbc:
    ( [\w]+ ):         # $1 protocol
    (?: ([\w]+) : )?   # $2 (sub-protocol)
    (?://)?
    (?: ([\w\-]*) (?: [/:] ([\w\-]*) )? @ (?://)? )?  # user[:password]@ or user[/password]@ ($3 $4)
    ( [\w\.\-]+ )?   # $5 host (or database if there's nothing left)
    (?: : (\d+) )?   # $6 port if any
    (?: :? (/?[\w\-\./~]+) [\?;]? )? ([^/]*?) $
    # $7 database part (ends with '?' or ';') and $8 query string - properties
  }x

  return nil unless match

  config = {}
  config['_protocol'] = match[1]
  config['_sub_protocol'] = match[2] if match[2]
  config['username'] = match[3] if match[3]
  config['password'] = match[4] if match[4]
  host = match[5]; port = match[6]
  database = match[7]
  if database.nil? && port.nil?
    config['database'] = database = host
  else
    config['host'] = host if host
    config['port'] = port if port
    config['database'] = database
  end
  if database && ! file_paths && database[0...1] == '/'
    config['database'] = database[1..-1]
  end
  if query_string = match[8]
    properties = query_string.split('&').inject({}) do |memo, pair|
      pair = pair.split("="); memo[ pair[0] ] = pair[1]; memo
    end
    config['properties'] = properties
  end
  config
end
rails_root() click to toggle source
# File lib/arjdbc/tasks/jdbc_database_tasks.rb, line 163
def rails_root
  defined?(Rails.root) ? Rails.root : ( RAILS_ROOT )
end