Skip to content

Commit

Permalink
Add tasks to check for pending/running migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
mvandiepen committed Nov 15, 2023
1 parent 577e885 commit c52b1a7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
12 changes: 12 additions & 0 deletions lib/sequent/migrations/versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ def self.migration_sql
where(status: [MIGRATE_ONLINE_RUNNING, MIGRATE_ONLINE_FINISHED, MIGRATE_OFFLINE_RUNNING])
}

def self.current_version
done.latest_version || 0
end

def self.running_version
running.latest_version
end

def self.latest_version
order('version desc').limit(1).first&.version
end

def self.start_online!(new_version)
create!(version: new_version, status: MIGRATE_ONLINE_RUNNING)
rescue ActiveRecord::RecordNotUnique
Expand Down
2 changes: 1 addition & 1 deletion lib/sequent/migrations/view_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def initialize(db_config:)
##
# Returns the current version from the database
def current_version
Versions.done.order('version desc').limit(1).first&.version || 0
Versions.current_version
end

##
Expand Down
27 changes: 23 additions & 4 deletions lib/sequent/rake/migration_tasks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ def register_tasks!
EOS
task init: :set_env_var

task connect_to_db: ['sequent:init', :init] do
ensure_sequent_env_set!
Sequent::Support::Database.connect!(@env)
end

namespace :db do
desc 'Creates the database and initializes the event_store schema for the current env'
task create: ['sequent:init'] do
Expand Down Expand Up @@ -91,12 +96,26 @@ def register_tasks!
task :init

desc 'Prints the current version in the database'
task current_version: ['sequent:init', :init] do
ensure_sequent_env_set!
task current_version: [:connect_to_db] do
puts "Current version in the database is: #{Sequent::Migrations::Versions.current_version}"
end

Sequent::Support::Database.connect!(@env)
desc 'Returns whether a migration is currently running'
task has_running_migrations: [:connect_to_db] do
if Sequent::Migrations::Versions.running.any?
puts "Migration is running, current version: #{Sequent::Migrations::Versions.current_version}, target version #{Sequent::Migrations::Versions.running_version}"
else
puts 'No running migrations'
end
end

puts "Current version in the database is: #{Sequent::Migrations::Versions.maximum(:version)}"
desc 'Returns whether a migration is pending'
task has_pending_migrations: [:connect_to_db] do
if Sequent.new_version != Sequent::Migrations::Versions.current_version
puts "Migration is pending, current version: #{Sequent::Migrations::Versions.current_version}, pending version: #{Sequent.new_version}"
else
puts 'No pending migrations'
end
end

desc <<~EOS
Expand Down

0 comments on commit c52b1a7

Please sign in to comment.