Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #184 from ajuckel/master

A few PostgreSQL updates.
  • Loading branch information...
commit 6f495b0e8073fe0da9ceed5a931aabb9132f57b4 2 parents ca1e37a + c795f56
Anthony Juckel ajuckel authored
3  README.rdoc
View
@@ -167,7 +167,8 @@ alse need to grant "blog" create privileges on
If you also have PostgreSQL available, those tests will be run if the
`psql' executable can be found. Also ensure you have a database named
<tt>weblog_development</tt> and a user named "blog" and an empty
-password.
+password. You can control the host and port the tests will attempt to
+by setting the environment variables PGHOST and PGPORT.
If you want rails logging enabled during these test runs you can edit
test/jdbc_common.rb and add the following line:
6 lib/arjdbc/postgresql/adapter.rb
View
@@ -455,7 +455,11 @@ def indexes(table_name, name = nil)
# take id from result of insert query
def last_inserted_id(result)
- Hash[Array(*result)].fetch("id") { result }
+ if result.is_a? Fixnum
+ result
+ else
+ result.first.first[1]
+ end
end
def last_insert_id(table, sequence_name)
9 test/db/postgres.rb
View
@@ -6,4 +6,13 @@
:password => 'arjdbc'
}
+POSTGRES_CONFIG[:host] = ENV['PGHOST'] if ENV['PGHOST']
+POSTGRES_CONFIG[:port] = ENV['PGPORT'] if ENV['PGPORT']
ActiveRecord::Base.establish_connection(POSTGRES_CONFIG)
+
+begin
+ result = ActiveRecord::Base.connection.execute("SHOW server_version_num")
+ PG_VERSION = result.first.first[1].to_i
+rescue
+ PG_VERSION = 0
+end
9 test/helper.rb
View
@@ -3,9 +3,16 @@ def find_executable?(name)
ENV['PATH'].split(File::PATH_SEPARATOR).detect {|p| File.executable?(File.join(p, name))}
end
+ def pg_cmdline_params
+ params = ""
+ params += "-h #{ENV['PGHOST']} " if ENV['PGHOST']
+ params += "-p #{ENV['PGPORT']} " if ENV['PGPORT']
+ params
+ end
+
def have_postgres?
if find_executable?("psql")
- if `psql -c '\\l' -U postgres 2>&1` && $?.exitstatus == 0
+ if `psql -c '\\l' -U postgres #{pg_cmdline_params}2>&1` && $?.exitstatus == 0
true
else
warn "No \"postgres\" role? You might need to execute `createuser postgres -drs' first."
1  test/jdbc_common.rb
View
@@ -12,6 +12,7 @@ def jruby?
require 'models/validates_uniqueness_of_string'
require 'models/string_id'
require 'models/thing'
+require 'models/custom_pk_name'
require 'simple'
require 'has_many_through'
require 'helper'
14 test/models/custom_pk_name.rb
View
@@ -0,0 +1,14 @@
+class CreateCustomPkName < ActiveRecord::Migration
+ def self.up
+ create_table :custom_pk_names, :force => true, :id => false do |t|
+ t.primary_key :custom_id
+ t.string :name
+ end
+ end
+ def self.down
+ drop_table :custom_pk_names
+ end
+end
+class CustomPkName < ActiveRecord::Base
+ self.primary_key = "custom_id"
+end
30 test/postgres_native_type_mapping_test.rb
View
@@ -5,20 +5,24 @@ class CreateNativeTypeMappingTestSchema < ActiveRecord::Migration
def self.up
execute "DROP SEQUENCE IF EXISTS seq_pk_customers"
execute "CREATE SEQUENCE seq_pk_customers"
- execute %q{
+ columns = [
+ "bigint_serial_should_be_integer bigint default nextval('seq_pk_customers')",
+ "integer_serial_should_be_integer integer default nextval('seq_pk_customers')",
+ "varchar_should_be_string varchar(2)",
+ "timestamp_should_be_datetime timestamp",
+ "bytea_should_be_binary bytea",
+ "double_precision_should_be_float double precision",
+ "real_should_be_float real",
+ "bool_should_be_boolean bool",
+ "interval_should_be_string interval",
+ "bigint_should_be_integer bigint"
+ ]
+ columns << "uuid_should_be_string uuid" if PG_VERSION >= 80300
+ table_sql = %Q{
CREATE TABLE customers (
- bigint_serial_should_be_integer bigint default nextval('seq_pk_customers'),
- integer_serial_should_be_integer integer default nextval('seq_pk_customers'),
- varchar_should_be_string varchar(2),
- timestamp_should_be_datetime timestamp,
- bytea_should_be_binary bytea,
- double_precision_should_be_float double precision,
- real_should_be_float real,
- bool_should_be_boolean bool,
- interval_should_be_string interval,
- bigint_should_be_integer bigint,
- uuid_should_be_string uuid
+ #{columns.join(",\n")}
)}
+ execute table_sql
end
def self.down
@@ -44,7 +48,7 @@ def column_type(column_name)
end
def test_uuid_column_should_map_to_string
- assert_equal :string, column_type("uuid_should_be_string")
+ assert_equal :string, column_type("uuid_should_be_string") if PG_VERSION >= 80300
end
def test_bigint_serial_should_be_mapped_to_integer
1  test/postgres_simple_test.rb
View
@@ -29,6 +29,7 @@ def test_multi_statement_support
end
def test_create_xml_column
+ return unless PG_VERSION >= 80300
assert_nothing_raised do
@connection.create_table :xml_testings do |t|
t.column :xml_test, :xml
14 test/simple.rb
View
@@ -14,6 +14,7 @@ def setup
CreateAutoIds.up
CreateValidatesUniquenessOf.up
CreateThings.up
+ CreateCustomPkName.up
@connection = ActiveRecord::Base.connection
end
@@ -25,6 +26,7 @@ def teardown
CreateAutoIds.down
CreateValidatesUniquenessOf.down
CreateThings.down
+ CreateCustomPkName.down
ActiveRecord::Base.clear_active_connections!
end
end
@@ -131,6 +133,18 @@ def test_insert_returns_id
assert !value.nil?
entry = Entry.find_by_title('insert_title')
assert_equal entry.id, value
+
+ # Ensure we get the id even if the PK column is not named 'id'
+ 1.upto(4) do |i|
+ cpn_name = "return id test#{i}"
+ cpn = CustomPkName.new
+ cpn.name = cpn_name
+ cpn.save
+ value = cpn.custom_id
+ assert !value.nil?
+ cpn = CustomPkName.find_by_name(cpn_name)
+ assert_equal cpn.custom_id, value
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.