Skip to content

Commit

Permalink
verify the database encoding is utf8 or unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardochimal committed Aug 21, 2008
1 parent ecfe469 commit 1917392
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/yaml_db.rb
Expand Up @@ -5,12 +5,16 @@

module YamlDb
def self.dump(filename)
verify_utf8

disable_logger
YamlDb::Dump.dump(File.new(filename, "w"))
reenable_logger
end

def self.load(filename)
verify_utf8

disable_logger
YamlDb::Load.load(File.new(filename, "r"))
reenable_logger
Expand All @@ -24,6 +28,22 @@ def self.disable_logger
def self.reenable_logger
ActiveRecord::Base.logger = @@old_logger
end

class EncodingException < RuntimeError; end

def self.verify_utf8
raise "RAILS_ENV is not defined" unless defined?(RAILS_ENV)

unless ActiveRecord::Base.configurations[RAILS_ENV].has_key?('encoding')
raise EncodingException, "Your database.yml configuration needs to specify encoding"
end

unless ['unicode', 'utf8'].include?(ActiveRecord::Base.configurations[RAILS_ENV]['encoding'])
raise EncodingException, "Your database encoding must be utf8 (mysql) or unicode (postgres)"
end

true
end
end


Expand Down
1 change: 1 addition & 0 deletions spec/yaml_dump_spec.rb
Expand Up @@ -11,6 +11,7 @@
ActiveRecord::Base.connection.stub!(:select_one).and_return({"count"=>"2"})
ActiveRecord::Base.connection.stub!(:select_all).and_return([ { 'a' => 1, 'b' => 2 }, { 'a' => 3, 'b' => 4 } ])
YamlDb::Utils.stub!(:quote_table).with('mytable').and_return('mytable')
YamlDb.stub!(:verify_utf8)
end

before(:each) do
Expand Down
1 change: 1 addition & 0 deletions spec/yaml_load_spec.rb
Expand Up @@ -3,6 +3,7 @@
describe YamlDb::Load do
before do
YamlDb::Utils.stub!(:quote_table).with('mytable').and_return('mytable')
YamlDb.stub!(:verify_utf8)

ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true)
ActiveRecord::Base.connection = mock('connection')
Expand Down
27 changes: 27 additions & 0 deletions spec/yaml_spec.rb
@@ -0,0 +1,27 @@
require File.dirname(__FILE__) + '/base'

describe YamlDb do
before do
ActiveRecord::Base = mock('ActiveRecord::Base', :null_object => true)
ActiveRecord::Base.stub!(:configurations).and_return(mock('configurations'))
RAILS_ENV = "test" unless defined?(RAILS_ENV)
end

it "verifies that the connection is encoded with unicode or utf8" do
@config = { 'encoding' => 'utf8' }
ActiveRecord::Base.configurations.stub!(:[]).with('test').and_return(@config)
lambda { YamlDb.verify_utf8 }.should_not raise_error(YamlDb::EncodingException)
end

it "raises an exception if encoding is not set" do
@config = { }
ActiveRecord::Base.configurations.stub!(:[]).with('test').and_return(@config)
lambda { YamlDb.verify_utf8 }.should raise_error(YamlDb::EncodingException)
end

it "raises an exception if encoding is not utf8 or unicode" do
@config = { 'encoding' => 'latin1' }
ActiveRecord::Base.configurations.stub!(:[]).with('test').and_return(@config)
lambda { YamlDb.verify_utf8 }.should raise_error(YamlDb::EncodingException)
end
end

0 comments on commit 1917392

Please sign in to comment.