72 lines
2.8 KiB
Ruby
72 lines
2.8 KiB
Ruby
# encoding: utf-8
|
|
|
|
# =================================================================
|
|
# move archives from main site to other sites depending on category
|
|
# =================================================================
|
|
|
|
|
|
require 'mongo'
|
|
include Mongo
|
|
|
|
DB_BASE_NAME = "production"
|
|
|
|
CATEGORIES = { 'TNR' => 3, 'DOC' => 1, 'PROPERTY' => 5, 'CASHIER' => 2, 'CON' => 10 }
|
|
|
|
main_db = MongoClient.new("localhost", 27017).db("#{DB_BASE_NAME}_#{0}")
|
|
main_categories = main_db["archive_file_categories"]
|
|
main_archive_files = main_db["archive_files"]
|
|
main_archive_file_multiples = main_db["archive_file_multiples"]
|
|
|
|
CATEGORIES.each do |key, number|
|
|
# set the target DB
|
|
target_db = MongoClient.new("localhost", 27017).db("#{DB_BASE_NAME}_#{number}")
|
|
target_categories = target_db["archive_file_categories"]
|
|
target_archive_files = target_db["archive_files"]
|
|
target_archive_file_multiples = target_db["archive_file_multiples"]
|
|
|
|
if main_cat = main_categories.find_one(key: key)
|
|
# get or create category
|
|
category_id = \
|
|
if cat = target_categories.find_one(key: main_cat['key'])
|
|
cat['_id']
|
|
else
|
|
copy_category = main_cat.clone
|
|
copy_category['_id'] = BSON::ObjectId.new
|
|
copy_category['create_user_id'] = nil
|
|
copy_category['update_user_id'] = nil
|
|
target_categories.save copy_category
|
|
end
|
|
|
|
main_archive_files.find(archive_file_category_id: main_cat['_id']).each do |archive_file|
|
|
# copy archive_file
|
|
copy_archive_file = archive_file.clone
|
|
copy_archive_file['_id'] = BSON::ObjectId.new
|
|
copy_archive_file['create_user_id'] = nil
|
|
copy_archive_file['update_user_id'] = nil
|
|
copy_archive_file['tagged_ids'] = []
|
|
copy_archive_file['user_ids'] = nil
|
|
copy_archive_file['archive_file_category_id'] = category_id
|
|
target_archive_files.save copy_archive_file
|
|
|
|
main_archive_file_multiples.find(archive_file_id: archive_file['_id']).each do |archive_file_multiple|
|
|
# copy archive_file_multiples
|
|
copy_archive_file_multiple = archive_file_multiple.clone
|
|
copy_archive_file_multiple['_id'] = BSON::ObjectId.new
|
|
copy_archive_file_multiple['create_user_id'] = nil
|
|
copy_archive_file_multiple['update_user_id'] = nil
|
|
copy_archive_file_multiple['archive_file_id'] = copy_archive_file['_id']
|
|
|
|
# copy gridfs file
|
|
main_grid = Mongo::GridFileSystem.new(main_db)
|
|
file = main_grid.open("assets/archive_file_multiple/file/#{archive_file_multiple['_id']}/#{archive_file_multiple['file']}", 'r')
|
|
target_grid = Mongo::GridFileSystem.new(target_db)
|
|
target_grid.open("assets/archive_file_multiple/file/#{copy_archive_file_multiple['_id']}/#{copy_archive_file_multiple['file']}", 'w') do |f|
|
|
f.write file
|
|
end
|
|
|
|
target_archive_file_multiples.save copy_archive_file_multiple
|
|
end
|
|
end
|
|
end
|
|
end
|