composite_primary_keys 0.8.6 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/History.txt +9 -0
  2. data/Manifest.txt +37 -0
  3. data/README.txt +34 -9
  4. data/README_DB2.txt +33 -0
  5. data/Rakefile +8 -119
  6. data/init.rb +2 -0
  7. data/lib/adapter_helper/base.rb +63 -0
  8. data/lib/adapter_helper/mysql.rb +13 -0
  9. data/lib/adapter_helper/oracle.rb +13 -0
  10. data/lib/adapter_helper/postgresql.rb +13 -0
  11. data/lib/adapter_helper/sqlite3.rb +13 -0
  12. data/lib/composite_primary_keys.rb +2 -2
  13. data/lib/composite_primary_keys/associations.rb +53 -28
  14. data/lib/composite_primary_keys/base.rb +4 -1
  15. data/lib/composite_primary_keys/connection_adapters/ibm_db_adapter.rb +21 -0
  16. data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +1 -1
  17. data/lib/composite_primary_keys/migration.rb +13 -0
  18. data/lib/composite_primary_keys/version.rb +2 -2
  19. data/loader.rb +24 -0
  20. data/local/database_connections.rb.sample +10 -0
  21. data/local/paths.rb.sample +2 -0
  22. data/local/tasks.rb.sample +2 -0
  23. data/scripts/console.rb +25 -0
  24. data/tasks/activerecord_selection.rake +43 -0
  25. data/tasks/databases.rake +10 -0
  26. data/tasks/databases/mysql.rake +30 -0
  27. data/tasks/databases/oracle.rake +15 -0
  28. data/tasks/databases/postgresql.rake +26 -0
  29. data/tasks/databases/sqlite3.rake +28 -0
  30. data/tasks/deployment.rake +22 -0
  31. data/tasks/local_setup.rake +13 -0
  32. data/tasks/website.rake +18 -0
  33. data/test/README_tests.txt +67 -0
  34. data/test/abstract_unit.rb +2 -4
  35. data/test/connections/native_ibm_db/connection.rb +23 -0
  36. data/test/connections/native_mysql/connection.rb +8 -13
  37. data/test/connections/native_oracle/connection.rb +8 -11
  38. data/test/connections/native_postgresql/connection.rb +3 -9
  39. data/test/connections/native_sqlite/connection.rb +4 -5
  40. data/test/fixtures/comment.rb +5 -0
  41. data/test/fixtures/comments.yml +14 -0
  42. data/test/fixtures/db_definitions/db2-create-tables.sql +92 -0
  43. data/test/fixtures/db_definitions/db2-drop-tables.sql +13 -0
  44. data/test/fixtures/db_definitions/mysql.sql +24 -0
  45. data/test/fixtures/db_definitions/oracle.drop.sql +6 -0
  46. data/test/fixtures/db_definitions/oracle.sql +28 -1
  47. data/test/fixtures/db_definitions/postgresql.sql +24 -0
  48. data/test/fixtures/db_definitions/sqlite.sql +21 -0
  49. data/test/fixtures/department.rb +5 -0
  50. data/test/fixtures/departments.yml +3 -0
  51. data/test/fixtures/employee.rb +4 -0
  52. data/test/fixtures/employees.yml +9 -0
  53. data/test/fixtures/hack.rb +6 -0
  54. data/test/fixtures/hacks.yml +2 -0
  55. data/test/fixtures/product.rb +3 -2
  56. data/test/fixtures/streets.yml +11 -1
  57. data/test/fixtures/suburb.rb +2 -0
  58. data/test/fixtures/suburbs.yml +6 -1
  59. data/test/fixtures/user.rb +1 -0
  60. data/test/test_associations.rb +29 -5
  61. data/test/test_delete.rb +23 -2
  62. data/test/test_polymorphic.rb +24 -0
  63. data/tmp/test.db +0 -0
  64. data/website/index.html +2 -2
  65. data/website/version-raw.js +1 -1
  66. data/website/version.js +1 -1
  67. metadata +43 -3
@@ -26,7 +26,6 @@ $:.unshift(File.dirname(__FILE__)) unless
26
26
 
27
27
  unless defined?(ActiveRecord)
28
28
  begin
29
- $:.unshift(File.dirname(__FILE__) + "/../../activerecord/lib")
30
29
  require 'active_record'
31
30
  rescue LoadError
32
31
  require 'rubygems'
@@ -40,6 +39,7 @@ require 'composite_primary_keys/associations'
40
39
  require 'composite_primary_keys/reflection'
41
40
  require 'composite_primary_keys/base'
42
41
  require 'composite_primary_keys/calculations'
42
+ require 'composite_primary_keys/migration'
43
43
 
44
44
  ActiveRecord::Base.class_eval do
45
45
  include CompositePrimaryKeys::ActiveRecord::Base
@@ -47,7 +47,7 @@ end
47
47
 
48
48
  Dir[File.dirname(__FILE__) + '/composite_primary_keys/connection_adapters/*.rb'].each do |adapter|
49
49
  begin
50
- require "composite_primary_keys/connection_adapters/" + adapter.gsub('.rb','')
50
+ require adapter.gsub('.rb','')
51
51
  rescue MissingSourceFile
52
52
  end
53
53
  end
@@ -80,7 +80,8 @@ module ActiveRecord::Associations::ClassMethods
80
80
  association = join.instantiate(row)
81
81
  collection.target.push(association) unless collection.target.include?(association)
82
82
  when :has_one, :belongs_to
83
- return if record.id.to_s != join.parent.record_id(row).to_s or row[join.aliased_primary_key].nil?
83
+ return if record.id.to_s != join.parent.record_id(row).to_s or
84
+ join.aliased_primary_key.to_a.any? { |key| row[key].nil? }
84
85
  association = join.instantiate(row)
85
86
  record.send("set_#{join.reflection.name}_target", association)
86
87
  else
@@ -125,17 +126,20 @@ module ActiveRecord::Associations::ClassMethods
125
126
  def composite_association_join
126
127
  join = case reflection.macro
127
128
  when :has_and_belongs_to_many
128
- # TODO replace (keys) = (ids), with key1=id1 and key2=id2
129
- " LEFT OUTER JOIN %s ON (%s) = (%s) " % [
130
- table_alias_for(options[:join_table], aliased_join_table_name),
131
- full_keys(aliased_join_table_name, options[:foreign_key] || reflection.active_record.to_s.classify.foreign_key),
132
- full_keys(reflection.active_record.table_name, reflection.active_record.primary_key)] +
133
- # TODO replace (keys) = (ids), with key1=id1 and key2=id2
134
- " LEFT OUTER JOIN %s ON (%s) = (%s) " % [
135
- table_name_and_alias,
136
- full_keys(aliased_table_name, klass.primary_key),
137
- full_keys(aliased_join_table_name, options[:association_foreign_key] || klass.table_name.classify.foreign_key)
138
- ]
129
+ " LEFT OUTER JOIN %s ON %s " % [
130
+ table_alias_for(options[:join_table], aliased_join_table_name),
131
+ composite_join_clause(
132
+ full_keys(aliased_join_table_name, options[:foreign_key] || reflection.active_record.to_s.classify.foreign_key),
133
+ full_keys(reflection.active_record.table_name, reflection.active_record.primary_key)
134
+ )
135
+ ] +
136
+ " LEFT OUTER JOIN %s ON %s " % [
137
+ table_name_and_alias,
138
+ composite_join_clause(
139
+ full_keys(aliased_table_name, klass.primary_key),
140
+ full_keys(aliased_join_table_name, options[:association_foreign_key] || klass.table_name.classify.foreign_key)
141
+ )
142
+ ]
139
143
  when :has_many, :has_one
140
144
  case
141
145
  when reflection.macro == :has_many && reflection.options[:through]
@@ -155,16 +159,19 @@ module ActiveRecord::Associations::ClassMethods
155
159
  second_key = options[:foreign_key] || primary_key
156
160
  end
157
161
 
158
- # TODO replace (keys) = (ids), with key1=id1 and key2=id2
159
- " LEFT OUTER JOIN %s ON (%s) = (%s) " % [
162
+ " LEFT OUTER JOIN %s ON %s " % [
160
163
  table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
161
- full_keys(aliased_join_table_name, through_reflection.primary_key_name),
162
- full_keys(parent.aliased_table_name, parent.primary_key)] +
163
- # TODO replace (keys) = (ids), with key1=id1 and key2=id2
164
- " LEFT OUTER JOIN %s ON (%s) = (%s) " % [
165
- table_name_and_alias,
166
- full_keys(aliased_table_name, first_key),
167
- full_keys(aliased_join_table_name, second_key)
164
+ composite_join_clause(
165
+ full_keys(aliased_join_table_name, through_reflection.primary_key_name),
166
+ full_keys(parent.aliased_table_name, parent.primary_key)
167
+ )
168
+ ] +
169
+ " LEFT OUTER JOIN %s ON %s " % [
170
+ table_name_and_alias,
171
+ composite_join_clause(
172
+ full_keys(aliased_table_name, first_key),
173
+ full_keys(aliased_join_table_name, second_key)
174
+ )
168
175
  ]
169
176
  end
170
177
  end
@@ -175,7 +182,6 @@ module ActiveRecord::Associations::ClassMethods
175
182
  raise AssociationNotSupported, "Polymorphic joins not supported for composite keys"
176
183
  else
177
184
  foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
178
- # TODO replace (keys) = (ids), with key1=id1 and key2=id2
179
185
  " LEFT OUTER JOIN %s ON %s " % [
180
186
  table_name_and_alias,
181
187
  composite_join_clause(
@@ -184,7 +190,6 @@ module ActiveRecord::Associations::ClassMethods
184
190
  ]
185
191
  end
186
192
  when :belongs_to
187
- # TODO replace (keys) = (ids), with key1=id1 and key2=id2
188
193
  " LEFT OUTER JOIN %s ON %s " % [
189
194
  table_name_and_alias,
190
195
  composite_join_clause(
@@ -197,7 +202,7 @@ module ActiveRecord::Associations::ClassMethods
197
202
  join << %(AND %s.%s = %s ) % [
198
203
  aliased_table_name,
199
204
  reflection.active_record.connection.quote_column_name(reflection.active_record.inheritance_column),
200
- klass.quote(klass.name)] unless klass.descends_from_active_record?
205
+ klass.connection.quote(klass.name)] unless klass.descends_from_active_record?
201
206
  join << "AND #{interpolate_sql(sanitize_sql(reflection.options[:conditions]))} " if reflection.options[:conditions]
202
207
  join
203
208
  end
@@ -273,7 +278,7 @@ module ActiveRecord::Associations
273
278
  when @reflection.options[:as]
274
279
  @finder_sql =
275
280
  "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " +
276
- "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.connection.quote_column_name(@owner.class.base_class.name.to_s)}"
281
+ "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"
277
282
  @finder_sql << " AND (#{conditions})" if conditions
278
283
 
279
284
  else
@@ -292,6 +297,26 @@ module ActiveRecord::Associations
292
297
  @counter_sql = @finder_sql
293
298
  end
294
299
  end
300
+
301
+ def delete_records(records)
302
+ if @reflection.options[:dependent]
303
+ records.each { |r| r.destroy }
304
+ else
305
+ field_names = @reflection.primary_key_name.split(',')
306
+ field_names.collect! {|n|
307
+ n + " = NULL"
308
+ }
309
+ records.each { |r|
310
+ where_class = nil
311
+ if r.quoted_id.include?(',')
312
+ where_class = [@reflection.klass.primary_key, r.quoted_id].transpose.map {|pair| "(#{pair[0]} = #{pair[1]})"}.join(" AND ")
313
+ else
314
+ where_class = @reflection.klass.primary_key + ' = ' + r.quoted_id
315
+ end
316
+ @reflection.klass.update_all( field_names.join(',') , where_class)
317
+ }
318
+ end
319
+ end
295
320
  end
296
321
 
297
322
  class HasOneAssociation < BelongsToAssociation #:nodoc:
@@ -300,7 +325,7 @@ module ActiveRecord::Associations
300
325
  when @reflection.options[:as]
301
326
  @finder_sql =
302
327
  "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " +
303
- "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.connection.quote_column_name(@owner.class.base_class.name.to_s)}"
328
+ "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"
304
329
  else
305
330
  @finder_sql = full_columns_equals(@reflection.klass.table_name,
306
331
  @reflection.primary_key_name, @owner.quoted_id)
@@ -313,7 +338,7 @@ module ActiveRecord::Associations
313
338
  def construct_conditions
314
339
  conditions = if @reflection.through_reflection.options[:as]
315
340
  "#{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.options[:as]}_id = #{@owner.quoted_id} " +
316
- "AND #{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.options[:as]}_type = #{@owner.class.connection.quote_column_name(@owner.class.base_class.name.to_s)}"
341
+ "AND #{@reflection.through_reflection.table_name}.#{@reflection.through_reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}"
317
342
  else
318
343
  @finder_sql = full_columns_equals(@reflection.through_reflection.table_name,
319
344
  @reflection.through_reflection.primary_key_name, @owner.quoted_id)
@@ -334,7 +359,7 @@ module ActiveRecord::Associations
334
359
  if @reflection.source_reflection.options[:as]
335
360
  polymorphic_join = "AND %s.%s = %s" % [
336
361
  @reflection.table_name, "#{@reflection.source_reflection.options[:as]}_type",
337
- @owner.class.connection.quote_column_name(@reflection.through_reflection.klass.name)
362
+ @owner.class.quote_value(@reflection.through_reflection.klass.name)
338
363
  ]
339
364
  end
340
365
  end
@@ -293,7 +293,10 @@ module CompositePrimaryKeys
293
293
 
294
294
  conditions = ids.map do |id_set|
295
295
  [primary_keys, id_set].transpose.map do |key, id|
296
- "#{table_name}.#{key.to_s}=#{sanitize(id)}"
296
+ col = columns_hash[key.to_s]
297
+ val = quote_value(id, col)
298
+
299
+ "#{table_name}.#{key.to_s}=#{val}"
297
300
  end.join(" AND ")
298
301
  end.join(") OR (")
299
302
  options.update :conditions => "(#{conditions})"
@@ -0,0 +1,21 @@
1
+ module ActiveRecord
2
+ module ConnectionAdapters
3
+ class IBM_DBAdapter < AbstractAdapter
4
+
5
+ # This mightn't be in Core, but count(distinct x,y) doesn't work for me
6
+ def supports_count_distinct? #:nodoc:
7
+ false
8
+ end
9
+
10
+ alias_method :quote_original, :quote
11
+ def quote(value, column = nil)
12
+ if value.kind_of?(String) && column && [:integer, :float].include?(column.type)
13
+ value = column.type == :integer ? value.to_i : value.to_f
14
+ value.to_s
15
+ else
16
+ quote_original(value, column)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -4,7 +4,7 @@ module ActiveRecord
4
4
 
5
5
  # This mightn't be in Core, but count(distinct x,y) doesn't work for me
6
6
  def supports_count_distinct? #:nodoc:
7
- false
7
+ true
8
8
  end
9
9
  end
10
10
  end
@@ -0,0 +1,13 @@
1
+ ActiveRecord::ConnectionAdapters::ColumnDefinition.send(:alias_method, :to_s_without_composite_keys, :to_s)
2
+
3
+ ActiveRecord::ConnectionAdapters::ColumnDefinition.class_eval <<-'EOF'
4
+ def to_s
5
+ if name.is_a? Array
6
+ "PRIMARY KEY (#{name.join(',')})"
7
+ else
8
+ to_s_without_composite_keys
9
+ end
10
+ end
11
+ EOF
12
+
13
+
@@ -1,8 +1,8 @@
1
1
  module CompositePrimaryKeys
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 0
4
- MINOR = 8
5
- TINY = 6
4
+ MINOR = 9
5
+ TINY = 0
6
6
 
7
7
  STRING = [MAJOR, MINOR, TINY].join('.')
8
8
  end
data/loader.rb ADDED
@@ -0,0 +1,24 @@
1
+ # Load local config files in /local
2
+ begin
3
+ local_file_supported = Dir[File.join(PROJECT_ROOT, 'local/*.sample')].map { |path| File.basename(path).sub(".sample","") }
4
+ local_file_supported.each do |file|
5
+ require "local/#{file}"
6
+ end
7
+ rescue LoadError
8
+ puts <<-EOS
9
+ This Gem supports local developer extensions in local/ folder.
10
+ Supported files:
11
+ #{local_file_supported.map { |f| "local/#{f}"}.join(', ')}
12
+
13
+ Setup default sample files:
14
+ rake local:setup
15
+
16
+ Current warning: #{$!}
17
+
18
+ EOS
19
+ end
20
+
21
+
22
+ # Now load Rake tasks from /tasks
23
+ rakefiles = Dir[File.join(File.dirname(__FILE__), "tasks/**/*.rake")]
24
+ rakefiles.each { |rakefile| load File.expand_path(rakefile) }
@@ -0,0 +1,10 @@
1
+ require 'yaml'
2
+
3
+ ENV['cpk_adapters'] = {
4
+ "mysql" => {
5
+ :adapter => "mysql",
6
+ :username => "root",
7
+ :password => "root",
8
+ # ...
9
+ }
10
+ }.to_yaml
@@ -0,0 +1,2 @@
1
+ # location of folder containing activerecord, railties, etc folders for each Rails gem
2
+ ENV['EDGE_RAILS_DIR'] ||= "/path/to/copy/of/edge/rails"
@@ -0,0 +1,2 @@
1
+ # This file loaded into Rakefile
2
+ # Place any extra development tasks you want here
@@ -0,0 +1,25 @@
1
+ # FIXME:
2
+ # I haven't figured out how to setup an irb console yet.
3
+ # So, from the root, run:
4
+ # irb -f -r scripts/console
5
+
6
+ PROJECT_ROOT = '.' #File.join(File.dirname(__FILE__), '..')
7
+
8
+ adapter = 'mysql'
9
+ $:.unshift 'lib'
10
+
11
+ require "local/database_connections"
12
+ begin
13
+ require "local/paths"
14
+ $:.unshift "#{ENV['EDGE_RAILS_DIR']}/activerecord/lib" if ENV['EDGE_RAILS_DIR']
15
+ $:.unshift "#{ENV['EDGE_RAILS_DIR']}/activesupport/lib" if ENV['EDGE_RAILS_DIR']
16
+ rescue
17
+ end
18
+
19
+ require 'active_support'
20
+ require 'active_record'
21
+
22
+ require "test/connections/native_#{adapter}/connection"
23
+ require 'composite_primary_keys'
24
+
25
+ Dir[File.join(PROJECT_ROOT,'test/fixtures/*.rb')].each { |model| require model }
@@ -0,0 +1,43 @@
1
+ namespace :ar do
2
+ desc 'Pre-load edge rails ActiveRecord'
3
+ task :edge do
4
+ unless path = ENV['EDGE_RAILS_DIR'] || ENV['EDGE_RAILS']
5
+ puts <<-EOS
6
+
7
+ Need to define env var EDGE_RAILS_DIR or EDGE_RAILS- root of edge rails on your machine.
8
+ i) Get copy of Edge Rails - http://dev.rubyonrails.org
9
+ ii) Set EDGE_RAILS_DIR to this folder in local/paths.rb - see local/paths.rb.sample for example
10
+ or
11
+ a) Set folder from environment or command line (rake ar:edge EDGE_RAILS_DIR=/path/to/rails)
12
+
13
+ EOS
14
+ exit
15
+ end
16
+
17
+ ENV['AR_LOAD_PATH'] = File.join(path, "activerecord/lib")
18
+ end
19
+
20
+ desc 'Pre-load ActiveRecord using VERSION=X.Y.Z, instead of latest'
21
+ task :set do
22
+ unless version = ENV['VERSION']
23
+ puts <<-EOS
24
+ Usage: rake ar:get_version VERSION=1.15.3
25
+ Specify the version number with VERSION=X.Y.Z; and make sure you have that activerecord gem version installed.
26
+
27
+ EOS
28
+ end
29
+ version = nil if version == "" || version == []
30
+ begin
31
+ version ? gem('activerecord', version) : gem('activerecord')
32
+ require 'active_record'
33
+ ENV['AR_LOAD_PATH'] = $:.reverse.find { |path| /activerecord/ =~ path }
34
+ rescue LoadError
35
+ puts <<-EOS
36
+ Missing: Cannot find activerecord #{version} installed.
37
+ Install: gem install activerecord -v #{version}
38
+
39
+ EOS
40
+ exit
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,10 @@
1
+ # UNTESTED - firebird sqlserver sqlserver_odbc db2 sybase openbase
2
+ for adapter in %w( mysql sqlite oracle postgresql ibm_db )
3
+ Rake::TestTask.new("test_#{adapter}") { |t|
4
+ t.libs << "test" << "test/connections/native_#{adapter}"
5
+ t.pattern = "test/test_*.rb"
6
+ t.verbose = true
7
+ }
8
+ end
9
+
10
+ SCHEMA_PATH = File.join(PROJECT_ROOT, *%w(test fixtures db_definitions))
@@ -0,0 +1,30 @@
1
+ namespace :mysql do
2
+ desc 'Build the MySQL test databases'
3
+ task :build_databases => :load_connection do
4
+ puts File.join(SCHEMA_PATH, 'mysql.sql')
5
+ options_str = ENV['cpk_adapter_options_str']
6
+ # creates something like "-u#{username} -p#{password} -S#{socket}"
7
+ sh %{ mysqladmin #{options_str} create "#{GEM_NAME}_unittest" }
8
+ sh %{ mysql #{options_str} "#{GEM_NAME}_unittest" < #{File.join(SCHEMA_PATH, 'mysql.sql')} }
9
+ end
10
+
11
+ desc 'Drop the MySQL test databases'
12
+ task :drop_databases => :load_connection do
13
+ options_str = ENV['cpk_adapter_options_str']
14
+ sh %{ mysqladmin #{options_str} -f drop "#{GEM_NAME}_unittest" }
15
+ end
16
+
17
+ desc 'Rebuild the MySQL test databases'
18
+ task :rebuild_databases => [:drop_databases, :build_databases]
19
+
20
+ task :load_connection do
21
+ require File.join(PROJECT_ROOT, %w[lib adapter_helper mysql])
22
+ spec = AdapterHelper::MySQL.load_connection_from_env
23
+ options = {}
24
+ options['u'] = spec[:username] if spec[:username]
25
+ options['p'] = spec[:password] if spec[:password]
26
+ options['S'] = spec[:sock] if spec[:sock]
27
+ options_str = options.map { |key, value| "-#{key}#{value}" }.join(" ")
28
+ ENV['cpk_adapter_options_str'] = options_str
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ namespace :oracle do
2
+ desc 'Build the Oracle test databases'
3
+ task :build_databases do
4
+ puts File.join(SCHEMA_PATH, 'oracle.sql')
5
+ sh %( sqlplus holstdl/holstdl@test < #{File.join(SCHEMA_PATH, 'oracle.sql')} )
6
+ end
7
+
8
+ desc 'Drop the Oracle test databases'
9
+ task :drop_databases do
10
+ sh %( sqlplus holstdl/holstdl@test < #{File.join(SCHEMA_PATH, 'oracle.drop.sql')} )
11
+ end
12
+
13
+ desc 'Rebuild the Oracle test databases'
14
+ task :rebuild_databases => [:drop_databases, :build_databases]
15
+ end
@@ -0,0 +1,26 @@
1
+ namespace :postgresql do
2
+ desc 'Build the PostgreSQL test databases'
3
+ task :build_databases => :load_connection do
4
+ sh %{ createdb "#{GEM_NAME}_unittest" }
5
+ sh %{ psql "#{GEM_NAME}_unittest" -f #{File.join(SCHEMA_PATH, 'postgresql.sql')} }
6
+ end
7
+
8
+ desc 'Drop the PostgreSQL test databases'
9
+ task :drop_databases => :load_connection do
10
+ sh %{ dropdb "#{GEM_NAME}_unittest" }
11
+ end
12
+
13
+ desc 'Rebuild the PostgreSQL test databases'
14
+ task :rebuild_databases => [:drop_databases, :build_databases]
15
+
16
+ task :load_connection do
17
+ require File.join(PROJECT_ROOT, %w[lib adapter_helper postgresql])
18
+ spec = AdapterHelper::Postgresql.load_connection_from_env
19
+ options = {}
20
+ options['u'] = spec[:username] if spec[:username]
21
+ options['p'] = spec[:password] if spec[:password]
22
+ options_str = options.map { |key, value| "-#{key}#{value}" }.join(" ")
23
+ ENV['cpk_adapter_options_str'] = options_str
24
+ end
25
+ end
26
+