From 4d040be1bd3acd98cd5cb766ac28f6cf78633dd5 Mon Sep 17 00:00:00 2001 From: Mike Gunderloy Date: Mon, 15 Sep 2008 20:08:50 -0500 Subject: [PATCH] Converting to plugin, adding code. --- MIT-LICENSE | 20 ++++++++++++++++++++ README | 1 + Rakefile | 22 ++++++++++++++++++++++ db_populate.gemspec | 28 ++++++++++++++++++++++++++++ init.rb | 1 + install.rb | 1 + lib/create_or_update.rb | 18 ++++++++++++++++++ rails/init.rb | 1 + tasks/db_populate_tasks.rake | 4 ++++ tasks/populate.rake | 9 +++++++-- test/database.yml | 18 ++++++++++++++++++ test/db_populate_test.rb | 8 ++++++++ test/schema.rb | 7 +++++++ test/test_helper.rb | 34 ++++++++++++++++++++++++++++++++++ uninstall.rb | 1 + 15 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 MIT-LICENSE create mode 100644 README create mode 100644 Rakefile create mode 100644 db_populate.gemspec create mode 100644 init.rb create mode 100644 install.rb create mode 100644 lib/create_or_update.rb create mode 100644 rails/init.rb create mode 100644 tasks/db_populate_tasks.rake create mode 100644 test/database.yml create mode 100644 test/db_populate_test.rb create mode 100644 test/schema.rb create mode 100644 test/test_helper.rb create mode 100644 uninstall.rb diff --git a/MIT-LICENSE b/MIT-LICENSE new file mode 100644 index 0000000..8eaf6db --- /dev/null +++ b/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2008 [name of plugin creator] + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README b/README new file mode 100644 index 0000000..2fd9f95 --- /dev/null +++ b/README @@ -0,0 +1 @@ +TBD \ No newline at end of file diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..6a1db41 --- /dev/null +++ b/Rakefile @@ -0,0 +1,22 @@ +require 'rake' +require 'rake/testtask' +require 'rake/rdoctask' + +desc 'Default: run unit tests.' +task :default => :test + +desc 'Test the db_populate plugin.' +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' + t.pattern = 'test/**/*_test.rb' + t.verbose = true +end + +desc 'Generate documentation for the db_populate plugin.' +Rake::RDocTask.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'rdoc' + rdoc.title = 'DbPopulate' + rdoc.options << '--line-numbers' << '--inline-source' + rdoc.rdoc_files.include('README') + rdoc.rdoc_files.include('lib/**/*.rb') +end diff --git a/db_populate.gemspec b/db_populate.gemspec new file mode 100644 index 0000000..b38426a --- /dev/null +++ b/db_populate.gemspec @@ -0,0 +1,28 @@ +Gem::Specification.new do |s| + s.name = "db_populate" + s.version = "0.1.0" + s.date = "2008-09-15" + s.summary = "Seed data populator for Rails" + s.email = "MikeG1@larkfarm.com" + s.homepage = "http://github.com/ffmike/db_populate" + 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 Franci." + s.has_rdoc = false + s.authors = ["Mike Gunderloy", "Josh Knowles", "Luke Franci"] + s.files = [ + "MIT-LICENSE", + "README", + "Rakefile", + "init.rb", + "install.rb", + "lib/create_or_update.rb", + "rails/init.rb" + "tasks/populate.rake", + "tasks/db_populate_tasks.rake" + "test/database.yml", + "test/schema.rb", + "test/test_helper.rb", + "test/db_populate_test.rb", + "uninstall.rb" + ] +end diff --git a/init.rb b/init.rb new file mode 100644 index 0000000..2644663 --- /dev/null +++ b/init.rb @@ -0,0 +1 @@ +require File.dirname(__FILE__) + "/rails/init" \ No newline at end of file diff --git a/install.rb b/install.rb new file mode 100644 index 0000000..f7732d3 --- /dev/null +++ b/install.rb @@ -0,0 +1 @@ +# Install hook code here diff --git a/lib/create_or_update.rb b/lib/create_or_update.rb new file mode 100644 index 0000000..b53b6aa --- /dev/null +++ b/lib/create_or_update.rb @@ -0,0 +1,18 @@ +class ActiveRecord::Base + # given a hash of attributes including the ID, look up the record by ID. + # If it does not exist, it is created with the rest of the options. + # If it exists, it is updated with the given options. + # + # Raises an exception if the record is invalid to ensure seed data is loaded correctly. + # + # Returns the record. + def self.create_or_update(options = {}) + id = options.delete(:id) + record = find_by_id(id) || new + record.id = id + record.attributes = options + record.save! + + record + end +end \ No newline at end of file diff --git a/rails/init.rb b/rails/init.rb new file mode 100644 index 0000000..268a2e4 --- /dev/null +++ b/rails/init.rb @@ -0,0 +1 @@ +require "create_or_update" diff --git a/tasks/db_populate_tasks.rake b/tasks/db_populate_tasks.rake new file mode 100644 index 0000000..12f89b5 --- /dev/null +++ b/tasks/db_populate_tasks.rake @@ -0,0 +1,4 @@ +# desc "Explaining what the task does" +# task :user_event_logger do +# # Task goes here +# end diff --git a/tasks/populate.rake b/tasks/populate.rake index 988278c..dc7b613 100644 --- a/tasks/populate.rake +++ b/tasks/populate.rake @@ -1,7 +1,8 @@ -amespace :db do +namespace :db do + desc "Loads initial database models for the current environment." task :populate => :environment do - require File.join(File.dirname(__FILE__), '/../../lib', 'create_or_update') + require File.join(File.dirname(__FILE__), '/../lib', 'create_or_update') Dir[File.join(RAILS_ROOT, 'db', 'populate', '*.rb')].sort.each do |fixture| load fixture puts "Loaded #{fixture}" @@ -11,4 +12,8 @@ amespace :db do puts "Loaded #{fixture}" end end + + desc "Runs migrations and then loads seed data" + task :migrate_and_load => [ 'db:migrate', 'db:populate' ] + end \ No newline at end of file diff --git a/test/database.yml b/test/database.yml new file mode 100644 index 0000000..6946f98 --- /dev/null +++ b/test/database.yml @@ -0,0 +1,18 @@ +sqlite: + :adapter: sqlite + :dbfile: db_populate_plugin.sqlite.db +sqlite3: + :adapter: sqlite3 + :dbfile: db_populate_plugin.sqlite3.db +postgresql: + :adapter: postgresql + :username: postgres + :password: postgres + :database: db_populate_plugin_test + :min_messages: ERROR +mysql: + :adapter: mysql + :host: localhost + :username: rails + :password: + :database: db_populate_plugin_test \ No newline at end of file diff --git a/test/db_populate_test.rb b/test/db_populate_test.rb new file mode 100644 index 0000000..04080ea --- /dev/null +++ b/test/db_populate_test.rb @@ -0,0 +1,8 @@ +require File.dirname(__FILE__) + '/test_helper.rb' +require 'test/unit' +require 'rubygems' +require 'mocha' + +class UserEventLoggerTest < Test::Unit::TestCase +end + diff --git a/test/schema.rb b/test/schema.rb new file mode 100644 index 0000000..9828fbc --- /dev/null +++ b/test/schema.rb @@ -0,0 +1,7 @@ +ActiveRecord::Schema.define(:version => 0) do + + create_table "users", :force => true do |t| + t.string "name" + end + +end diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000..93db929 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,34 @@ +ENV['RAILS_ENV'] = 'test' +ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..' + +require 'test/unit' +require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb')) + +config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) +ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") + +db_adapter = ENV['DB'] + +# no db passed, try one of these fine config-free DBs before bombing. +db_adapter ||= begin + require 'rubygems' + require 'sqlite' + 'sqlite' + rescue MissingSourceFile + begin + require 'sqlite3' + 'sqlite3' + rescue MissingSourceFile + end +end + +if db_adapter.nil? + raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3." +end + +ActiveRecord::Base.establish_connection(config[db_adapter]) + +load(File.dirname(__FILE__) + "/schema.rb") + +require File.dirname(__FILE__) + '/../init.rb' + diff --git a/uninstall.rb b/uninstall.rb new file mode 100644 index 0000000..9738333 --- /dev/null +++ b/uninstall.rb @@ -0,0 +1 @@ +# Uninstall hook code here