Class: SynchronizePostgresStoredProcedures::StoredProcedures::Pg

Inherits:
Object
  • Object
show all
Defined in:
lib/synchronize_postgres_stored_procedures/stored_procedures/pg.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection = ActiveRecord::Base.connection) ⇒ Pg

Returns a new instance of Pg.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/synchronize_postgres_stored_procedures/stored_procedures/pg.rb', line 7

def initialize(connection = ActiveRecord::Base.connection)
  @connection = connection

  major,minor,point = get_version

  if (major < 9 || major > 16)
    raise "Unsupported postgres version #{get_version}"
  end

  @synchronizer = (
    case major
    when 12,13,14,15,16 then Pg12
    when 11 then Pg11
    else Pg9
    end
  ).new(@connection)

  # We want to write logs to the rails default logger and stdout
  loggers = [Logger.new(STDOUT)]
  loggers.append(Rails.logger) if Rails.logger
  self.logger = ActiveSupport::BroadcastLogger.new(*loggers)
end

Instance Method Details

#synchronize(directory = "db/sp") ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/synchronize_postgres_stored_procedures/stored_procedures/pg.rb', line 30

def synchronize(directory = "db/sp")
  logger.info("Synchronizing stored procedures")

  dir = Dir.new(directory)
  stored_procedures = dir.entries.select {|f| f =~ /\.sql$/}

  @synchronizer.synchronize_all(stored_procedures.sort.map {|filename|
    [File.open(File.join(dir, filename)) { |f| f.read }, File.basename(filename, ".sql")]
  })
end