Class: Chouette::Loader

Inherits:
Object
  • Object
show all
Includes:
CommandLineSupport
Defined in:
app/models/chouette/loader.rb

Constant Summary collapse

@@binarisation_command =
"binarisation"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#databaseObject (readonly)

Returns the value of attribute database.



3
4
5
# File 'app/models/chouette/loader.rb', line 3

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'app/models/chouette/loader.rb', line 3

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



3
4
5
# File 'app/models/chouette/loader.rb', line 3

def password
  @password
end

#schemaObject (readonly)

Returns the value of attribute schema.



3
4
5
# File 'app/models/chouette/loader.rb', line 3

def schema
  @schema
end

#userObject (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

.chouette_command=(command) ⇒ Object



25
26
27
# File 'app/models/chouette/loader.rb', line 25

def self.chouette_command=(command)
  Chouette::Command.command = command
end

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} #{pg_options}")
  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_commandObject



33
34
35
# File 'app/models/chouette/loader.rb', line 33

def chouette_command
  @chouette_command ||= Chouette::Command.new(:schema => schema)
end

#createObject



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}\";' #{pg_options}")
  end
  self
end

#dropObject



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;' #{pg_options}")
  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, options = {})
  options = {
    :format => :neptune
  }.merge(options)

  command_options = {
    :c => "import", 
    :o => "line", 
    :format => options.delete(:format).to_s.upcase, 
    :input_file => File.expand_path(file), 
    :optimize_memory => true
  }.merge(options)

  logger.info "Import #{file} in schema #{schema}"
  chouette_command.run! command_options
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 #{pg_options} --set ON_ERROR_ROLLBACK=1 --set ON_ERROR_STOP=1")
  end
  self
end

#pg_optionsObject



64
65
66
67
68
69
70
# File 'app/models/chouette/loader.rb', line 64

def pg_options
  [].tap do |options|
    options << "-U #{user}" if user
    options << "-h #{host}" if host
    options << 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