Skip to content

Commit

Permalink
improving migrate task
Browse files Browse the repository at this point in the history
  • Loading branch information
daniloisr committed Apr 19, 2013
1 parent 3a1f3ae commit 6472c3f
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 84 deletions.
2 changes: 1 addition & 1 deletion lib/migrate/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/migrate/ips_migrate.rb → lib/migrate/ips.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Migrate
class IpsMigrate < Migrate::Base
class Ips < Migrate::Base
def migrate
puts "Migrando ips..."

Expand Down
58 changes: 58 additions & 0 deletions lib/migrate/projects.rb
Original file line number Diff line number Diff line change
@@ -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
57 changes: 0 additions & 57 deletions lib/migrate/projects_migrate.rb

This file was deleted.

2 changes: 1 addition & 1 deletion lib/migrate/roles_migrate.rb → lib/migrate/roles.rb
Original file line number Diff line number Diff line change
@@ -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..."

Expand Down
1 change: 0 additions & 1 deletion lib/migrate/user_map.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module Migrate

# hash para recuperar os ids antigos/novos da migracao
class UserMap
# map dos ids novos para os antigos
Expand Down
7 changes: 4 additions & 3 deletions lib/migrate/users_migrate.rb → lib/migrate/users.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Migrate
class UsersMigrate < Migrate::Base
class Users < Migrate::Base
def migrate
puts "Migrando usuarios..."

Expand All @@ -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']
Expand All @@ -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

Expand Down
1 change: 0 additions & 1 deletion lib/phpmigrate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@
require 'ostruct'

require 'migrate/base'
require 'migrate/users_migrate'
24 changes: 5 additions & 19 deletions lib/tasks/migrate.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 6472c3f

Please sign in to comment.