Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/tags/rel_0-13-1/railties/fresh_rakefile

Revision 1785, 8.7 kB (checked in by david, 3 years ago)

Added support for SQL Server in the database rake tasks #1652 [ken.barker@gmail.com]

  • Property svn:executable set to *
Line 
1 require 'rake'
2 require 'rake/testtask'
3 require 'rake/rdoctask'
4
5 $VERBOSE = nil
6 TEST_CHANGES_SINCE = Time.now - 600
7
8 desc "Run all the tests on a fresh test database"
9 task :default => [ :test_units, :test_functional ]
10
11
12 desc 'Require application environment.'
13 task :environment do
14   unless defined? RAILS_ROOT
15     require File.dirname(__FILE__) + '/config/environment'
16   end
17 end
18
19 desc "Generate API documentation, show coding stats"
20 task :doc => [ :appdoc, :stats ]
21
22
23 # Look up tests for recently modified sources.
24 def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago)
25   FileList[source_pattern].map do |path|
26     if File.mtime(path) > touched_since
27       test = "#{test_path}/#{File.basename(path, '.rb')}_test.rb"
28       test if File.exists?(test)
29     end
30   end.compact
31 end
32
33 desc 'Test recent changes.'
34 Rake::TestTask.new(:recent => [ :clone_structure_to_test ]) do |t|
35   since = TEST_CHANGES_SINCE
36   touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
37     recent_tests('app/models/*.rb', 'test/unit', since) +
38     recent_tests('app/controllers/*.rb', 'test/functional', since)
39
40   t.libs << 'test'
41   t.verbose = true
42   t.test_files = touched.uniq
43 end
44 task :test_recent => [ :clone_structure_to_test ]
45
46 desc "Run the unit tests in test/unit"
47 Rake::TestTask.new("test_units") { |t|
48   t.libs << "test"
49   t.pattern = 'test/unit/**/*_test.rb'
50   t.verbose = true
51 }
52 task :test_units => [ :clone_structure_to_test ]
53
54 desc "Run the functional tests in test/functional"
55 Rake::TestTask.new("test_functional") { |t|
56   t.libs << "test"
57   t.pattern = 'test/functional/**/*_test.rb'
58   t.verbose = true
59 }
60 task :test_functional => [ :clone_structure_to_test ]
61
62 desc "Generate documentation for the application"
63 Rake::RDocTask.new("appdoc") { |rdoc|
64   rdoc.rdoc_dir = 'doc/app'
65   rdoc.title    = "Rails Application Documentation"
66   rdoc.options << '--line-numbers --inline-source'
67   rdoc.rdoc_files.include('doc/README_FOR_APP')
68   rdoc.rdoc_files.include('app/**/*.rb')
69 }
70
71 desc "Generate documentation for the Rails framework"
72 Rake::RDocTask.new("apidoc") { |rdoc|
73   rdoc.rdoc_dir = 'doc/api'
74   rdoc.template = "#{ENV['template']}.rb" if ENV['template']
75   rdoc.title    = "Rails Framework Documentation"
76   rdoc.options << '--line-numbers --inline-source'
77   rdoc.rdoc_files.include('README')
78   rdoc.rdoc_files.include('CHANGELOG')
79   rdoc.rdoc_files.include('vendor/rails/railties/CHANGELOG')
80   rdoc.rdoc_files.include('vendor/rails/railties/MIT-LICENSE')
81   rdoc.rdoc_files.include('vendor/rails/activerecord/README')
82   rdoc.rdoc_files.include('vendor/rails/activerecord/CHANGELOG')
83   rdoc.rdoc_files.include('vendor/rails/activerecord/lib/active_record/**/*.rb')
84   rdoc.rdoc_files.exclude('vendor/rails/activerecord/lib/active_record/vendor/*')
85   rdoc.rdoc_files.include('vendor/rails/actionpack/README')
86   rdoc.rdoc_files.include('vendor/rails/actionpack/CHANGELOG')
87   rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_controller/**/*.rb')
88   rdoc.rdoc_files.include('vendor/rails/actionpack/lib/action_view/**/*.rb')
89   rdoc.rdoc_files.include('vendor/rails/actionmailer/README')
90   rdoc.rdoc_files.include('vendor/rails/actionmailer/CHANGELOG')
91   rdoc.rdoc_files.include('vendor/rails/actionmailer/lib/action_mailer/base.rb')
92   rdoc.rdoc_files.include('vendor/rails/actionwebservice/README')
93   rdoc.rdoc_files.include('vendor/rails/actionwebservice/CHANGELOG')
94   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service.rb')
95   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/*.rb')
96   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/api/*.rb')
97   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/client/*.rb')
98   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/container/*.rb')
99   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/dispatcher/*.rb')
100   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/protocol/*.rb')
101   rdoc.rdoc_files.include('vendor/rails/actionwebservice/lib/action_web_service/support/*.rb')
102   rdoc.rdoc_files.include('vendor/rails/activesupport/README')
103   rdoc.rdoc_files.include('vendor/rails/activesupport/CHANGELOG')
104   rdoc.rdoc_files.include('vendor/rails/activesupport/lib/active_support/**/*.rb')
105 }
106
107 desc "Report code statistics (KLOCs, etc) from the application"
108 task :stats => [ :environment ] do
109   require 'code_statistics'
110   CodeStatistics.new(
111     ["Helpers", "app/helpers"],
112     ["Controllers", "app/controllers"],
113     ["APIs", "app/apis"],
114     ["Components", "components"],
115     ["Functionals", "test/functional"],
116     ["Models", "app/models"],
117     ["Units", "test/unit"]
118   ).to_s
119 end
120
121 desc "Recreate the test databases from the development structure"
122 task :clone_structure_to_test => [ :db_structure_dump, :purge_test_database ] do
123   abcs = ActiveRecord::Base.configurations
124   case abcs["test"]["adapter"]
125     when  "mysql"
126       ActiveRecord::Base.establish_connection(:test)
127       ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
128       IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
129         ActiveRecord::Base.connection.execute(table)
130       end
131     when "postgresql"
132       ENV['PGHOST']     = abcs["test"]["host"] if abcs["test"]["host"]
133       ENV['PGPORT']     = abcs["test"]["port"].to_s if abcs["test"]["port"]
134       ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
135       `psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}`
136     when "sqlite", "sqlite3"
137       `#{abcs[RAILS_ENV]["adapter"]} #{abcs["test"]["dbfile"]} < db/#{RAILS_ENV}_structure.sql`
138     when "sqlserver"
139       `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql`
140     else
141       raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
142   end
143 end
144
145 desc "Dump the database structure to a SQL file"
146 task :db_structure_dump => :environment do
147   abcs = ActiveRecord::Base.configurations
148   case abcs[RAILS_ENV]["adapter"]
149     when "mysql"
150       ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
151       File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
152     when "postgresql"
153       ENV['PGHOST']     = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"]
154       ENV['PGPORT']     = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"]
155       ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"]
156       `pg_dump -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{abcs[RAILS_ENV]["database"]}`
157     when "sqlite", "sqlite3"
158       `#{abcs[RAILS_ENV]["adapter"]} #{abcs[RAILS_ENV]["dbfile"]} .schema > db/#{RAILS_ENV}_structure.sql`
159     when "sqlserver"
160       `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r`
161       `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r`
162     else
163       raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
164   end
165 end
166
167 desc "Empty the test database"
168 task :purge_test_database => :environment do
169   abcs = ActiveRecord::Base.configurations
170   case abcs["test"]["adapter"]
171     when "mysql"
172       ActiveRecord::Base.establish_connection(:test)
173       ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"])
174     when "postgresql"
175       ENV['PGHOST']     = abcs["test"]["host"] if abcs["test"]["host"]
176       ENV['PGPORT']     = abcs["test"]["port"].to_s if abcs["test"]["port"]
177       ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
178       `dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
179       `createdb -T template0 -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
180     when "sqlite","sqlite3"
181       File.delete(abcs["test"]["dbfile"]) if File.exist?(abcs["test"]["dbfile"])
182     when "sqlserver"
183       dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-')
184       `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}`
185       `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql`
186     else
187       raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
188   end
189 end
190
191 desc "Clears all *.log files in log/"
192 task :clear_logs => :environment do
193   FileList["log/*.log"].each do |log_file|
194     f = File.open(log_file, "w")
195     f.close
196   end
197 end
198
199 desc "Migrate the database according to the migrate scripts in db/migrate (only supported on PG/MySQL). A specific version can be targetted with VERSION=x"
200 task :migrate => :environment do
201   ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/db/migrate/', ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
202 end
Note: See TracBrowser for help on using the browser.