Module: Mimi::DB::Helpers
- Included in:
- Mimi::DB
- Defined in:
- lib/mimi/db/helpers.rb
Instance Method Summary collapse
-
#all_table_names ⇒ Array<String>
Returns a list of all discovered table names, both defined in models and existing in DB.
-
#clear! ⇒ Object
Clears (but not drops) the database specified in the current configuration.
-
#create! ⇒ Object
Creates the database specified in the current configuration.
-
#create_if_not_exist! ⇒ Object
Creates the database specified in the current configuration, if it does NOT exist.
-
#database_exist? ⇒ Boolean
Tries to establish connection, returns true if the database exist.
-
#db_table_names ⇒ Array<String>
Returns a list of all DB table names.
-
#diff_schema(opts = {}) ⇒ Hash
Discovers differences between existing DB schema and target schema defined in models.
-
#drop! ⇒ Object
Drops the database specified in the current configuration.
-
#execute(statement, *args) ⇒ Object
Executes raw SQL, with variables interpolation.
-
#model_table_names ⇒ Array<String>
Returns a list of table names defined in models.
-
#models ⇒ Array<ActiveRecord::Base>
Returns a list of model classes.
-
#transaction(params = {}, &_block) ⇒ Object
Starts a transaction and executes a given block within the transaction.
-
#update_schema!(opts = {}) ⇒ Object
Updates the DB schema.
-
#with_log_level(log_level, &_block) ⇒ Object
Executes a block with a given DB log level.
Instance Method Details
#all_table_names ⇒ Array<String>
Returns a list of all discovered table names, both defined in models and existing in DB
36 37 38 |
# File 'lib/mimi/db/helpers.rb', line 36 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.
129 130 131 132 133 134 135 |
# 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 |
#create! ⇒ Object
Creates the database specified in the current configuration.
97 98 99 |
# File 'lib/mimi/db/helpers.rb', line 97 def create! raise 'Not implemented' end |
#create_if_not_exist! ⇒ Object
Creates the database specified in the current configuration, if it does NOT exist.
113 114 115 116 117 118 119 |
# 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 |
#database_exist? ⇒ Boolean
Tries to establish connection, returns true if the database exist
103 104 105 106 107 108 109 |
# 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 |
#db_table_names ⇒ Array<String>
Returns a list of all DB table names
27 28 29 |
# File 'lib/mimi/db/helpers.rb', line 27 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.
89 90 91 92 93 |
# 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 |
#drop! ⇒ Object
Drops the database specified in the current configuration.
123 124 125 |
# File 'lib/mimi/db/helpers.rb', line 123 def drop! raise 'Not implemented' end |
#execute(statement, *args) ⇒ Object
Executes raw SQL, with variables interpolation.
142 143 144 145 |
# File 'lib/mimi/db/helpers.rb', line 142 def execute(statement, *args) sql = Sequel.fetch(statement, *args).sql Mimi::DB.connection.run(sql) end |
#model_table_names ⇒ Array<String>
Returns a list of table names defined in models
19 20 21 |
# File 'lib/mimi/db/helpers.rb', line 19 def model_table_names models.map(&:table_name).uniq end |
#models ⇒ Array<ActiveRecord::Base>
Returns a list of model classes
11 12 13 |
# File 'lib/mimi/db/helpers.rb', line 11 def models Mimi::DB::Model.descendants end |
#transaction(params = {}, &_block) ⇒ Object
Starts a transaction and executes a given block within the transaction
151 152 153 154 155 156 |
# 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 |
#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
60 61 62 63 64 |
# 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 |
#with_log_level(log_level, &_block) ⇒ Object
Executes a block with a given DB log level
162 163 164 165 166 167 168 |
# 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 |