Module: Mimi::DB::Dictate

Included in:
Model
Defined in:
lib/mimi/db/dictate.rb,
lib/mimi/db/dictate/dsl.rb,
lib/mimi/db/dictate/explorer.rb,
lib/mimi/db/dictate/migrator.rb,
lib/mimi/db/dictate/schema_diff.rb,
lib/mimi/db/dictate/type_defaults.rb,
lib/mimi/db/dictate/schema_definition.rb

Defined Under Namespace

Modules: DSL, Explorer, SchemaDiff, TypeDefaults Classes: Migrator, SchemaDefinition

Class Method Summary collapse

Class Method Details

.diff_schema(opts = {}) ⇒ Hash

Diff existing DB schema and the target schema

Parameters:

  • opts (Hash) (defaults to: {})

Returns:

  • (Hash)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mimi/db/dictate.rb', line 55

def self.diff_schema(opts = {})
  logger = opts[:logger] || Mimi::DB.logger
  diff = { add_tables: [], change_tables: [], drop_tables: [] }
  Mimi::DB.all_table_names.each do |t|
    m = Mimi::DB::Dictate::Migrator.new(t, opts)
    if m.from_schema && m.to_schema.nil?
      diff[:drop_tables] << t
    elsif m.from_schema && m.to_schema
      logger.debug "DB::Dictate comparing '#{t}'"
      t_diff = Mimi::DB::Dictate::SchemaDiff.diff(m.from_schema, m.to_schema)
      diff[:change_tables] << t_diff unless t_diff[:columns].empty? && t_diff[:indexes].empty?
    elsif m.from_schema.nil? && m.to_schema
      diff[:add_tables] << m.to_schema
    end
  end
  diff
rescue StandardError => e
  logger.error "DB::Dictate failed to compare schema: #{e}"
  raise
end

.included(base) ⇒ Object



11
12
13
# File 'lib/mimi/db/dictate.rb', line 11

def self.included(base)
  base.extend Mimi::DB::Dictate::DSL
end

.schema_definitionsHash

Returns all registered schema definitions

Returns:

  • (Hash)

    a map of <table_name> -> <schema_definition>



22
23
24
# File 'lib/mimi/db/dictate.rb', line 22

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

.startObject



15
16
# File 'lib/mimi/db/dictate.rb', line 15

def self.start
end

.update_schema!(opts = {}) ⇒ Object

Updates the DB schema to the target schema defined in models

Default options from Migrator::DEFAULTS:

destructive: {
  tables: false,
  columns: false,
  indexes: false
},
dry_run: false,
logger: nil # will use Mimi::DB.logger

Parameters:

  • opts (Hash) (defaults to: {})


39
40
41
42
43
44
45
46
47
48
# File 'lib/mimi/db/dictate.rb', line 39

def self.update_schema!(opts = {})
  logger = opts[:logger] || Mimi::DB.logger
  logger.debug 'Mimi::DB::Dictate started updating DB schema'
  t_start = Time.now
  Mimi::DB.all_table_names.each { |t| Mimi::DB::Dictate::Migrator.new(t, opts).run! }
  logger.debug 'Mimi::DB::Dictate finished updating DB schema (%.3fs)' % [Time.now - t_start]
rescue StandardError => e
  logger.error "Mimi::DB::Dictate failed to update DB schema: #{e}"
  raise
end