From 6472c3fb96b91b206d823b506b2e98d7cfb46aca Mon Sep 17 00:00:00 2001 From: Danilo Resende Date: Fri, 19 Apr 2013 20:23:54 -0300 Subject: [PATCH] improving migrate task --- lib/migrate/base.rb | 2 +- lib/migrate/{ips_migrate.rb => ips.rb} | 2 +- lib/migrate/projects.rb | 58 ++++++++++++++++++++++ lib/migrate/projects_migrate.rb | 57 --------------------- lib/migrate/{roles_migrate.rb => roles.rb} | 2 +- lib/migrate/user_map.rb | 1 - lib/migrate/{users_migrate.rb => users.rb} | 7 +-- lib/phpmigrate.rb | 1 - lib/tasks/migrate.rake | 24 ++------- 9 files changed, 70 insertions(+), 84 deletions(-) rename lib/migrate/{ips_migrate.rb => ips.rb} (89%) create mode 100644 lib/migrate/projects.rb delete mode 100644 lib/migrate/projects_migrate.rb rename lib/migrate/{roles_migrate.rb => roles.rb} (90%) rename lib/migrate/{users_migrate.rb => users.rb} (78%) diff --git a/lib/migrate/base.rb b/lib/migrate/base.rb index cc5301f..a121b52 100644 --- a/lib/migrate/base.rb +++ b/lib/migrate/base.rb @@ -15,7 +15,7 @@ def config def mysql if @@mysql.nil? c = self::config - @@mysql = Mysql2::Client.new( + @@mysql = ::Mysql2::Client.new( :host => c.host, :username => c.user, :password => c.pass, diff --git a/lib/migrate/ips_migrate.rb b/lib/migrate/ips.rb similarity index 89% rename from lib/migrate/ips_migrate.rb rename to lib/migrate/ips.rb index 9a9ce52..99ad5c3 100644 --- a/lib/migrate/ips_migrate.rb +++ b/lib/migrate/ips.rb @@ -1,5 +1,5 @@ module Migrate - class IpsMigrate < Migrate::Base + class Ips < Migrate::Base def migrate puts "Migrando ips..." diff --git a/lib/migrate/projects.rb b/lib/migrate/projects.rb new file mode 100644 index 0000000..792e976 --- /dev/null +++ b/lib/migrate/projects.rb @@ -0,0 +1,58 @@ +module Migrate + class FakeRepository < ::ActiveRecord::Base + self.table_name = 'repositories' + end + + class Projects < Migrate::Base + def migrate + puts "Migrando projetos..." + + phpsvn_projects.each do |reg| + copy_repository_from_php(reg['alias']) + + pj = ::Project.create(name: reg['alias']) + debug_obj(pj) + + repo = FakeRepository.create(name: reg['alias'], + url: reg['nome'], + enable_autoupdate: reg['autoupdate'], + login: reg['co_login'], + password: reg['co_password'], + project_id: pj.id) + + add_users_to_project(pj, reg['id']) + end + end + + def phpsvn_projects + mysql.query('select * from projetos') + end + + def add_users_to_project(cr_project, old_project_id) + regs = mysql.query("select * from user_projetos + where id_projetos = #{old_project_id}") + + users_ids = regs.map{ |reg| ::Migrate::UserMap.new_id(reg['id_user']) } + debugger if users_ids.include?(0) + cr_project.users = ::User.find(users_ids) if users_ids.any? + end + + def copy_repository_from_php(repository) + origin = File.join(config.php_projects_path, repository) + destiny = Rails.root.join('repositories', repository) + + FileUtils.mkdir destiny + FileUtils.cp_r Dir[origin], File.join(destiny, ''), :verbose => true + end + + def create_fake_folders + puts "Criando pastas fakes para desenvolvimento..." + + regs = mysql.query('select * from projetos') + regs.each do |r| + name = r['alias'] + FileUtils.mkpath File.join(config.php_projects_path, name, name) + end + end + end +end diff --git a/lib/migrate/projects_migrate.rb b/lib/migrate/projects_migrate.rb deleted file mode 100644 index 7d7bf62..0000000 --- a/lib/migrate/projects_migrate.rb +++ /dev/null @@ -1,57 +0,0 @@ -class Migrate::ProjectsMigrate < Migrate::Base - class FakeRepository < ::ActiveRecord::Base - self.table_name = 'repositories' - end - - def migrate - puts "Migrando projetos..." - - regs = mysql.query('select * from projetos') - regs.each do |reg| - - copy_repository_from_php(reg['alias']) - - pj = ::Project.create :name => reg['alias'] - debug_obj(pj) - - repo = FakeRepository.create :name => reg['alias'], - :url => reg['nome'], - :enable_autoupdate => reg['autoupdate'], - :login => reg['co_login'], - :password => reg['co_password'], - :project_id => pj.id - - add_users_to_project(pj, reg['id']) - - end - end - - def add_users_to_project(cr_project, old_project_id) - regs = mysql.query("select * from user_projetos where id_projetos = #{old_project_id}") - users_ids = [] - regs.each do |reg| - users_ids << ::Migrate::UserMap.new_id(reg['id_user']) - end - - cr_project.users = ::User.find(users_ids) if users_ids.any? - end - - def copy_repository_from_php(repository) - origin = File.join(config.php_projects_path, repository) - destiny = Rails.root.join('repositories', repository) - - FileUtils.mkdir destiny - FileUtils.cp_r Dir[origin], File.join(destiny, ''), :verbose => true - end - - def create_fake_folders - puts "Criando pastas fakes para desenvolvimento..." - - regs = mysql.query('select * from projetos') - regs.each do |r| - name = r['alias'] - FileUtils.mkpath File.join(config.php_projects_path, name, name) - end - end - -end diff --git a/lib/migrate/roles_migrate.rb b/lib/migrate/roles.rb similarity index 90% rename from lib/migrate/roles_migrate.rb rename to lib/migrate/roles.rb index df75359..391fee2 100644 --- a/lib/migrate/roles_migrate.rb +++ b/lib/migrate/roles.rb @@ -1,7 +1,7 @@ # NOTE Papeis não serão migrados do projeto antigo module Migrate - class RolesMigrate < Migrate::Base + class Roles < Migrate::Base def migrate puts "Migrando papeis..." diff --git a/lib/migrate/user_map.rb b/lib/migrate/user_map.rb index b4c83a3..401b2ec 100644 --- a/lib/migrate/user_map.rb +++ b/lib/migrate/user_map.rb @@ -1,5 +1,4 @@ module Migrate - # hash para recuperar os ids antigos/novos da migracao class UserMap # map dos ids novos para os antigos diff --git a/lib/migrate/users_migrate.rb b/lib/migrate/users.rb similarity index 78% rename from lib/migrate/users_migrate.rb rename to lib/migrate/users.rb index 87db75b..e8b0169 100644 --- a/lib/migrate/users_migrate.rb +++ b/lib/migrate/users.rb @@ -1,5 +1,5 @@ module Migrate - class UsersMigrate < Migrate::Base + class Users < Migrate::Base def migrate puts "Migrando usuarios..." @@ -10,7 +10,8 @@ def migrate u.password = u.password_confirmation = (0...8).map{65.+(rand(25)).chr}.join u.admin = user['admin'] # NOTE todos os novos usuários serão cadastrados como mantenedores - u.role_id = ::Role.find_by_name('Mantenedor').id + # TODO find or create um papel de mantenedor + u.role_id = ::Role.find_by_name('Mantenedor').try(:id) u.ip_block = user['noipblock'] == 1 u.name = user['name'] u.email = user['email'] @@ -19,7 +20,7 @@ def migrate end debug_obj(obj) - ::Migrate::UserMap.store(obj.id, user['id']) + ::Migrate::UserMap.store(obj.id, user['id']) if obj.persisted? end end diff --git a/lib/phpmigrate.rb b/lib/phpmigrate.rb index 0b591f2..d78362d 100644 --- a/lib/phpmigrate.rb +++ b/lib/phpmigrate.rb @@ -3,4 +3,3 @@ require 'ostruct' require 'migrate/base' -require 'migrate/users_migrate' diff --git a/lib/tasks/migrate.rake b/lib/tasks/migrate.rake index dd8b389..a7b2e83 100644 --- a/lib/tasks/migrate.rake +++ b/lib/tasks/migrate.rake @@ -2,25 +2,11 @@ require 'rake/dsl_definition' require 'phpmigrate' namespace :cranelift do - namespace :migrate do - include Rake::DSL - - migrate_models = [:users, :ips, :projects] - - migrate_models.each do |model| - class_eval <<-BLOCK - task :#{model} => :environment do - Migrate::#{(model.to_s + '_migrate').classify}.new.migrate - end - BLOCK - end - - desc "Migrate all database from old php version" - task :all => :environment do - migrate_models.each do |model| - eval "Migrate::#{(model.to_s + '_migrate').classify}.new.migrate" - end - end + desc "Migrate all database from old php version" + task :migrate => :environment do + Migrate::Users.new.migrate + Migrate::Ips.new.migrate + Migrate::Projects.new.migrate end desc "Create fake php projects folders - to dev"