Skip to content

Commit

Permalink
initialization of ES fields including deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
Tereci committed Jun 21, 2012
1 parent 0675c02 commit 44c02fc
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
25 changes: 25 additions & 0 deletions bin/es
Expand Up @@ -73,6 +73,31 @@ command :types do |c|
end
end

desc 'Init ES'
command :init do |c|
c.desc 'Execute only for one entity.'
c.default_value false
c.flag [:o, :only]

c.desc 'Init also IsDeleted and DeletedAt columns.'
c.default_value false
c.switch [:d, :deleted]

c.desc 'Verbose mode'
c.default_value false
c.switch [:v, :verbose]

c.desc 'Base files directory.'
c.default_value nil
c.flag [:b, :basedir]

c.action do |global_options,options,args|
options[:pid] = PID
options[:es_name] = ES_NAME
Es::Commands::init(options)
end
end

desc 'Load data'
command :load do |c|
c.desc 'Execute only for one entity.'
Expand Down
54 changes: 54 additions & 0 deletions lib/commands.rb
Expand Up @@ -22,6 +22,60 @@ def self.delete(options)
def self.get_types
Es::Field::FIELD_TYPES
end

def self.init(options)
pid = options[:pid]
es_name = options[:es_name]
base_dir = options[:basedir]
deleted = options[:deleted]
only = options[:only]

fail "Provide path to the loading configuration" if base_dir.nil?
filenames = Dir::glob("#{base_dir}/gen_json*.json")

# for each config file
filenames.each do |filename|
fail "File #{filename} cannot be found" unless File.exist?(filename)
load_config_file = Es::Helpers.load_config(filename)
load = Es::Load.parse(load_config_file)

load.entities.each do |entity|
next if only && entity.name != only
begin
tmp_file = Tempfile.new(entity.name)
header_row = []
content_row = []
entity.fields.each do |field|
header_row << field.name
content_row << 2147483647 if field.is_timestamp?
content_row << 1 if field.is_recordid?
content_row << "" if !field.is_recordid? && !field.is_timestamp?
end
if deleted
header_row << "IsDeleted" << "DeletedAt"
content_row << "" << ""
end
tmp_file.puts(header_row.join(","))
tmp_file.puts(content_row.join(","))
entity.file = tmp_file.path
# create temp file, link it to entity
web_dav_file = Es::Helpers.load_destination_dir(pid, entity) + '/' + Es::Helpers.destination_file(entity)
if options[:verbose]
puts "Entity #{entity.name}".bright
puts "Configuration from #{filename}"
puts "Will load from #{entity.file} to #{web_dav_file}"
puts JSON::pretty_generate(entity.to_load_fragment(pid))
end
entity.load(pid, es_name)
puts "Done" if options[:verbose]
ensure
tmp_file.close
tmp_file.unlink
end
truncate(:pid => options[:pid], :es_name => options[:es_name], :load_filenames => [filename], :timestamp => 2147483646, :entity => entity)
end
end
end

def self.truncate(options)
pid = options[:pid]
Expand Down
5 changes: 5 additions & 0 deletions lib/es.rb
Expand Up @@ -8,6 +8,7 @@
require 'active_support/ordered_hash'
require 'terminal-table'
require 'pathname'
require 'tempfile'
require 'commands'

module Es
Expand Down Expand Up @@ -424,6 +425,10 @@ def self.parse(spec)
def is_recordid?
type == RECORDID_TYPE
end

def is_timestamp?
type == TIMESTAMP_TYPE
end

def is_attribute?
type == ATTRIBUTE_TYPE
Expand Down

0 comments on commit 44c02fc

Please sign in to comment.