Class: Mimi::DB::Dictate::Migrator
- Inherits:
-
Object
- Object
- Mimi::DB::Dictate::Migrator
- Defined in:
- lib/mimi/db/dictate/migrator.rb
Constant Summary collapse
- DEFAULTS =
{ destructive: { tables: false, columns: false, indexes: false }, dry_run: false, logger: nil # will use ActiveRecord::Base.logger }.freeze
Instance Attribute Summary collapse
-
#from_schema ⇒ Object
readonly
Returns the value of attribute from_schema.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#table_name ⇒ Object
readonly
Returns the value of attribute table_name.
-
#to_schema ⇒ Object
readonly
Returns the value of attribute to_schema.
Class Method Summary collapse
- .db_schema_definition(table_name) ⇒ Object
- .db_schema_definitions ⇒ Object
- .reset_db_schema_definition!(table_name) ⇒ Object
Instance Method Summary collapse
- #db_connection ⇒ Object
-
#destructive?(key) ⇒ Boolean
Returns true if the Migrator is permitted to do destructive operations (DROP …) on resources identified by :key.
-
#dry_run? ⇒ Boolean
Returns true if the Migrator is configured to do a dry run (no actual changes to DB).
-
#initialize(table_name, options) ⇒ Migrator
constructor
Creates a migrator to update table schema from DB state to defined state.
- #logger ⇒ Object
- #run! ⇒ Object
Constructor Details
#initialize(table_name, options) ⇒ Migrator
Creates a migrator to update table schema from DB state to defined state
24 25 26 27 28 29 30 31 32 |
# File 'lib/mimi/db/dictate/migrator.rb', line 24 def initialize(table_name, ) @table_name = table_name.to_sym @options = DEFAULTS.merge(.dup) @from_schema = self.class.db_schema_definition(@table_name) @to_schema = Mimi::DB::Dictate.schema_definitions[@table_name] if from_schema.nil? && to_schema.nil? raise "Failed to migrate '#{@table_name}', no DB or target schema found" end end |
Instance Attribute Details
#from_schema ⇒ Object (readonly)
Returns the value of attribute from_schema.
17 18 19 |
# File 'lib/mimi/db/dictate/migrator.rb', line 17 def from_schema @from_schema end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/mimi/db/dictate/migrator.rb', line 17 def @options end |
#table_name ⇒ Object (readonly)
Returns the value of attribute table_name.
17 18 19 |
# File 'lib/mimi/db/dictate/migrator.rb', line 17 def table_name @table_name end |
#to_schema ⇒ Object (readonly)
Returns the value of attribute to_schema.
17 18 19 |
# File 'lib/mimi/db/dictate/migrator.rb', line 17 def to_schema @to_schema end |
Class Method Details
.db_schema_definition(table_name) ⇒ Object
65 66 67 68 |
# File 'lib/mimi/db/dictate/migrator.rb', line 65 def self.db_schema_definition(table_name) db_schema_definitions[table_name] ||= Mimi::DB::Dictate::Explorer.discover_schema(table_name) end |
.db_schema_definitions ⇒ Object
70 71 72 |
# File 'lib/mimi/db/dictate/migrator.rb', line 70 def self.db_schema_definitions @db_schema_definitions ||= {} end |
.reset_db_schema_definition!(table_name) ⇒ Object
74 75 76 |
# File 'lib/mimi/db/dictate/migrator.rb', line 74 def self.reset_db_schema_definition!(table_name) db_schema_definitions[table_name] = nil end |
Instance Method Details
#db_connection ⇒ Object
38 39 40 |
# File 'lib/mimi/db/dictate/migrator.rb', line 38 def db_connection Mimi::DB.connection end |
#destructive?(key) ⇒ Boolean
Returns true if the Migrator is permitted to do destructive operations (DROP …) on resources identified by :key
51 52 53 54 |
# File 'lib/mimi/db/dictate/migrator.rb', line 51 def destructive?(key) [:destructive] == true || ([:destructive].is_a?(Hash) && [:destructive][key]) end |
#dry_run? ⇒ Boolean
Returns true if the Migrator is configured to do a dry run (no actual changes to DB)
44 45 46 |
# File 'lib/mimi/db/dictate/migrator.rb', line 44 def dry_run? [:dry_run] end |
#logger ⇒ Object
34 35 36 |
# File 'lib/mimi/db/dictate/migrator.rb', line 34 def logger @logger ||= [:logger] || ActiveRecord::Base.logger end |
#run! ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/mimi/db/dictate/migrator.rb', line 56 def run! db_ddl_transaction do run_drop_table! if from_schema && to_schema.nil? run_change_table! if from_schema && to_schema run_create_table! if from_schema.nil? && to_schema end self.class.reset_db_schema_definition!(table_name) end |