Sha256: 465d376b96d5924875a9ba28c1ff6141d0d6fb39bc6afce2b61496d1fde7404e

Contents?: true

Size: 1.04 KB

Versions: 20

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

require "sequel/timestamp_migrator_undo_extension"

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

    old_migrations = extract_migrations.call(ENV.fetch("OLD_RELEASE"))
    new_migrations = extract_migrations.call(ENV.fetch("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

20 entries across 20 versions & 1 rubygems

Version Path
umbrellio-sequel-plugins-0.16.1 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.16.0.239 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.16.0.238 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.16.0.235 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.16.0.234 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.16.0.233 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.16.0 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.16.0.211 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.15.0.198 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.14.0.192 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.14.0.189 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.14.0.188 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.14.0.187 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.14.0 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.13.0.185 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.13.0.172 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.13.0 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.12.0.169 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.12.0 lib/tasks/sequel/rollback_missing_migrations.rake
umbrellio-sequel-plugins-0.12.0.159 lib/tasks/sequel/rollback_missing_migrations.rake