Class: TableCopy::PG::Destination

Inherits:
Object
  • Object
show all
Defined in:
lib/table_copy/pg/destination.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Destination

Returns a new instance of Destination.



6
7
8
9
10
11
12
13
14
15
# File 'lib/table_copy/pg/destination.rb', line 6

def initialize(args)
  @table_name        = args[:table_name]
  @primary_key       = args[:primary_key]
  @sequence_field    = args[:sequence_field]
  @conn_method       = args[:conn_method]
  @indexes           = args[:indexes] || []
  @fields            = args[:fields]
  @after_create      = args[:after_create]
  @soft_delete_field = args[:soft_delete_field]
end

Instance Attribute Details

#after_createObject (readonly)

Returns the value of attribute after_create.



4
5
6
# File 'lib/table_copy/pg/destination.rb', line 4

def after_create
  @after_create
end

#conn_methodObject (readonly)

Returns the value of attribute conn_method.



4
5
6
# File 'lib/table_copy/pg/destination.rb', line 4

def conn_method
  @conn_method
end

#fieldsObject (readonly)

Returns the value of attribute fields.



4
5
6
# File 'lib/table_copy/pg/destination.rb', line 4

def fields
  @fields
end

#indexesObject (readonly)

Returns the value of attribute indexes.



4
5
6
# File 'lib/table_copy/pg/destination.rb', line 4

def indexes
  @indexes
end

#sequence_fieldObject (readonly)

Returns the value of attribute sequence_field.



4
5
6
# File 'lib/table_copy/pg/destination.rb', line 4

def sequence_field
  @sequence_field
end

#soft_delete_fieldObject (readonly)

Returns the value of attribute soft_delete_field.



4
5
6
# File 'lib/table_copy/pg/destination.rb', line 4

def soft_delete_field
  @soft_delete_field
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



4
5
6
# File 'lib/table_copy/pg/destination.rb', line 4

def table_name
  @table_name
end

Instance Method Details

#copy_data_from(source_table, temp: nil, pk_only: false, update: false) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/table_copy/pg/destination.rb', line 78

def copy_data_from(source_table, temp: nil, pk_only: false, update: false)
  temp = 'temp_' if temp
  fl = pk_only ? primary_key : fields_list
  where = "where #{sequence_field} > '#{update}'" if update && sequence_field
  count = 0
  source_table.copy_from(fl, where) do |source_conn|
    with_conn do |conn|
      conn.copy_data("COPY #{temp}#{table_name} (#{fl}) FROM STDOUT CSV") do
        while row = source_conn.get_copy_data
          count += 1
          conn.put_copy_data(row)
        end
      end
    end
  end
  count
end

#copy_from_temp(except: except_statement) ⇒ Object



96
97
98
99
100
# File 'lib/table_copy/pg/destination.rb', line 96

def copy_from_temp(except: except_statement)
  with_conn do |conn|
    conn.exec(upsert_sql(except))
  end
end

#create(fields_ddl) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/table_copy/pg/destination.rb', line 30

def create(fields_ddl)
  sd = ", #{soft_delete_field} bool default false" if soft_delete_field
  with_conn do |conn|
    conn.exec("create table #{table_name} (#{fields_ddl}#{sd})")
  end
  after_create.call(table_name) if after_create
end

#create_indexesObject



45
46
47
48
49
50
51
52
# File 'lib/table_copy/pg/destination.rb', line 45

def create_indexes
  indexes.each do |index|
    create_ddl = index.class.new(table_name, index.name, index.columns).create
    with_conn do |conn|
      conn.exec(create_ddl)
    end
  end
end

#create_temp(fields_ddl) ⇒ Object



66
67
68
69
70
# File 'lib/table_copy/pg/destination.rb', line 66

def create_temp(fields_ddl)
  with_conn do |conn|
    conn.exec("create temp table temp_#{table_name} (#{fields_ddl}) on commit drop")
  end
end

#create_views(views) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/table_copy/pg/destination.rb', line 118

def create_views(views)
  with_conn do |conn|
    views.inject({}) do |result, view|
      begin
        conn.exec("create or replace view #{view['viewname']} as (#{view['definition'].gsub(/;\z/, '')})")
        result[view['viewname']] = true
      rescue ::PG::UndefinedTable, ::PG::UndefinedColumn => e
        result[view['viewname']] = false
      end
      result
    end
  end
end

#delete_not_in_tempObject



102
103
104
105
106
107
108
109
110
# File 'lib/table_copy/pg/destination.rb', line 102

def delete_not_in_temp
  with_conn do |conn|
    if soft_delete_field
      conn.exec("update #{table_name} set #{soft_delete_field}=true where #{not_in_temp} and (#{soft_delete_field} is null or #{soft_delete_field} != true)")
    else
      conn.exec("delete from #{table_name} where #{not_in_temp}")
    end
  end
end

#drop(opts = {}) ⇒ Object



38
39
40
41
42
43
# File 'lib/table_copy/pg/destination.rb', line 38

def drop(opts={})
  cascade = ' cascade' if opts[:cascade]
  with_conn do |conn|
    conn.exec("#{drop_sql}#{cascade}")
  end
end

#max_sequenceObject



58
59
60
61
62
63
64
# File 'lib/table_copy/pg/destination.rb', line 58

def max_sequence
  return unless sequence_field
  with_conn do |conn|
    row = conn.exec(max_sequence_sql).first
    row['max'] if row
  end
end

#none?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/table_copy/pg/destination.rb', line 72

def none?
  with_conn do |conn|
    conn.exec("select count(*) from #{table_name}").first['count'] == '0'
  end
end

#query_viewsObject



112
113
114
115
116
# File 'lib/table_copy/pg/destination.rb', line 112

def query_views
  with_conn do |conn|
    conn.exec(views_sql)
  end
end

#to_sObject



54
55
56
# File 'lib/table_copy/pg/destination.rb', line 54

def to_s
  table_name
end

#transactionObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/table_copy/pg/destination.rb', line 17

def transaction
  with_conn do |conn|
    begin
      conn.exec('begin')
      yield
      conn.exec('commit')
    rescue Exception => e
      conn.exec('rollback')
      raise e
    end
  end
end