Class: SeedMigration::Migrator
- Inherits:
-
Object
- Object
- SeedMigration::Migrator
- Defined in:
- lib/seed_migration/migrator.rb
Constant Summary collapse
- SEEDS_FILE_PATH =
Rails.root.join('db', 'seeds.rb')
Class Method Summary collapse
- .bootstrap(last_timestamp = nil) ⇒ Object
- .data_migration_directory ⇒ Object
- .last_migration ⇒ Object
- .migration_path(filename) ⇒ Object
- .rollback_migrations(filename = nil, steps = 1) ⇒ Object
- .run_migrations(filename = nil) ⇒ Object
-
.run_new_migrations ⇒ Object
Rake methods.
Instance Method Summary collapse
- #down ⇒ Object
-
#initialize(migration_path) ⇒ Migrator
constructor
A new instance of Migrator.
- #up ⇒ Object
Constructor Details
#initialize(migration_path) ⇒ Migrator
Returns a new instance of Migrator.
15 16 17 18 |
# File 'lib/seed_migration/migrator.rb', line 15 def initialize(migration_path) @path = Pathname.new(migration_path) raise "Can't find migration at #{@path.to_s}." if !@path.exist? end |
Class Method Details
.bootstrap(last_timestamp = nil) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/seed_migration/migrator.rb', line 108 def self.bootstrap( = nil) # replace with logger ? p "Assume migrated up to #{}" files = get_migration_files(.to_s) files.each do |file| name = file.split('/').last version = name.split('_').first migration = SeedMigration::DataMigration.new migration.version = version migration.runtime = 0 migration.migrated_on = DateTime.now migration.save! end end |
.data_migration_directory ⇒ Object
7 8 9 |
# File 'lib/seed_migration/migrator.rb', line 7 def self.data_migration_directory Rails.root.join("db", SeedMigration.migrations_path) end |
.last_migration ⇒ Object
91 92 93 |
# File 'lib/seed_migration/migrator.rb', line 91 def self.last_migration return SeedMigration::DataMigration.maximum("version") end |
.migration_path(filename) ⇒ Object
11 12 13 |
# File 'lib/seed_migration/migrator.rb', line 11 def self.migration_path(filename) data_migration_directory.join(filename).to_s end |
.rollback_migrations(filename = nil, steps = 1) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/seed_migration/migrator.rb', line 95 def self.rollback_migrations(filename = nil, steps = 1) if filename.blank? to_run = get_last_x_migrations(steps) to_run.each do |migration| new(migration).down end else path = migration_path(filename) new(path).down end create_seed_file end |
.run_migrations(filename = nil) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/seed_migration/migrator.rb', line 80 def self.run_migrations(filename = nil) if filename.blank? # Run any outstanding migrations run_new_migrations else path = self.migration_path(filename) new(path).up end create_seed_file end |
.run_new_migrations ⇒ Object
Rake methods
72 73 74 75 76 77 78 |
# File 'lib/seed_migration/migrator.rb', line 72 def self.run_new_migrations # TODO : Add warning about empty registered_models get_new_migrations.each do |migration| migration = migration_path(migration) new(migration).up end end |
Instance Method Details
#down ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/seed_migration/migrator.rb', line 47 def down klass = class_from_path version = @path.basename.to_s.split("_", 2).first # Get migration record migration = SeedMigration::DataMigration.where(version: version).first # Do not proceed without it! raise "#{klass} hasn't been migrated." if migration.nil? # Revert start_time = Time.now announce("#{klass}: reverting") ActiveRecord::Base.transaction do klass.new.down end_time = Time.now runtime = (end_time - start_time).to_d.round(2) # Delete record of migration migration.destroy announce("#{klass}: reverted (#{runtime}s)") end end |
#up ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/seed_migration/migrator.rb', line 20 def up # Check if we already migrated this file klass = class_from_path version = @path.basename.to_s.split("_", 2).first raise "#{klass} has already been migrated." if SeedMigration::DataMigration.where(version: version).first start_time = Time.now announce("#{klass}: migrating") ActiveRecord::Base.transaction do klass.new.up end_time = Time.now runtime = (end_time - start_time).to_d.round(2) # Create record migration = SeedMigration::DataMigration.new migration.version = version migration.runtime = runtime.to_i migration.migrated_on = DateTime.now begin migration.save! rescue Exception => e puts e end announce("#{klass}: migrated (#{runtime}s)") end end |