From 925cee582d1e047c3e1f4be9bd1e49009dbf0ad2 Mon Sep 17 00:00:00 2001 From: Sebastian A. Espindola Date: Tue, 8 Jul 2008 01:14:11 -0300 Subject: [PATCH] Added db:charset support to PostgreSQL --- .../connection_adapters/postgresql_adapter.rb | 13 +++++++++++++ activerecord/test/cases/adapter_test.rb | 6 ++++++ cleanlogs.sh | 1 - railties/lib/tasks/databases.rake | 3 +++ 4 files changed, 22 insertions(+), 1 deletions(-) delete mode 100755 cleanlogs.sh diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 2e2d50c..e0ad933 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -622,6 +622,19 @@ module ActiveRecord PostgreSQLColumn.new(name, default, type, notnull == 'f') end end + + # Returns the current database name. + def current_database + query('select current_database()')[0][0] + end + + # Returns the current database encoding format. + def encoding + query(<<-end_sql)[0][0] + SELECT pg_encoding_to_char(pg_database.encoding) FROM pg_database + WHERE pg_database.datname LIKE '#{current_database}' + end_sql + end # Sets the schema search path to a string of comma-separated schema names. # Names beginning with $ have to be quoted (e.g. $user => '$user'). diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index 11f9870..227d6b4 100644 --- a/activerecord/test/cases/adapter_test.rb +++ b/activerecord/test/cases/adapter_test.rb @@ -65,6 +65,12 @@ class AdapterTest < ActiveRecord::TestCase end end + if current_adapter?(:PostgreSQLAdapter) + def test_encoding + assert_not_nil @connection.encoding + end + end + def test_table_alias def @connection.test_table_alias_length() 10; end class << @connection diff --git a/cleanlogs.sh b/cleanlogs.sh deleted file mode 100755 index a4e6baf..0000000 --- a/cleanlogs.sh +++ /dev/null @@ -1 +0,0 @@ -rm activerecord/debug.log activerecord/test/debug.log actionpack/debug.log activeresource/test/debug.log diff --git a/railties/lib/tasks/databases.rake b/railties/lib/tasks/databases.rake index 75fba8b..8f561f4 100644 --- a/railties/lib/tasks/databases.rake +++ b/railties/lib/tasks/databases.rake @@ -141,6 +141,9 @@ namespace :db do when 'mysql' ActiveRecord::Base.establish_connection(config) puts ActiveRecord::Base.connection.charset + when 'postgresql' + ActiveRecord::Base.establish_connection(config) + puts ActiveRecord::Base.connection.encoding else puts 'sorry, your database adapter is not supported yet, feel free to submit a patch' end -- 1.5.3.7