##// END OF EJS Templates
Adds a task for moving Redmine data to a different DBMS....
Jean-Philippe Lang -
r12384:b25c7003da4b
parent child
Show More
@@ -1,126 +1,169
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 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 namespace :redmine do
18 namespace :redmine do
19 namespace :attachments do
19 namespace :attachments do
20 desc 'Removes uploaded files left unattached after one day.'
20 desc 'Removes uploaded files left unattached after one day.'
21 task :prune => :environment do
21 task :prune => :environment do
22 Attachment.prune
22 Attachment.prune
23 end
23 end
24
24
25 desc 'Moves attachments stored at the root of the file directory (ie. created before Redmine 2.3) to their subdirectories'
25 desc 'Moves attachments stored at the root of the file directory (ie. created before Redmine 2.3) to their subdirectories'
26 task :move_to_subdirectories => :environment do
26 task :move_to_subdirectories => :environment do
27 Attachment.move_from_root_to_target_directory
27 Attachment.move_from_root_to_target_directory
28 end
28 end
29 end
29 end
30
30
31 namespace :tokens do
31 namespace :tokens do
32 desc 'Removes expired tokens.'
32 desc 'Removes expired tokens.'
33 task :prune => :environment do
33 task :prune => :environment do
34 Token.destroy_expired
34 Token.destroy_expired
35 end
35 end
36 end
36 end
37
37
38 namespace :watchers do
38 namespace :watchers do
39 desc 'Removes watchers from what they can no longer view.'
39 desc 'Removes watchers from what they can no longer view.'
40 task :prune => :environment do
40 task :prune => :environment do
41 Watcher.prune
41 Watcher.prune
42 end
42 end
43 end
43 end
44
44
45 desc 'Fetch changesets from the repositories'
45 desc 'Fetch changesets from the repositories'
46 task :fetch_changesets => :environment do
46 task :fetch_changesets => :environment do
47 Repository.fetch_changesets
47 Repository.fetch_changesets
48 end
48 end
49
49
50 desc 'Migrates and copies plugins assets.'
50 desc 'Migrates and copies plugins assets.'
51 task :plugins do
51 task :plugins do
52 Rake::Task["redmine:plugins:migrate"].invoke
52 Rake::Task["redmine:plugins:migrate"].invoke
53 Rake::Task["redmine:plugins:assets"].invoke
53 Rake::Task["redmine:plugins:assets"].invoke
54 end
54 end
55
55
56 desc <<-DESC
57 FOR EXPERIMENTAL USE ONLY, Moves Redmine data from production database to the development database.
58 This task should only be used when you need to move data from one DBMS to a different one (eg. MySQL to PostgreSQL).
59 WARNING: All data in the development database is deleted.
60 DESC
61
62 task :migrate_dbms => :environment do
63 ActiveRecord::Base.establish_connection :production
64
65 (ActiveRecord::Base.connection.tables - %w(schema_migrations plugin_schema_info)).each do |table_name|
66 Source = Class.new(ActiveRecord::Base)
67 Target = Class.new(ActiveRecord::Base)
68 Target.establish_connection(:development)
69
70 [Source, Target].each do |klass|
71 klass.table_name = table_name
72 klass.reset_column_information
73 klass.inheritance_column = "foo"
74 klass.record_timestamps = false
75 end
76 Target.primary_key = (Target.column_names.include?("id") ? "id" : nil)
77
78 source_count = Source.count
79 puts "Migrating %6d records from #{table_name}..." % source_count
80
81 Target.delete_all
82 offset = 0
83 while (objects = Source.offset(offset).limit(5000).order("1,2").to_a) && objects.any?
84 offset += objects.size
85 Target.transaction do
86 objects.each do |object|
87 new_object = Target.new(object.attributes)
88 new_object.id = object.id if Target.primary_key
89 new_object.save(:validate => false)
90 end
91 end
92 end
93 Target.connection.reset_pk_sequence!(table_name) if Target.primary_key
94 target_count = Target.count
95 abort "Some records were not migrated" unless source_count == target_count
96 end
97 end
98
56 namespace :plugins do
99 namespace :plugins do
57 desc 'Migrates installed plugins.'
100 desc 'Migrates installed plugins.'
58 task :migrate => :environment do
101 task :migrate => :environment do
59 name = ENV['NAME']
102 name = ENV['NAME']
60 version = nil
103 version = nil
61 version_string = ENV['VERSION']
104 version_string = ENV['VERSION']
62 if version_string
105 if version_string
63 if version_string =~ /^\d+$/
106 if version_string =~ /^\d+$/
64 version = version_string.to_i
107 version = version_string.to_i
65 if name.nil?
108 if name.nil?
66 abort "The VERSION argument requires a plugin NAME."
109 abort "The VERSION argument requires a plugin NAME."
67 end
110 end
68 else
111 else
69 abort "Invalid VERSION #{version_string} given."
112 abort "Invalid VERSION #{version_string} given."
70 end
113 end
71 end
114 end
72
115
73 begin
116 begin
74 Redmine::Plugin.migrate(name, version)
117 Redmine::Plugin.migrate(name, version)
75 rescue Redmine::PluginNotFound
118 rescue Redmine::PluginNotFound
76 abort "Plugin #{name} was not found."
119 abort "Plugin #{name} was not found."
77 end
120 end
78
121
79 Rake::Task["db:schema:dump"].invoke
122 Rake::Task["db:schema:dump"].invoke
80 end
123 end
81
124
82 desc 'Copies plugins assets into the public directory.'
125 desc 'Copies plugins assets into the public directory.'
83 task :assets => :environment do
126 task :assets => :environment do
84 name = ENV['NAME']
127 name = ENV['NAME']
85
128
86 begin
129 begin
87 Redmine::Plugin.mirror_assets(name)
130 Redmine::Plugin.mirror_assets(name)
88 rescue Redmine::PluginNotFound
131 rescue Redmine::PluginNotFound
89 abort "Plugin #{name} was not found."
132 abort "Plugin #{name} was not found."
90 end
133 end
91 end
134 end
92
135
93 desc 'Runs the plugins tests.'
136 desc 'Runs the plugins tests.'
94 task :test do
137 task :test do
95 Rake::Task["redmine:plugins:test:units"].invoke
138 Rake::Task["redmine:plugins:test:units"].invoke
96 Rake::Task["redmine:plugins:test:functionals"].invoke
139 Rake::Task["redmine:plugins:test:functionals"].invoke
97 Rake::Task["redmine:plugins:test:integration"].invoke
140 Rake::Task["redmine:plugins:test:integration"].invoke
98 end
141 end
99
142
100 namespace :test do
143 namespace :test do
101 desc 'Runs the plugins unit tests.'
144 desc 'Runs the plugins unit tests.'
102 Rake::TestTask.new :units => "db:test:prepare" do |t|
145 Rake::TestTask.new :units => "db:test:prepare" do |t|
103 t.libs << "test"
146 t.libs << "test"
104 t.verbose = true
147 t.verbose = true
105 t.pattern = "plugins/#{ENV['NAME'] || '*'}/test/unit/**/*_test.rb"
148 t.pattern = "plugins/#{ENV['NAME'] || '*'}/test/unit/**/*_test.rb"
106 end
149 end
107
150
108 desc 'Runs the plugins functional tests.'
151 desc 'Runs the plugins functional tests.'
109 Rake::TestTask.new :functionals => "db:test:prepare" do |t|
152 Rake::TestTask.new :functionals => "db:test:prepare" do |t|
110 t.libs << "test"
153 t.libs << "test"
111 t.verbose = true
154 t.verbose = true
112 t.pattern = "plugins/#{ENV['NAME'] || '*'}/test/functional/**/*_test.rb"
155 t.pattern = "plugins/#{ENV['NAME'] || '*'}/test/functional/**/*_test.rb"
113 end
156 end
114
157
115 desc 'Runs the plugins integration tests.'
158 desc 'Runs the plugins integration tests.'
116 Rake::TestTask.new :integration => "db:test:prepare" do |t|
159 Rake::TestTask.new :integration => "db:test:prepare" do |t|
117 t.libs << "test"
160 t.libs << "test"
118 t.verbose = true
161 t.verbose = true
119 t.pattern = "plugins/#{ENV['NAME'] || '*'}/test/integration/**/*_test.rb"
162 t.pattern = "plugins/#{ENV['NAME'] || '*'}/test/integration/**/*_test.rb"
120 end
163 end
121 end
164 end
122 end
165 end
123 end
166 end
124
167
125 # Load plugins' rake tasks
168 # Load plugins' rake tasks
126 Dir[File.join(Rails.root, "plugins/*/lib/tasks/**/*.rake")].sort.each { |ext| load ext }
169 Dir[File.join(Rails.root, "plugins/*/lib/tasks/**/*.rake")].sort.each { |ext| load ext }
General Comments 0
You need to be logged in to leave comments. Login now