Sha256: 242d2ec29268de33c5716dce60a19f2bbe4b375b981a370b23608e673859481d

Contents?: true

Size: 1.03 KB

Versions: 30

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

require "sequel/timestamp_migrator_undo_extension"

namespace :sequel do
  # Rollback migrations that are absent in revision when deploying on staging
  task rollback_missing_migrations: :environment do
    # Extract migrations
    def extract_migrations(path)
      Dir.glob("#{path}/db/migrate/*.rb").map { |filename| File.basename(filename).to_i }
    end

    old_migrations = extract_migrations(ENV["OLD_RELEASE"])
    new_migrations = extract_migrations(ENV["NEW_RELEASE"])
    migrations_to_rollback = old_migrations - new_migrations

    next if migrations_to_rollback.empty?

    puts "Rolling back migrations:"
    puts migrations_to_rollback

    path = ::Rails.root.join("db/migrate")
    migrator = Sequel::TimestampMigrator.new(DB, path, allow_missing_migration_files: true)
    applied_migrations = migrator.applied_migrations.map(&:to_i)
    migrations = applied_migrations.select { |m| m.in?(migrations_to_rollback) }.sort.reverse

    migrations.each { |migration| migrator.undo(migration) }
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
umbrellio-sequel-plugins-0.7.0.52 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.6.0.46 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.6.0 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.6.0.36 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.5.1.27 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.5.1.26 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.5.1.25 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.5.1.18 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.5.1.17 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.5.1 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.5.0 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.4.0.164 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.4.0.152 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.4.0.145 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.4.0.134 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.4.0.121 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.4.0.114 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.4.0.112 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.4.0.107 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.4.0.81 lib/tasks/sequel/rollback_missing_migrations.rake