Skip to content

Commit

Permalink
LTI-265: Add rake tasks for tenant settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Mariam05 committed Aug 8, 2023
1 parent 99b6ae3 commit 3b68b5a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_02_24_170133) do
ActiveRecord::Schema.define(version: 2023_03_09_132533) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -82,6 +82,7 @@
t.string "uid"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.jsonb "settings", default: {}, null: false
t.index ["uid"], name: "index_tenant_uid", unique: true
end

Expand Down
87 changes: 87 additions & 0 deletions lib/tasks/db_tenant_settings.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# frozen_string_literal: true

require 'bbb_lti_broker/helpers'

namespace :db do
namespace :tenants do
namespace :settings do
desc 'Show all settings for a tenant. If no id is specified, settings for all tenants will be shown'
task :showall, [:uid] => :environment do |_t, args|
tenant_id = args[:uid] || ''

if tenant_id.present?
tenant = RailsLti2Provider::Tenant.find_by(uid: tenant_id)
if tenant.nil?
puts("Tenant '#{tenant_id}' does not exist.")
exit(1)
end

puts("Settings for tenant #{tenant_id}: \n #{tenant.settings}")
else
tenants = RailsLti2Provider::Tenant.all
tenants_list = tenants.map do |t|
{
tenant: t.uid,
settings: t.settings,
}
end
puts(tenants_list)
end
end

desc 'Add a new tenant setting'
task :upsert, [:uid, :key, :value] => :environment do |_t, args|
tenant_id = args[:uid] || ''
key = args[:key]
value = args[:value]

unless key.present? && value.present?
puts('Error: key and value are required to create a tenant setting')
exit(1)
end

tenant = RailsLti2Provider::Tenant.find_by(uid: tenant_id)
if tenant.nil?
puts("Tenant '#{tenant_id}' does not exist.")
exit(1)
end

# Add the setting
tenant.settings[key] = value
tenant.save!

puts("Added setting #{key}=#{value} to tenant #{tenant_id}")
rescue StandardError => e
puts(e.backtrace)
exit(1)
end

desc 'Delete a setting'
task :delete, [:uid, :key] => :environment do |_t, args|
tenant_id = args[:uid] || ''
key = args[:key]

if key.present?
puts('Error: The setting key is required to delete a tenant setting')
exit(1)
end

tenant = RailsLti2Provider::Tenant.find_by(uid: tenant_id)
if tenant.nil?
puts("Tenant '#{tenant_id}' does not exist.")
exit(1)
end

puts("Key '#{key}' not found for tenant #{tenant}") unless tenant.settings[key]

tenant.settings.delete(key)
tenant.save!

puts("Setting #{key} for tenant '#{tenant_id}' has been deleted")
rescue StandardError => e
puts(e.backtrace)
exit(1)
end
end
end
end

0 comments on commit 3b68b5a

Please sign in to comment.