class SeedingUtils::Table
Attributes
name[RW]
prefix[RW]
quoted_name[RW]
Public Class Methods
new(name, prefix = ' ->')
click to toggle source
# File lib/seeding_utils.rb, line 27 def initialize(name, prefix = ' ->') @conn = SeedingUtils.connection self.name = name self.quoted_name = PG::Connection.quote_ident(name) self.prefix = prefix end
Public Instance Methods
dump(io)
click to toggle source
Expects an open IO object for writing.
# File lib/seeding_utils.rb, line 56 def dump(io) @conn.exec %{COPY #{quoted_name} TO STDOUT WITH (FORMAT 'binary')} while row = @conn.get_copy_data do io.write(row) end io.rewind return io end
load(io)
click to toggle source
Expects an open IO object for reading.
# File lib/seeding_utils.rb, line 66 def load(io) @conn.exec %{DELETE FROM #{quoted_name}} @conn.exec %{COPY #{quoted_name} FROM STDIN WITH (FORMAT 'binary')} while data = io.read(256) @conn.put_copy_data(data) end @conn.put_copy_end end
seed(cache_file = nil, &block)
click to toggle source
# File lib/seeding_utils.rb, line 34 def seed(cache_file = nil, &block) path = Pathname(cache_file.to_s) return seed_uncached(&block) if cache_file.nil? if path.file? puts %{#{prefix} loading #{name} seeds from cache} @conn.exec %{DELETE FROM #{quoted_name}} load path.open 'r:binary' elsif block_given? seed_uncached(&block) path.open 'w:binary' do |io| dump(io) end end end
seed_uncached() { || ... }
click to toggle source
# File lib/seeding_utils.rb, line 49 def seed_uncached(&block) puts %{#{prefix} loading #{name} seeds without cache} yield end