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
|
2020-04-19 13:35:58 -07:00
|
|
|
desc "Gives admin status to a user."
|
2023-01-02 02:33:30 -08:00
|
|
|
task :admin, [:screen_name] => :environment do |_t, args|
|
2020-04-19 13:35:58 -07:00
|
|
|
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])
|
2020-04-19 13:35:58 -07:00
|
|
|
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
|
|
|
|
|
2020-04-19 13:35:58 -07:00
|
|
|
desc "Removes admin status from a user."
|
2023-01-02 02:33:30 -08:00
|
|
|
task :deadmin, [:screen_name] => :environment do |_t, args|
|
2020-04-19 13:35:58 -07:00
|
|
|
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])
|
2020-04-19 13:35:58 -07:00
|
|
|
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
|
2015-01-12 13:44:13 -08:00
|
|
|
|
2020-04-19 13:35:58 -07:00
|
|
|
desc "Gives moderator status to a user."
|
2023-01-02 02:33:30 -08:00
|
|
|
task :mod, [:screen_name] => :environment do |_t, args|
|
2020-04-19 13:35:58 -07:00
|
|
|
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])
|
2020-04-19 13:35:58 -07:00
|
|
|
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
|
|
|
|
|
2020-04-19 13:35:58 -07:00
|
|
|
desc "Removes moderator status from a user."
|
2023-01-02 02:33:30 -08:00
|
|
|
task :demod, [:screen_name] => :environment do |_t, args|
|
2020-04-19 13:35:58 -07:00
|
|
|
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])
|
2020-04-19 13:35:58 -07:00
|
|
|
abort "user #{args[:screen_name]} not found" if user.nil?
|
|
|
|
|
|
|
|
user.remove_role :moderator
|
|
|
|
puts "#{user.screen_name} is no longer a moderator."
|
2015-01-12 13:44:13 -08:00
|
|
|
end
|
2014-11-29 03:21:42 -08:00
|
|
|
end
|
2020-07-04 11:12:53 -07:00
|
|
|
|
|
|
|
namespace :db do
|
|
|
|
namespace :schema do
|
2023-01-02 02:33:30 -08:00
|
|
|
task create_timestampid_function: :environment do
|
2020-07-04 11:12:53 -07:00
|
|
|
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]
|
2020-07-04 11:12:53 -07:00
|
|
|
conn.execute(statement)
|
|
|
|
end
|
|
|
|
|
2023-01-02 02:33:30 -08:00
|
|
|
task create_id_sequences: :environment do
|
2020-07-04 11:12:53 -07:00
|
|
|
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|
|
2020-07-04 11:12:53 -07:00
|
|
|
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"]
|
2020-07-04 11:12:53 -07:00
|
|
|
# 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
|
2020-07-04 11:12:53 -07:00
|
|
|
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
|
2022-01-11 14:16:56 -08:00
|
|
|
end
|
2020-07-04 11:12:53 -07:00
|
|
|
end
|