Module: Mimi::DB::Helpers

Included in:
Mimi::DB
Defined in:
lib/mimi/db/helpers.rb

Instance Method Summary collapse

Instance Method Details

#all_table_namesArray<String>

Returns a list of all discovered table names, both defined in models and existing in DB

Returns:

  • (Array<String>)


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

def all_table_names
  (model_table_names + db_table_names).uniq
end

#clear!Object

Clears (but not drops) the database specified in the current configuration.



127
128
129
130
131
132
133
# File 'lib/mimi/db/helpers.rb', line 127

def clear!
  Mimi::DB.start
  db_table_names.each do |table_name|
    Mimi::DB.logger.debug "Mimi::DB dropping table: #{table_name}"
    Mimi::DB.connection.drop_table(table_name)
  end
end

#create!Object

Creates the database specified in the current configuration.



95
96
97
# File 'lib/mimi/db/helpers.rb', line 95

def create!
  raise 'Not implemented'
end

#create_if_not_exist!Object

Creates the database specified in the current configuration, if it does NOT exist.



111
112
113
114
115
116
117
# File 'lib/mimi/db/helpers.rb', line 111

def create_if_not_exist!
  if database_exist?
    Mimi::DB.logger.debug 'Mimi::DB.create_if_not_exist! database exists, skipping...'
    return
  end
  create!
end

#database_exist?Boolean

Tries to establish connection, returns true if the database exist

Returns:

  • (Boolean)


101
102
103
104
105
106
107
# File 'lib/mimi/db/helpers.rb', line 101

def database_exist?
  Mimi::DB.connection.test_connection
  true
rescue StandardError => e
  Mimi::DB.logger.error "DB: database_exist? failed with: #{e}"
  false
end

#db_table_namesArray<String>

Returns a list of all DB table names

Returns:

  • (Array<String>)


25
26
27
# File 'lib/mimi/db/helpers.rb', line 25

def db_table_names
  Mimi::DB.connection.tables
end

#diff_schema(opts = {}) ⇒ Hash

Discovers differences between existing DB schema and target schema defined in models.

Examples:

Mimi::DB.diff_schema

# =>
# {
#   add_tables: [<table_schema1>, <table_schema2> ...],
#   change_tables: [
#     { table_name: ...,
#       columns: {
#         "<column_name1>" => {
#           from: { <column_definition or nil> },
#           to: { <column_definition or nil> }
#         }
#       }
#     }, ...
#   ],
#   drop_tables: [<table_name1>, ...]
# }

Returns:

  • (Hash)


87
88
89
90
91
# File 'lib/mimi/db/helpers.rb', line 87

def diff_schema(opts = {})
  Mimi::DB.start
  opts[:logger] ||= Mimi::DB.logger
  Mimi::DB::Dictate.diff_schema(opts)
end

#drop!Object

Drops the database specified in the current configuration.



121
122
123
# File 'lib/mimi/db/helpers.rb', line 121

def drop!
  raise 'Not implemented'
end

#execute(statement, *args) ⇒ Object

Executes raw SQL, with variables interpolation.

Examples:

Mimi::DB.execute('insert into table1 values(?, ?, ?)', 'foo', :bar, 123)


140
141
142
143
# File 'lib/mimi/db/helpers.rb', line 140

def execute(statement, *args)
  sql = Sequel.fetch(statement, *args).sql
  Mimi::DB.connection.run(sql)
end

#model_table_namesArray<String>

Returns a list of table names defined in models

Returns:

  • (Array<String>)


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

def model_table_names
  models.map(&:table_name).uniq
end

#modelsArray<ActiveRecord::Base>

Returns a list of model classes

Returns:

  • (Array<ActiveRecord::Base>)


9
10
11
# File 'lib/mimi/db/helpers.rb', line 9

def models
  Mimi::DB::Model.descendants
end

#transaction(params = {}, &_block) ⇒ Object

Starts a transaction and executes a given block within the transaction

Parameters:

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

    parameters to Sequel #transaction() method



149
150
151
152
153
154
# File 'lib/mimi/db/helpers.rb', line 149

def transaction(params = {}, &_block)
  unless Mimi::DB.connection
    raise 'Failed to start transaction, Mimi::DB.connection not available'
  end
  Mimi::DB.connection.transaction(params) { yield }
end

#update_schema!(opts = {}) ⇒ Object

Updates the DB schema.

Brings DB schema to a state defined in models.

Default options from Migrator::DEFAULTS:

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

Examples:

# only detect and report planned changes
Mimi::DB.update_schema!(dry_run: true)

# modify the DB schema, including all destructive operations
Mimi::DB.update_schema!(destructive: true)


58
59
60
61
62
# File 'lib/mimi/db/helpers.rb', line 58

def update_schema!(opts = {})
  Mimi::DB.start
  opts[:logger] ||= Mimi::DB.logger
  Mimi::DB::Dictate.update_schema!(opts)
end

#with_log_level(log_level, &_block) ⇒ Object

Executes a block with a given DB log level

Parameters:

  • log_level (Symbol, nil)

    :debug, :info etc



160
161
162
163
164
165
166
# File 'lib/mimi/db/helpers.rb', line 160

def with_log_level(log_level, &_block)
  current_log_level = Mimi::DB.connection.sql_log_level
  Mimi::DB.connection.sql_log_level = log_level
  yield
ensure
  Mimi::DB.connection.sql_log_level = current_log_level
end