local-llm-database/Rakefile

60 lines
1.5 KiB
Ruby

# frozen_string_literal: true
# convert any arguments to a hash, maybe convert to a method and only call when needed
begin
arguments = ARGV.drop(1).map { |pair| pair.split('=', 2) }.to_h
rescue => e
puts "Arguments error: #{e.message}"
end
require 'rake/testtask'
Rake::TestTask.new do |task|
task.pattern = 'test/*_test.rb'
end
namespace :db do
require_relative 'db'
require 'sequel/core'
Sequel.extension :migration
migration_folder='db/migrations'
desc "Run all pending migrations or to [:version]"
task :migrate, [:version] do |t, args|
begin
version = args[:version].to_i if args[:version] # rake's default way of passing in a single argument
version = arguments["version"].to_i if arguments["version"] # override if given key=value arguments
Sequel::Migrator.run(DB, "db/migrations", target: version)
puts "Migration completed successfully!"
rescue => e
puts "Migration failed: #{e.message}"
exit 1
end
end
desc "Rollback the last migration"
task :rollback do
begin
Sequel::Migrator.run(DB, "db/migrations", relative: -1)
rescue => e
puts "Rollback failed: #{e.message}"
exit 1
end
end
desc "Show current migration status"
task :status do
begin
status = ""
status = "NOT " if Sequel::Migrator.is_current?(DB, migration_folder) == false
puts "The database is #{status}current."
#puts Sequel::Migrator.is_current?(DB, migration_folder)
rescue => e
puts "Status check failed: #{e.message}"
exit 1
end
end
end