Class: ActiveRecord::Tasks::PostgreSQLDatabaseTasks

Inherits:
Object
  • Object
show all
Defined in:
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.



11
12
13
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 11

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#charsetObject



33
34
35
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 33

def charset
  connection.encoding
end

#collationObject



37
38
39
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 37

def collation
  connection.collation
end

#create(master_established = false) ⇒ Object



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

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



28
29
30
31
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 28

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

#purgeObject



41
42
43
44
45
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 41

def purge
  clear_active_connections!
  drop
  create true
end

#structure_dump(filename) ⇒ Object



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

def structure_dump(filename)
  set_psql_env
  search_path = configuration['schema_search_path']
  unless search_path.blank?
    search_path = search_path.split(",").map{|search_path_part| "--schema=#{Shellwords.escape(search_path_part.strip)}" }.join(" ")
  end

  command = "pg_dump -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(configuration['database'])}"
  raise 'Error dumping database' unless Kernel.system(command)

  File.open(filename, "a") { |f| f << "SET search_path TO #{ActiveRecord::Base.connection.schema_search_path};\n\n" }
end

#structure_load(filename) ⇒ Object



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

def structure_load(filename)
  set_psql_env
  Kernel.system("psql -q -f #{Shellwords.escape(filename)} #{configuration['database']}")
end