Module: Octopus::Migrator

Defined in:
lib/octopus/migration.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/octopus/migration.rb', line 64

def self.included(base)
  unless Octopus.atleast_rails52?
    base.extend(ClassMethods)

    base.class_eval do
      class << self
          alias_method :migrate_without_octopus, :migrate
          alias_method :migrate, :migrate_with_octopus
          
          alias_method :up_without_octopus, :up
          alias_method :up, :up_with_octopus

          alias_method :down_without_octopus, :down
          alias_method :down, :down_with_octopus

          alias_method :run_without_octopus, :run
          alias_method :run, :run_with_octopus

          alias_method :rollback_without_octopus, :rollback
          alias_method :rollback, :rollback_with_octopus
      end
    end
  end

  base.send :alias_method, :run_without_octopus, :run
  base.send :alias_method, :run, :run_with_octopus

  base.send :alias_method, :migrate_without_octopus, :migrate
  base.send :alias_method, :migrate, :migrate_with_octopus

  base.send :alias_method, :migrations_without_octopus, :migrations
  base.send :alias_method, :migrations, :migrations_with_octopus
end

Instance Method Details

#migrate_with_octopus(&block) ⇒ Object


110
111
112
113
114
115
116
117
118
# File 'lib/octopus/migration.rb', line 110

def migrate_with_octopus(&block)
  return migrate_without_octopus(&block) unless connection.is_a?(Octopus::Proxy)
  shards = migrations.map(&:shards).flatten.map(&:to_s)
  connection.send_queries_to_multiple_shards(shards) do
    migrate_without_octopus(&block)
  end
  rescue ActiveRecord::UnknownMigrationVersionError => e
    raise unless migrations(true).detect { |m| m.version == e.version }
end

#migrations_with_octopus(shard_agnostic = false) ⇒ Object


120
121
122
123
124
125
# File 'lib/octopus/migration.rb', line 120

def migrations_with_octopus(shard_agnostic = true)
  migrations = migrations_without_octopus
  return migrations if !connection.is_a?(Octopus::Proxy) || shard_agnostic

  migrations.select { |m| m.shards.include?(connection.current_shard.to_sym) }
end

#run_with_octopus(&block) ⇒ Object

Pre RAILS 5.2 Migration methods


100
101
102
103
104
105
106
107
108
# File 'lib/octopus/migration.rb', line 100

def run_with_octopus(&block)
  return run_without_octopus(&block) unless connection.is_a?(Octopus::Proxy)
  shards = migrations.map(&:shards).flatten.map(&:to_s)
  connection.send_queries_to_multiple_shards(shards) do
    run_without_octopus(&block)
  end
  rescue ActiveRecord::UnknownMigrationVersionError => e
    raise unless migrations(true).detect { |m| m.version == e.version }
end