You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

redmine.rake 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2023 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  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
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. namespace :redmine do
  18. namespace :attachments do
  19. desc 'Removes uploaded files left unattached after one day.'
  20. task :prune => :environment do
  21. Attachment.prune
  22. end
  23. desc 'Moves attachments stored at the root of the file directory (ie. created before Redmine 2.3) to their subdirectories'
  24. task :move_to_subdirectories => :environment do
  25. Attachment.move_from_root_to_target_directory
  26. end
  27. desc 'Updates attachment digests to SHA256'
  28. task :update_digests => :environment do
  29. Attachment.update_digests_to_sha256
  30. end
  31. end
  32. namespace :tokens do
  33. desc 'Removes expired tokens.'
  34. task :prune => :environment do
  35. Token.destroy_expired
  36. end
  37. end
  38. namespace :users do
  39. desc 'Removes registered users that have not been activated after a number of days. Use DAYS to set the number of days, defaults to 30 days.'
  40. task :prune => :environment do
  41. days = 30
  42. env_days = ENV['DAYS']
  43. if env_days
  44. if env_days.to_i <= 0
  45. abort "Invalid DAYS #{env_days} given. The value must be a integer."
  46. else
  47. days = env_days.to_i
  48. end
  49. end
  50. User.prune(days.days)
  51. end
  52. end
  53. namespace :watchers do
  54. desc 'Removes watchers from what they can no longer view.'
  55. task :prune => :environment do
  56. Watcher.prune
  57. end
  58. end
  59. desc 'Fetch changesets from the repositories'
  60. task :fetch_changesets => :environment do
  61. Repository.fetch_changesets
  62. end
  63. desc 'Migrates and copies plugins assets.'
  64. task :plugins do
  65. Rake::Task["redmine:plugins:migrate"].invoke
  66. Rake::Task["redmine:plugins:assets"].invoke
  67. end
  68. desc <<-DESC
  69. FOR EXPERIMENTAL USE ONLY, Moves Redmine data from production database to the development database.
  70. This task should only be used when you need to move data from one DBMS to a different one (eg. MySQL to PostgreSQL).
  71. WARNING: All data in the development database is deleted.
  72. DESC
  73. task :migrate_dbms => :environment do
  74. ActiveRecord::Base.establish_connection :development
  75. target_tables = ActiveRecord::Base.connection.tables
  76. ActiveRecord::Base.remove_connection
  77. ActiveRecord::Base.establish_connection :production
  78. tables = ActiveRecord::Base.connection.tables.sort - %w(schema_migrations plugin_schema_info)
  79. if (tables - target_tables).any?
  80. list = (tables - target_tables).map {|table| "* #{table}"}.join("\n")
  81. abort "The following table(s) are missing from the target database:\n#{list}"
  82. end
  83. tables.each do |table_name|
  84. Source = Class.new(ActiveRecord::Base)
  85. Target = Class.new(ActiveRecord::Base)
  86. Target.establish_connection(:development)
  87. [Source, Target].each do |klass|
  88. klass.table_name = table_name
  89. klass.reset_column_information
  90. klass.inheritance_column = "foo"
  91. klass.record_timestamps = false
  92. end
  93. Target.primary_key = (Target.column_names.include?("id") ? "id" : nil)
  94. source_count = Source.count
  95. puts "Migrating %6d records from #{table_name}..." % source_count
  96. Target.delete_all
  97. offset = 0
  98. while (objects = Source.offset(offset).limit(5000).order("1,2").to_a) && objects.any?
  99. offset += objects.size
  100. Target.transaction do
  101. objects.each do |object|
  102. new_object = Target.new(object.attributes)
  103. new_object.id = object.id if Target.primary_key
  104. new_object.save(:validate => false)
  105. end
  106. end
  107. end
  108. Target.connection.reset_pk_sequence!(table_name) if Target.primary_key
  109. target_count = Target.count
  110. abort "Some records were not migrated" unless source_count == target_count
  111. Object.send(:remove_const, :Target)
  112. Object.send(:remove_const, :Source)
  113. end
  114. end
  115. namespace :plugins do
  116. desc 'Migrates installed plugins.'
  117. task :migrate => :environment do
  118. name = ENV['NAME']
  119. version = nil
  120. version_string = ENV['VERSION']
  121. if version_string
  122. if version_string =~ /^\d+$/
  123. version = version_string.to_i
  124. if name.nil?
  125. abort "The VERSION argument requires a plugin NAME."
  126. end
  127. else
  128. abort "Invalid VERSION #{version_string} given."
  129. end
  130. end
  131. begin
  132. Redmine::Plugin.migrate(name, version)
  133. rescue Redmine::PluginNotFound
  134. abort "Plugin #{name} was not found."
  135. end
  136. Rake::Task["db:schema:dump"].invoke
  137. end
  138. desc 'Copies plugins assets into the public directory.'
  139. task :assets => :environment do
  140. name = ENV['NAME']
  141. begin
  142. Redmine::PluginLoader.mirror_assets(name)
  143. rescue Redmine::PluginNotFound
  144. abort "Plugin #{name} was not found."
  145. end
  146. end
  147. desc 'Runs the plugins tests.'
  148. task :test do
  149. Rake::Task["redmine:plugins:test:units"].invoke
  150. Rake::Task["redmine:plugins:test:functionals"].invoke
  151. Rake::Task["redmine:plugins:test:integration"].invoke
  152. Rake::Task["redmine:plugins:test:system"].invoke
  153. end
  154. namespace :test do
  155. desc 'Runs the plugins unit tests.'
  156. task :units => "db:test:prepare" do |t|
  157. $: << "test"
  158. Rails::TestUnit::Runner.rake_run ["plugins/#{ENV['NAME'] || '*'}/test/unit/**/*_test.rb"]
  159. end
  160. desc 'Runs the plugins functional tests.'
  161. task :functionals => "db:test:prepare" do |t|
  162. $: << "test"
  163. Rails::TestUnit::Runner.rake_run ["plugins/#{ENV['NAME'] || '*'}/test/functional/**/*_test.rb"]
  164. end
  165. desc 'Runs the plugins integration tests.'
  166. task :integration => "db:test:prepare" do |t|
  167. $: << "test"
  168. Rails::TestUnit::Runner.rake_run ["plugins/#{ENV['NAME'] || '*'}/test/integration/**/*_test.rb"]
  169. end
  170. desc 'Runs the plugins system tests.'
  171. task :system => "db:test:prepare" do |t|
  172. $: << "test"
  173. Rails::TestUnit::Runner.rake_run ["plugins/#{ENV['NAME'] || '*'}/test/system/**/*_test.rb"]
  174. end
  175. desc 'Runs the plugins ui tests.'
  176. task :ui => "db:test:prepare" do |t|
  177. $: << "test"
  178. Rails::TestUnit::Runner.rake_run ["plugins/#{ENV['NAME'] || '*'}/test/ui/**/*_test.rb"]
  179. end
  180. end
  181. end
  182. end
  183. # Load plugins' rake tasks
  184. Dir[File.join(Rails.root, "plugins/*/lib/tasks/**/*.rake")].sort.each { |ext| load ext }