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"
ON_ERROR_STOP_1 =
"ON_ERROR_STOP=1".freeze
SQL_COMMENT_BEGIN =
"--".freeze

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ PostgreSQLDatabaseTasks

Returns a new instance of PostgreSQLDatabaseTasks.



15
16
17
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 15

def initialize(configuration)
  @configuration = configuration
end

Instance Method Details

#charsetObject



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

def charset
  connection.encoding
end

#collationObject



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

def collation
  connection.collation
end

#create(master_established = false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 19

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 error.cause.is_a?(PG::DuplicateDatabase)
    raise DatabaseAlreadyExists
  else
    raise
  end
end

#dropObject



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

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

#purgeObject



45
46
47
48
49
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 45

def purge
  clear_active_connections!
  drop
  create true
end

#structure_dump(filename, extra_flags) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 51

def structure_dump(filename, extra_flags)
  set_psql_env

  search_path = \
    case ActiveRecord::Base.dump_schemas
    when :schema_search_path
      configuration["schema_search_path"]
    when :all
      nil
    when String
      ActiveRecord::Base.dump_schemas
    end

  args = ["-s", "-x", "-O", "-f", filename]
  args.concat(Array(extra_flags)) if extra_flags
  unless search_path.blank?
    args += search_path.split(",").map do |part|
      "--schema=#{part.strip}"
    end
  end

  ignore_tables = ActiveRecord::SchemaDumper.ignore_tables
  if ignore_tables.any?
    args += ignore_tables.flat_map { |table| ["-T", table] }
  end

  args << configuration["database"]
  run_cmd("pg_dump", args, "dumping")
  remove_sql_header_comments(filename)
  File.open(filename, "a") { |f| f << "SET search_path TO #{connection.schema_search_path};\n\n" }
end

#structure_load(filename, extra_flags) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 83

def structure_load(filename, extra_flags)
  set_psql_env
  args = ["-v", ON_ERROR_STOP_1, "-q", "-f", filename]
  args.concat(Array(extra_flags)) if extra_flags
  args << configuration["database"]
  run_cmd("psql", args, "loading")
end