Permalink
Browse files

General code cleanup

  • Loading branch information...
1 parent 8f9b627 commit af62c57aa54705817ff4a13079f7db15a332498e @brasten committed Jul 28, 2010
View
@@ -1 +1 @@
-0.1.3
+0.1.4
@@ -21,16 +21,19 @@ module Sequel
class Railtie < Rails::Railtie
- log_subscriber :sequel, ::Sequel::Railties::LogSubscriber.new
+ ::Sequel::Railties::LogSubscriber.attach_to :sequel
config.generators.orm :sequel, :migration => true
+ config.rails_fancy_pants_logging = true
rake_tasks do
load 'sequel-rails/railties/database.rake'
end
initializer 'sequel.configuration' do |app|
configure_sequel(app)
+
+ Rails::Sequel.setup(Rails.env)
end
initializer 'sequel.logger' do |app|
@@ -51,15 +54,15 @@ class Railtie < Rails::Railtie
# for the cascaded adapter wrappers that need to be declared before setup.
config.after_initialize do |app|
- setup_sequel(app)
- end
-
+ ::Sequel::Model.plugin :active_model
+ ::Sequel::Model.plugin :validation_helpers
+ ::Sequel::Model.raise_on_save_failure = false
+ end
# Support overwriting crucial steps in subclasses
-
def configure_sequel(app)
app.config.sequel = Rails::Sequel::Configuration.for(
Rails.root, app.config.database_configuration
@@ -79,32 +82,6 @@ def setup_logger(app, logger)
app.config.sequel.logger=logger
end
- module Setup
-
- def setup_sequel(app)
- # preload_lib(app)
- # preload_models(app)
- Rails::Sequel.setup(Rails.env)
- end
-
- # def preload_lib(app)
- # app.config.paths.lib.each do |path|
- # Dir.glob("#{path}/**/*.rb").sort.each do |file|
- # require_dependency file unless file.match(/#{path}\/generators\/*/)
- # end
- # end
- # end
- #
- # def preload_models(app)
- # app.config.paths.app.models.each do |path|
- # Dir.glob("#{path}/**/*.rb").sort.each { |file| require_dependency file }
- # end
- # end
-
- end
-
- extend Setup
-
end
end
@@ -10,10 +10,32 @@ namespace :db do
desc 'Create the database, load the schema, and initialize with the seed data'
task :setup => [ 'db:create', 'db:migrate', 'db:seed' ]
+ # namespace :test do
+ # task :prepare do
+ # Rails.env = "test"
+ # Rake::Task["db:setup"].invoke
+ # end
+ # end
+
namespace :test do
- task :prepare do
- Rails.env = "test"
- Rake::Task["db:setup"].invoke
+ desc "Recreate the test database from the current schema.rb"
+ task :load => 'db:test:purge' do
+ db = Rails::Sequel.setup('test')
+ Rake::Task['db:schema:load'].invoke(db)
+ end
+
+ desc 'Runs db:test:load'
+ task :prepare => :load
+
+ desc 'Empty the test database'
+ task :purge => :environment do
+ Rake::Task['db:drop'].invoke()
+ Rake::Task['db:create'].invoke()
+ end
+
+ task :environment do
+ Rails.env = 'test'
+ Rake::Task['environment'].invoke()
end
end
@@ -76,20 +98,32 @@ namespace :db do
##
# TODO: deal with this at some point
#
- # namespace :schema do
- # desc 'Create a db/schema.rb file that can be portably used against any DB supported by Sequel'
- # task :dump => :environment do
- # File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file|
- # file.puts(::Sequel::Model.db.schema())
- # end
- # end
- #
- # desc 'Load a schema.rb file into the database'
- # task :load => :environment do
- #
- # end
- # end
+ namespace :schema do
+ desc "Create a db/schema.rb file that can be portably used against any DB supported by Sequel."
+ task :dump => :environment do
+ Sequel.extension :schema_dumper
+
+ File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file|
+ file.puts Sequel::Model.db.dump_schema_migration
+ end
+ end
+ desc "Load a schema.rb file into the database."
+ task :load, :db, :needs => :environment do |t, args|
+ args.with_defaults(:db => Sequel::Model.db)
+
+ file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
+ if File.exists?(file) then
+ Sequel.extension :migration
+ schema_migration = eval(File.read(file))
+ schema_migration.apply(args.db, :up)
+ else
+ abort "#{file} doesn't exist."
+ end
+ end
+ end
+
+
desc 'Migrate the database to the latest version'
task :migrate => 'db:migrate:up'
@@ -108,5 +142,6 @@ namespace :db do
puts "Deleted entries from '#{::Rails::Sequel.configuration.environments[Rails.env]['database']}.sessions'"
end
end
-
end
+
+task 'test:prepare' => 'db:test:prepare'
@@ -1,7 +1,7 @@
module Sequel
module Railties
- class LogSubscriber < Rails::LogSubscriber
+ class LogSubscriber < ActiveSupport::LogSubscriber
def sql(event)
name = '%s (%.1fms)' % [event.payload[:name], event.duration]
@@ -161,13 +161,46 @@ def _drop
end
class Jdbc < Storage
+
+ def _is_mysql?
+ database.match(/^jdbc:mysql/)
+ end
+
+ def _root_url
+ database.scan /^jdbc:mysql:\/\/\w*:?\d*/
+ end
+
+ def db_name
+ database.scan(/^jdbc:mysql:\/\/\w+:?\d*\/(\w+)/).flatten.first
+ end
+
+ def _params
+ database.scan /\?.*$/
+ end
+
def _create
- raise NotImplementedError
+ if _is_mysql?
+ ::Sequel.connect("#{_root_url}#{_params}") do |db|
+ db.execute("CREATE DATABASE `#{db_name}` DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}")
+ end
+ end
end
def _drop
- raise NotImplementedError
+ if _is_mysql?
+ ::Sequel.connect("#{_root_url}#{_params}") do |db|
+ db.execute("DROP DATABASE IF EXISTS `#{db_name}`")
+ end
+ end
+ end
+
+ private
+
+ def collation
+ @collation ||= config['collation'] || ENV['COLLATION'] || 'utf8_unicode_ci'
end
+
+
end
end
View
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Brasten Sager (brasten)"]
- s.date = %q{2010-06-26}
+ s.date = %q{2010-07-19}
s.description = %q{Integrate Sequel with Rails 3}
s.email = %q{brasten@gmail.com}
s.extra_rdoc_files = [
@@ -73,18 +73,18 @@ Gem::Specification.new do |s|
s.specification_version = 3
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<sequel>, ["~> 3.10"])
+ s.add_runtime_dependency(%q<sequel>, ["~> 3.11"])
s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0.beta3"])
s.add_runtime_dependency(%q<actionpack>, ["~> 3.0.0.beta3"])
s.add_runtime_dependency(%q<railties>, ["~> 3.0.0.beta3"])
else
- s.add_dependency(%q<sequel>, ["~> 3.10"])
+ s.add_dependency(%q<sequel>, ["~> 3.11"])
s.add_dependency(%q<activesupport>, ["~> 3.0.0.beta3"])
s.add_dependency(%q<actionpack>, ["~> 3.0.0.beta3"])
s.add_dependency(%q<railties>, ["~> 3.0.0.beta3"])
end
else
- s.add_dependency(%q<sequel>, ["~> 3.10"])
+ s.add_dependency(%q<sequel>, ["~> 3.11"])
s.add_dependency(%q<activesupport>, ["~> 3.0.0.beta3"])
s.add_dependency(%q<actionpack>, ["~> 3.0.0.beta3"])
s.add_dependency(%q<railties>, ["~> 3.0.0.beta3"])

0 comments on commit af62c57

Please sign in to comment.