Class: Ec::Pg::SchemaRegistry::Cloner

Inherits:
Struct
  • Object
show all
Includes:
Mixin
Defined in:
lib/ec/pg/schema_registry/cloner.rb

Direct Known Subclasses

SchemaMigrationCloner

Constant Summary collapse

PgDumpBlacklistedStatements =
[
  /SET search_path/i,
  /SET lock_timeout/i,
  /SET row_security/i,
  /SET idle_in_transaction_session_timeout/i,
  /CREATE SCHEMA #{Ec::Pg.configuration.default_schema}/i,
  /COMMENT ON SCHEMA #{Ec::Pg.configuration.default_schema}/i
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Mixin

#configurations, #shell_command, #with_pg_env

Instance Attribute Details

#connectionObject

Returns the value of attribute connection

Returns:

  • (Object)

    the current value of connection



6
7
8
# File 'lib/ec/pg/schema_registry/cloner.rb', line 6

def connection
  @connection
end

#schemaObject

Returns the value of attribute schema

Returns:

  • (Object)

    the current value of schema



6
7
8
# File 'lib/ec/pg/schema_registry/cloner.rb', line 6

def schema
  @schema
end

Instance Method Details

#callObject



18
19
20
21
22
# File 'lib/ec/pg/schema_registry/cloner.rb', line 18

def call
  connection.execute(
    patched_search_path(default_schema_dump)
  )
end

#default_schema_dumpObject



35
36
37
38
39
40
41
42
# File 'lib/ec/pg/schema_registry/cloner.rb', line 35

def default_schema_dump
  # TODO: check exit code of pg_dump
  with_pg_env {
    shell_command(
      %(pg_dump -s -x -O -n #{Ec::Pg.configuration.default_schema} #{configurations[:database]})
    )
  }
end

#matches_blacklist?(input, regexps) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/ec/pg/schema_registry/cloner.rb', line 54

def matches_blacklist?(input, regexps)
  regexps.select {|c| input.match(c)}
end

#patched_search_path(sql) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/ec/pg/schema_registry/cloner.rb', line 24

def patched_search_path(sql)
  search_path = %[SET search_path = "#{schema}", #{Ec::Pg.configuration.default_schema};]

  swap_schema_qualifier(sql)
    .split("\n")
    .reject{|line| line =~ /\\(un)?restrict/}
    .reject{|line| matches_blacklist?(line, PgDumpBlacklistedStatements).present?}
    .prepend(search_path)
    .join("\n")
end

#swap_schema_qualifier(sql) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/ec/pg/schema_registry/cloner.rb', line 44

def swap_schema_qualifier(sql)
  sql.gsub(/#{Ec::Pg.configuration.default_schema}\.\w*/) do |match|
    if Ec::Pg.configuration.pg_excluded_names.any? {|name| match.include? name}
      match
    else
      match.gsub("#{Ec::Pg.configuration.default_schema}.", %("#{schema}".))
    end
  end
end