OK, OK, it's fairly not only for Drupal source databases :-)
STEP 1 - init databases connections
Create two new connections in database.yml file like this:
# DATABASE CONVERTING SPECIFIC
# old connection
old:
adapter: mysql
database: dbname
username: username
password: userpass
host: localhost
# new connection (may be like your development or production connection)
new:
adapter: postgresql
database: dbname
username: username
password: userpass
schema_order: development # for example
host: localhost
STEP 2 - write rake task
create convert.rake file in your lib/tasks rails application directory
1 namespace :db do
2 namespace :convert do
3 task :old2new => :environment do
4 print "OK, let's do it now!\n"; STDOUT.flush
5
6 # define universal model for old tables
7 class OldModel < ActiveRecord::Base
8 end
9
10 # demonic convertor
11 def convert(srctable, dstclass)
12 print "- converting class " + dstclass.to_s + "...\n"; STDOUT.flush
13
14 dstclass.establish_connection(:new)
15 dstclass.delete_all
16
17 OldModel.set_table_name(srctable)
18 OldModel.find(:all).each { |old|
19 new = dstclass.new
20 new = yield(old, new)
21 new.save(false)
22 }
23 end
24
25 # connect to old dying database
26 ActiveRecord::Base.establish_connection(:old)
27
28 # CONVERTING
29
30 # users
31 convert('rctc_users', User) {|old, new|
32 # prepare each field individually, yeah.
33 new.id = old.uid
34 new.login = old.name
35 new.email = old.mail
36 new.password = Digest::MD5.hexdigest(old.pass + SALT)
37 new
38 }
39
40 # convert your tables below like above
41 # ...
42
43 print "Wow! Processing complete!\n"
44 end
45 end
46 end
STEP 3 - RUN IT!
$ rake db:convert:old2new
Комментариев нет:
Отправить комментарий