Class: ActiveRecord::Tasks::PostgreSQLDatabaseTasks

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/tasks/postgresql_database_tasks.rb

Overview

:nodoc:

Constant Summary collapse

DEFAULT_ENCODING =
ENV['CHARSET'] || 'utf8'

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ PostgreSQLDatabaseTasks

Returns a new instance of PostgreSQLDatabaseTasks.


9
10
11
# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 9

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#charsetObject


31
32
33
# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 31

def charset
  connection.encoding
end

#collationObject


35
36
37
# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 35

def collation
  connection.collation
end

#create(master_established = false) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
24
# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 13

def create(master_established = false)
  establish_master_connection unless master_established
  connection.create_database configuration['database'],
    configuration.merge('encoding' => encoding)
  establish_connection configuration
rescue ActiveRecord::StatementInvalid => error
  if /database .* already exists/ === error.message
    raise DatabaseAlreadyExists
  else
    raise
  end
end

#dropObject


26
27
28
29
# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 26

def drop
  establish_master_connection
  connection.drop_database configuration['database']
end

#purgeObject


39
40
41
42
43
# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 39

def purge
  clear_active_connections!
  drop
  create true
end

#structure_dump(filename) ⇒ Object


45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 45

def structure_dump(filename)
  set_psql_env
  args = ['-s', '-x', '-O', '-f', filename]
  search_path = configuration['schema_search_path']
  unless search_path.blank?
    args += search_path.split(',').map do |part|
      "--schema=#{part.strip}"
    end
  end
  args << configuration['database']
  run_cmd('pg_dump', args, 'dumping')
  File.open(filename, "a") { |f| f << "SET search_path TO #{connection.schema_search_path};\n\n" }
end

#structure_load(filename) ⇒ Object


59
60
61
62
63
# File 'activerecord/lib/active_record/tasks/postgresql_database_tasks.rb', line 59

def structure_load(filename)
  set_psql_env
  args = [ '-q', '-f', filename, configuration['database'] ]
  run_cmd('psql', args, 'loading')
end