Skip to content

Commit

Permalink
Add sequel:rollback_archived_migrations rake task (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
tycooon authored Jul 11, 2023
1 parent 642dfe0 commit 528bbbe
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 71 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

services:
postgres:
image: postgres:12
image: postgres:14
env:
POSTGRES_USER: root
POSTGRES_HOST_AUTH_METHOD: trust
Expand All @@ -30,12 +30,12 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: ["2.7", "3.0", "3.1"]
ruby: ["2.7", "3.0", "3.1", "3.2"]

name: ${{ matrix.ruby }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- uses: ruby/setup-ruby@v1
with:
Expand All @@ -58,7 +58,7 @@ jobs:
if: github.event_name == 'push' && github.ref == 'refs/heads/master'

steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v3

- uses: umbrellio/publish-ruby-gem-action@v1.0.4
with:
Expand Down
9 changes: 9 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@
source "https://rubygems.org"

gemspec

gem "money"
gem "pg"
gem "pry"
gem "rake"
gem "rspec"
gem "rubocop-config-umbrellio"
gem "simplecov"
gem "simplecov-lcov"
96 changes: 50 additions & 46 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,106 +1,110 @@
PATH
remote: .
specs:
umbrellio-sequel-plugins (0.11.0)
umbrellio-sequel-plugins (0.12.0)
sequel
symbiont-ruby

GEM
remote: https://rubygems.org/
specs:
activesupport (7.0.4)
activesupport (7.0.6)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
ast (2.4.2)
coderay (1.1.3)
concurrent-ruby (1.1.10)
concurrent-ruby (1.2.2)
diff-lcs (1.5.0)
docile (1.4.0)
i18n (1.12.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
json (2.6.2)
json (2.6.3)
method_source (1.0.0)
minitest (5.16.3)
minitest (5.18.1)
money (6.16.0)
i18n (>= 0.6.4, <= 2)
parallel (1.22.1)
parser (3.1.2.1)
parallel (1.23.0)
parser (3.2.2.3)
ast (~> 2.4.1)
pg (1.4.3)
pry (0.14.1)
racc
pg (1.5.3)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
rack (3.0.0)
racc (1.7.1)
rack (3.0.8)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.6.0)
regexp_parser (2.8.1)
rexml (3.2.5)
rspec (3.11.0)
rspec-core (~> 3.11.0)
rspec-expectations (~> 3.11.0)
rspec-mocks (~> 3.11.0)
rspec-core (3.11.0)
rspec-support (~> 3.11.0)
rspec-expectations (3.11.1)
rspec (3.12.0)
rspec-core (~> 3.12.0)
rspec-expectations (~> 3.12.0)
rspec-mocks (~> 3.12.0)
rspec-core (3.12.2)
rspec-support (~> 3.12.0)
rspec-expectations (3.12.3)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-mocks (3.11.1)
rspec-support (~> 3.12.0)
rspec-mocks (3.12.5)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.11.0)
rspec-support (3.11.1)
rubocop (1.35.1)
rspec-support (~> 3.12.0)
rspec-support (3.12.1)
rubocop (1.50.2)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.1.2.1)
parser (>= 3.2.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.20.1, < 2.0)
rubocop-ast (>= 1.28.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.21.0)
parser (>= 3.1.1.0)
rubocop-config-umbrellio (1.35.0.69)
rubocop (~> 1.35.0)
rubocop-performance (~> 1.14.0)
rubocop-rails (~> 2.15.0)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
rubocop-capybara (2.18.0)
rubocop (~> 1.41)
rubocop-config-umbrellio (1.50.0.85)
rubocop (~> 1.50.0)
rubocop-performance (~> 1.17.0)
rubocop-rails (~> 2.19.0)
rubocop-rake (~> 0.6.0)
rubocop-rspec (~> 2.12.0)
rubocop-rspec (~> 2.20.0)
rubocop-sequel (~> 0.3.3)
rubocop-performance (1.14.3)
rubocop-performance (1.17.1)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
rubocop-rails (2.15.2)
rubocop-rails (2.19.1)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.7.0, < 2.0)
rubocop (>= 1.33.0, < 2.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (2.12.1)
rubocop (~> 1.31)
rubocop-rspec (2.20.0)
rubocop (~> 1.33)
rubocop-capybara (~> 2.17)
rubocop-sequel (0.3.4)
rubocop (~> 1.0)
ruby-progressbar (1.11.0)
sequel (5.61.0)
simplecov (0.21.2)
ruby-progressbar (1.13.0)
sequel (5.70.0)
simplecov (0.22.0)
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov-lcov (0.8.0)
simplecov_json_formatter (0.1.4)
symbiont-ruby (0.7.0)
tzinfo (2.0.5)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.3.0)
unicode-display_width (2.4.2)

PLATFORMS
ruby

DEPENDENCIES
bundler
money
pg
pry
Expand All @@ -112,4 +116,4 @@ DEPENDENCIES
umbrellio-sequel-plugins!

BUNDLED WITH
2.3.23
2.4.15
26 changes: 26 additions & 0 deletions lib/tasks/sequel/rollback_archived_migrations.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

require "sequel/timestamp_migrator_undo_extension"

namespace :sequel do
desc "Rollback migrations that were applied earlier but are not present in current release"
task rollback_archived_migrations: :environment do
DB.log_info("Finding applied migrations not present in current release...")

archive_path = Pathname.new(ENV.fetch("ARCHIVE_PATH")).expand_path.join("db/migrate")
migrator = Sequel::TimestampMigrator.new(DB, archive_path, allow_missing_migration_files: true)

applied_migrations = migrator.applied_migrations.map(&:to_i)
filesystem_migrations = Rails.root.glob("db/migrate/*.rb").map { |x| File.basename(x).to_i }
missing_migrations = applied_migrations - filesystem_migrations

if missing_migrations.any?
missing_migrations.each do |migration|
DB.log_info("Rolling back migration #{migration}...")
migrator.undo(migration)
end
else
DB.log_info("No migrations found")
end
end
end
11 changes: 5 additions & 6 deletions lib/tasks/sequel/rollback_missing_migrations.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
require "sequel/timestamp_migrator_undo_extension"

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

old_migrations = extract_migrations(ENV.fetch("OLD_RELEASE"))
new_migrations = extract_migrations(ENV.fetch("NEW_RELEASE"))
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")
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
Expand Down
6 changes: 3 additions & 3 deletions lib/tasks/sequel/undo.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
require "sequel/timestamp_migrator_undo_extension"

namespace :sequel do
# Rollback a specific migration
desc "Rollback a specific migration"
task undo: :environment do
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
version = ENV["VERSION"]&.to_i
raise "VERSION is required" unless version

path = ::Rails.root.join("db/migrate")
path = Rails.root.join("db/migrate")
migrator = Sequel::TimestampMigrator.new(DB, path, allow_missing_migration_files: true)
migrator.undo(version)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/extensions/methods_in_migrations_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

def outer_method(direction)
::INTERCEPTOR << "outer_#{direction}"
INTERCEPTOR << "outer_#{direction}"
end

RSpec.describe "methods_in_migrations" do
Expand Down
12 changes: 1 addition & 11 deletions umbrellio-sequel-plugins.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ lib = File.expand_path("lib", __dir__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
gem_version = "0.11.0"
gem_version = "0.12.0"

if ENV.fetch("PUBLISH_JOB", nil)
release_version = "#{gem_version}.#{ENV.fetch("GITHUB_RUN_NUMBER")}"
Expand All @@ -26,14 +26,4 @@ Gem::Specification.new do |spec|

spec.add_runtime_dependency "sequel"
spec.add_runtime_dependency "symbiont-ruby"

spec.add_development_dependency "bundler"
spec.add_development_dependency "money"
spec.add_development_dependency "pg"
spec.add_development_dependency "pry"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "rubocop-config-umbrellio"
spec.add_development_dependency "simplecov"
spec.add_development_dependency "simplecov-lcov"
end

0 comments on commit 528bbbe

Please sign in to comment.