Retrospring/Rakefile

88 lines
3.0 KiB
Ruby
Raw Normal View History

2023-01-02 02:33:30 -08:00
# frozen_string_literal: true
2014-08-01 02:23:47 -07:00
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
2023-01-02 02:33:30 -08:00
require File.expand_path("config/application", __dir__)
2014-08-01 02:23:47 -07:00
Rails.application.load_tasks
2014-11-28 13:02:32 -08:00
namespace :justask do
desc "Gives admin status to a user."
2023-01-02 02:33:30 -08:00
task :admin, [:screen_name] => :environment do |_t, args|
abort "screen name required" if args[:screen_name].nil?
2023-01-02 02:33:30 -08:00
user = User.find_by(screen_name: args[:screen_name])
abort "user #{args[:screen_name]} not found" if user.nil?
user.add_role :administrator
puts "#{user.screen_name} is now an administrator."
2014-11-28 13:31:29 -08:00
end
desc "Removes admin status from a user."
2023-01-02 02:33:30 -08:00
task :deadmin, [:screen_name] => :environment do |_t, args|
abort "screen name required" if args[:screen_name].nil?
2023-01-02 02:33:30 -08:00
user = User.find_by(screen_name: args[:screen_name])
abort "user #{args[:screen_name]} not found" if user.nil?
user.remove_role :administrator
puts "#{user.screen_name} is no longer an administrator."
2014-11-28 13:31:29 -08:00
end
desc "Gives moderator status to a user."
2023-01-02 02:33:30 -08:00
task :mod, [:screen_name] => :environment do |_t, args|
abort "screen name required" if args[:screen_name].nil?
2023-01-02 02:33:30 -08:00
user = User.find_by(screen_name: args[:screen_name])
abort "user #{args[:screen_name]} not found" if user.nil?
user.add_role :moderator
puts "#{user.screen_name} is now a moderator."
2014-12-26 04:04:05 -08:00
end
desc "Removes moderator status from a user."
2023-01-02 02:33:30 -08:00
task :demod, [:screen_name] => :environment do |_t, args|
abort "screen name required" if args[:screen_name].nil?
2023-01-02 02:33:30 -08:00
user = User.find_by(screen_name: args[:screen_name])
abort "user #{args[:screen_name]} not found" if user.nil?
user.remove_role :moderator
puts "#{user.screen_name} is no longer a moderator."
end
2014-11-29 03:21:42 -08:00
end
namespace :db do
namespace :schema do
2023-01-02 02:33:30 -08:00
task create_timestampid_function: :environment do
conn = ActiveRecord::Base.connection
have_func = conn.execute("SELECT EXISTS(SELECT * FROM pg_proc WHERE proname = 'gen_timestamp_id');").values.first.first
next if have_func
2023-01-02 02:33:30 -08:00
statement = File.read(File.join(__dir__, "db/migrate/20200704163504_use_timestamped_ids.rb")).match(/<~SQL(?<stmt>.+)SQL$/m)[:stmt]
conn.execute(statement)
end
2023-01-02 02:33:30 -08:00
task create_id_sequences: :environment do
conn = ActiveRecord::Base.connection
# required for the timestampid function to work properly
2023-01-02 02:33:30 -08:00
%i[questions answers comments users mute_rules].each do |tbl|
conn.execute("CREATE SEQUENCE IF NOT EXISTS #{tbl}_id_seq")
end
end
end
# create timestampid before loading schema
2023-01-02 02:33:30 -08:00
Rake::Task["db:schema:load"].enhance ["db:schema:create_timestampid_function"]
Rake::Task["db:test:load_schema"].enhance ["db:schema:create_timestampid_function"]
# create id_sequences after loading schema
2023-01-02 02:33:30 -08:00
Rake::Task["db:schema:load"].enhance do
Rake::Task["db:schema:create_id_sequences"].invoke
end
2023-01-02 02:33:30 -08:00
Rake::Task["db:test:load_schema"].enhance do
Rake::Task["db:schema:create_id_sequences"].invoke
end
end