module Mimi::DB::Helpers
Public Instance Methods
Returns a list of all discovered table names, both defined in models and existing in DB
@return [Array<String>]
# File lib/mimi/db/helpers.rb, line 36 def all_table_names (model_table_names + db_table_names).uniq end
Clears (but not drops) the database specified in the current configuration.
# File lib/mimi/db/helpers.rb, line 129 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
Creates the database specified in the current configuration.
# File lib/mimi/db/helpers.rb, line 97 def create! raise 'Not implemented' end
Creates the database specified in the current configuration, if it does NOT exist.
# File lib/mimi/db/helpers.rb, line 113 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
Tries to establish connection, returns true if the database exist
# File lib/mimi/db/helpers.rb, line 103 def database_exist? Mimi::DB.connection.test_connection true rescue StandardError => e Mimi::DB.logger.error "DB: database_exist? failed with: #{e}" false end
Returns a list of all DB
table names
@return [Array<String>]
# File lib/mimi/db/helpers.rb, line 27 def db_table_names Mimi::DB.connection.tables end
Discovers differences between existing DB
schema and target schema defined in models.
@example
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>, ...] # }
@return [Hash]
# File lib/mimi/db/helpers.rb, line 89 def diff_schema(opts = {}) Mimi::DB.start opts[:logger] ||= Mimi::DB.logger Mimi::DB::Dictate.diff_schema(opts) end
Drops the database specified in the current configuration.
# File lib/mimi/db/helpers.rb, line 123 def drop! raise 'Not implemented' end
Executes raw SQL, with variables interpolation.
@example
Mimi::DB.execute('insert into table1 values(?, ?, ?)', 'foo', :bar, 123)
# File lib/mimi/db/helpers.rb, line 142 def execute(statement, *args) sql = Sequel.fetch(statement, *args).sql Mimi::DB.connection.run(sql) end
Returns a list of table names defined in models
@return [Array<String>]
# File lib/mimi/db/helpers.rb, line 19 def model_table_names models.map(&:table_name).uniq end
Returns a list of model classes
@return [Array<ActiveRecord::Base>]
# File lib/mimi/db/helpers.rb, line 11 def models Mimi::DB::Model.descendants end
Starts a transaction and executes a given block within the transaction
@param params [Hash] parameters to Sequel
transaction()
method
# File lib/mimi/db/helpers.rb, line 151 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
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
@example
# 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)
# File lib/mimi/db/helpers.rb, line 60 def update_schema!(opts = {}) Mimi::DB.start opts[:logger] ||= Mimi::DB.logger Mimi::DB::Dictate.update_schema!(opts) end
Executes a block with a given DB
log level
@param log_level [Symbol,nil] :debug, :info etc
# File lib/mimi/db/helpers.rb, line 162 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