Method: Dataflow.import

Defined in:
lib/dataflow-rb.rb

.import(archive_path:) ⇒ Object

Raises:

  • (ArgumentError)


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/dataflow-rb.rb', line 117

def self.import(archive_path:)
  raise ArgumentError, 'expecting a tar archive file' unless archive_path.end_with?('.tar')

  # extract the tar
  folder_name = archive_path.split('/')[-1].split('.')[0]
  `tar -xvf #{archive_path}`

  # load and restore the content in the metadata.yaml
   = YAML.load_file("#{folder_name}/metadata.yaml")

  # restore the nodes
  .each do |m|
    klass = m[:_type].constantize

    # try to delete previously existing node
    begin
      previous_node = klass.find(m[:_id])
      previous_node.delete
    rescue Mongoid::Errors::DocumentNotFound
    end

    # create the node
    klass.create(m)
  end

  # look for dataset dumps and restore them
  filepaths = Dir["./#{folder_name}/**/*.gz"] + Dir["./#{folder_name}/**/*.dump"]

  filepaths.each do |filepath|
    # filepath: "./folder/db_name/dataset.1.gz"
    db_name = filepath.split('/')[2]
    dataset = filepath.split('/')[3].split('.')[0]
    n = Dataflow::Nodes::DataNode.find_by(db_name: db_name, name: dataset)
    n.restore_dataset(filepath: filepath)
  end


  # clean up the extracted folder
  `rm -rf #{folder_name}`
end