@@ -1,416 +1,428 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2007 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require 'active_record' |
|
18 | require 'active_record' | |
19 | require 'iconv' |
|
19 | require 'iconv' | |
20 | require 'pp' |
|
20 | require 'pp' | |
21 |
|
21 | |||
22 | namespace :redmine do |
|
22 | namespace :redmine do | |
23 | desc 'Trac migration script' |
|
23 | desc 'Trac migration script' | |
24 | task :migrate_from_trac => :environment do |
|
24 | task :migrate_from_trac => :environment do | |
25 |
|
25 | |||
26 | module TracMigrate |
|
26 | module TracMigrate | |
27 |
|
27 | |||
28 | DEFAULT_STATUS = IssueStatus.default |
|
28 | DEFAULT_STATUS = IssueStatus.default | |
29 | assigned_status = IssueStatus.find_by_position(2) |
|
29 | assigned_status = IssueStatus.find_by_position(2) | |
30 | resolved_status = IssueStatus.find_by_position(3) |
|
30 | resolved_status = IssueStatus.find_by_position(3) | |
31 | feedback_status = IssueStatus.find_by_position(4) |
|
31 | feedback_status = IssueStatus.find_by_position(4) | |
32 | closed_status = IssueStatus.find :first, :conditions => { :is_closed => true } |
|
32 | closed_status = IssueStatus.find :first, :conditions => { :is_closed => true } | |
33 | STATUS_MAPPING = {'new' => DEFAULT_STATUS, |
|
33 | STATUS_MAPPING = {'new' => DEFAULT_STATUS, | |
34 | 'reopened' => feedback_status, |
|
34 | 'reopened' => feedback_status, | |
35 | 'assigned' => assigned_status, |
|
35 | 'assigned' => assigned_status, | |
36 | 'closed' => closed_status |
|
36 | 'closed' => closed_status | |
37 | } |
|
37 | } | |
38 |
|
38 | |||
39 | priorities = Enumeration.get_values('IPRI') |
|
39 | priorities = Enumeration.get_values('IPRI') | |
40 | DEFAULT_PRIORITY = priorities[0] |
|
40 | DEFAULT_PRIORITY = priorities[0] | |
41 | PRIORITY_MAPPING = {'lowest' => priorities[0], |
|
41 | PRIORITY_MAPPING = {'lowest' => priorities[0], | |
42 | 'low' => priorities[0], |
|
42 | 'low' => priorities[0], | |
43 | 'normal' => priorities[1], |
|
43 | 'normal' => priorities[1], | |
44 | 'high' => priorities[2], |
|
44 | 'high' => priorities[2], | |
45 | 'highest' => priorities[3] |
|
45 | 'highest' => priorities[3] | |
46 | } |
|
46 | } | |
47 |
|
47 | |||
48 | TRACKER_BUG = Tracker.find_by_position(1) |
|
48 | TRACKER_BUG = Tracker.find_by_position(1) | |
49 | TRACKER_FEATURE = Tracker.find_by_position(2) |
|
49 | TRACKER_FEATURE = Tracker.find_by_position(2) | |
50 | DEFAULT_TRACKER = TRACKER_BUG |
|
50 | DEFAULT_TRACKER = TRACKER_BUG | |
51 | TRACKER_MAPPING = {'defect' => TRACKER_BUG, |
|
51 | TRACKER_MAPPING = {'defect' => TRACKER_BUG, | |
52 | 'enhancement' => TRACKER_FEATURE, |
|
52 | 'enhancement' => TRACKER_FEATURE, | |
53 | 'task' => TRACKER_FEATURE, |
|
53 | 'task' => TRACKER_FEATURE, | |
54 | 'patch' =>TRACKER_FEATURE |
|
54 | 'patch' =>TRACKER_FEATURE | |
55 | } |
|
55 | } | |
56 |
|
56 | |||
57 | DEFAULT_ROLE = Role.find_by_position(3) |
|
57 | DEFAULT_ROLE = Role.find_by_position(3) | |
58 | manager_role = Role.find_by_position(1) |
|
58 | manager_role = Role.find_by_position(1) | |
59 | developer_role = Role.find_by_position(2) |
|
59 | developer_role = Role.find_by_position(2) | |
60 | ROLE_MAPPING = {'admin' => manager_role, |
|
60 | ROLE_MAPPING = {'admin' => manager_role, | |
61 | 'developer' => developer_role |
|
61 | 'developer' => developer_role | |
62 | } |
|
62 | } | |
63 |
|
63 | |||
64 | class TracComponent < ActiveRecord::Base |
|
64 | class TracComponent < ActiveRecord::Base | |
65 | set_table_name :component |
|
65 | set_table_name :component | |
66 | end |
|
66 | end | |
67 |
|
67 | |||
68 | class TracMilestone < ActiveRecord::Base |
|
68 | class TracMilestone < ActiveRecord::Base | |
69 | set_table_name :milestone |
|
69 | set_table_name :milestone | |
70 |
|
70 | |||
71 | def due |
|
71 | def due | |
72 | if read_attribute(:due) > 0 |
|
72 | if read_attribute(:due) > 0 | |
73 | Time.at(read_attribute(:due)).to_date |
|
73 | Time.at(read_attribute(:due)).to_date | |
74 | else |
|
74 | else | |
75 | nil |
|
75 | nil | |
76 | end |
|
76 | end | |
77 | end |
|
77 | end | |
78 | end |
|
78 | end | |
79 |
|
79 | |||
80 | class TracTicketCustom < ActiveRecord::Base |
|
80 | class TracTicketCustom < ActiveRecord::Base | |
81 | set_table_name :ticket_custom |
|
81 | set_table_name :ticket_custom | |
82 | end |
|
82 | end | |
83 |
|
83 | |||
84 | class TracTicket < ActiveRecord::Base |
|
84 | class TracTicket < ActiveRecord::Base | |
85 | set_table_name :ticket |
|
85 | set_table_name :ticket | |
86 | set_inheritance_column :none |
|
86 | set_inheritance_column :none | |
87 |
|
87 | |||
88 | has_many :comments, :class_name => "TracTicketChange", :foreign_key => :ticket, :conditions => "field = 'comment'" |
|
88 | has_many :comments, :class_name => "TracTicketChange", :foreign_key => :ticket, :conditions => "field = 'comment'" | |
89 | has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "type = 'ticket'" |
|
89 | has_many :attachments, :class_name => "TracAttachment", :foreign_key => :id, :conditions => "type = 'ticket'" | |
90 | has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket |
|
90 | has_many :customs, :class_name => "TracTicketCustom", :foreign_key => :ticket | |
91 |
|
91 | |||
92 | def ticket_type |
|
92 | def ticket_type | |
93 | read_attribute(:type) |
|
93 | read_attribute(:type) | |
94 | end |
|
94 | end | |
95 |
|
95 | |||
96 | def summary |
|
96 | def summary | |
97 | read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary) |
|
97 | read_attribute(:summary).blank? ? "(no subject)" : read_attribute(:summary) | |
98 | end |
|
98 | end | |
99 |
|
99 | |||
100 | def description |
|
100 | def description | |
101 | read_attribute(:description).blank? ? summary : read_attribute(:description) |
|
101 | read_attribute(:description).blank? ? summary : read_attribute(:description) | |
102 | end |
|
102 | end | |
103 |
|
103 | |||
104 | def time; Time.at(read_attribute(:time)) end |
|
104 | def time; Time.at(read_attribute(:time)) end | |
105 | end |
|
105 | end | |
106 |
|
106 | |||
107 | class TracTicketChange < ActiveRecord::Base |
|
107 | class TracTicketChange < ActiveRecord::Base | |
108 | set_table_name :ticket_change |
|
108 | set_table_name :ticket_change | |
109 |
|
109 | |||
110 | def time; Time.at(read_attribute(:time)) end |
|
110 | def time; Time.at(read_attribute(:time)) end | |
111 | end |
|
111 | end | |
112 |
|
112 | |||
113 | class TracAttachment < ActiveRecord::Base |
|
113 | class TracAttachment < ActiveRecord::Base | |
114 | set_table_name :attachment |
|
114 | set_table_name :attachment | |
115 | set_inheritance_column :none |
|
115 | set_inheritance_column :none | |
116 |
|
116 | |||
117 | def time; Time.at(read_attribute(:time)) end |
|
117 | def time; Time.at(read_attribute(:time)) end | |
118 |
|
118 | |||
119 | def original_filename |
|
119 | def original_filename | |
120 | filename |
|
120 | filename | |
121 | end |
|
121 | end | |
122 |
|
122 | |||
123 | def content_type |
|
123 | def content_type | |
124 | Redmine::MimeType.of(filename) || '' |
|
124 | Redmine::MimeType.of(filename) || '' | |
125 | end |
|
125 | end | |
126 |
|
126 | |||
127 | def exist? |
|
127 | def exist? | |
128 | File.file? trac_fullpath |
|
128 | File.file? trac_fullpath | |
129 | end |
|
129 | end | |
130 |
|
130 | |||
131 | def read |
|
131 | def read | |
132 | File.open("#{trac_fullpath}", 'rb').read |
|
132 | File.open("#{trac_fullpath}", 'rb').read | |
133 | end |
|
133 | end | |
134 |
|
134 | |||
135 | private |
|
135 | private | |
136 | def trac_fullpath |
|
136 | def trac_fullpath | |
137 | attachment_type = read_attribute(:type) |
|
137 | attachment_type = read_attribute(:type) | |
138 | trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) } |
|
138 | trac_file = filename.gsub( /[^a-zA-Z0-9\-_\.!~*']/n ) {|x| sprintf('%%%02x', x[0]) } | |
139 |
"#{TracMigrate.trac_directory}/ |
|
139 | "#{TracMigrate.trac_attachments_directory}/#{attachment_type}/#{id}/#{trac_file}" | |
140 | end |
|
140 | end | |
141 | end |
|
141 | end | |
142 |
|
142 | |||
143 | class TracWikiPage < ActiveRecord::Base |
|
143 | class TracWikiPage < ActiveRecord::Base | |
144 | set_table_name :wiki |
|
144 | set_table_name :wiki | |
145 | end |
|
145 | end | |
146 |
|
146 | |||
147 | class TracPermission < ActiveRecord::Base |
|
147 | class TracPermission < ActiveRecord::Base | |
148 | set_table_name :permission |
|
148 | set_table_name :permission | |
149 | end |
|
149 | end | |
150 |
|
150 | |||
151 | def self.find_or_create_user(username, project_member = false) |
|
151 | def self.find_or_create_user(username, project_member = false) | |
152 | u = User.find_by_login(username) |
|
152 | u = User.find_by_login(username) | |
153 | if !u |
|
153 | if !u | |
154 | # Create a new user if not found |
|
154 | # Create a new user if not found | |
155 | mail = username[0,limit_for(User, 'mail')] |
|
155 | mail = username[0,limit_for(User, 'mail')] | |
156 | mail = "#{mail}@foo.bar" unless mail.include?("@") |
|
156 | mail = "#{mail}@foo.bar" unless mail.include?("@") | |
157 | u = User.new :firstname => username[0,limit_for(User, 'firstname')].gsub(/[^\w\s\'\-]/i, '-'), |
|
157 | u = User.new :firstname => username[0,limit_for(User, 'firstname')].gsub(/[^\w\s\'\-]/i, '-'), | |
158 | :lastname => '-', |
|
158 | :lastname => '-', | |
159 | :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-') |
|
159 | :mail => mail.gsub(/[^-@a-z0-9\.]/i, '-') | |
160 | u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-') |
|
160 | u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-') | |
161 | u.password = 'trac' |
|
161 | u.password = 'trac' | |
162 | u.admin = true if TracPermission.find_by_username_and_action(username, 'admin') |
|
162 | u.admin = true if TracPermission.find_by_username_and_action(username, 'admin') | |
163 | # finally, a default user is used if the new user is not valid |
|
163 | # finally, a default user is used if the new user is not valid | |
164 | u = User.find(:first) unless u.save |
|
164 | u = User.find(:first) unless u.save | |
165 | end |
|
165 | end | |
166 | # Make sure he is a member of the project |
|
166 | # Make sure he is a member of the project | |
167 | if project_member && !u.member_of?(@target_project) |
|
167 | if project_member && !u.member_of?(@target_project) | |
168 | role = DEFAULT_ROLE |
|
168 | role = DEFAULT_ROLE | |
169 | if u.admin |
|
169 | if u.admin | |
170 | role = ROLE_MAPPING['admin'] |
|
170 | role = ROLE_MAPPING['admin'] | |
171 | elsif TracPermission.find_by_username_and_action(username, 'developer') |
|
171 | elsif TracPermission.find_by_username_and_action(username, 'developer') | |
172 | role = ROLE_MAPPING['developer'] |
|
172 | role = ROLE_MAPPING['developer'] | |
173 | end |
|
173 | end | |
174 | Member.create(:user => u, :project => @target_project, :role => DEFAULT_ROLE) |
|
174 | Member.create(:user => u, :project => @target_project, :role => DEFAULT_ROLE) | |
175 | u.reload |
|
175 | u.reload | |
176 | end |
|
176 | end | |
177 | u |
|
177 | u | |
178 | end |
|
178 | end | |
179 |
|
179 | |||
180 | # Basic wiki syntax conversion |
|
180 | # Basic wiki syntax conversion | |
181 | def self.convert_wiki_text(text) |
|
181 | def self.convert_wiki_text(text) | |
182 | # Titles |
|
182 | # Titles | |
183 | text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "h#{$1.length}. #{$2}\n"} |
|
183 | text = text.gsub(/^(\=+)\s(.+)\s(\=+)/) {|s| "h#{$1.length}. #{$2}\n"} | |
184 | # Links |
|
184 | # Links | |
185 | text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"} |
|
185 | text = text.gsub(/\[(http[^\s]+)\s+([^\]]+)\]/) {|s| "\"#{$2}\":#{$1}"} | |
186 | # Revisions links |
|
186 | # Revisions links | |
187 | text = text.gsub(/\[(\d+)\]/, 'r\1') |
|
187 | text = text.gsub(/\[(\d+)\]/, 'r\1') | |
188 | text |
|
188 | text | |
189 | end |
|
189 | end | |
190 |
|
190 | |||
191 | def self.migrate |
|
191 | def self.migrate | |
192 | # Quick database test before clearing Redmine data |
|
192 | # Quick database test before clearing Redmine data | |
193 | TracComponent.count |
|
193 | TracComponent.count | |
194 |
|
194 | |||
195 | puts "Deleting data" |
|
195 | puts "Deleting data" | |
196 | CustomField.destroy_all |
|
196 | CustomField.destroy_all | |
197 | Issue.destroy_all |
|
197 | Issue.destroy_all | |
198 | IssueCategory.destroy_all |
|
198 | IssueCategory.destroy_all | |
199 | Version.destroy_all |
|
199 | Version.destroy_all | |
200 | User.destroy_all "login <> 'admin'" |
|
200 | User.destroy_all "login <> 'admin'" | |
201 |
|
201 | |||
202 | migrated_components = 0 |
|
202 | migrated_components = 0 | |
203 | migrated_milestones = 0 |
|
203 | migrated_milestones = 0 | |
204 | migrated_tickets = 0 |
|
204 | migrated_tickets = 0 | |
205 | migrated_ticket_comments = 0 |
|
205 | migrated_ticket_comments = 0 | |
206 | migrated_custom_values = 0 |
|
206 | migrated_custom_values = 0 | |
207 | trac_ticket_comments = 0 |
|
207 | trac_ticket_comments = 0 | |
208 | migrated_ticket_attachments = 0 |
|
208 | migrated_ticket_attachments = 0 | |
209 | migrated_wiki_edits = 0 |
|
209 | migrated_wiki_edits = 0 | |
210 |
|
210 | |||
211 | # Components |
|
211 | # Components | |
212 | print "Migrating components" |
|
212 | print "Migrating components" | |
213 | issues_category_map = {} |
|
213 | issues_category_map = {} | |
214 | TracComponent.find(:all).each do |component| |
|
214 | TracComponent.find(:all).each do |component| | |
215 | print '.' |
|
215 | print '.' | |
216 | c = IssueCategory.new :project => @target_project, |
|
216 | c = IssueCategory.new :project => @target_project, | |
217 | :name => encode(component.name[0, limit_for(IssueCategory, 'name')]) |
|
217 | :name => encode(component.name[0, limit_for(IssueCategory, 'name')]) | |
218 | next unless c.save |
|
218 | next unless c.save | |
219 | issues_category_map[component.name] = c |
|
219 | issues_category_map[component.name] = c | |
220 | migrated_components += 1 |
|
220 | migrated_components += 1 | |
221 | end |
|
221 | end | |
222 | puts |
|
222 | puts | |
223 |
|
223 | |||
224 | # Milestones |
|
224 | # Milestones | |
225 | print "Migrating milestones" |
|
225 | print "Migrating milestones" | |
226 | version_map = {} |
|
226 | version_map = {} | |
227 | TracMilestone.find(:all).each do |milestone| |
|
227 | TracMilestone.find(:all).each do |milestone| | |
228 | print '.' |
|
228 | print '.' | |
229 | v = Version.new :project => @target_project, |
|
229 | v = Version.new :project => @target_project, | |
230 | :name => encode(milestone.name[0, limit_for(Version, 'name')]), |
|
230 | :name => encode(milestone.name[0, limit_for(Version, 'name')]), | |
231 | :description => encode(milestone.description[0, limit_for(Version, 'description')]), |
|
231 | :description => encode(milestone.description[0, limit_for(Version, 'description')]), | |
232 | :effective_date => milestone.due |
|
232 | :effective_date => milestone.due | |
233 | next unless v.save |
|
233 | next unless v.save | |
234 | version_map[milestone.name] = v |
|
234 | version_map[milestone.name] = v | |
235 | migrated_milestones += 1 |
|
235 | migrated_milestones += 1 | |
236 | end |
|
236 | end | |
237 | puts |
|
237 | puts | |
238 |
|
238 | |||
239 | # Custom fields |
|
239 | # Custom fields | |
240 | # TODO: read trac.ini instead |
|
240 | # TODO: read trac.ini instead | |
241 | print "Custom fields" |
|
241 | print "Custom fields" | |
242 | custom_field_map = {} |
|
242 | custom_field_map = {} | |
243 | TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field| |
|
243 | TracTicketCustom.find_by_sql("SELECT DISTINCT name FROM #{TracTicketCustom.table_name}").each do |field| | |
244 | print '.' |
|
244 | print '.' | |
245 | f = IssueCustomField.new :name => encode(field.name[0, limit_for(IssueCustomField, 'name')]), |
|
245 | f = IssueCustomField.new :name => encode(field.name[0, limit_for(IssueCustomField, 'name')]), | |
246 | :field_format => 'string', |
|
246 | :field_format => 'string', | |
247 | :is_for_all => true |
|
247 | :is_for_all => true | |
248 | next unless f.save |
|
248 | next unless f.save | |
249 | f.trackers = Tracker.find(:all) |
|
249 | f.trackers = Tracker.find(:all) | |
250 | custom_field_map[field.name] = f |
|
250 | custom_field_map[field.name] = f | |
251 | end |
|
251 | end | |
252 | puts |
|
252 | puts | |
253 |
|
253 | |||
254 | # Tickets |
|
254 | # Tickets | |
255 | print "Migrating tickets" |
|
255 | print "Migrating tickets" | |
256 | TracTicket.find(:all).each do |ticket| |
|
256 | TracTicket.find(:all).each do |ticket| | |
257 | print '.' |
|
257 | print '.' | |
258 | i = Issue.new :project => @target_project, |
|
258 | i = Issue.new :project => @target_project, | |
259 | :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]), |
|
259 | :subject => encode(ticket.summary[0, limit_for(Issue, 'subject')]), | |
260 | :description => convert_wiki_text(encode(ticket.description)), |
|
260 | :description => convert_wiki_text(encode(ticket.description)), | |
261 | :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY, |
|
261 | :priority => PRIORITY_MAPPING[ticket.priority] || DEFAULT_PRIORITY, | |
262 | :created_on => ticket.time |
|
262 | :created_on => ticket.time | |
263 | i.author = find_or_create_user(ticket.reporter) |
|
263 | i.author = find_or_create_user(ticket.reporter) | |
264 | i.category = issues_category_map[ticket.component] unless ticket.component.blank? |
|
264 | i.category = issues_category_map[ticket.component] unless ticket.component.blank? | |
265 | i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank? |
|
265 | i.fixed_version = version_map[ticket.milestone] unless ticket.milestone.blank? | |
266 | i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS |
|
266 | i.status = STATUS_MAPPING[ticket.status] || DEFAULT_STATUS | |
267 | i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER |
|
267 | i.tracker = TRACKER_MAPPING[ticket.ticket_type] || DEFAULT_TRACKER | |
268 | i.id = ticket.id |
|
268 | i.id = ticket.id | |
269 | next unless i.save |
|
269 | next unless i.save | |
270 | migrated_tickets += 1 |
|
270 | migrated_tickets += 1 | |
271 |
|
271 | |||
272 | # Owner |
|
272 | # Owner | |
273 | unless ticket.owner.blank? |
|
273 | unless ticket.owner.blank? | |
274 | i.assigned_to = find_or_create_user(ticket.owner, true) |
|
274 | i.assigned_to = find_or_create_user(ticket.owner, true) | |
275 | i.save |
|
275 | i.save | |
276 | end |
|
276 | end | |
277 |
|
277 | |||
278 | # Comments |
|
278 | # Comments | |
279 | # TODO: migrate status changes history |
|
279 | # TODO: migrate status changes history | |
280 | ticket.comments.each do |comment| |
|
280 | ticket.comments.each do |comment| | |
281 | next if comment.newvalue.blank? |
|
281 | next if comment.newvalue.blank? | |
282 | trac_ticket_comments += 1 |
|
282 | trac_ticket_comments += 1 | |
283 | n = Journal.new :notes => convert_wiki_text(encode(comment.newvalue)), |
|
283 | n = Journal.new :notes => convert_wiki_text(encode(comment.newvalue)), | |
284 | :created_on => comment.time |
|
284 | :created_on => comment.time | |
285 | n.user = find_or_create_user(comment.author) |
|
285 | n.user = find_or_create_user(comment.author) | |
286 | n.journalized = i |
|
286 | n.journalized = i | |
287 | migrated_ticket_comments += 1 if n.save |
|
287 | migrated_ticket_comments += 1 if n.save | |
288 | end |
|
288 | end | |
289 |
|
289 | |||
290 | # Attachments |
|
290 | # Attachments | |
291 | ticket.attachments.each do |attachment| |
|
291 | ticket.attachments.each do |attachment| | |
292 | next unless attachment.exist? |
|
292 | next unless attachment.exist? | |
293 | a = Attachment.new :created_on => attachment.time |
|
293 | a = Attachment.new :created_on => attachment.time | |
294 | a.file = attachment |
|
294 | a.file = attachment | |
295 | a.author = find_or_create_user(attachment.author) |
|
295 | a.author = find_or_create_user(attachment.author) | |
296 | a.container = i |
|
296 | a.container = i | |
297 | migrated_ticket_attachments += 1 if a.save |
|
297 | migrated_ticket_attachments += 1 if a.save | |
298 | end |
|
298 | end | |
299 |
|
299 | |||
300 | # Custom fields |
|
300 | # Custom fields | |
301 | ticket.customs.each do |custom| |
|
301 | ticket.customs.each do |custom| | |
302 | v = CustomValue.new :custom_field => custom_field_map[custom.name], |
|
302 | v = CustomValue.new :custom_field => custom_field_map[custom.name], | |
303 | :value => custom.value |
|
303 | :value => custom.value | |
304 | v.customized = i |
|
304 | v.customized = i | |
305 | next unless v.save |
|
305 | next unless v.save | |
306 | migrated_custom_values += 1 |
|
306 | migrated_custom_values += 1 | |
307 | end |
|
307 | end | |
308 | end |
|
308 | end | |
309 | puts |
|
309 | puts | |
310 |
|
310 | |||
311 | # Wiki |
|
311 | # Wiki | |
312 | print "Migrating wiki" |
|
312 | print "Migrating wiki" | |
313 | @target_project.wiki.destroy if @target_project.wiki |
|
313 | @target_project.wiki.destroy if @target_project.wiki | |
314 | @target_project.reload |
|
314 | @target_project.reload | |
315 | wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart') |
|
315 | wiki = Wiki.new(:project => @target_project, :start_page => 'WikiStart') | |
316 | if wiki.save |
|
316 | if wiki.save | |
317 | TracWikiPage.find(:all, :order => 'name, version').each do |page| |
|
317 | TracWikiPage.find(:all, :order => 'name, version').each do |page| | |
318 | print '.' |
|
318 | print '.' | |
319 | p = wiki.find_or_new_page(page.name) |
|
319 | p = wiki.find_or_new_page(page.name) | |
320 | p.content = WikiContent.new(:page => p) if p.new_record? |
|
320 | p.content = WikiContent.new(:page => p) if p.new_record? | |
321 | p.content.text = page.text |
|
321 | p.content.text = page.text | |
322 | p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac' |
|
322 | p.content.author = find_or_create_user(page.author) unless page.author.blank? || page.author == 'trac' | |
323 | p.content.comments = page.comment |
|
323 | p.content.comments = page.comment | |
324 | p.new_record? ? p.save : p.content.save |
|
324 | p.new_record? ? p.save : p.content.save | |
325 | migrated_wiki_edits += 1 unless p.content.new_record? |
|
325 | migrated_wiki_edits += 1 unless p.content.new_record? | |
326 | end |
|
326 | end | |
327 |
|
327 | |||
328 | wiki.reload |
|
328 | wiki.reload | |
329 | wiki.pages.each do |page| |
|
329 | wiki.pages.each do |page| | |
330 | page.content.text = convert_wiki_text(page.content.text) |
|
330 | page.content.text = convert_wiki_text(page.content.text) | |
331 | page.content.save |
|
331 | page.content.save | |
332 | end |
|
332 | end | |
333 | end |
|
333 | end | |
334 | puts |
|
334 | puts | |
335 |
|
335 | |||
336 | puts |
|
336 | puts | |
337 | puts "Components: #{migrated_components}/#{TracComponent.count}" |
|
337 | puts "Components: #{migrated_components}/#{TracComponent.count}" | |
338 | puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}" |
|
338 | puts "Milestones: #{migrated_milestones}/#{TracMilestone.count}" | |
339 | puts "Tickets: #{migrated_tickets}/#{TracTicket.count}" |
|
339 | puts "Tickets: #{migrated_tickets}/#{TracTicket.count}" | |
340 | puts "Ticket comments: #{migrated_ticket_comments}/#{trac_ticket_comments}" |
|
340 | puts "Ticket comments: #{migrated_ticket_comments}/#{trac_ticket_comments}" | |
341 | puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count("type = 'ticket'").to_s |
|
341 | puts "Ticket files: #{migrated_ticket_attachments}/" + TracAttachment.count("type = 'ticket'").to_s | |
342 | puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}" |
|
342 | puts "Custom values: #{migrated_custom_values}/#{TracTicketCustom.count}" | |
343 | puts "Wiki edits: #{migrated_wiki_edits}/#{TracWikiPage.count}" |
|
343 | puts "Wiki edits: #{migrated_wiki_edits}/#{TracWikiPage.count}" | |
344 | end |
|
344 | end | |
345 |
|
345 | |||
346 | def self.limit_for(klass, attribute) |
|
346 | def self.limit_for(klass, attribute) | |
347 | klass.columns_hash[attribute.to_s].limit |
|
347 | klass.columns_hash[attribute.to_s].limit | |
348 | end |
|
348 | end | |
349 |
|
349 | |||
350 | def self.encoding(charset) |
|
350 | def self.encoding(charset) | |
351 | @ic = Iconv.new('UTF-8', charset) |
|
351 | @ic = Iconv.new('UTF-8', charset) | |
352 | rescue Iconv::InvalidEncoding |
|
352 | rescue Iconv::InvalidEncoding | |
|
353 | puts "Invalid encoding!" | |||
353 |
return false |
|
354 | return false | |
354 | end |
|
355 | end | |
355 |
|
356 | |||
356 |
def self.trac_directory |
|
357 | def self.set_trac_directory(path) | |
357 |
@trac_directory = path |
|
358 | @trac_directory = path | |
|
359 | raise "This directory doesn't exist!" unless File.directory?(path) | |||
|
360 | raise "#{trac_db_path} doesn't exist!" unless File.exist?(trac_db_path) | |||
|
361 | raise "#{trac_attachments_directory} doesn't exist!" unless File.directory?(trac_attachments_directory) | |||
|
362 | @trac_directory | |||
|
363 | rescue Exception => e | |||
|
364 | puts e | |||
|
365 | return false | |||
358 | end |
|
366 | end | |
359 |
|
367 | |||
360 | def self.trac_directory |
|
368 | def self.trac_directory | |
361 | @trac_directory |
|
369 | @trac_directory | |
362 | end |
|
370 | end | |
363 |
|
371 | |||
|
372 | def self.trac_db_path; "#{trac_directory}/db/trac.db" end | |||
|
373 | def self.trac_attachments_directory; "#{trac_directory}/attachments" end | |||
|
374 | ||||
364 | def self.target_project_identifier(identifier) |
|
375 | def self.target_project_identifier(identifier) | |
365 |
|
|
376 | project = Project.find_by_identifier(identifier) | |
|
377 | if !project | |||
|
378 | # create the target project | |||
|
379 | project = Project.new :name => identifier.humanize, | |||
|
380 | :description => identifier.humanize | |||
|
381 | project.identifier = identifier | |||
|
382 | puts "Unable to create a project with identifier '#{identifier}'!" unless project.save | |||
|
383 | end | |||
|
384 | @target_project = project.new_record? ? nil : project | |||
366 | end |
|
385 | end | |
367 |
|
386 | |||
368 | def self.establish_connection(params) |
|
387 | def self.establish_connection(params) | |
369 | constants.each do |const| |
|
388 | constants.each do |const| | |
370 | klass = const_get(const) |
|
389 | klass = const_get(const) | |
371 | next unless klass.respond_to? 'establish_connection' |
|
390 | next unless klass.respond_to? 'establish_connection' | |
372 | klass.establish_connection params |
|
391 | klass.establish_connection params | |
373 | end |
|
392 | end | |
374 | end |
|
393 | end | |
375 |
|
394 | |||
376 | private |
|
395 | private | |
377 | def self.encode(text) |
|
396 | def self.encode(text) | |
378 | @ic.iconv text |
|
397 | @ic.iconv text | |
379 | rescue |
|
398 | rescue | |
380 | text |
|
399 | text | |
381 | end |
|
400 | end | |
382 | end |
|
401 | end | |
383 |
|
402 | |||
384 | puts |
|
403 | puts | |
385 | puts "WARNING: Your Redmine data will be deleted during this process." |
|
404 | puts "WARNING: Your Redmine data will be deleted during this process." | |
386 | print "Are you sure you want to continue ? [y/N] " |
|
405 | print "Are you sure you want to continue ? [y/N] " | |
387 | break unless STDIN.gets.match(/^y$/i) |
|
406 | break unless STDIN.gets.match(/^y$/i) | |
388 | puts |
|
407 | puts | |
389 |
|
408 | |||
|
409 | def prompt(text, options = {}, &block) | |||
|
410 | default = options[:default] || '' | |||
390 | while true |
|
411 | while true | |
391 | print "Trac directory: " |
|
412 | print "#{text} [#{default}]: " | |
392 |
|
|
413 | value = STDIN.gets.chomp! | |
393 | TracMigrate.trac_directory = directory |
|
414 | value = default if value.blank? | |
394 | break if TracMigrate.trac_directory |
|
415 | break if yield value | |
395 | puts " This directory doesn't exist!" |
|
|||
396 | end |
|
416 | end | |
397 | while true |
|
|||
398 | print "Database encoding [UTF-8]: " |
|
|||
399 | encoding = STDIN.gets.chomp! |
|
|||
400 | encoding = 'UTF-8' if encoding.blank? |
|
|||
401 | break if TracMigrate.encoding encoding |
|
|||
402 | puts " Invalid encoding!" |
|
|||
403 | end |
|
|||
404 | while true |
|
|||
405 | print "Target project identifier: " |
|
|||
406 | identifier = STDIN.gets.chomp! |
|
|||
407 | break if TracMigrate.target_project_identifier identifier |
|
|||
408 | puts " Project not found in Redmine database!" |
|
|||
409 | end |
|
417 | end | |
|
418 | ||||
|
419 | prompt('Trac directory') {|directory| TracMigrate.set_trac_directory directory} | |||
|
420 | prompt('Database encoding', :default => 'UTF-8') {|encoding| TracMigrate.encoding encoding} | |||
|
421 | prompt('Target project identifier') {|identifier| TracMigrate.target_project_identifier identifier} | |||
410 | puts |
|
422 | puts | |
411 |
|
423 | |||
412 | TracMigrate.establish_connection({:adapter => 'sqlite', |
|
424 | TracMigrate.establish_connection({:adapter => 'sqlite', | |
413 |
:database => "#{TracMigrate.trac_d |
|
425 | :database => "#{TracMigrate.trac_db_path}"}) | |
414 | TracMigrate.migrate |
|
426 | TracMigrate.migrate | |
415 | end |
|
427 | end | |
416 | end |
|
428 | end |
General Comments 0
You need to be logged in to leave comments.
Login now