From 6be575cd5c851f90694626c2bddba257a784b56e Mon Sep 17 00:00:00 2001 From: Tom Meier Date: Fri, 18 Mar 2011 12:22:35 +1100 Subject: [PATCH 1/5] Update rails beta version --- Gemfile | 6 ++-- Gemfile.lock | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 Gemfile.lock diff --git a/Gemfile b/Gemfile index cbf51f9..e1985ee 100644 --- a/Gemfile +++ b/Gemfile @@ -6,9 +6,9 @@ gem 'yard', '~> 0.5' git 'git://github.com/rails/rails.git' do - gem 'activesupport', '~> 3.0.0.beta3', :require => 'active_support' - gem 'actionpack', '~> 3.0.0.beta3', :require => 'action_pack' - gem 'railties', '~> 3.0.0.beta3', :require => 'rails' + gem 'activesupport', '~> 3.1.0.beta', :require => 'active_support' + gem 'actionpack', '~> 3.1.0.beta', :require => 'action_pack' + gem 'railties', '~> 3.1.0.beta', :require => 'rails' end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..10eebb4 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,81 @@ +GIT + remote: git://github.com/rails/rails.git + revision: 4532b39f5faa15af957e3b41b671f07ed201488d + specs: + actionpack (3.1.0.beta) + activemodel (= 3.1.0.beta) + activesupport (= 3.1.0.beta) + builder (~> 3.0.0) + erubis (~> 2.6.6) + i18n (~> 0.5.0) + rack (~> 1.2.1) + rack-cache (~> 1.0.0) + rack-mount (~> 0.6.13) + rack-test (~> 0.5.7) + tzinfo (~> 0.3.23) + activemodel (3.1.0.beta) + activesupport (= 3.1.0.beta) + bcrypt-ruby (~> 2.1.4) + builder (~> 3.0.0) + i18n (~> 0.5.0) + activesupport (3.1.0.beta) + railties (3.1.0.beta) + actionpack (= 3.1.0.beta) + activesupport (= 3.1.0.beta) + rake (>= 0.8.7) + thor (~> 0.14.4) + +GEM + remote: http://rubygems.org/ + specs: + ZenTest (4.5.0) + abstract (1.0.0) + autotest (4.4.6) + ZenTest (>= 4.4.1) + bcrypt-ruby (2.1.4) + builder (3.0.0) + diff-lcs (1.1.2) + erubis (2.6.6) + abstract (>= 1.0.0) + git (1.2.5) + i18n (0.5.0) + jeweler (1.5.2) + bundler (~> 1.0.0) + git (>= 1.2.5) + rake + rack (1.2.2) + rack-cache (1.0) + rack (>= 0.4) + rack-mount (0.6.13) + rack (>= 1.0.0) + rack-test (0.5.7) + rack (>= 1.0) + rake (0.8.7) + rcov (0.9.9) + rspec (2.5.0) + rspec-core (~> 2.5.0) + rspec-expectations (~> 2.5.0) + rspec-mocks (~> 2.5.0) + rspec-core (2.5.1) + rspec-expectations (2.5.0) + diff-lcs (~> 1.1.2) + rspec-mocks (2.5.0) + sequel (3.11.0) + thor (0.14.6) + tzinfo (0.3.25) + yard (0.6.5) + +PLATFORMS + ruby + +DEPENDENCIES + actionpack (~> 3.1.0.beta)! + activesupport (~> 3.1.0.beta)! + autotest + jeweler (~> 1.4) + railties (~> 3.1.0.beta)! + rake (~> 0.8.7) + rcov + rspec + sequel (~> 3.11.0) + yard (~> 0.5) From 3d98f8c4437ecdecd439283a8234d78ffe23574c Mon Sep 17 00:00:00 2001 From: Tom Meier Date: Fri, 18 Mar 2011 12:28:04 +1100 Subject: [PATCH 2/5] On Create / Drop add missing required config options. Locally it works without these options, but when running with remote databases (eg, UAT environments) these config options are required or obscure error messages happen. Postgres - - Add config options for owner, port, and host MySQL - - Add config options for host --- lib/sequel-rails/storage.rb | 49 +++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/lib/sequel-rails/storage.rb b/lib/sequel-rails/storage.rb index b039c17..cdc6bad 100644 --- a/lib/sequel-rails/storage.rb +++ b/lib/sequel-rails/storage.rb @@ -26,7 +26,7 @@ def self.drop_environment(config) def self.new(config) config = Rails::Sequel.configuration.environments[config.to_s] unless config.kind_of?(Hash) - + klass = lookup_class(config['adapter']) if klass.equal?(self) super(config) @@ -86,6 +86,18 @@ def password @password ||= config['password'] || '' end + def host + @host ||= config['host'] || '' + end + + def port + @port ||= config['port'] || '' + end + + def owner + @owner ||= config['owner'] || '' + end + def charset @charset ||= config['charset'] || ENV['CHARSET'] || 'utf8' end @@ -129,6 +141,7 @@ def execute(statement) 'mysql', (username.blank? ? '' : "--user=#{username}"), (password.blank? ? '' : "--password=#{password}"), + (host.blank? ? '' : "--host=#{host}"), '-e', statement ) @@ -141,13 +154,17 @@ def collation end class Postgres < Storage + def _create system( 'createdb', - '-E', + '--encoding', charset, - '-U', + '--username', username, + (owner.blank? ? '' : "--owner=#{owner}"), + (port.blank? ? '' : "--port=#{port}"), + (host.blank? ? '' : "--host=#{host}"), database ) end @@ -155,31 +172,33 @@ def _create def _drop system( 'dropdb', - '-U', + '--username', username, + (port.blank? ? '' : "--port=#{port}"), + (host.blank? ? '' : "--host=#{host}"), database ) end 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 if _is_mysql? ::Sequel.connect("#{_root_url}#{_params}") do |db| @@ -195,16 +214,16 @@ def _drop end end end - + private - + def collation @collation ||= config['collation'] || ENV['COLLATION'] || 'utf8_unicode_ci' end - - + + end - + end end end From f8e6bb4dd22496474ebdb17eb07981f6350ff0f3 Mon Sep 17 00:00:00 2001 From: Tom Meier Date: Fri, 13 May 2011 19:26:08 +1000 Subject: [PATCH 3/5] Fix bug with setting multiple arguments in system command --- lib/sequel-rails/storage.rb | 50 +++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/lib/sequel-rails/storage.rb b/lib/sequel-rails/storage.rb index cdc6bad..8c92d74 100644 --- a/lib/sequel-rails/storage.rb +++ b/lib/sequel-rails/storage.rb @@ -137,14 +137,14 @@ def _drop private def execute(statement) - system( - 'mysql', - (username.blank? ? '' : "--user=#{username}"), - (password.blank? ? '' : "--password=#{password}"), - (host.blank? ? '' : "--host=#{host}"), - '-e', - statement - ) + commands = 'mysql ' + commands << "--user=#{username} " unless username.blank? + commands << "--password=#{password} " unless password.blank? + commands << "--host=#{host} " unless host.blank? + commands << '-e ' + commands << statement + + system(commands) end def collation @@ -156,28 +156,24 @@ def collation class Postgres < Storage def _create - system( - 'createdb', - '--encoding', - charset, - '--username', - username, - (owner.blank? ? '' : "--owner=#{owner}"), - (port.blank? ? '' : "--port=#{port}"), - (host.blank? ? '' : "--host=#{host}"), - database - ) + commands = "createdb --encoding=#{charset} " + commands << "--username=#{username} " unless username.blank? + commands << "--owner=#{owner} " unless owner.blank? + commands << "--port=#{port} " unless port.blank? + commands << "--host=#{host} " unless host.blank? + commands << database + + system(commands) end def _drop - system( - 'dropdb', - '--username', - username, - (port.blank? ? '' : "--port=#{port}"), - (host.blank? ? '' : "--host=#{host}"), - database - ) + commands = "dropdb " + commands << "--username=#{username} " unless username.blank? + commands << "--port=#{port} " unless port.blank? + commands << "--host=#{host} " unless host.blank? + commands << database + + system(commands) end end From 8f0906fa25822855721689f3036f0d07e1520ec1 Mon Sep 17 00:00:00 2001 From: Tom Meier Date: Fri, 13 May 2011 19:29:10 +1000 Subject: [PATCH 4/5] Force close any open connections on postgres --- lib/sequel-rails/railties/database.rake | 46 ++++++++++++++++++------- 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/lib/sequel-rails/railties/database.rake b/lib/sequel-rails/railties/database.rake index bca8ad4..c5bd127 100644 --- a/lib/sequel-rails/railties/database.rake +++ b/lib/sequel-rails/railties/database.rake @@ -10,12 +10,12 @@ namespace :db do end Rake::Task["db:schema:dump"].reenable end - + desc "Load a schema.rb file into the database" task :load, :needs => :environment do require 'sequel-rails/storage' Rails::Sequel::Storage.new(Rails.env).create - + file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb" if File.exists?(file) load(file) @@ -36,15 +36,15 @@ namespace :db do desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?" task :create, :env, :needs => :environment do |t, args| args.with_defaults(:env => Rails.env) - + require 'sequel-rails/storage' Rails::Sequel::Storage.new(args.env).create - + if Rails.env.development? && Rails.configuration.database_configuration['test'] Rails::Sequel::Storage.new('test').create end end - + namespace :drop do desc 'Drops all the local databases defined in config/database.yml' task :all, :needs => :environment do @@ -52,14 +52,14 @@ namespace :db do Rails::Sequel::Storage.drop_all end end - + desc "Create the database defined in config/database.yml for the current Rails.env - also creates the test database if Rails.env.development?" task :drop, :env, :needs => :environment do |t, args| args.with_defaults(:env => Rails.env) - + require 'sequel-rails/storage' Rails::Sequel::Storage.new(args.env).drop - + if Rails.env.development? && Rails.configuration.database_configuration['test'] Rails::Sequel::Storage.new('test').drop end @@ -101,7 +101,7 @@ namespace :db do Rake::Task["db:schema:dump"].invoke if Rails.env != 'test' end end - + desc 'Migrate the database to the latest version' task :migrate => :'migrate:load' do Rails::Sequel::Migrations.migrate_up!(ENV["VERSION"] ? ENV["VERSION"].to_i : nil) @@ -121,22 +121,42 @@ namespace :db do Sequel::Migrator.forward('db/migrate/', step) Rake::Task["db:schema:dump"].invoke if Rails.env != 'test' end - + desc 'Load the seed data from db/seeds.rb' task :seed => :environment do seed_file = File.join(Rails.root, 'db', 'seeds.rb') load(seed_file) if File.exist?(seed_file) end - + desc 'Create the database, load the schema, and initialize with the seed data' task :setup => [ 'db:create', 'db:migrate', 'db:seed' ] - + desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.' task :reset => [ 'db:drop', 'db:setup' ] - + + desc 'Forcibly close any open connections to the test database' + task :force_close_open_connections => :environment do + if Rails.env.test? + db_config = Rails.configuration.database_configuration[Rails.env].symbolize_keys + + begin + #Will only work on Postgres > 8.4 + db_adapter.execute <<-SQL.gsub(/^\s{9}/,'') + SELECT COUNT(pg_terminate_backend(procpid)) + FROM pg_stat_activity + WHERE datname = '#{db_config[:database]}'; + SQL + rescue => e + #Will raise an error as it kills existing process running this command + #Seems to be only way to ensure *all* test connections are closed + end + end + end + namespace :test do task :prepare do Rails.env = 'test' + Rake::Task['db:force_close_open_connections'].invoke() Rake::Task['db:reset'].invoke() Sequel::DATABASES.each do |db| db.disconnect From 9b87d093bdd5ccf9ad75cd0eab705b17e2ecb94f Mon Sep 17 00:00:00 2001 From: Tom Meier Date: Fri, 13 May 2011 19:38:06 +1000 Subject: [PATCH 5/5] Fix - Use Sequel model directly --- lib/sequel-rails/railties/database.rake | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/sequel-rails/railties/database.rake b/lib/sequel-rails/railties/database.rake index c5bd127..63f43d0 100644 --- a/lib/sequel-rails/railties/database.rake +++ b/lib/sequel-rails/railties/database.rake @@ -138,10 +138,9 @@ namespace :db do task :force_close_open_connections => :environment do if Rails.env.test? db_config = Rails.configuration.database_configuration[Rails.env].symbolize_keys - begin #Will only work on Postgres > 8.4 - db_adapter.execute <<-SQL.gsub(/^\s{9}/,'') + Sequel::Model.db.execute <<-SQL.gsub(/^\s{9}/,'') SELECT COUNT(pg_terminate_backend(procpid)) FROM pg_stat_activity WHERE datname = '#{db_config[:database]}';