Class: DatabaseTransform::Schema

Inherits:
Object
  • Object
show all
Extended by:
SchemaModelStore, SchemaTables
Defined in:
lib/database_transform/schema.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SchemaModelStore

inherited

Methods included from SchemaTables

extended, inherited

Class Method Details

.transform_table(source_table, args = {}, &proc) ⇒ Object

Transforms a table from the source database to the new database.

Specify the source table to get entries from; a proc with the transform steps can be specified.

Parameters:

  • args (Hash) (defaults to: {})

    a customizable set of options

Options Hash (args):

  • to (String, Symbol, Class)

    The name of the destination table; if this is not specified, no tables are copied. This can be a class, or a symbol which will be constantised; each column mapping proc will have access to one instance of this table when performing a transformation.

  • depends (Array<String, Symbol>)

    An array of symbols (source tables) which must be transformed before the specified source table can be transformed.

  • default_scope (Proc)

    The default scope of the old table to use when transforming.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
# File 'lib/database_transform/schema.rb', line 15

def self.transform_table(source_table, args = {}, &proc)
  raise ArgumentError.new if source_table.nil?
  raise DatabaseTransform::DuplicateError.new(source_table) if tables.has_key?(source_table)

  source_table, args[:to] = prepare_models(source_table, args[:to])

  transform = DatabaseTransform::SchemaTable.new(source_table, args[:to], args[:default_scope])
  tables[source_table] = { depends: args[:depends] || [], transform: transform }
  transform.instance_eval(&proc) if proc
end

Instance Method Details

#transform!Void

Runs the transform.

Returns:

  • (Void)

Raises:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/database_transform/schema.rb', line 83

def transform!
  # The tables have dependencies; we must run them in order.
  transformed = Set.new
  queue = Set.new(tables.keys)

  # We try to run all the transforms we can until no more can be run.
  # If no more can run and the input queue is empty, we are done.
  # If no more can run and the input queue is not empty, we have a dependency cycle.
  begin
    transformed_this_pass = transform_pass(transformed, queue)
    fail DatabaseTransform::UnsatisfiedDependencyError.new(queue.to_a) if transformed_this_pass.empty? && !queue.empty?

    queue -= transformed_this_pass
    transformed += transformed_this_pass
  end until queue.empty?
end