Class: Cequel::Schema::TableSynchronizer

Inherits:
Object
  • Object
show all
Defined in:
lib/cequel/schema/table_synchronizer.rb

Overview

Synchronize a table schema in the database with a desired table schema

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(updater, existing, updated) ⇒ void

Parameters:

  • updater (TableUpdater)

    table updater to hold schema modifications

  • existing (Table)

    table schema as it is currently defined

  • updated (Table)

    table schema as it is desired

Since:

  • 1.0.0



46
47
48
# File 'lib/cequel/schema/table_synchronizer.rb', line 46

def initialize(updater, existing, updated)
  @updater, @existing, @updated = updater, existing, updated
end

Instance Attribute Details

#existingTable (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns table as it is currently defined.

Returns:

  • (Table)

    table as it is currently defined

Since:

  • 1.0.0



12
13
14
# File 'lib/cequel/schema/table_synchronizer.rb', line 12

def existing
  @existing
end

#updatedTable (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns table schema as it is desired.

Returns:

  • (Table)

    table schema as it is desired

Since:

  • 1.0.0



15
16
17
# File 'lib/cequel/schema/table_synchronizer.rb', line 15

def updated
  @updated
end

Class Method Details

.apply(keyspace, existing, updated) ⇒ void

This method returns an undefined value.

Takes an existing table schema read from the database, and a desired schema for that table. Modifies the table schema in the database to match the desired schema, or creates the table as specified if it does not yet exist

Parameters:

  • keyspace (Metal::Keyspace)

    keyspace that contains table

  • existing (Table)

    table schema as it is currently defined

  • updated (Table)

    table schema as it is desired

Raises:

Since:

  • 1.0.0



28
29
30
31
32
33
34
35
36
# File 'lib/cequel/schema/table_synchronizer.rb', line 28

def self.apply(keyspace, existing, updated)
  if existing
    TableUpdater.apply(keyspace, existing.name) do |updater|
      new(updater, existing, updated).apply
    end
  else
    TableWriter.apply(keyspace, updated)
  end
end

Instance Method Details

#applyvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Apply the changes needed to synchronize the schema in the database with the desired schema

Raises:

Since:

  • 1.0.0



60
61
62
63
64
65
# File 'lib/cequel/schema/table_synchronizer.rb', line 60

def apply
  validate!
  update_keys
  update_columns
  update_properties
end

#each_clustering_column_pair {|old_clustering_column, new_clustering_column| ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Iterate over pairs of (old_clustering_column, new_clustering_column)

Yield Parameters:

  • old_clustering_column (Column)

    key in existing schema

  • new_clustering_column (Column)

    corresponding key in updated schema

Since:

  • 1.0.0



112
113
114
# File 'lib/cequel/schema/table_synchronizer.rb', line 112

def each_clustering_column_pair(&block)
  existing.clustering_columns.zip(updated.clustering_columns, &block)
end

#each_data_column_pair {|old_column, new_column| ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Iterate over pairs of (old_column, new_column)

Yield Parameters:

  • old_column (Column)

    column in existing schema

  • new_column (Column)

    corresponding column in updated schema

Since:

  • 1.0.0



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/cequel/schema/table_synchronizer.rb', line 89

def each_data_column_pair(&block)
  if existing.compact_storage? && existing.clustering_columns.any?
    yield existing.data_columns.first, updated.data_columns.first
  else
    old_columns = existing.data_columns.index_by { |col| col.name }
    new_columns = updated.data_columns.index_by { |col| col.name }
    all_column_names = (old_columns.keys + new_columns.keys).tap(&:uniq!)
    all_column_names.each do |name|
      yield old_columns[name], new_columns[name]
    end
  end
end

#each_key_pair {|old_key, new_key| ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Iterate over pairs of (old_key, new_key)

Yield Parameters:

  • old_key (Column)

    key in existing schema

  • new_key (Column)

    corresponding key in updated schema

Since:

  • 1.0.0



76
77
78
# File 'lib/cequel/schema/table_synchronizer.rb', line 76

def each_key_pair(&block)
  existing.key_columns.zip(updated.key_columns, &block)
end