Class: SlashMigrate::MigrationRunner

Inherits:
Object
  • Object
show all
Defined in:
app/services/slash_migrate/migration_runner.rb

Overview

Reports migration status and runs db:migrate / db:rollback.

Status is computed directly from the migration files and the schema_migrations table rather than through MigrationContext, whose migrations_paths are relative to the process cwd (which isn’t Rails.root in a mounted-engine dev setup). Running shells out to bin/rails so students see the real task output; chdir keeps it anchored to the host app.

Defined Under Namespace

Classes: Migration, Result

Instance Method Summary collapse

Instance Method Details

#applied_any?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/services/slash_migrate/migration_runner.rb', line 33

def applied_any?
  status.any?(&:applied?)
end

#delete(version) ⇒ Object

Deletes a migration file, but only when it hasn’t been run (pending, or already rolled back). Deleting an applied migration would orphan its schema_migrations row and leave it unreversible, so we refuse.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/services/slash_migrate/migration_runner.rb', line 48

def delete(version)
  version = version.to_s
  migration = status.find { |candidate| candidate.version == version }

  return Result.new(output: "No migration #{version} found.", success: false) unless migration
  if migration.applied?
    return Result.new(output: "#{migration.name}” has already been run — roll it back before deleting.", success: false)
  end

  path = migration_files.find { |file| File.basename(file).start_with?("#{version}_") }
  File.delete(path) if path
  Result.new(output: "Deleted “#{migration.name}”.", success: true)
end

#migrateObject



37
38
39
# File 'app/services/slash_migrate/migration_runner.rb', line 37

def migrate
  run("db:migrate")
end

#pending?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'app/services/slash_migrate/migration_runner.rb', line 29

def pending?
  status.any? { |migration| !migration.applied? }
end

#rollbackObject



41
42
43
# File 'app/services/slash_migrate/migration_runner.rb', line 41

def rollback
  run("db:rollback")
end

#statusObject



21
22
23
24
25
26
27
# File 'app/services/slash_migrate/migration_runner.rb', line 21

def status
  applied = applied_versions
  migration_files.map do |path|
    version, name = parse(path)
    Migration.new(version: version, name: name, applied: applied.include?(version))
  end
end