Class: Pakyow::Data::Adapters::Sql::Migrator Private

Inherits:
Sources::Relational::Migrator show all
Defined in:
lib/pakyow/data/adapters/sql/migrator.rb,
lib/pakyow/data/adapters/sql/migrator/adapter_methods.rb

Overview

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

API:

  • private

Defined Under Namespace

Modules: AdapterMethods

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Sources::Relational::Migrator

#auto_migrate!, #finalize!

Constructor Details

#initializeMigrator

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 a new instance of Migrator.

API:

  • private



14
15
16
17
18
19
20
# File 'lib/pakyow/data/adapters/sql/migrator.rb', line 14

def initialize(*)
  super

  extend self.class.adapter_methods_for_adapter(
    @connection.opts[:adapter]
  )
end

Class Method Details

.adapter_methods_for_adapter(adapter) ⇒ Object

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.

API:

  • private



159
160
161
162
163
164
165
166
167
168
# File 'lib/pakyow/data/adapters/sql/migrator.rb', line 159

def adapter_methods_for_adapter(adapter)
  case adapter
  when "mysql", "mysql2"
    AdapterMethods::Mysql
  when "postgres"
    AdapterMethods::Postgres
  when "sqlite"
    AdapterMethods::Sqlite
  end
end

.globalize_connection_opts!(connection_opts) ⇒ Object

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.

API:

  • private



170
171
172
173
174
175
176
# File 'lib/pakyow/data/adapters/sql/migrator.rb', line 170

def globalize_connection_opts!(connection_opts)
  adapter_methods_for_adapter(
    connection_opts[:adapter]
  ).globalize_connection_opts!(
    connection_opts
  )
end

Instance Method Details

#change_source!(source, attributes) ⇒ Object

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.

API:

  • private



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/pakyow/data/adapters/sql/migrator.rb', line 66

def change_source!(source, attributes)
  differ = differ(source, attributes)

  if differ.changes?
    local_context = self

    alter_table differ.table_name do
      differ.attributes_to_add.each do |attribute_name, attribute|
        local_context.send(:add_column_for_attribute, attribute_name, attribute, self, source, method_prefix: "add_")
      end

      differ.column_types_to_change.each do |column_name, _column_type|
        local_context.send(:change_column_type_for_attribute, column_name, differ.attributes[column_name], self)
      end

      # TODO: revisit when we're ready to tackle foreign key removal
      #
      # differ.columns_to_remove.keys.each do |column_name|
      #   local_context.send(:remove_column_by_name, column_name, self)
      # end
    end
  end
end

#change_source?(source, attributes = source.attributes) ⇒ Boolean

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:

API:

  • private



30
31
32
# File 'lib/pakyow/data/adapters/sql/migrator.rb', line 30

def change_source?(source, attributes = source.attributes)
  create_source?(source) || differ(source, attributes).changes?
end

#create_source!(source, attributes) ⇒ Object

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.

API:

  • private



34
35
36
37
38
39
40
41
42
# File 'lib/pakyow/data/adapters/sql/migrator.rb', line 34

def create_source!(source, attributes)
  local_context = self
  differ = differ(source, attributes)
  create_table differ.table_name do
    differ.attributes.each do |attribute_name, attribute|
      local_context.send(:add_column_for_attribute, attribute_name, attribute, self, source)
    end
  end
end

#create_source?(source) ⇒ Boolean

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:

API:

  • private



26
27
28
# File 'lib/pakyow/data/adapters/sql/migrator.rb', line 26

def create_source?(source)
  !differ(source).exists?
end

#disconnect!Object

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.

API:

  • private



22
23
24
# File 'lib/pakyow/data/adapters/sql/migrator.rb', line 22

def disconnect!
  @connection.disconnect
end

#reassociate_source!(source, foreign_keys) ⇒ Object

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.

API:

  • private



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pakyow/data/adapters/sql/migrator.rb', line 44

def reassociate_source!(source, foreign_keys)
  foreign_keys.each do |foreign_key_name, foreign_key|
    differ = differ(source, foreign_key_name => foreign_key)

    if create_source?(source) || differ.changes?
      local_context = self

      associate_table differ.table_name, with: foreign_key.meta[:foreign_key] do
        attributes_to_add = if local_context.send(:create_source?, source)
          differ.attributes
        else
          differ.attributes_to_add
        end

        attributes_to_add.each do |attribute_name, attribute|
          local_context.send(:add_column_for_attribute, attribute_name, attribute, self, source, method_prefix: "add_")
        end
      end
    end
  end
end