From 920002e084c6c649eeec2f6a59630123e9736cf3 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Tue, 7 Jan 2025 13:37:46 +1100 Subject: [PATCH] Copy admin attribute to users --- ...107014617_copy_admin_attribute_to_users.rb | 13 ++++++++++++ ...4617_copy_admin_attribute_to_users_spec.rb | 21 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 db/migrate/20250107014617_copy_admin_attribute_to_users.rb create mode 100644 spec/migrations/20250107014617_copy_admin_attribute_to_users_spec.rb diff --git a/db/migrate/20250107014617_copy_admin_attribute_to_users.rb b/db/migrate/20250107014617_copy_admin_attribute_to_users.rb new file mode 100644 index 0000000000..869c20d11f --- /dev/null +++ b/db/migrate/20250107014617_copy_admin_attribute_to_users.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class CopyAdminAttributeToUsers < ActiveRecord::Migration[7.0] + def up + execute <<~SQL.squish + UPDATE spree_users SET admin = true WHERE id IN ( + SELECT user_id FROM spree_roles_users WHERE role_id IN ( + SELECT id FROM spree_roles WHERE name = 'admin' + ) + ) + SQL + end +end diff --git a/spec/migrations/20250107014617_copy_admin_attribute_to_users_spec.rb b/spec/migrations/20250107014617_copy_admin_attribute_to_users_spec.rb new file mode 100644 index 0000000000..1391b66897 --- /dev/null +++ b/spec/migrations/20250107014617_copy_admin_attribute_to_users_spec.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_relative "../../db/migrate/#{File.basename(__FILE__, '_spec.rb')}" + +RSpec.describe CopyAdminAttributeToUsers do + describe "#up" do + it "marks current admins as admin" do + admin = create(:admin_user) + enterprise_user = create(:enterprise_user) + customer = create(:user) + + expect { subject.up }.to change { + admin.reload.admin + }.from(false).to(true) + + expect(enterprise_user.reload.admin).to eq false + expect(customer.reload.admin).to eq false + end + end +end