Module: Inform::ImplicitMigration
- Included in:
- Inform::InheritanceListener::ClassMethods
- Defined in:
- lib/runtime/persistence.rb
Overview
The ImplicitMigration module
Constant Summary collapse
- NoDatabasePattern =
%r{No database associated with Sequel::Model}.freeze
- MigrationSetupTemplate =
'%<model>sSetup'.freeze
- ModuleNamespaceDelimiterPattern =
/::/.freeze
Instance Method Summary collapse
- #after_inherited(_subclass) ⇒ Object
- #before_inherited(subclass) ⇒ Object
-
#examine_schema ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength.
-
#maybe_migrate(subclass) ⇒ Object
rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength.
- #migration(model) ⇒ Object
Instance Method Details
#after_inherited(_subclass) ⇒ Object
202 203 204 |
# File 'lib/runtime/persistence.rb', line 202 def after_inherited(_subclass) examine_schema end |
#before_inherited(subclass) ⇒ Object
195 196 197 198 199 200 |
# File 'lib/runtime/persistence.rb', line 195 def before_inherited(subclass) return if self != Sequel::Model descendants << subclass log.debug "#{subclass} << #{self} [#{descendants}]" maybe_migrate(subclass) end |
#examine_schema ⇒ Object
rubocop: disable Metrics/AbcSize rubocop: disable Metrics/MethodLength
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/runtime/persistence.rb', line 208 def examine_schema descendants.each do |model_class| table_name = model_class.table_name indexes = self.db.indexes(table_name) columns = model_class.columns associations = model_class.associations log.debug "Table: #{table_name}" log.debug "Columns: #{columns.join(', ')}" log.debug "Indexes: #{indexes}" associations.each do |assoc_name, assoc_data| log.debug "Association: #{assoc_name} (#{assoc_data[:type]}) to #{assoc_data[:class_name]}" end log.debug "===========" end end |
#maybe_migrate(subclass) ⇒ Object
rubocop: enable Metrics/AbcSize rubocop: enable Metrics/MethodLength
229 230 231 232 233 234 235 236 237 238 |
# File 'lib/runtime/persistence.rb', line 229 def maybe_migrate(subclass) migration_class = migration(subclass) log.debug "Found migration class: #{migration_class}" migration_class&.up rescue Sequel::Error => e if NoDatabasePattern.match?(e.) Inform::Persistence.instance.connect retry end end |
#migration(model) ⇒ Object
240 241 242 243 244 245 246 247 248 |
# File 'lib/runtime/persistence.rb', line 240 def migration(model) names = format(MigrationSetupTemplate, model: model).split(ModuleNamespaceDelimiterPattern) names.inject(Object) do |mod, class_name| mod.const_get(class_name) rescue StandardError => e log.warn "Error getting reference to migration model class: #{e.}" next end end |