Class: ArJdbc::Tasks::JdbcDatabaseTasks

Inherits:
Object
  • Object
show all
Defined in:
lib/arjdbc/tasks/jdbc_database_tasks.rb

Overview

Note:

this class needs to conform to the API available since AR 4.0

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

mostly to be usable with ActiveRecord::Tasks::DatabaseTasks module

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ JdbcDatabaseTasks

Returns a new instance of JdbcDatabaseTasks.



12
13
14
# File 'lib/arjdbc/tasks/jdbc_database_tasks.rb', line 12

def initialize(configuration)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject (readonly) Also known as: config

Returns the value of attribute configuration.



9
10
11
# File 'lib/arjdbc/tasks/jdbc_database_tasks.rb', line 9

def configuration
  @configuration
end

Instance Method Details

#charsetObject



57
58
59
60
61
62
63
64
65
66
# 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

#collationObject



68
69
70
71
72
73
74
75
# 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

#createObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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

#dropObject



40
41
42
43
44
45
46
# 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

#expand_path(path) ⇒ Object (protected)



100
101
102
103
104
105
# 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

#purgeObject



48
49
50
51
52
53
54
55
# 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

#resolve_database(config, file_paths = false) ⇒ Object (protected)



107
108
109
# 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) ⇒ Object (protected)



111
112
113
# 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

#structure_dump(filename) ⇒ Object



77
78
79
80
81
82
83
84
# 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) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# 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