Class: Mimi::DB::Dictate::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/mimi/db/dictate/migrator.rb

Constant Summary collapse

DEFAULTS =
{
  destructive: {
    tables: false,
    columns: false,
    indexes: false
  },
  dry_run: false,
  logger: nil # will use ActiveRecord::Base.logger
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name, options) ⇒ Migrator

Creates a migrator to update table schema from DB state to defined state

Parameters:

  • table_name (String, Symbol)

    table name

  • options (Hash)


24
25
26
27
28
29
30
31
32
# File 'lib/mimi/db/dictate/migrator.rb', line 24

def initialize(table_name, options)
  @table_name = table_name.to_sym
  @options = DEFAULTS.merge(options.dup)
  @from_schema = self.class.db_schema_definition(@table_name)
  @to_schema   = Mimi::DB::Dictate.schema_definitions[@table_name]
  if from_schema.nil? && to_schema.nil?
    raise "Failed to migrate '#{@table_name}', no DB or target schema found"
  end
end

Instance Attribute Details

#from_schemaObject (readonly)

Returns the value of attribute from_schema.



17
18
19
# File 'lib/mimi/db/dictate/migrator.rb', line 17

def from_schema
  @from_schema
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/mimi/db/dictate/migrator.rb', line 17

def options
  @options
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



17
18
19
# File 'lib/mimi/db/dictate/migrator.rb', line 17

def table_name
  @table_name
end

#to_schemaObject (readonly)

Returns the value of attribute to_schema.



17
18
19
# File 'lib/mimi/db/dictate/migrator.rb', line 17

def to_schema
  @to_schema
end

Class Method Details

.db_schema_definition(table_name) ⇒ Object



65
66
67
68
# File 'lib/mimi/db/dictate/migrator.rb', line 65

def self.db_schema_definition(table_name)
  db_schema_definitions[table_name] ||=
    Mimi::DB::Dictate::Explorer.discover_schema(table_name)
end

.db_schema_definitionsObject



70
71
72
# File 'lib/mimi/db/dictate/migrator.rb', line 70

def self.db_schema_definitions
  @db_schema_definitions ||= {}
end

.reset_db_schema_definition!(table_name) ⇒ Object



74
75
76
# File 'lib/mimi/db/dictate/migrator.rb', line 74

def self.reset_db_schema_definition!(table_name)
  db_schema_definitions[table_name] = nil
end

Instance Method Details

#db_connectionObject



38
39
40
# File 'lib/mimi/db/dictate/migrator.rb', line 38

def db_connection
  Mimi::DB.connection
end

#destructive?(key) ⇒ Boolean

Returns true if the Migrator is permitted to do destructive operations (DROP …) on resources identified by :key

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/mimi/db/dictate/migrator.rb', line 51

def destructive?(key)
  options[:destructive] == true ||
    (options[:destructive].is_a?(Hash) && options[:destructive][key])
end

#dry_run?Boolean

Returns true if the Migrator is configured to do a dry run (no actual changes to DB)

Returns:

  • (Boolean)


44
45
46
# File 'lib/mimi/db/dictate/migrator.rb', line 44

def dry_run?
  options[:dry_run]
end

#loggerObject



34
35
36
# File 'lib/mimi/db/dictate/migrator.rb', line 34

def logger
  @logger ||= options[:logger] || ActiveRecord::Base.logger
end

#run!Object



56
57
58
59
60
61
62
63
# File 'lib/mimi/db/dictate/migrator.rb', line 56

def run!
  db_ddl_transaction do
    run_drop_table! if from_schema && to_schema.nil?
    run_change_table! if from_schema && to_schema
    run_create_table! if from_schema.nil? && to_schema
  end
  self.class.reset_db_schema_definition!(table_name)
end