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
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
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_names ⇒ Array<String>
Returns a list of all DB table names
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.
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.
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_names ⇒ Array<String>
Returns a list of table names defined in models
17 18 19 |
# File 'lib/mimi/db/helpers.rb', line 17 def model_table_names models.map(&:table_name).uniq end |
#models ⇒ Array<ActiveRecord::Base>
Returns a list of model classes
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
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
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
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 |