From 8279144073b4be118d826a23edd2b37b741d92e6 Mon Sep 17 00:00:00 2001 From: Holger Just Date: Sun, 15 May 2011 23:14:02 +0200 Subject: [PATCH 1/4] [#112] Add ChiliProject::Database This module can be used to gather information about the currently used database. --- lib/chili_project/database.rb | 78 ++++++++++++++++++++ test/unit/lib/chili_project/database_test.rb | 39 ++++++++++ 2 files changed, 117 insertions(+) create mode 100644 lib/chili_project/database.rb create mode 100644 test/unit/lib/chili_project/database_test.rb diff --git a/lib/chili_project/database.rb b/lib/chili_project/database.rb new file mode 100644 index 00000000..918704ad --- /dev/null +++ b/lib/chili_project/database.rb @@ -0,0 +1,78 @@ +# ChiliProject is a project management system. +# Copyright (C) 2010-2011 The ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +module ChiliProject + + # This module provides some information about the currently used database + # adapter. It can be used to write code specific to certain database + # vendors which, while not not encouraged, is sometimes necessary due to + # syntax differences. + + module Database + + # This method returns a hash which maps the identifier of the supported + # adapter to a regex matching the adapter_name. + def self.supported_adapters + @adapters ||= ({ + :mysql => /mysql/i, + :postgresql => /postgres/i, + :sqlite => /sqlite/i + }) + end + + # Get the raw namme of the currently used database adapter. + # This string is set by the used adapter gem. + def self.adapter_name + ActiveRecord::Base.connection.adapter_name + end + + # returns the identifier of the currently used database type + def self.name + supported_adapters.find(proc{ [:unknown, //] }) { |adapter, regex| + self.adapter_name =~ regex + }[0] + end + + # Provide helper methods to quickly check the database type + # ChiliProject::Database.mysql? returns true, if we have a MySQL DB + supported_adapters.keys.each do |adapter| + (class << self; self; end).class_eval do + define_method(:"#{adapter.to_s}?"){ send(:name) == adapter } + end + end + + # Return the version of the underlying database engine. + # Set the +raw+ argument to true to return the unmangled string + # from the database. + def self.version(raw = false) + case self.name + when :mysql + version = ActiveRecord::Base.connection.select_value('SELECT VERSION()') + when :postgresql + version = ActiveRecord::Base.connection.select_value('SELECT version()') + version.match(/^PostgreSQL (\S+)/i)[1] unless raw + when :sqlite + if SQLite3.const_defined? 'SQLITE_VERSION' + SQLite3::SQLITE_VERSION + else + SQLite3::Driver::Native::API.sqlite3_libversion + end + end + end + + end +end \ No newline at end of file diff --git a/test/unit/lib/chili_project/database_test.rb b/test/unit/lib/chili_project/database_test.rb new file mode 100644 index 00000000..f4cdf0e0 --- /dev/null +++ b/test/unit/lib/chili_project/database_test.rb @@ -0,0 +1,39 @@ +# ChiliProject is a project management system. +# Copyright (C) 2010-2011 The ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +require File.expand_path('../../../../test_helper', __FILE__) + +class ChiliProject::DatabaseTest < ActiveSupport::TestCase + setup do + ChiliProject::Database.stubs(:adapter_name).returns "SQLite" + SQLite3::Driver::Native::API.stubs(:sqlite3_libversion).returns "3.6.12" + end + + should "return the correct identifier" do + assert_equal :sqlite, ChiliProject::Database.name + end + + should "be able to use the helper methods" do + assert_equal false, ChiliProject::Database.mysql? + assert_equal false, ChiliProject::Database.postgresql? + assert_equal true, ChiliProject::Database.sqlite? + end + + should "return a version string" do + assert_equal "3.6.12", ChiliProject::Database.version + end +end From d90102420d9704fed197474148477371c59ec2fe Mon Sep 17 00:00:00 2001 From: Holger Just Date: Sun, 15 May 2011 23:15:52 +0200 Subject: [PATCH 2/4] [#112] Adapt database switches to use ChiliProject::Database --- app/models/user.rb | 2 +- db/migrate/20091227112908_change_wiki_contents_text_limit.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 17610e02..693c721e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -299,7 +299,7 @@ class User < Principal # version. Exact matches will be given priority. def self.find_by_login(login) # force string comparison to be case sensitive on MySQL - type_cast = (ActiveRecord::Base.connection.adapter_name =~ /mysql/i) ? 'BINARY' : '' + type_cast = (ChiliProject::Database.mysql?) ? 'BINARY' : '' # First look for an exact match user = first(:conditions => ["#{type_cast} login = ?", login]) # Fail over to case-insensitive if none was found diff --git a/db/migrate/20091227112908_change_wiki_contents_text_limit.rb b/db/migrate/20091227112908_change_wiki_contents_text_limit.rb index 225f71e6..5e92cf01 100644 --- a/db/migrate/20091227112908_change_wiki_contents_text_limit.rb +++ b/db/migrate/20091227112908_change_wiki_contents_text_limit.rb @@ -3,7 +3,7 @@ class ChangeWikiContentsTextLimit < ActiveRecord::Migration # Migrates MySQL databases only # Postgres would raise an error (see http://dev.rubyonrails.org/ticket/3818) # Not fixed in Rails 2.3.5 - if ActiveRecord::Base.connection.adapter_name =~ /mysql/i + if ChiliProject::Database.mysql? max_size = 16.megabytes change_column :wiki_contents, :text, :text, :limit => max_size change_column :wiki_content_versions, :data, :binary, :limit => max_size From 705c0db00006bb68e6314a0b884e8e62d20beb3a Mon Sep 17 00:00:00 2001 From: Holger Just Date: Mon, 16 May 2011 00:01:09 +0200 Subject: [PATCH 3/4] [#112] Fix failing raw version for postgres --- lib/chili_project/database.rb | 2 +- test/unit/lib/chili_project/database_test.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/chili_project/database.rb b/lib/chili_project/database.rb index 918704ad..3aea3959 100644 --- a/lib/chili_project/database.rb +++ b/lib/chili_project/database.rb @@ -64,7 +64,7 @@ module ChiliProject version = ActiveRecord::Base.connection.select_value('SELECT VERSION()') when :postgresql version = ActiveRecord::Base.connection.select_value('SELECT version()') - version.match(/^PostgreSQL (\S+)/i)[1] unless raw + raw ? version : version.match(/^PostgreSQL (\S+)/i)[1] when :sqlite if SQLite3.const_defined? 'SQLITE_VERSION' SQLite3::SQLITE_VERSION diff --git a/test/unit/lib/chili_project/database_test.rb b/test/unit/lib/chili_project/database_test.rb index f4cdf0e0..1296a0dd 100644 --- a/test/unit/lib/chili_project/database_test.rb +++ b/test/unit/lib/chili_project/database_test.rb @@ -36,4 +36,15 @@ class ChiliProject::DatabaseTest < ActiveSupport::TestCase should "return a version string" do assert_equal "3.6.12", ChiliProject::Database.version end + + should "return long version string for raw==true" do + ChiliProject::Database.stubs(:adapter_name).returns "PostgreSQL" + + raw_version = "PostgreSQL 8.3.11 on x86_64-pc-linux-gnu, compiled by GCC gcc-4.3.real (Debian 4.3.2-1.1) 4.3.2" + ActiveRecord::Base.connection.stubs(:select_value).returns raw_version + + assert_equal "8.3.11", ChiliProject::Database.version + assert_equal raw_version, ChiliProject::Database.version(true) + end + end From 7b3a2047fd3c75e505d297755b3d027a1315190d Mon Sep 17 00:00:00 2001 From: Holger Just Date: Mon, 16 May 2011 00:03:51 +0200 Subject: [PATCH 4/4] Whitespace fixes --- lib/chili_project/database.rb | 2 +- test/unit/lib/chili_project/database_test.rb | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/chili_project/database.rb b/lib/chili_project/database.rb index 3aea3959..c7837667 100644 --- a/lib/chili_project/database.rb +++ b/lib/chili_project/database.rb @@ -46,7 +46,7 @@ module ChiliProject self.adapter_name =~ regex }[0] end - + # Provide helper methods to quickly check the database type # ChiliProject::Database.mysql? returns true, if we have a MySQL DB supported_adapters.keys.each do |adapter| diff --git a/test/unit/lib/chili_project/database_test.rb b/test/unit/lib/chili_project/database_test.rb index 1296a0dd..e47c7a2f 100644 --- a/test/unit/lib/chili_project/database_test.rb +++ b/test/unit/lib/chili_project/database_test.rb @@ -22,7 +22,7 @@ class ChiliProject::DatabaseTest < ActiveSupport::TestCase ChiliProject::Database.stubs(:adapter_name).returns "SQLite" SQLite3::Driver::Native::API.stubs(:sqlite3_libversion).returns "3.6.12" end - + should "return the correct identifier" do assert_equal :sqlite, ChiliProject::Database.name end @@ -32,19 +32,19 @@ class ChiliProject::DatabaseTest < ActiveSupport::TestCase assert_equal false, ChiliProject::Database.postgresql? assert_equal true, ChiliProject::Database.sqlite? end - + should "return a version string" do assert_equal "3.6.12", ChiliProject::Database.version end - + should "return long version string for raw==true" do ChiliProject::Database.stubs(:adapter_name).returns "PostgreSQL" raw_version = "PostgreSQL 8.3.11 on x86_64-pc-linux-gnu, compiled by GCC gcc-4.3.real (Debian 4.3.2-1.1) 4.3.2" ActiveRecord::Base.connection.stubs(:select_value).returns raw_version - + assert_equal "8.3.11", ChiliProject::Database.version assert_equal raw_version, ChiliProject::Database.version(true) end - + end