Class: Dynamo::Record::TaskHelpers::MigrationRunner
- Inherits:
-
Object
- Object
- Dynamo::Record::TaskHelpers::MigrationRunner
- Defined in:
- lib/dynamo/record/task_helpers/migration_runner.rb
Class Method Summary collapse
- .migration(file, filename_regexp, constants) ⇒ Object
- .run(path = 'db/dynamo_migrate') ⇒ Object
- .status_message(status) ⇒ Object
- .table_config_check(migration) ⇒ Object
- .up(migration) ⇒ Object
- .update(migration) ⇒ Object
Class Method Details
.migration(file, filename_regexp, constants) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/dynamo/record/task_helpers/migration_runner.rb', line 33 def self.migration(file, filename_regexp, constants) raise "Non-numeric prefix: #{file}" if File.basename(file).scan(filename_regexp).first.nil? require file # finds the constant that was added on the require statement above migration_sym = (DynamoMigrate.constants - constants).first migration = DynamoMigrate.const_get(migration_sym) constants.push migration_sym migration end |
.run(path = 'db/dynamo_migrate') ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dynamo/record/task_helpers/migration_runner.rb', line 7 def self.run(path = 'db/dynamo_migrate') constants = [] filename_regexp = /\A([0-9]+)_([_a-z0-9]*)\.?([_a-z0-9]*)?\.rb\z/ # Sorts the files located in `db/dynamo_migrate` to ensure order is preserved Rails.root.glob("#{path}/*.rb").sort.each do |file| migration = migration(file, filename_regexp, constants) # starts the migration yield "Migrating: #{migration}" begin status = table_config_check(migration) yield status if status status = up(migration) yield status if status status = update(migration) yield status if status rescue StandardError => e yield "Migration failed: #{e}" end end end |
.status_message(status) ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dynamo/record/task_helpers/migration_runner.rb', line 45 def self.(status) case status when :exists 'Table already exists' when :migrated 'Migration successful' else raise 'Migration failed' end end |
.table_config_check(migration) ⇒ Object
62 63 64 65 66 |
# File 'lib/dynamo/record/task_helpers/migration_runner.rb', line 62 def self.table_config_check(migration) return unless migration.respond_to? :table_config migration.table_config_check end |
.up(migration) ⇒ Object
56 57 58 59 60 |
# File 'lib/dynamo/record/task_helpers/migration_runner.rb', line 56 def self.up(migration) return unless migration.respond_to? :up migration.up end |
.update(migration) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/dynamo/record/task_helpers/migration_runner.rb', line 68 def self.update(migration) return unless migration.respond_to? :update status = migration.update return 'Migration successful' if status == :updated status end |