Browse files

Added PostgreSQL to build

  • Loading branch information...
1 parent 7fc8205 commit d2988420fc6dd91ca751d96ed648fd1ed52ce342 @brynary brynary committed May 17, 2009
View
4 Rakefile
@@ -17,7 +17,7 @@ Spec::Rake::SpecTask.new(:coverage) do |t|
end
namespace :spec do
- for adapter in %w[mysql sqlite3]
+ for adapter in %w[mysql sqlite3 postgresql]
desc "Run specs with the #{adapter} database adapter"
Spec::Rake::SpecTask.new(adapter) do |t|
t.spec_files =
@@ -29,7 +29,7 @@ namespace :spec do
end
desc "Run specs with mysql and sqlite3 database adapters (default)"
-task :spec => ["spec:sqlite3", "spec:mysql"]
+task :spec => ["spec:sqlite3", "spec:mysql", "spec:postgresql"]
desc "Default task is to run specs"
task :default => :spec
View
18 spec/arel/engines/sql/unit/predicates/binary_spec.rb
@@ -28,7 +28,11 @@ def predicate_sql
sql.should be_like(%Q{(`users`.`id` <=> 1 OR `users`.`name` <=> 'name')})
end
- adapter_is_not :mysql do
+ adapter_is :postgresql do
+ sql.should be_like(%Q{("users"."id" <=> 1 OR "users"."name" <=> E'name')})
+ end
+
+ adapter_is :sqlite3 do
sql.should be_like(%Q{("users"."id" <=> 1 OR "users"."name" <=> 'name')})
end
end
@@ -44,9 +48,13 @@ def predicate_sql
sql.should be_like(%Q{(`users`.`id` <=> 1 AND `users`.`name` <=> 'name')})
end
- adapter_is_not :mysql do
+ adapter_is :sqlite3 do
sql.should be_like(%Q{("users"."id" <=> 1 AND "users"."name" <=> 'name')})
end
+
+ adapter_is :postgresql do
+ sql.should be_like(%Q{("users"."id" <=> 1 AND "users"."name" <=> E'name')})
+ end
end
end
end
@@ -94,9 +102,13 @@ def predicate_sql
sql.should be_like(%Q{`users`.`name` <=> '1-asdf'})
end
- adapter_is_not :mysql do
+ adapter_is :sqlite3 do
sql.should be_like(%Q{"users"."name" <=> '1-asdf'})
end
+
+ adapter_is :postgresql do
+ sql.should be_like(%Q{"users"."name" <=> E'1-asdf'})
+ end
end
end
end
View
9 spec/arel/engines/sql/unit/primitives/value_spec.rb
@@ -9,7 +9,14 @@ module Arel
describe '#to_sql' do
it "appropriately quotes the value" do
Value.new(1, @relation).to_sql.should be_like('1')
- Value.new('asdf', @relation).to_sql.should be_like("'asdf'")
+
+ adapter_is_not :postgresql do
+ Value.new('asdf', @relation).to_sql.should be_like("'asdf'")
+ end
+
+ adapter_is :postgresql do
+ Value.new('asdf', @relation).to_sql.should be_like("E'asdf'")
+ end
end
end
View
20 spec/arel/engines/sql/unit/relations/insert_spec.rb
@@ -30,13 +30,21 @@ module Arel
})
end
- adapter_is_not :mysql do
+ adapter_is :sqlite3 do
@insertion.to_sql.should be_like(%Q{
INSERT
INTO "users"
("id", "name") VALUES (1, 'nick')
})
end
+
+ adapter_is :postgresql do
+ @insertion.to_sql.should be_like(%Q{
+ INSERT
+ INTO "users"
+ ("id", "name") VALUES (1, E'nick')
+ })
+ end
end
describe 'when given values whose types correspond to the types of the attributes' do
@@ -53,13 +61,21 @@ module Arel
})
end
- adapter_is_not :mysql do
+ adapter_is :sqlite3 do
@insertion.to_sql.should be_like(%Q{
INSERT
INTO "users"
("name") VALUES ('nick')
})
end
+
+ adapter_is :postgresql do
+ @insertion.to_sql.should be_like(%Q{
+ INSERT
+ INTO "users"
+ ("name") VALUES (E'nick')
+ })
+ end
end
end
View
38 spec/arel/engines/sql/unit/relations/update_spec.rb
@@ -17,12 +17,19 @@ module Arel
})
end
- adapter_is_not :mysql do
+ adapter_is :sqlite3 do
sql.should be_like(%Q{
UPDATE "users"
SET "id" = 1, "name" = 'nick'
})
end
+
+ adapter_is :postgresql do
+ sql.should be_like(%Q{
+ UPDATE "users"
+ SET "id" = 1, "name" = E'nick'
+ })
+ end
end
it "manufactures sql updating attributes when given a ranged relation" do
@@ -36,13 +43,21 @@ module Arel
})
end
- adapter_is_not :mysql do
+ adapter_is :sqlite3 do
sql.should be_like(%Q{
UPDATE "users"
SET "name" = 'nick'
LIMIT 1
})
end
+
+ adapter_is :postgresql do
+ sql.should be_like(%Q{
+ UPDATE "users"
+ SET "name" = E'nick'
+ LIMIT 1
+ })
+ end
end
describe 'when given values whose types correspond to the types of the attributes' do
@@ -58,12 +73,19 @@ module Arel
})
end
- adapter_is_not :mysql do
+ adapter_is :sqlite3 do
@update.to_sql.should be_like(%Q{
UPDATE "users"
SET "name" = 'nick'
})
end
+
+ adapter_is :postgresql do
+ @update.to_sql.should be_like(%Q{
+ UPDATE "users"
+ SET "name" = E'nick'
+ })
+ end
end
end
@@ -106,13 +128,21 @@ module Arel
})
end
- adapter_is_not :mysql do
+ adapter_is :sqlite3 do
@update.to_sql.should be_like(%Q{
UPDATE "users"
SET "name" = 'nick'
WHERE "users"."id" = 1
})
end
+
+ adapter_is :postgresql do
+ @update.to_sql.should be_like(%Q{
+ UPDATE "users"
+ SET "name" = E'nick'
+ WHERE "users"."id" = 1
+ })
+ end
end
end
end
View
16 spec/arel/unit/predicates/predicates_spec.rb
@@ -21,11 +21,17 @@ module Arel
})
end
- adapter_is_not :mysql do
+ adapter_is :sqlite3 do
sql.should be_like(%Q{
("users"."id" = 1 AND "users"."name" = 'name')
})
end
+
+ adapter_is :postgresql do
+ sql.should be_like(%Q{
+ ("users"."id" = 1 AND "users"."name" = E'name')
+ })
+ end
end
end
end
@@ -41,11 +47,17 @@ module Arel
})
end
- adapter_is_not :mysql do
+ adapter_is :sqlite3 do
sql.should be_like(%Q{
("users"."id" = 1 OR "users"."name" = 'name')
})
end
+
+ adapter_is :postgresql do
+ sql.should be_like(%Q{
+ ("users"."id" = 1 OR "users"."name" = E'name')
+ })
+ end
end
end
end
View
12 spec/connections/postgresql_connection.rb
@@ -0,0 +1,12 @@
+require "activerecord"
+puts "Using native PostgreSQL"
+
+ActiveRecord::Base.configurations = {
+ 'unit' => {
+ :adapter => 'postgresql',
+ :encoding => 'utf8',
+ :database => 'arel_unit',
+ }
+}
+
+ActiveRecord::Base.establish_connection 'unit'
View
18 spec/schemas/postgresql_schema.rb
@@ -0,0 +1,18 @@
+sql = <<-SQL
+ DROP TABLE IF EXISTS users;
+ CREATE TABLE users (
+ id SERIAL PRIMARY KEY NOT NULL,
+ name VARCHAR(255) NOT NULL
+ );
+
+ DROP TABLE IF EXISTS photos;
+ CREATE TABLE photos (
+ id SERIAL PRIMARY KEY NOT NULL,
+ user_id INTEGER NOT NULL,
+ camera_id INTEGER NOT NULL
+ );
+SQL
+
+sql.split(/;/).select(&:present?).each do |sql_statement|
+ ActiveRecord::Base.connection.execute sql_statement
+end
View
14 spec/spec_helper.rb
@@ -13,15 +13,27 @@
module AdapterGuards
def adapter_is(name)
+ verify_adapter_name(name)
yield if name.to_s == adapter_name
end
def adapter_is_not(name)
+ verify_adapter_name(name)
yield if name.to_s != adapter_name
end
def adapter_name
- Arel::Table.engine.connection.class.name.underscore.split("/").last.gsub(/_adapter/, '')
+ name = ActiveRecord::Base.configurations["unit"][:adapter]
+ verify_adapter_name(name)
+ name
+ end
+
+ def verify_adapter_name(name)
+ raise "Invalid adapter name: #{name}" unless valid_adapters.include?(name.to_s)
+ end
+
+ def valid_adapters
+ %w[mysql postgresql sqlite3]
end
end

0 comments on commit d298842

Please sign in to comment.