Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for Postgres search path #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Gemfile.lock
.DS_Store
pkg/*
.rvmrc
.*.swp

72 changes: 47 additions & 25 deletions lib/delayed/serialization/sequel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,62 @@
module Delayed
module Serialization
module Sequel
def self.configure(klass)
klass.class_eval do
if YAML.parser.class.name =~ /syck/i
yaml_as "tag:ruby.yaml.org,2002:Sequel"
end

module ClassMethods
def search_path
search_path_str = self.db["SHOW search_path"].get
search_path_str.split(/[\s,]+/).map{|s| s.gsub(/\A"|"\z/, '')}.map(&:to_sym)
end
end

if YAML.parser.class.name =~ /syck/i
module ClassMethods
def yaml_new(klass, tag, val)
pk = val["values"][klass.primary_key]
klass[pk] ||
raise(Delayed::DeserializationError, "Sequel Record not found, class: #{klass} , primary key: #{pk}")
def use_search_path(search_path, &block)
self.db.synchronize do
previous_search_path = search_path
begin
set_search_path(search_path)
yield
ensure
set_search_path(previous_search_path)
end
end
end
end

private

def set_search_path(search_path)
placeholders = search_path.map{'?'}.join(', ')
placeholders = "''" if placeholders.empty?
self.db["SET search_path TO #{placeholders}", *search_path].get
end

end
module InstanceMethods
if YAML.parser.class.name =~ /syck/i
def to_yaml_properties
["@values"]
end
else
def encode_with(coder)
coder["values"] = @values
end

def init_with(coder)
@values = coder["values"]
def search_path
self.class.search_path
end

def use_search_path(search_path, &block)
self.class.use_search_path(search_path, &block)
end

def postgres?
self.class.db.database_type == :postgres
end

def encode_with(coder)
coder["values"] = @values
coder["search_path"] = search_path if postgres?
end

def init_with(coder)
@values = coder["values"]
if postgres? && coder["search_path"]
use_search_path(coder["search_path"]) { reload }
else
reload
rescue ::Sequel::Error
raise Delayed::DeserializationError, "Sequel Record not found, class: #{self.class.name} , primary key: #{pk}"
end
rescue ::Sequel::Error
raise Delayed::DeserializationError, "Sequel Record not found, class: #{self.class.name} , primary key: #{pk}"
end
end
end
Expand Down
31 changes: 31 additions & 0 deletions spec/delayed/serialization/sequel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,35 @@
YAML.load(Story.create.to_yaml)
end.not_to raise_error
end


it "should use the correct search_path for postgres" do
if DB.database_type == :postgres
Story.all.map &:delete

DB.run "drop schema if exists other_schema CASCADE"
DB.run "create schema other_schema"

# create a Story inside the other schema
yaml = Story.use_search_path([:other_schema]) do
DB.drop_table? :stories
DB.create_table :stories do
primary_key :story_id
String :text
TrueClass :scoped, :default => true
end
Story.create.to_yaml
end

# this should be empty as the search has now been reset
expect(Story.all).to be_empty
Story.use_search_path([:other_schema]) do
expect(Story.all).not_to be_empty
end

expect{YAML.load(yaml)}.not_to raise_error
end
end


end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def jruby?
end

DB.drop_table :delayed_jobs rescue Sequel::DatabaseError
DB.drop_table :another_delayed_jobs rescue Sequel::DatabaseError
DB.drop_table :stories rescue Sequel::DatabaseError

DB.create_table :delayed_jobs do
Expand Down