Created
December 27, 2013 09:57
-
-
Save pechorin/8144796 to your computer and use it in GitHub Desktop.
drupal -> rails db example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
namespace :migrator do | |
def db | |
connection_string = ENV['DB'] | |
default_connection_string = 'mysql://root:***@localhost:3306/***' | |
@connection ||= Sequel.connect(connection_string || default_connection_string) | |
@connection | |
end | |
def drupal_root | |
root_dir = ENV['DRUPAL_ROOT'] | |
root_dir || '/Users/rbdev/db_backup/site/2013.04.01.01.00.02/sites_backup/archives/home/rbdev/sites/apache/sail-cruise.ru/www' | |
end | |
def yachts_pdf_root | |
root_dir = ENV['ROOT_DIR'] | |
root_dir || '/Users/rbdev/db_backup/site/pdf/yachts.sail-cruise.ru/www' | |
end | |
def term(id) | |
row = db[:term_data].where(:tid => id.to_i).first | |
row ? row[:name] : nil | |
end | |
def pdf_field_file_path(nid) | |
field = db[:content_field_pdf].where(:nid => nid).first | |
if field and field[:field_pdf_fid].present? | |
file_data = db[:files].where(:fid => field[:field_pdf_fid]).first | |
if file_data | |
return File.join(drupal_root, file_data[:filepath]) | |
end | |
end | |
end | |
def pdf_file_path_in_nid_body?(nid) | |
!!pdf_file_path_from_nid_body(nid) | |
end | |
def pdf_file_path_from_nid_body(nid) | |
body = db[:node_revisions].where(:nid => nid).first[:body] | |
match_data = body.match(/\[pdf=.*yachts\.sail\-cruise\.ru(.*)\]/) | |
if match_data and match_data[1] | |
return File.join(yachts_pdf_root, match_data[1]) | |
end | |
end | |
def error_on(field, record, meta = nil) | |
raise Exception, "error on :#{field}\n #{record.inspect} \n #{meta.inspect}" | |
end | |
YACHT_TYPES = { | |
1 => 'sail', | |
2 => 'motor', | |
3 => 'gulet', | |
38 => 'catamaran' | |
} | |
# таблица node | |
# type -> 'yachtpage' (нужен только этот тип) | |
# | |
# связь данных: | |
# content_node_field_instance -> content_type_yachtpage -> term data | |
# берем имя поля -> добавляем к имени _value -> ищем тут по vid | |
# | |
# поля node | |
# - * title | |
# node_revisions.title | |
# | |
# - body | |
# node_revisions.body | |
# | |
# - Производитель | |
# content_type_yachtpage.field_creator_value | |
# | |
# - * тип яхты | |
# content_type_yachtpage.field_type_value | |
# | |
# - * мега | |
# content_type_yachtpage.field_mega_value | |
# vid:16 -> яхта | |
# vid:17 -> мега | |
# | |
# - год выпуска | |
# content_type_yachtpage.field_year_value -> to_i | |
# | |
# - страна | |
# content_field_country.field_country_value | |
# | |
# - длинна | |
# content_type_yachtpage.field_long_value | |
# | |
# - кабины | |
# content_type_yachtpage.field_cabins_value | |
# | |
# - спальные места | |
# content_type_yachtpage.field_places_value | |
# | |
# - цена | |
# content_type_yachtpage.field_price_value | |
# | |
# некоторые ноды не имеют залитого пдф-а | |
# эти пдфы лежат на yachts.sail-cruise.ru (и ссылки на них есть в body) | |
# | |
# связующая таблица - content_field_pdf(nid, field_pdf_fid) | |
# таблица файлов - files(nid, filename, filepath) | |
# | |
# - Главная картинка | |
# content_field_yacht_icon(nid, field_yacht_icon_imceimage_path) | |
# | |
# | |
# таблица vocabulary | |
# нужны только: (vid, name) | |
# * 1, Тип яхты | |
# * 2, Производитель | |
# * 3, Длинна | |
# * 4, Мега | |
# * 5, Год выпуска (относится к мега яхтам) | |
# * 6, Страна (TODO: посмотреть, нет ли там трешевых данных) | |
# * 7, Каюты | |
# * 8, Спальные места | |
# * 9, Цена | |
# | |
# все значения словарей лежат в `term_data` (tid, vid, name) | |
# соотношения между нодой -> словарем -> термом лежат в `term_node` (nid, vid, tid) | |
task :do => :environment do | |
db[:node].where(:type => 'yachtpage').each do |node| | |
yacht = Yacht.new | |
revision = db[:node_revisions].where(:nid => node[:nid]).first | |
meta = db[:content_type_yachtpage].where(:nid => node[:nid]).first | |
# -- NID | |
yacht.drupal_nid = node[:nid] | |
# -- NAME && BODY | |
yacht.name = revision[:title].strip | |
yacht.body = revision[:body].strip | |
# -- Manufacture | |
manufacture = Manufacture.find_or_create_by_name( term(meta[:field_creator_value]) ) | |
yacht.manufacture = manufacture | |
# -- is_mega | |
# 16 - обычная | |
# 17 - мега | |
if meta[:field_mega_value].blank? | |
error_on(:field_mega_value, node, meta) | |
else | |
if meta[:field_mega_value] == 17 | |
yacht.is_mega = true | |
else | |
yacht.is_mega = false | |
end | |
end | |
# -- boat type | |
# 1 - sail | |
# 2 - motor | |
# 3 - gulet | |
# 38 - catamaran | |
if meta[:field_type_value].blank? | |
error_on(:field_type_value, node, meta) | |
else | |
yacht.boat_type = YACHT_TYPES[meta[:field_type_value]] | |
end | |
# -- creation year | |
year_value = term(meta[:field_year_value]) | |
if year_value.present? | |
yacht.creation_year = year_value | |
end | |
# -- country | |
# drupal_country_field = db[:content_field_country].where(:nid => node[:nid]).first | |
# country_name = term(drupal_country_field[:field_country_value]) | |
# if country_name.present? | |
# country = Country.find_or_create_by_name(country_name) | |
# yacht.country = country | |
# end | |
# -- boat length | |
length_value = term(meta[:field_long_value]) | |
if length_value.blank? | |
error_on(:length_value, node, meta) | |
else | |
boat_length = BoatLength.find_or_create_by_name(length_value) | |
yacht.boat_length = boat_length | |
end | |
# -- cabins_amount | |
cabins_amount_value = term(meta[:field_cabins_value]) | |
if cabins_amount_value.present? | |
yacht.cabins_amount = cabins_amount_value | |
end | |
# -- sleepers_amount | |
sleepers_amount_value = term(meta[:field_places_value]) | |
if sleepers_amount_value.present? | |
yacht.sleepers_amount = sleepers_amount_value | |
end | |
# -- price | |
price_value = term(meta[:field_price_value]) | |
if price_value.present? | |
yacht.price_string = price_value | |
end | |
# -- content logo | |
logo_metadata = db[:content_field_yacht_icon].where(:nid => node[:nid]).first | |
if logo_metadata and logo_metadata[:field_yacht_icon_imceimage_path].present? | |
drupal_file_path = File.join(drupal_root, logo_metadata[:field_yacht_icon_imceimage_path]) | |
drupal_file_path = drupal_file_path.gsub('%20', ' ') | |
if File.exists?(drupal_file_path) | |
file = File.new(drupal_file_path, 'r') | |
logo = ContentLogo.new | |
logo.image = file | |
logo.save! | |
yacht.content_logo = logo | |
file.close | |
end | |
end | |
pdf_path = pdf_field_file_path(node[:nid]) || pdf_file_path_from_nid_body(node[:nid]) | |
if pdf_path | |
if File.exists?(pdf_path) | |
file = File.open(pdf_path, 'r') | |
yacht.pdf = file | |
file.close | |
if pdf_file_path_in_nid_body?(node[:nid]) | |
yacht.body = nil | |
end | |
else | |
raise Exception, "not exists -> #{pdf_path}" | |
end | |
end | |
if yacht.valid? | |
yacht.save! | |
puts "yachts created with NID => #{yacht.drupal_nid}" | |
else | |
raise Exception, yacht.errors.inspect | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment