Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 91 lines (73 sloc) 2.693 kb
d8dfcac @kennyj Refactor db:structure:dump task.
kennyj authored
1 require 'shellwords'
2
588e80f @frodsan add :nodoc: to AR::Tasks and update to follow the coding conventions
frodsan authored
3 module ActiveRecord
4 module Tasks # :nodoc:
5 class PostgreSQLDatabaseTasks # :nodoc:
6 DEFAULT_ENCODING = ENV['CHARSET'] || 'utf8'
7
8 delegate :connection, :establish_connection, :clear_active_connections!,
9 to: ActiveRecord::Base
10
11 def initialize(configuration)
12 @configuration = configuration
13 end
14
15 def create(master_established = false)
16 establish_master_connection unless master_established
17 connection.create_database configuration['database'],
18 configuration.merge('encoding' => encoding)
19 establish_connection configuration
9b636dc @carlosantoniodasilva Bring back "database already exists" messages when running rake tasks
carlosantoniodasilva authored
20 rescue ActiveRecord::StatementInvalid => error
21 if /database .* already exists/ === error.message
22 raise DatabaseAlreadyExists
23 else
24 raise
25 end
588e80f @frodsan add :nodoc: to AR::Tasks and update to follow the coding conventions
frodsan authored
26 end
27
28 def drop
29 establish_master_connection
30 connection.drop_database configuration['database']
31 end
32
363ab88 @simonjefford Refactor db:charset task
simonjefford authored
33 def charset
34 connection.encoding
35 end
36
bece59e @kennyj Extract collation tasks to database tasks.
kennyj authored
37 def collation
aafa00f @rafaelfranca Unify the collation API for the database adpters
rafaelfranca authored
38 connection.collation
bece59e @kennyj Extract collation tasks to database tasks.
kennyj authored
39 end
40
588e80f @frodsan add :nodoc: to AR::Tasks and update to follow the coding conventions
frodsan authored
41 def purge
42 clear_active_connections!
43 drop
44 create true
45 end
46
d8dfcac @kennyj Refactor db:structure:dump task.
kennyj authored
47 def structure_dump(filename)
48 set_psql_env
49 search_path = configuration['schema_search_path']
50 unless search_path.blank?
51 search_path = search_path.split(",").map{|search_path_part| "--schema=#{Shellwords.escape(search_path_part.strip)}" }.join(" ")
52 end
53
54 command = "pg_dump -i -s -x -O -f #{Shellwords.escape(filename)} #{search_path} #{Shellwords.escape(configuration['database'])}"
55 raise 'Error dumping database' unless Kernel.system(command)
56
57 File.open(filename, "a") { |f| f << "SET search_path TO #{ActiveRecord::Base.connection.schema_search_path};\n\n" }
58 end
59
1991195 @kennyj Refactor db:structure:load task.
kennyj authored
60 def structure_load(filename)
61 set_psql_env
1db8f9d @tenderlove Merge pull request #10349 from caliper-io/mute_psql_output
tenderlove authored
62 Kernel.system("psql -q -f #{filename} #{configuration['database']}")
1991195 @kennyj Refactor db:structure:load task.
kennyj authored
63 end
64
588e80f @frodsan add :nodoc: to AR::Tasks and update to follow the coding conventions
frodsan authored
65 private
66
67 def configuration
68 @configuration
69 end
70
71 def encoding
72 configuration['encoding'] || DEFAULT_ENCODING
73 end
74
75 def establish_master_connection
76 establish_connection configuration.merge(
77 'database' => 'postgres',
78 'schema_search_path' => 'public'
79 )
80 end
d8dfcac @kennyj Refactor db:structure:dump task.
kennyj authored
81
82 def set_psql_env
83 ENV['PGHOST'] = configuration['host'] if configuration['host']
84 ENV['PGPORT'] = configuration['port'].to_s if configuration['port']
85 ENV['PGPASSWORD'] = configuration['password'].to_s if configuration['password']
86 ENV['PGUSER'] = configuration['username'].to_s if configuration['username']
87 end
588e80f @frodsan add :nodoc: to AR::Tasks and update to follow the coding conventions
frodsan authored
88 end
bca52b0 @pat db:drop and some of db:test:purge.
pat authored
89 end
363ab88 @simonjefford Refactor db:charset task
simonjefford authored
90 end
Something went wrong with that request. Please try again.