Class: Sq::Dbsync::StaticTablePlan

Inherits:
Object
  • Object
show all
Defined in:
lib/sq/dbsync/static_table_plan.rb

Overview

Generates a plan given a static “spec” of tables, columns, optional data types, and indexes. This is useful for partial replication of tables, for instance when some columns contain sensitive or large information that is not to be replicated.

Simple Example:

spec = [{
  table_name: :users,
  columns: [:id, :updated_at],
  indexes: {
    index_users_on_updated_at: {columns: [:updated_at]}
  }
}]

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ StaticTablePlan

Returns a new instance of StaticTablePlan.



16
17
18
# File 'lib/sq/dbsync/static_table_plan.rb', line 16

def initialize(spec)
  @spec = format_spec(spec)
end

Instance Method Details

#deep_clone(object) ⇒ Object



26
27
28
# File 'lib/sq/dbsync/static_table_plan.rb', line 26

def deep_clone(object)
  Marshal.load(Marshal.dump(object))
end

#format_spec(spec) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sq/dbsync/static_table_plan.rb', line 30

def format_spec(spec)
  # Support copying from different relations of a Postgres DB, but to the
  # same target database in MySQL.
  spec.map do |table_def|
    unless table_def[:source_table_name]
      table_def[:source_table_name] = table_def[:table_name]
      table_def[:table_name] = table_def[:source_table_name].
        to_s.gsub('__', '_').to_sym
    end
    table_def
  end
end

#tables(source) ⇒ Object



20
21
22
23
24
# File 'lib/sq/dbsync/static_table_plan.rb', line 20

def tables(source)
  deep_clone(@spec).map do |tplan|
    tplan.update(source_db: source)
  end
end