Class: Chouette::Loader
- Inherits:
-
Object
- Object
- Chouette::Loader
- Includes:
- CommandLineSupport
- Defined in:
- app/models/chouette/loader.rb
Constant Summary collapse
- @@binarisation_command =
"binarisation"
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #backup(file) ⇒ Object
- #binarize(period, target_directory) ⇒ Object
- #chouette_command ⇒ Object
- #create ⇒ Object
- #drop ⇒ Object
- #import(file, options = {}) ⇒ Object
-
#initialize(schema) ⇒ Loader
constructor
A new instance of Loader.
-
#load_dump(file) ⇒ Object
Load dump where datas are in schema ‘chouette’.
- #pg_options ⇒ Object
- #with_pg_password(&block) ⇒ Object
Methods included from CommandLineSupport
#available_loggers, #execute!, #logger, #max_output_length
Constructor Details
#initialize(schema) ⇒ Loader
Returns a new instance of Loader.
5 6 7 8 9 10 11 12 13 14 |
# File 'app/models/chouette/loader.rb', line 5 def initialize(schema) @schema = schema Chouette::ActiveRecord.connection_pool.spec.config.tap do |config| @database = config[:database] @user = config[:username] @password = config[:password] @host = (config[:host] or "localhost") end end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
3 4 5 |
# File 'app/models/chouette/loader.rb', line 3 def database @database end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
3 4 5 |
# File 'app/models/chouette/loader.rb', line 3 def host @host end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
3 4 5 |
# File 'app/models/chouette/loader.rb', line 3 def password @password end |
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
3 4 5 |
# File 'app/models/chouette/loader.rb', line 3 def schema @schema end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
3 4 5 |
# File 'app/models/chouette/loader.rb', line 3 def user @user end |
Class Method Details
Instance Method Details
#backup(file) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'app/models/chouette/loader.rb', line 54 def backup(file) logger.info "Backup schema #{schema} in #{file}" with_pg_password do execute!("pg_dump -n #{schema} -f #{file} #{}") end self end |
#binarize(period, target_directory) ⇒ Object
100 101 102 103 104 105 106 |
# File 'app/models/chouette/loader.rb', line 100 def binarize(period, target_directory) # TODO check these computed daybefore/dayafter day_before = Date.today - period.begin day_after = period.end - period.begin execute! "#{binarisation_command} --host=#{host} --dbname=#{database} --user=#{user} --password=#{password} --schema=#{schema} --daybefore=#{day_before} --dayafter=#{day_after} --targetdirectory=#{target_directory}" end |
#chouette_command ⇒ Object
33 34 35 |
# File 'app/models/chouette/loader.rb', line 33 def chouette_command @chouette_command ||= Chouette::Command.new(:schema => schema) end |
#create ⇒ Object
72 73 74 75 76 77 78 |
# File 'app/models/chouette/loader.rb', line 72 def create logger.info "Create schema #{schema}" with_pg_password do execute!("psql -c 'CREATE SCHEMA \"#{schema}\";' #{}") end self end |
#drop ⇒ Object
80 81 82 83 84 85 86 |
# File 'app/models/chouette/loader.rb', line 80 def drop logger.info "Drop schema #{schema}" with_pg_password do execute!("psql -c 'DROP SCHEMA \"#{schema}\" CASCADE;' #{}") end self end |
#import(file, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/models/chouette/loader.rb', line 37 def import(file, = {}) = { :format => :neptune }.merge() = { :c => "import", :o => "line", :format => .delete(:format).to_s.upcase, :input_file => File.(file), :optimize_memory => true }.merge() logger.info "Import #{file} in schema #{schema}" chouette_command.run! end |
#load_dump(file) ⇒ Object
Load dump where datas are in schema ‘chouette’
17 18 19 20 21 22 23 |
# File 'app/models/chouette/loader.rb', line 17 def load_dump(file) logger.info "Load #{file} in schema #{schema}" with_pg_password do execute!("sed -e 's/ chouette/ \"#{schema}\"/' -e 's/ OWNER TO .*;/ OWNER TO #{user};/' #{file} | psql #{} --set ON_ERROR_ROLLBACK=1 --set ON_ERROR_STOP=1") end self end |
#pg_options ⇒ Object
64 65 66 67 68 69 70 |
# File 'app/models/chouette/loader.rb', line 64 def [].tap do || << "-U #{user}" if user << "-h #{host}" if host << database end.join(" ") end |
#with_pg_password(&block) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'app/models/chouette/loader.rb', line 88 def with_pg_password(&block) ENV['PGPASSWORD'] = password.to_s if password begin yield ensure ENV['PGPASSWORD'] = nil end end |