Skip to content

Instantly share code, notes, and snippets.

@ycherniavskyi
Last active December 15, 2015 16:59
Show Gist options
  • Save ycherniavskyi/5293366 to your computer and use it in GitHub Desktop.
Save ycherniavskyi/5293366 to your computer and use it in GitHub Desktop.
Migration script from ChiliProject v2.4.0 to Redmine v2.x.x for MySQL DB.
The main target of this script is to migrate from "cool" ChiliProject journals concept to normal Redmine versions concept.
How to migrate:
1. backup your ChiliProject DB => ch_db.sql
2. run db:migrate:down from script comments
3. backup your migrated ChiliProject DB => ch_db_m.sql
4. create new DB for Redmine from ch_db_m.sql with name redmine
5. create temp DB for ChiliProject from ch_db.sql with name chiliproject
6. run script
7. rub db:migrate for Redmine
# encoding: UTF-8
# bundle exec rake db:migrate:down VERSION=20110729125454 RAILS_ENV="production"
# bundle exec rake db:migrate:down VERSION=20110519194936 RAILS_ENV="production"
# bundle exec rake db:migrate:down VERSION=20110314014400 RAILS_ENV="production"
# bundle exec rake db:migrate:down VERSION=20100804112053 RAILS_ENV="production"
# bundle exec rake db:migrate:down VERSION=20100714111654 RAILS_ENV="production"
# bundle exec rake db:migrate:down VERSION=20100714111653 RAILS_ENV="production"
# bundle exec rake db:migrate:down VERSION=20100714111652 RAILS_ENV="production" -- undefined method `collect' for nil:NilClass -> need to comment lite 53
# bundle exec rake db:migrate:down VERSION=20100714111651 RAILS_ENV="production"
require 'yaml'
require 'mysql2'
client_ch = Mysql2::Client.new(:host => 'localhost',
:username => 'chiliproject',
:password => 'chiliproject',
:database => 'chiliproject',
:encoding => 'utf8')
client_r = Mysql2::Client.new(:host => 'localhost',
:username => 'redmine',
:password => 'redmine',
:database => 'redmine',
:encoding => 'utf8')
wiki_content_journals = client_ch.query("SELECT * FROM `journals` WHERE `type` LIKE 'WikiContentJournal'")
hash_j2p = Hash.new
wiki_content_journals.each do |row|
changes_hash = YAML.load row['changes']
page_id = changes_hash['page_id']
journaled_id = row['journaled_id']
if page_id
page_id = changes_hash['page_id'][1]
hash_j2p[journaled_id] = page_id
else
page_id = hash_j2p[journaled_id]
end
client_r.query("INSERT INTO wiki_content_versions SET " \
"wiki_content_id=#{journaled_id}, " \
"page_id=#{page_id}, " \
"author_id=#{row['user_id']}, " \
"data='#{client_r.escape(changes_hash['data']).force_encoding('UTF-8')}', " \
"compression='#{changes_hash['compression']}', " \
"comments='#{row['notes']}', " \
"updated_on='#{row['created_at']}', " \
"version=#{row['version']}")
end
issue_journals = client_ch.query("SELECT * FROM `journals` WHERE `type` LIKE 'IssueJournal' ORDER BY `id` ASC")
issue_journals.each do |row|
next if row['version'] == 1
changes_hash = YAML.load row['changes']
client_r.query("INSERT INTO journals SET " \
"journalized_id=#{row['journaled_id']}, " \
"user_id=#{row['user_id']}, " \
"notes='#{client_r.escape(row['notes'])}', " \
"created_on='#{row['created_at']}', " \
"journalized_type='Issue'")
last_inserted_journal_id = client_r.last_id
changes_hash.each do |key, value|
if key.start_with? 'attachments'
prop_key = key['attachments'.length..-1]
property = 'attachment'
elsif key.start_with? 'custom_values_'
prop_key = key['custom_values_'.length..-1]
property = 'cf'
else
prop_key = key
property = 'attr'
end
old_value = if value[0].instance_of? String then
client_r.escape(value[0].force_encoding('UTF-8'))
else
value[0]
end
new_value = if value[1].instance_of? String then
client_r.escape(value[1].force_encoding('UTF-8'))
else
value[1]
end
client_r.query("INSERT INTO journal_details SET " \
"journal_id=#{last_inserted_journal_id}, " \
"property='#{property}', " \
"prop_key='#{prop_key}', " \
"old_value='#{old_value}', " \
"value='#{new_value}'")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment