Module: Support::Database
- Defined in:
- lib/support/database.rb
Class Method Summary collapse
-
.pg_database_exists? ⇒ Boolean
This doesn’t actually say if the database exists or not! Try it, load a console, drop the database in another terminal, and call this method.
-
.pg_dump(table_name, data_directory, dump_filename = nil) ⇒ Object
Dump a specific table from a database.
- .pg_dump_all(data_directory, dump_filename) ⇒ Object
-
.pg_env_args(database_arg = '-d') ⇒ Object
Extracts DB settings to be used for pg_dump, pg_restore and psql.
-
.pg_restore(table_name, data_directory, dump_filename = nil) ⇒ Object
For example: pg_restore -Fc -d taxonworks_development -t geographic_area_types ~/src/gaz/data/internal/dump/geographic_area_types.dump.
- .pg_restore_all(data_directory, dump_filename) ⇒ Object
Class Method Details
.pg_database_exists? ⇒ Boolean
This doesn’t actually say if the database exists or not!
Try it, load a console, drop the database in another terminal, and call this method.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/support/database.rb', line 61 def self.pg_database_exists? begin ApplicationRecord.connection # ApplicationRecord.connection.disconnect! rescue ActiveRecord::NoDatabaseError false else true end end |
.pg_dump(table_name, data_directory, dump_filename = nil) ⇒ Object
Dump a specific table from a database.
21 22 23 24 25 26 27 28 |
# File 'lib/support/database.rb', line 21 def self.pg_dump(table_name, data_directory, dump_filename = nil) config = pg_env_args('-a') dump_filename ||= "/#{table_name}.dump" system(config[:env], 'pg_dump', '-Fc', '-t', table_name, *config[:args], '-f', File.join(data_directory, dump_filename)) $?.to_i end |
.pg_dump_all(data_directory, dump_filename) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/support/database.rb', line 41 def self.pg_dump_all(data_directory, dump_filename) config = pg_env_args('-a') dump_filename ||= "/#{table_name}.dump" system(config[:env], 'pg_dump', '-Fc', *config[:args], '-f', File.join(data_directory, dump_filename)) $?.to_i end |
.pg_env_args(database_arg = '-d') ⇒ Object
Extracts DB settings to be used for pg_dump, pg_restore and psql.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/support/database.rb', line 4 def self.pg_env_args(database_arg = '-d') config = ActiveRecord::Base.connection_config { env: { 'PGPASSWORD' => config[:password] }, args: [ [database_arg, config[:database]], ['-h', config[:host]], ['-U', config[:username]], ['-p', config[:port]&.to_s] ].reject { |(a, v)| v.nil? }.flatten! } end |
.pg_restore(table_name, data_directory, dump_filename = nil) ⇒ Object
For example:
pg_restore -Fc -d taxonworks_development -t geographic_area_types ~/src/gaz/data/internal/dump/geographic_area_types.dump
32 33 34 35 36 37 38 39 |
# File 'lib/support/database.rb', line 32 def self.pg_restore(table_name, data_directory, dump_filename = nil) config = pg_env_args dump_filename ||= "/#{table_name}.dump" system(config[:env], 'pg_restore', '-Fc', '-t', table_name, *config[:args], File.join(data_directory, dump_filename)) $?.to_i end |
.pg_restore_all(data_directory, dump_filename) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/support/database.rb', line 50 def self.pg_restore_all(data_directory, dump_filename) config = pg_env_args dump_filename ||= "/#{table_name}.dump" system(config[:env], 'pg_restore', '-Fc', *config[:args], File.join(data_directory, dump_filename)) $?.to_i end |