1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Added PostgreSQL to build

This commit is contained in:
Bryan Helmkamp 2009-05-17 17:53:40 -04:00
parent 7fc820501c
commit d2988420fc
9 changed files with 134 additions and 15 deletions

View file

@ -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 file

@ -28,7 +28,11 @@ module Arel
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 @@ module Arel
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 @@ module Arel
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 file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -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 file

@ -13,15 +13,27 @@ end
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