bio-sra 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 48531f3fc7b4facb8b4ed2b4523f6ab5eabb6897
4
+ data.tar.gz: 1ce0ebaccc4601144aa0150caf2913e2c3d96de1
5
+ SHA512:
6
+ metadata.gz: 3a81f703d495b138cb4a2d53ec2cced77fe888cd6f505ca28c9bec2bb20cddff4ca3b57c2b7cfcf31f8eda5fe00679d1bab503c91423f1f66ab141028ddf10c0
7
+ data.tar.gz: 7f4564e2b124d49b6313548f88020202942092ac41788f6d3228ce9608f8c92c955eadc0e5e789cc05744eb312341511a162848b783fdb3d1550bb317834a2c3
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
@@ -0,0 +1,12 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - jruby-19mode # JRuby in 1.9 mode
6
+ - rbx-19mode
7
+ # - 1.8.7
8
+ # - jruby-18mode # JRuby in 1.8 mode
9
+ # - rbx-18mode
10
+
11
+ # uncomment this line if your project needs to run something other than `rake`:
12
+ # script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,20 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+ gem 'activerecord', '~> 3.2'
6
+ gem "activesupport", "~> 3.2"
7
+ gem "sqlite3", "~> 1.3"
8
+ gem 'bio-logger', '~> 1.0'
9
+
10
+ # Add dependencies to develop your gem here.
11
+ # Include everything needed to run rake, tests, features, etc.
12
+ group :development do
13
+ gem "shoulda", "~> 3.5"
14
+ gem "rdoc", "~> 3.12"
15
+ gem "jeweler", "~> 2.0", '>= 2.0.1'
16
+ gem "bundler", "~> 1.6"
17
+ gem "bio", '~>1.4', ">= 1.4.2"
18
+
19
+ gem 'bio-commandeer', '~>0.1'
20
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Ben J. Woodcroft
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ # bio-sra
2
+
3
+ [![Build Status](https://secure.travis-ci.org/wwood/bioruby-sra.png)](http://travis-ci.org/wwood/bioruby-sra)
4
+
5
+ A Sequence Read Archive (SRA) download script and Ruby interface to the [SRAdb](ncbi.nlm.nih.gov/pmc/articles/PMC3560148/) (SRA metadata) SQLite database.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ gem install bio-sra
11
+ ```
12
+
13
+ ## Download script usage
14
+
15
+ Download a single run file to the current directory:
16
+ ```sh
17
+ sra_download --runs ERR229501.sra
18
+ ```
19
+
20
+ Download a list of runs
21
+ ```sh
22
+ $ cat srr_list.txt
23
+ ERR229501
24
+ ERR229498
25
+ $ sra_download --runs -f srr_list.txt
26
+ ```
27
+
28
+ Download all runs that are a part of the experiment ERP001779 (Microbial biogeography of public restroom surfaces)
29
+ ```sh
30
+ $ sra_download ERP001779
31
+ ```
32
+ This finds ERP001779 and links it to runs through the SRAdb
33
+
34
+ ## Ruby interface script
35
+
36
+ ```ruby
37
+ require 'bio-sra'
38
+ ```
39
+
40
+ The API doc is online. For more code examples see the test files in
41
+ the source tree.
42
+
43
+ ## Project home page
44
+
45
+ Information on the source tree, documentation, examples, issues and
46
+ how to contribute, see
47
+
48
+ http://github.com/wwood/bioruby-sra
49
+
50
+ The BioRuby community is on IRC server: irc.freenode.org, channel: #bioruby.
51
+
52
+ ## Cite
53
+
54
+ This Ruby code is unpublished, but there's a problem with
55
+
56
+ * [BioRuby: bioinformatics software for the Ruby programming language](http://dx.doi.org/10.1093/bioinformatics/btq475)
57
+ * [Biogem: an effective tool-based approach for scaling up open source software development in bioinformatics](http://dx.doi.org/10.1093/bioinformatics/bts080)
58
+
59
+ ## Biogems.info
60
+
61
+ This Biogem is published at [#bio-sra](http://biogems.info/index.html)
62
+
63
+ ## Copyright
64
+
65
+ Copyright (c) 2012 Ben J. Woodcroft. See LICENSE.txt for further details.
66
+
@@ -0,0 +1,634 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "bio-sra"
18
+ gem.homepage = "http://github.com/wwood/bioruby-sra"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A Sequence Read Archive (SRA) download script and Ruby interface to the SRAdb (SRA metadata) SQLite database}
21
+ gem.description = %Q{A Sequence Read Archive (SRA) download script and Ruby interface to the SRAdb (SRA metadata) SQLite database.}
22
+ gem.email = "donttrustben near gmail.com"
23
+ gem.authors = ["Ben J. Woodcroft"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
37
+ require 'rdoc/task'
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "bio-sra #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
45
+ end# encoding: utf-8
46
+ Encoding.default_external = Encoding::UTF_8
47
+
48
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__),'lib')
49
+ require 'active_support'
50
+ require 'active_support/core_ext/class/attribute_accessors'
51
+ require 'active_support/core_ext/module/delegation'
52
+ require 'active_record'
53
+
54
+ namespace :db do
55
+ namespace :schema do
56
+ desc 'Dump out the schema in markdown format suitable for inclusion at https://github.com/wwood/bioruby-sra/wiki/MetadatabaseSchema'
57
+ task :dump_wiki_format do
58
+ require 'bio-sra'
59
+ include Bio::SRA::Tables
60
+ Bio::SRA.connect
61
+
62
+ # grep 'class ' lib/bio/sra/tables.rb |awk '{print $2}'
63
+ tables = [
64
+ Submission,
65
+ Experiment,
66
+ Study,
67
+ Sample,
68
+ Run,
69
+ SRA,
70
+ SRAFt,
71
+ SRAFtContent,
72
+ SRAFtSegDir,
73
+ SRAFtSegments,
74
+ MetaInfo,
75
+ ColDesc]
76
+ tables.each do |table|
77
+ puts "### The table '#{table.table_name}'"
78
+ puts "bioruby-sra class name: #{table.to_s.gsub('Bio::SRA::Tables::','')}"
79
+ puts
80
+ documented_columns = ColDesc.where(:table_name => table.table_name).order('field_name').all.collect do |c|
81
+ f = c.field_name
82
+ unless table.first.respond_to?(f.to_sym) #e.g. ID => submission_ID
83
+ f = "#{c.table_name}_#{f}"
84
+ end
85
+ if table.first.respond_to?(f.to_sym)
86
+ examples = table.limit(2).where("#{f} not null").select("distinct(#{f})").all.collect{|e| e.send(f.to_sym)}
87
+ examples.collect! do |eg|
88
+ if eg.kind_of?(String) and eg.length > 50
89
+ eg[0..49]+'..'
90
+ else
91
+ eg
92
+ end
93
+ end
94
+ examples.collect!{|eg| eg.to_s.gsub('_','\_').strip}
95
+ print "* **#{f}** #{c.description.gsub('_','\_')} "+
96
+ "(#{c.type}) "
97
+ if examples[0].to_s.length > 0
98
+ puts "e.g. _#{examples.join('_, _')}_"
99
+ else
100
+ puts '_(currently always null in the database)_'
101
+ end
102
+ f
103
+ else
104
+ nil
105
+ end
106
+ end
107
+ undocumented_columns = table.content_columns.collect{|col| col.name}.reject{|col| documented_columns.include?(col)}
108
+ if !undocumented_columns.empty?
109
+ puts
110
+ puts 'Currently undocumented columns: '+undocumented_columns.sort.join(', ')
111
+ end
112
+ puts
113
+ puts
114
+ end
115
+ end
116
+ end
117
+ end
118
+
119
+ #This code is coming from activerecord-3.0.7/lib/active_record/railties/databases.rake
120
+ namespace :db do
121
+ task :load_config do
122
+ require 'active_record'
123
+ ActiveRecord::Base.configurations = YAML.load_file("config/database.yml")
124
+ end
125
+
126
+ task :environment => :load_config do
127
+ ActiveRecord::Base.establish_connection('default')
128
+ end
129
+
130
+ namespace :create do
131
+ # desc 'Create all the local databases defined in config/database.yml'
132
+ task :all => :load_config do
133
+ ActiveRecord::Base.configurations.each_value do |config|
134
+ # Skip entries that don't have a database key, such as the first entry here:
135
+ #
136
+ # defaults: &defaults
137
+ # adapter: mysql
138
+ # username: root
139
+ # password:
140
+ # host: localhost
141
+ #
142
+ # development:
143
+ # database: blog_development
144
+ # <<: *defaults
145
+ next unless config['database']
146
+ # Only connect to local databases
147
+ local_database?(config) { create_database(config) }
148
+ end
149
+ end
150
+ end
151
+
152
+ desc 'Create the database from config/database.yml for the current default (use db:create:all to create all dbs in the config)'
153
+ task :create => :load_config do
154
+ # Make the test database at the same time as the development one, if it exists
155
+ # if 'default'.development? && ActiveRecord::Base.configurations['test']
156
+ create_database(ActiveRecord::Base.configurations['default'])
157
+ # end
158
+ # create_database(ActiveRecord::Base.configurations['default'])
159
+ end
160
+
161
+ def create_database(config)
162
+ begin
163
+ if config['adapter'] =~ /sqlite/
164
+ if File.exist?(config['database'])
165
+ $stderr.puts "#{config['database']} already exists"
166
+ else
167
+ begin
168
+ # Create the SQLite database
169
+ ActiveRecord::Base.establish_connection(config)
170
+ ActiveRecord::Base.connection
171
+ rescue Exception => e
172
+ $stderr.puts e, *(e.backtrace)
173
+ $stderr.puts "Couldn't create database for #{config.inspect}"
174
+ end
175
+ end
176
+ return # Skip the else clause of begin/rescue
177
+ else
178
+ ActiveRecord::Base.establish_connection(config)
179
+ ActiveRecord::Base.connection
180
+ end
181
+ rescue
182
+ case config['adapter']
183
+ when /mysql/
184
+ @charset = ENV['CHARSET'] || 'utf8'
185
+ @collation = ENV['COLLATION'] || 'utf8_unicode_ci'
186
+ creation_options = {:charset => (config['charset'] || @charset), :collation => (config['collation'] || @collation)}
187
+ error_class = config['adapter'] =~ /mysql2/ ? Mysql2::Error : Mysql::Error
188
+ access_denied_error = 1045
189
+ begin
190
+ ActiveRecord::Base.establish_connection(config.merge('database' => nil))
191
+ ActiveRecord::Base.connection.create_database(config['database'], creation_options)
192
+ ActiveRecord::Base.establish_connection(config)
193
+ rescue error_class => sqlerr
194
+ if sqlerr.errno == access_denied_error
195
+ print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
196
+ root_password = $stdin.gets.strip
197
+ grant_statement = "GRANT ALL PRIVILEGES ON #{config['database']}.* " \
198
+ "TO '#{config['username']}'@'localhost' " \
199
+ "IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;"
200
+ ActiveRecord::Base.establish_connection(config.merge(
201
+ 'database' => nil, 'username' => 'root', 'password' => root_password))
202
+ ActiveRecord::Base.connection.create_database(config['database'], creation_options)
203
+ ActiveRecord::Base.connection.execute grant_statement
204
+ ActiveRecord::Base.establish_connection(config)
205
+ else
206
+ $stderr.puts sqlerr.error
207
+ $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation}"
208
+ $stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
209
+ end
210
+ end
211
+ when 'postgresql'
212
+ @encoding = config['encoding'] || ENV['CHARSET'] || 'utf8'
213
+ begin
214
+ ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
215
+ ActiveRecord::Base.connection.create_database(config['database'], config.merge('encoding' => @encoding))
216
+ ActiveRecord::Base.establish_connection(config)
217
+ rescue Exception => e
218
+ $stderr.puts e, *(e.backtrace)
219
+ $stderr.puts "Couldn't create database for #{config.inspect}"
220
+ end
221
+ end
222
+ else
223
+ $stderr.puts "#{config['database']} already exists"
224
+ end
225
+ end
226
+
227
+ namespace :drop do
228
+ # desc 'Drops all the local databases defined in config/database.yml'
229
+ task :all => :load_config do
230
+ ActiveRecord::Base.configurations.each_value do |config|
231
+ # Skip entries that don't have a database key
232
+ next unless config['database']
233
+ begin
234
+ # Only connect to local databases
235
+ local_database?(config) { drop_database(config) }
236
+ rescue Exception => e
237
+ $stderr.puts "Couldn't drop #{config['database']} : #{e.inspect}"
238
+ end
239
+ end
240
+ end
241
+ end
242
+
243
+ desc 'Drops the database for the current default (use db:drop:all to drop all databases)'
244
+ task :drop => :load_config do
245
+ config = ActiveRecord::Base.configurations['default' || 'development']
246
+ begin
247
+ drop_database(config)
248
+ rescue Exception => e
249
+ $stderr.puts "Couldn't drop #{config['database']} : #{e.inspect}"
250
+ end
251
+ end
252
+
253
+ def local_database?(config, &block)
254
+ if %w( 127.0.0.1 localhost ).include?(config['host']) || config['host'].blank?
255
+ yield
256
+ else
257
+ $stderr.puts "This task only modifies local databases. #{config['database']} is on a remote host."
258
+ end
259
+ end
260
+
261
+ desc "Migrate the database (options: VERSION=x, VERBOSE=false)."
262
+ task :migrate => :environment do
263
+ ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
264
+ ActiveRecord::Migrator.migrate("db/migrate/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
265
+ Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
266
+ end
267
+
268
+ namespace :migrate do
269
+ # desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
270
+ task :redo => :environment do
271
+ if ENV["VERSION"]
272
+ Rake::Task["db:migrate:down"].invoke
273
+ Rake::Task["db:migrate:up"].invoke
274
+ else
275
+ Rake::Task["db:rollback"].invoke
276
+ Rake::Task["db:migrate"].invoke
277
+ end
278
+ end
279
+
280
+ # desc 'Resets your database using your migrations for the current environment'
281
+ task :reset => ["db:drop", "db:create", "db:migrate"]
282
+
283
+ # desc 'Runs the "up" for a given migration VERSION.'
284
+ task :up => :environment do
285
+ version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
286
+ raise "VERSION is required" unless version
287
+ ActiveRecord::Migrator.run(:up, "db/migrate/", version)
288
+ Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
289
+ end
290
+
291
+ # desc 'Runs the "down" for a given migration VERSION.'
292
+ task :down => :environment do
293
+ version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
294
+ raise "VERSION is required" unless version
295
+ ActiveRecord::Migrator.run(:down, "db/migrate/", version)
296
+ Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
297
+ end
298
+
299
+ desc "Display status of migrations"
300
+ task :status => :environment do
301
+ config = ActiveRecord::Base.configurations['default']
302
+ ActiveRecord::Base.establish_connection(config)
303
+ unless ActiveRecord::Base.connection.table_exists?(ActiveRecord::Migrator.schema_migrations_table_name)
304
+ puts 'Schema migrations table does not exist yet.'
305
+ next # means "return" for rake task
306
+ end
307
+ db_list = ActiveRecord::Base.connection.select_values("SELECT version FROM #{ActiveRecord::Migrator.schema_migrations_table_name}")
308
+ file_list = []
309
+ Dir.foreach(File.join('db', 'migrate')) do |file|
310
+ # only files matching "20091231235959_some_name.rb" pattern
311
+ if match_data = /(\d{14})_(.+)\.rb/.match(file)
312
+ status = db_list.delete(match_data[1]) ? 'up' : 'down'
313
+ file_list << [status, match_data[1], match_data[2]]
314
+ end
315
+ end
316
+ # output
317
+ puts "\ndatabase: #{config['database']}\n\n"
318
+ puts "#{"Status".center(8)} #{"Migration ID".ljust(14)} Migration Name"
319
+ puts "-" * 50
320
+ file_list.each do |file|
321
+ puts "#{file[0].center(8)} #{file[1].ljust(14)} #{file[2].humanize}"
322
+ end
323
+ db_list.each do |version|
324
+ puts "#{'up'.center(8)} #{version.ljust(14)} *** NO FILE ***"
325
+ end
326
+ puts
327
+ end
328
+ end
329
+
330
+ desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
331
+ task :rollback => :environment do
332
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
333
+ ActiveRecord::Migrator.rollback('db/migrate/', step)
334
+ Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
335
+ end
336
+
337
+ # desc 'Pushes the schema to the next version (specify steps w/ STEP=n).'
338
+ task :forward => :environment do
339
+ step = ENV['STEP'] ? ENV['STEP'].to_i : 1
340
+ ActiveRecord::Migrator.forward('db/migrate/', step)
341
+ Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
342
+ end
343
+
344
+ # desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
345
+ task :reset => [ 'db:drop', 'db:setup' ]
346
+
347
+ # desc "Retrieves the charset for the current environment's database"
348
+ task :charset => :environment do
349
+ config = ActiveRecord::Base.configurations['default' || 'development']
350
+ case config['adapter']
351
+ when /mysql/
352
+ ActiveRecord::Base.establish_connection(config)
353
+ puts ActiveRecord::Base.connection.charset
354
+ when 'postgresql'
355
+ ActiveRecord::Base.establish_connection(config)
356
+ puts ActiveRecord::Base.connection.encoding
357
+ when 'sqlite3'
358
+ ActiveRecord::Base.establish_connection(config)
359
+ puts ActiveRecord::Base.connection.encoding
360
+ else
361
+ $stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
362
+ end
363
+ end
364
+
365
+ # desc "Retrieves the collation for the current environment's database"
366
+ task :collation => :environment do
367
+ config = ActiveRecord::Base.configurations['default']
368
+ case config['adapter']
369
+ when /mysql/
370
+ ActiveRecord::Base.establish_connection(config)
371
+ puts ActiveRecord::Base.connection.collation
372
+ else
373
+ $stderr.puts 'sorry, your database adapter is not supported yet, feel free to submit a patch'
374
+ end
375
+ end
376
+
377
+ desc "Retrieves the current schema version number"
378
+ task :version => :environment do
379
+ puts "Current version: #{ActiveRecord::Migrator.current_version}"
380
+ end
381
+
382
+ # desc "Raises an error if there are pending migrations"
383
+ task :abort_if_pending_migrations => :environment do
384
+ if defined? ActiveRecord
385
+ pending_migrations = ActiveRecord::Migrator.new(:up, 'db/migrate').pending_migrations
386
+
387
+ if pending_migrations.any?
388
+ puts "You have #{pending_migrations.size} pending migrations:"
389
+ pending_migrations.each do |pending_migration|
390
+ puts ' %4d %s' % [pending_migration.version, pending_migration.name]
391
+ end
392
+ abort %{Run "rake db:migrate" to update your database then try again.}
393
+ end
394
+ end
395
+ end
396
+
397
+ desc 'Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)'
398
+ task :setup => [ 'db:create', 'db:schema:load', 'db:seed' ]
399
+
400
+ desc 'Load the seed data from db/seeds.rb'
401
+ task :seed => 'db:abort_if_pending_migrations' do
402
+ seed_file = File.join('db', 'seeds.rb')
403
+ load(seed_file) if File.exist?(seed_file)
404
+ end
405
+
406
+ namespace :fixtures do
407
+ desc "Load fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
408
+ task :load => :environment do
409
+ require 'active_record/fixtures'
410
+
411
+ ActiveRecord::Base.establish_connection('default')
412
+ base_dir = ENV['FIXTURES_PATH'] ? File.join(ENV['FIXTURES_PATH']) : File.join('test', 'fixtures')
413
+ fixtures_dir = ENV['FIXTURES_DIR'] ? File.join(base_dir, ENV['FIXTURES_DIR']) : base_dir
414
+
415
+ (ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/).map {|f| File.join(fixtures_dir, f) } : Dir["#{fixtures_dir}/**/*.{yml,csv}"]).each do |fixture_file|
416
+ ActiveRecord::Fixtures.create_fixtures(fixtures_dir, fixture_file[(fixtures_dir.size + 1)..-5])
417
+ end
418
+ end
419
+
420
+ # desc "Search for a fixture given a LABEL or ID. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
421
+ task :identify => :environment do
422
+ require 'active_record/fixtures'
423
+
424
+ label, id = ENV["LABEL"], ENV["ID"]
425
+ raise "LABEL or ID required" if label.blank? && id.blank?
426
+
427
+ puts %Q(The fixture ID for "#{label}" is #{Fixtures.identify(label)}.) if label
428
+
429
+ base_dir = ENV['FIXTURES_PATH'] ? File.join(ENV['FIXTURES_PATH']) : File.join('test', 'fixtures')
430
+ Dir["#{base_dir}/**/*.yml"].each do |file|
431
+ if data = YAML::load(ERB.new(IO.read(file)).result)
432
+ data.keys.each do |key|
433
+ key_id = Fixtures.identify(key)
434
+
435
+ if key == label || key_id == id.to_i
436
+ puts "#{file}: #{key} (#{key_id})"
437
+ end
438
+ end
439
+ end
440
+ end
441
+ end
442
+ end
443
+
444
+ namespace :schema do
445
+ desc "Create a db/schema.rb file that can be portably used against any DB supported by AR"
446
+ task :dump => :environment do
447
+ require 'active_record/schema_dumper'
448
+ File.open(ENV['SCHEMA'] || "db/schema.rb", "w") do |file|
449
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
450
+ end
451
+ Rake::Task["db:schema:dump"].reenable
452
+ end
453
+
454
+ desc "Load a schema.rb file into the database"
455
+ task :load => :environment do
456
+ file = ENV['SCHEMA'] || "db/schema.rb"
457
+ if File.exists?(file)
458
+ load(file)
459
+ else
460
+ abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter config/application.rb to limit the frameworks that will be loaded}
461
+ end
462
+ end
463
+ end
464
+
465
+ namespace :structure do
466
+ desc "Dump the database structure to an SQL file"
467
+ task :dump => :environment do
468
+ abcs = ActiveRecord::Base.configurations
469
+ case abcs['default']["adapter"]
470
+ when /mysql/, "oci", "oracle"
471
+ ActiveRecord::Base.establish_connection(abcs['default'])
472
+ File.open("db/#{'default'}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
473
+ when "postgresql"
474
+ ENV['PGHOST'] = abcs['default']["host"] if abcs['default']["host"]
475
+ ENV['PGPORT'] = abcs['default']["port"].to_s if abcs['default']["port"]
476
+ ENV['PGPASSWORD'] = abcs['default']["password"].to_s if abcs['default']["password"]
477
+ search_path = abcs['default']["schema_search_path"]
478
+ unless search_path.blank?
479
+ search_path = search_path.split(",").map{|search_path| "--schema=#{search_path.strip}" }.join(" ")
480
+ end
481
+ `pg_dump -i -U "#{abcs['default']["username"]}" -s -x -O -f db/default_structure.sql #{search_path} #{abcs['default']["database"]}`
482
+ raise "Error dumping database" if $?.exitstatus == 1
483
+ when "sqlite", "sqlite3"
484
+ dbfile = abcs['default']["database"] || abcs['default']["dbfile"]
485
+ `#{abcs['default']["adapter"]} #{dbfile} .schema > db/#{'default'}_structure.sql`
486
+ when "sqlserver"
487
+ `scptxfr /s #{abcs['default']["host"]} /d #{abcs['default']["database"]} /I /f db\\#{'default'}_structure.sql /q /A /r`
488
+ `scptxfr /s #{abcs['default']["host"]} /d #{abcs['default']["database"]} /I /F db\ /q /A /r`
489
+ when "firebird"
490
+ set_firebird_env(abcs['default'])
491
+ db_string = firebird_db_string(abcs['default'])
492
+ sh "isql -a #{db_string} > db/#{'default'}_structure.sql"
493
+ else
494
+ raise "Task not supported by '#{abcs['default']["adapter"]}'"
495
+ end
496
+
497
+ if ActiveRecord::Base.connection.supports_migrations?
498
+ File.open("db/#{'default'}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
499
+ end
500
+ end
501
+ end
502
+
503
+ namespace :test do
504
+ # desc "Recreate the test database from the current schema.rb"
505
+ task :load => 'db:test:purge' do
506
+ ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
507
+ ActiveRecord::Schema.verbose = false
508
+ Rake::Task["db:schema:load"].invoke
509
+ end
510
+
511
+ # desc "Recreate the test database from the current environment's database schema"
512
+ task :clone => %w(db:schema:dump db:test:load)
513
+
514
+ # desc "Recreate the test databases from the development structure"
515
+ task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do
516
+ abcs = ActiveRecord::Base.configurations
517
+ case abcs["test"]["adapter"]
518
+ when /mysql/
519
+ ActiveRecord::Base.establish_connection(:test)
520
+ ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
521
+ IO.readlines("db/#{'default'}_structure.sql").join.split("\n\n").each do |table|
522
+ ActiveRecord::Base.connection.execute(table)
523
+ end
524
+ when "postgresql"
525
+ ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
526
+ ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
527
+ ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
528
+ `psql -U "#{abcs["test"]["username"]}" -f db/#{'default'}_structure.sql #{abcs["test"]["database"]}`
529
+ when "sqlite", "sqlite3"
530
+ dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"]
531
+ `#{abcs["test"]["adapter"]} #{dbfile} < db/#{'default'}_structure.sql`
532
+ when "sqlserver"
533
+ `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{'default'}_structure.sql`
534
+ when "oci", "oracle"
535
+ ActiveRecord::Base.establish_connection(:test)
536
+ IO.readlines("db/#{'default'}_structure.sql").join.split(";\n\n").each do |ddl|
537
+ ActiveRecord::Base.connection.execute(ddl)
538
+ end
539
+ when "firebird"
540
+ set_firebird_env(abcs["test"])
541
+ db_string = firebird_db_string(abcs["test"])
542
+ sh "isql -i db/#{'default'}_structure.sql #{db_string}"
543
+ else
544
+ raise "Task not supported by '#{abcs["test"]["adapter"]}'"
545
+ end
546
+ end
547
+
548
+ # desc "Empty the test database"
549
+ task :purge => :environment do
550
+ abcs = ActiveRecord::Base.configurations
551
+ case abcs["test"]["adapter"]
552
+ when /mysql/
553
+ ActiveRecord::Base.establish_connection(:test)
554
+ ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"], abcs["test"])
555
+ when "postgresql"
556
+ ActiveRecord::Base.clear_active_connections!
557
+ drop_database(abcs['test'])
558
+ create_database(abcs['test'])
559
+ when "sqlite","sqlite3"
560
+ dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"]
561
+ File.delete(dbfile) if File.exist?(dbfile)
562
+ when "sqlserver"
563
+ dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-')
564
+ `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}`
565
+ `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{'default'}_structure.sql`
566
+ when "oci", "oracle"
567
+ ActiveRecord::Base.establish_connection(:test)
568
+ ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl|
569
+ ActiveRecord::Base.connection.execute(ddl)
570
+ end
571
+ when "firebird"
572
+ ActiveRecord::Base.establish_connection(:test)
573
+ ActiveRecord::Base.connection.recreate_database!
574
+ else
575
+ raise "Task not supported by '#{abcs["test"]["adapter"]}'"
576
+ end
577
+ end
578
+
579
+ # desc 'Check for pending migrations and load the test schema'
580
+ task :prepare => 'db:abort_if_pending_migrations' do
581
+ if defined?(ActiveRecord) && !ActiveRecord::Base.configurations.blank?
582
+ Rake::Task[{ :sql => "db:test:clone_structure", :ruby => "db:test:load" }[ActiveRecord::Base.schema_format]].invoke
583
+ end
584
+ end
585
+ end
586
+
587
+ namespace :sessions do
588
+ # desc "Creates a sessions migration for use with ActiveRecord::SessionStore"
589
+ task :create => :environment do
590
+ raise "Task unavailable to this database (no migration support)" unless ActiveRecord::Base.connection.supports_migrations?
591
+ require 'rails/generators'
592
+ Rails::Generators.configure!
593
+ require 'rails/generators/rails/session_migration/session_migration_generator'
594
+ Rails::Generators::SessionMigrationGenerator.start [ ENV["MIGRATION"] || "add_sessions_table" ]
595
+ end
596
+
597
+ # desc "Clear the sessions table"
598
+ task :clear => :environment do
599
+ ActiveRecord::Base.connection.execute "DELETE FROM #{session_table_name}"
600
+ end
601
+ end
602
+ end
603
+
604
+ task 'test:prepare' => 'db:test:prepare'
605
+
606
+ def drop_database(config)
607
+ case config['adapter']
608
+ when /mysql/
609
+ ActiveRecord::Base.establish_connection(config)
610
+ ActiveRecord::Base.connection.drop_database config['database']
611
+ when /^sqlite/
612
+ require 'pathname'
613
+ path = Pathname.new(config['database'])
614
+ file = path.absolute? ? path.to_s : File.join( path)
615
+
616
+ FileUtils.rm(file)
617
+ when 'postgresql'
618
+ ActiveRecord::Base.establish_connection(config.merge('database' => 'postgres', 'schema_search_path' => 'public'))
619
+ ActiveRecord::Base.connection.drop_database config['database']
620
+ end
621
+ end
622
+
623
+ def session_table_name
624
+ ActiveRecord::SessionStore::Session.table_name
625
+ end
626
+
627
+ def set_firebird_env(config)
628
+ ENV["ISC_USER"] = config["username"].to_s if config["username"]
629
+ ENV["ISC_PASSWORD"] = config["password"].to_s if config["password"]
630
+ end
631
+
632
+ def firebird_db_string(config)
633
+ FireRuby::Database.db_string_for(config.symbolize_keys)
634
+ end