Class: Alterity

Inherits:
Object
  • Object
show all
Defined in:
lib/alterity.rb,
lib/alterity/railtie.rb,
lib/alterity/version.rb,
lib/alterity/configuration.rb,
lib/alterity/mysql_client_additions.rb

Defined Under Namespace

Modules: MysqlClientAdditions Classes: Configuration, CurrentState, Railtie

Constant Summary collapse

VERSION =
"1.4.2"

Class Method Summary collapse

Class Method Details

.after_running_migrationsObject



46
47
48
# File 'lib/alterity.rb', line 46

def after_running_migrations
  state.migrating = false
end

.before_running_migrationsObject

hooks



39
40
41
42
43
44
# File 'lib/alterity.rb', line 39

def before_running_migrations
  require "open3"
  state.migrating = true
  set_database_config
  prepare_replicas_dsns_table
end

.configure {|config| ... } ⇒ Object

Yields:

  • (config)


38
39
40
# File 'lib/alterity/configuration.rb', line 38

def configure
  yield config
end

.disableObject



42
43
44
45
46
47
# File 'lib/alterity/configuration.rb', line 42

def disable
  state.disabled = true
  yield
ensure
  state.disabled = false
end

.process_sql_query(sql, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/alterity.rb', line 10

def process_sql_query(sql, &block)
  return block.call if state.disabled

  case sql.tr("\n", " ").strip
  when /^alter\s+table\s+(?<table>.+?)\s+(?<updates>.+)/i
    table = $~[:table]
    updates = $~[:updates]
    if updates.split(",").all? { |s| s =~ /^\s*drop\s+foreign\s+key/i } ||
       updates.split(",").all? { |s| s =~ /^\s*add\s+constraint/i }
      block.call
    elsif updates =~ /drop\s+foreign\s+key/i || updates =~ /add\s+constraint/i
      # ADD CONSTRAINT / DROP FOREIGN KEY have to go to the original table,
      # other alterations need to got to the new table.
      raise "[Alterity] Can't change a FK and do something else in the same query. Split it."
    else
      execute_alter(table, updates)
    end
  when /^create\s+index\s+(?<index>.+?)\s+on\s+(?<table>.+?)\s+(?<updates>.+)/i
    execute_alter($~[:table], "ADD INDEX #{$~[:index]} #{$~[:updates]}")
  when /^create\s+unique\s+index\s+(?<index>.+?)\s+on\s+(?<table>.+?)\s+(?<updates>.+)/i
    execute_alter($~[:table], "ADD UNIQUE INDEX #{$~[:index]} #{$~[:updates]}")
  when /^drop\s+index\s+(?<index>.+?)\s+on\s+(?<table>.+)/i
    execute_alter($~[:table], "DROP INDEX #{$~[:index]}")
  else
    block.call
  end
end

.reset_state_and_configurationObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/alterity/configuration.rb', line 17

def reset_state_and_configuration
  self.config = Configuration.new
  class << config
    def replicas(database:, table:, dsns:)
      return ArgumentError.new("database & table must be present") if database.blank? || table.blank?

      self.replicas_dsns_database = database
      self.replicas_dsns_table = table
      self.replicas_dsns = dsns.uniq.map do |dsn|
        parts = dsn.split(",")
        # automatically add default port
        parts << "P=3306" unless parts.any? { |part| part.start_with?("P=") }
        parts.join(",")
      end.compact
    end
  end

  self.state = CurrentState.new
  load "#{__dir__}/default_configuration.rb"
end