Class: SeedMigration::Migrator
- Inherits:
-
Object
- Object
- SeedMigration::Migrator
show all
- Defined in:
- lib/seed_migration/migrator.rb
Defined Under Namespace
Classes: PendingMigrationError
Constant Summary
collapse
- 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.
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}." if !@path.exist?
end
|
Class Method Details
.bootstrap(last_timestamp = nil) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/seed_migration/migrator.rb', line 127
def self.bootstrap(last_timestamp = nil)
p "Assume seed data migrated up to #{last_timestamp}"
files = get_migration_files(last_timestamp.to_s)
files.each do |file|
migration = SeedMigration::DataMigration.new
migration.version, _ = parse_migration_filename(file)
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
|
.display_migrations_status ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/seed_migration/migrator.rb', line 114
def self.display_migrations_status
puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
puts "-" * 50
up_versions = get_all_migration_versions
get_migration_files.each do |file|
version, name = parse_migration_filename(file)
status = up_versions.include?(version) ? "up" : "down"
puts "#{status.center(8)} #{version.ljust(14)} #{name}"
end
end
|
.last_migration ⇒ Object
97
98
99
|
# File 'lib/seed_migration/migrator.rb', line 97
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
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/seed_migration/migrator.rb', line 101
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
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/seed_migration/migrator.rb', line 86
def self.run_migrations(filename = nil)
if filename.blank?
run_new_migrations
else
path = self.migration_path(filename)
new(path).up
end
create_seed_file
end
|
.run_new_migrations ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/seed_migration/migrator.rb', line 78
def self.run_new_migrations
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
migration = SeedMigration::DataMigration.where(version: version).first
raise "#{klass} hasn't been migrated." if migration.nil?
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)
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
klass = class_from_path
version, _ = self.class.parse_migration_filename(@path)
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)
migration = SeedMigration::DataMigration.new
migration.version = version
migration.runtime = runtime.to_i
migration.migrated_on = DateTime.now
begin
migration.save!
rescue StandardError => e
puts e
end
announce("#{klass}: migrated (#{runtime}s)")
end
end
|