Fix to accomodate non-standard PK columns.
This commit is contained in:
parent
fc49421890
commit
cd5e2a53f4
7
README
7
README
@ -54,4 +54,9 @@ db_populate rake tasks
|
|||||||
db_populate includes two rake tasks:
|
db_populate includes two rake tasks:
|
||||||
|
|
||||||
rake db:populate loads all of the data for the current environment
|
rake db:populate loads all of the data for the current environment
|
||||||
rake db:migrate_and_populate is the same as calling rake db:migrate followed by rake db:populate
|
rake db:migrate_and_populate is the same as calling rake db:migrate followed by rake db:populate
|
||||||
|
|
||||||
|
History
|
||||||
|
=======
|
||||||
|
2009-03-21 Patch from Ahmed El-Daly to allow PKs with names other than id
|
||||||
|
2008-10-11 Initial release
|
@ -1,10 +1,10 @@
|
|||||||
Gem::Specification.new do |s|
|
Gem::Specification.new do |s|
|
||||||
s.name = "db_populate"
|
s.name = "db_populate"
|
||||||
s.version = "0.2.0"
|
s.version = "0.2.1"
|
||||||
s.date = "2008-10-11"
|
s.date = "2009-03-21"
|
||||||
s.summary = "Seed data populator for Rails"
|
s.summary = "Seed data populator for Rails"
|
||||||
s.email = "MikeG1@larkfarm.com"
|
s.email = "MikeG1@larkfarm.com"
|
||||||
s.homepage = "http://github.com/ffmike/db_populate"
|
s.homepage = "http://github.com/ffmike/db-populate/tree/master"
|
||||||
s.description = "db_populate provides rake and code support for adding seed data to Rails projects. Forked from a rake task by
|
s.description = "db_populate provides rake and code support for adding seed data to Rails projects. Forked from a rake task by
|
||||||
Josh Knowles, plus code by Luke Francl."
|
Josh Knowles, plus code by Luke Francl."
|
||||||
s.has_rdoc = false
|
s.has_rdoc = false
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
class ActiveRecord::Base
|
class ActiveRecord::Base
|
||||||
# given a hash of attributes including the ID, look up the record by ID.
|
# given a hash of attributes including the ID, look up the record by ID.
|
||||||
|
# uses whatever the PK of the model is to do the lookup
|
||||||
# If it does not exist, it is created with the rest of the options.
|
# If it does not exist, it is created with the rest of the options.
|
||||||
# If it exists, it is updated with the given options.
|
# If it exists, it is updated with the given options.
|
||||||
#
|
#
|
||||||
@ -7,8 +8,8 @@ class ActiveRecord::Base
|
|||||||
#
|
#
|
||||||
# Returns the record.
|
# Returns the record.
|
||||||
def self.create_or_update(options = {})
|
def self.create_or_update(options = {})
|
||||||
id = options.delete(:id)
|
id = options.delete(primary_key.to_sym)
|
||||||
record = find_by_id(id) || new
|
record = send("find_by_#{primary_key}", id) || new
|
||||||
record.id = id
|
record.id = id
|
||||||
record.attributes = options
|
record.attributes = options
|
||||||
record.save!
|
record.save!
|
||||||
|
@ -6,6 +6,10 @@ require 'mocha'
|
|||||||
class User < ActiveRecord::Base
|
class User < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class Customer < ActiveRecord::Base
|
||||||
|
set_primary_key "cust_id"
|
||||||
|
end
|
||||||
|
|
||||||
class DbPopulateTest < Test::Unit::TestCase
|
class DbPopulateTest < Test::Unit::TestCase
|
||||||
|
|
||||||
def test_creates_new_record
|
def test_creates_new_record
|
||||||
@ -25,5 +29,22 @@ class DbPopulateTest < Test::Unit::TestCase
|
|||||||
assert_equal u.name, "George"
|
assert_equal u.name, "George"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_creates_new_record_with_nonstandard_pk
|
||||||
|
Customer.delete_all
|
||||||
|
Customer.create_or_update(:cust_id => 1, :name => "Fred")
|
||||||
|
assert_equal Customer.count, 1
|
||||||
|
c = Customer.find(:first)
|
||||||
|
assert_equal c.name, "Fred"
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_updates_existing_record
|
||||||
|
Customer.delete_all
|
||||||
|
Customer.create_or_update(:cust_id => 1, :name => "Fred")
|
||||||
|
Customer.create_or_update(:cust_id => 1, :name => "George")
|
||||||
|
assert_equal Customer.count, 1
|
||||||
|
c = Customer.find(:first)
|
||||||
|
assert_equal c.name, "George"
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -4,4 +4,8 @@ ActiveRecord::Schema.define(:version => 0) do
|
|||||||
t.string "name"
|
t.string "name"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "customers", :primary_key => 'cust_id', :force => true do |t|
|
||||||
|
t.string "name"
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user