Class: ActiveRecord::Tasks::PostgreSQLDatabaseTasks
- Inherits:
-
Object
- Object
- ActiveRecord::Tasks::PostgreSQLDatabaseTasks
- Defined in:
- lib/active_record/tasks/postgresql_database_tasks.rb
Overview
:nodoc:
Constant Summary collapse
- DEFAULT_ENCODING =
ENV['CHARSET'] || 'utf8'
Instance Method Summary collapse
- #charset ⇒ Object
- #collation ⇒ Object
- #create(master_established = false) ⇒ Object
- #drop ⇒ Object
-
#initialize(configuration) ⇒ PostgreSQLDatabaseTasks
constructor
A new instance of PostgreSQLDatabaseTasks.
- #purge ⇒ Object
- #structure_dump(filename) ⇒ Object
- #structure_load(filename) ⇒ Object
Constructor Details
#initialize(configuration) ⇒ PostgreSQLDatabaseTasks
Returns a new instance of PostgreSQLDatabaseTasks.
9 10 11 |
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 9 def initialize(configuration) @configuration = configuration end |
Instance Method Details
#charset ⇒ Object
31 32 33 |
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 31 def charset connection.encoding end |
#collation ⇒ Object
35 36 37 |
# File '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 '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. raise DatabaseAlreadyExists else raise end end |
#drop ⇒ Object
26 27 28 29 |
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 26 def drop establish_master_connection connection.drop_database configuration['database'] end |
#purge ⇒ Object
39 40 41 42 43 |
# File '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 58 59 60 61 62 63 64 65 66 |
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 45 def structure_dump(filename) 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] 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
68 69 70 71 72 |
# File 'lib/active_record/tasks/postgresql_database_tasks.rb', line 68 def structure_load(filename) set_psql_env args = [ '-q', '-f', filename, configuration['database'] ] run_cmd('psql', args, 'loading' ) end |