Class: SeedMigration::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/seed_migration/migrator.rb

Constant Summary collapse

DATA_MIGRATION_DIRECTORY =
Rails.root.join("db", "data")
SEEDS_FILE_PATH =
Rails.root.join('db', 'seeds.rb')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(migration_path) ⇒ Migrator

Returns a new instance of Migrator.



12
13
14
15
# File 'lib/seed_migration/migrator.rb', line 12

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/seed_migration/migrator.rb', line 105

def self.bootstrap(last_timestamp = nil)
  # replace with logger ?
  p "Assume migrated up to #{last_timestamp}"
  files = get_migration_files(last_timestamp.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

.last_migrationObject



88
89
90
# File 'lib/seed_migration/migrator.rb', line 88

def self.last_migration
  return SeedMigration::DataMigration.maximum("version")
end

.migration_path(filename) ⇒ Object



8
9
10
# File 'lib/seed_migration/migrator.rb', line 8

def self.migration_path(filename)
  DATA_MIGRATION_DIRECTORY.join(filename).to_s
end

.rollback_migrations(filename = nil, steps = 1) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/seed_migration/migrator.rb', line 92

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



77
78
79
80
81
82
83
84
85
86
# File 'lib/seed_migration/migrator.rb', line 77

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_migrationsObject

Rake methods



69
70
71
72
73
74
75
# File 'lib/seed_migration/migrator.rb', line 69

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

#downObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/seed_migration/migrator.rb', line 44

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

#upObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/seed_migration/migrator.rb', line 17

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