ffmike-db_populate 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README +62 -0
- data/Rakefile +22 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/create_or_update.rb +19 -0
- data/rails/init.rb +1 -0
- data/tasks/db_populate_tasks.rake +4 -0
- data/tasks/populate.rake +21 -0
- data/test/database.yml +18 -0
- data/test/db_populate_test.rb +50 -0
- data/test/schema.rb +11 -0
- data/test/test_helper.rb +34 -0
- data/uninstall.rb +1 -0
- metadata +68 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 [name of plugin creator]
|
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.
|
data/README
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
db_populate
|
2
|
+
===========
|
3
|
+
db_populate is an answer to the question "how do I get seed data into a Rails application?"
|
4
|
+
Seed data is normally the contents of lookup tables that are essential to the normal
|
5
|
+
functioning of your application: lists of roles, administrative accounts, choices for
|
6
|
+
dropdown boxes, and so on.
|
7
|
+
|
8
|
+
The inspiration (and some of the code) for this plugin come from a blog entry by Luke
|
9
|
+
Francl (http://railspikes.com/2008/2/1/loading-seed-data) that looked at some of the
|
10
|
+
available alternatives for loading seed data. Some more of the code came from Josh
|
11
|
+
Knowles' db_populate plugin (http://code.google.com/p/db-populate/). But I didn't like
|
12
|
+
having to assemble bits, and had some ideas to extend it, and...well, you know how it
|
13
|
+
goes.
|
14
|
+
|
15
|
+
Using db_populate
|
16
|
+
=================
|
17
|
+
The basic idea behind db_populate is simple: to put seed data in your application's
|
18
|
+
tables, it executes ruby code. The code needs to be in a specific place, and there's a
|
19
|
+
helper to make it easier to create and update consistent seed data. Then there are a
|
20
|
+
couple of rake tasks. That's it.
|
21
|
+
|
22
|
+
Setting up for db_populate
|
23
|
+
==========================
|
24
|
+
|
25
|
+
To get started with db_populate, create the folder db/populate in your Rails application.
|
26
|
+
Any code you put in this folder will be run by db_populate. Optionally, you can create
|
27
|
+
subfolders for your Rails environments, just as you can with config files. db_populate
|
28
|
+
executes all of the top-level populate files first, followed by any environment-specific
|
29
|
+
populate files, sorting each list by name. So, for example, with 4 files in the production
|
30
|
+
environment, db_populate would order this way:
|
31
|
+
|
32
|
+
db/populate/01_roles.rb
|
33
|
+
db/populate/02_services.rb
|
34
|
+
db/populate/production/01_users.rb
|
35
|
+
db/populate/production/02_options.rb
|
36
|
+
|
37
|
+
Within each file, you can place whatever ruby code you like. To help create consistent
|
38
|
+
records, db_populate adds create_or_populate to ActiveRecord::Base. This method looks up
|
39
|
+
a record by ID; if the record exists, it is updated, and if it doesn't, it is created. Using
|
40
|
+
this technique means that you can edit and re-run your db_populate tasks without damaging
|
41
|
+
data that have already been loaded once. For example, assuming your roles table has already
|
42
|
+
been populated, a db_populate file to create an administrative user might look like this:
|
43
|
+
|
44
|
+
user = User.create_or_update(:id => 1, :login => "admin", :email => "admin@example.com",
|
45
|
+
:name => "Site Administrator", :password => "admin", :password_confirmation => "admin")
|
46
|
+
role = Role.find_by_rolename('administrator')
|
47
|
+
Permission.create_or_update(:id => 1, :role_id => role.id, :user_id => user.id)
|
48
|
+
|
49
|
+
If you change your mind about the name for the site administrator, you can just edit the data
|
50
|
+
and re-run the task.
|
51
|
+
|
52
|
+
db_populate rake tasks
|
53
|
+
======================
|
54
|
+
db_populate includes two rake tasks:
|
55
|
+
|
56
|
+
rake db:populate loads all of the data for the current environment
|
57
|
+
rake db:migrate_and_populate is the same as calling rake db:migrate followed by rake db:populate
|
58
|
+
|
59
|
+
History
|
60
|
+
=======
|
61
|
+
2009-03-21 Patch from Ahmed El-Daly to allow PKs with names other than id
|
62
|
+
2008-10-11 Initial release
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the db_populate plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the db_populate plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'DbPopulate'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/rails/init"
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class ActiveRecord::Base
|
2
|
+
# given a hash of attributes including the ID, look up the record by ID.
|
3
|
+
# uses whatever the PK of the model is to do the lookup
|
4
|
+
# If it does not exist, it is created with the rest of the options.
|
5
|
+
# If it exists, it is updated with the given options.
|
6
|
+
#
|
7
|
+
# Raises an exception if the record is invalid to ensure seed data is loaded correctly.
|
8
|
+
#
|
9
|
+
# Returns the record.
|
10
|
+
def self.create_or_update(options = {})
|
11
|
+
id = options.delete(primary_key.to_sym)
|
12
|
+
record = send("find_by_#{primary_key}", id) || new
|
13
|
+
record.id = id
|
14
|
+
record.attributes = options
|
15
|
+
record.save!
|
16
|
+
|
17
|
+
record
|
18
|
+
end
|
19
|
+
end
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "create_or_update"
|
data/tasks/populate.rake
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
namespace :db do
|
2
|
+
|
3
|
+
desc "Loads initial database models for the current environment."
|
4
|
+
task :populate => :environment do
|
5
|
+
require File.join(File.dirname(__FILE__), '/../lib', 'create_or_update')
|
6
|
+
Dir[File.join(RAILS_ROOT, 'db', 'populate', '*.rb')].sort.each do |fixture|
|
7
|
+
load fixture
|
8
|
+
puts "Loaded #{fixture}"
|
9
|
+
end
|
10
|
+
Dir[File.join(RAILS_ROOT, 'db', 'populate', RAILS_ENV, '*.rb')].sort.each do |fixture|
|
11
|
+
load fixture
|
12
|
+
puts "Loaded #{fixture}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Runs migrations and then loads seed data"
|
17
|
+
task :migrate_and_populate => [ 'db:migrate', 'db:populate' ]
|
18
|
+
|
19
|
+
task :migrate_and_load => [ 'db:migrate', 'db:populate' ]
|
20
|
+
|
21
|
+
end
|
data/test/database.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
sqlite:
|
2
|
+
:adapter: sqlite
|
3
|
+
:dbfile: db_populate_plugin.sqlite.db
|
4
|
+
sqlite3:
|
5
|
+
:adapter: sqlite3
|
6
|
+
:dbfile: db_populate_plugin.sqlite3.db
|
7
|
+
postgresql:
|
8
|
+
:adapter: postgresql
|
9
|
+
:username: postgres
|
10
|
+
:password: postgres
|
11
|
+
:database: db_populate_plugin_test
|
12
|
+
:min_messages: ERROR
|
13
|
+
mysql:
|
14
|
+
:adapter: mysql
|
15
|
+
:host: localhost
|
16
|
+
:username: rails
|
17
|
+
:password:
|
18
|
+
:database: db_populate_plugin_test
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'mocha'
|
5
|
+
|
6
|
+
class User < ActiveRecord::Base
|
7
|
+
end
|
8
|
+
|
9
|
+
class Customer < ActiveRecord::Base
|
10
|
+
set_primary_key "cust_id"
|
11
|
+
end
|
12
|
+
|
13
|
+
class DbPopulateTest < Test::Unit::TestCase
|
14
|
+
|
15
|
+
def test_creates_new_record
|
16
|
+
User.delete_all
|
17
|
+
User.create_or_update(:id => 1, :name => "Fred")
|
18
|
+
assert_equal User.count, 1
|
19
|
+
u = User.find(:first)
|
20
|
+
assert_equal u.name, "Fred"
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_updates_existing_record
|
24
|
+
User.delete_all
|
25
|
+
User.create_or_update(:id => 1, :name => "Fred")
|
26
|
+
User.create_or_update(:id => 1, :name => "George")
|
27
|
+
assert_equal User.count, 1
|
28
|
+
u = User.find(:first)
|
29
|
+
assert_equal u.name, "George"
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_creates_new_record_with_nonstandard_pk
|
33
|
+
Customer.delete_all
|
34
|
+
Customer.create_or_update(:cust_id => 1, :name => "Fred")
|
35
|
+
assert_equal Customer.count, 1
|
36
|
+
c = Customer.find(:first)
|
37
|
+
assert_equal c.name, "Fred"
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_updates_existing_record
|
41
|
+
Customer.delete_all
|
42
|
+
Customer.create_or_update(:cust_id => 1, :name => "Fred")
|
43
|
+
Customer.create_or_update(:cust_id => 1, :name => "George")
|
44
|
+
assert_equal Customer.count, 1
|
45
|
+
c = Customer.find(:first)
|
46
|
+
assert_equal c.name, "George"
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
data/test/schema.rb
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
ENV['RAILS_ENV'] = 'test'
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'action_controller'
|
6
|
+
require 'active_record'
|
7
|
+
require 'action_view'
|
8
|
+
|
9
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
|
10
|
+
|
11
|
+
db_adapter = ENV['DB']
|
12
|
+
|
13
|
+
# no db passed, try one of these fine config-free DBs before bombing.
|
14
|
+
db_adapter ||= begin
|
15
|
+
require 'rubygems'
|
16
|
+
require 'sqlite'
|
17
|
+
'sqlite'
|
18
|
+
rescue MissingSourceFile
|
19
|
+
begin
|
20
|
+
require 'sqlite3'
|
21
|
+
'sqlite3'
|
22
|
+
rescue MissingSourceFile
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
if db_adapter.nil?
|
27
|
+
raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
|
28
|
+
end
|
29
|
+
|
30
|
+
ActiveRecord::Base.establish_connection(config[db_adapter])
|
31
|
+
|
32
|
+
load(File.dirname(__FILE__) + "/schema.rb")
|
33
|
+
|
34
|
+
require File.dirname(__FILE__) + '/../init.rb'
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ffmike-db_populate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Gunderloy
|
8
|
+
- Josh Knowles
|
9
|
+
- Luke Francl
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
|
14
|
+
date: 2009-03-21 00:00:00 -07:00
|
15
|
+
default_executable:
|
16
|
+
dependencies: []
|
17
|
+
|
18
|
+
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.
|
19
|
+
email: MikeG1@larkfarm.com
|
20
|
+
executables: []
|
21
|
+
|
22
|
+
extensions: []
|
23
|
+
|
24
|
+
extra_rdoc_files: []
|
25
|
+
|
26
|
+
files:
|
27
|
+
- MIT-LICENSE
|
28
|
+
- README
|
29
|
+
- Rakefile
|
30
|
+
- init.rb
|
31
|
+
- install.rb
|
32
|
+
- lib/create_or_update.rb
|
33
|
+
- rails/init.rb
|
34
|
+
- tasks/populate.rake
|
35
|
+
- tasks/db_populate_tasks.rake
|
36
|
+
- test/database.yml
|
37
|
+
- test/schema.rb
|
38
|
+
- test/test_helper.rb
|
39
|
+
- test/db_populate_test.rb
|
40
|
+
- uninstall.rb
|
41
|
+
has_rdoc: false
|
42
|
+
homepage: http://github.com/ffmike/db-populate/tree/master
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
version:
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.2.0
|
64
|
+
signing_key:
|
65
|
+
specification_version: 2
|
66
|
+
summary: Seed data populator for Rails
|
67
|
+
test_files: []
|
68
|
+
|