diff --git a/db/migrate/20240213044159_copy_oidc_data_to_oidc_accounts.rb b/db/migrate/20240213044159_copy_oidc_data_to_oidc_accounts.rb new file mode 100644 index 0000000000..72b1d88d14 --- /dev/null +++ b/db/migrate/20240213044159_copy_oidc_data_to_oidc_accounts.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class CopyOidcDataToOidcAccounts < ActiveRecord::Migration[7.0] + def up + execute <<~SQL.squish + INSERT INTO oidc_accounts (user_id, provider, uid, created_at, updated_at) + SELECT id, provider, uid, updated_at, updated_at + FROM spree_users WHERE provider IS NOT NULL + SQL + end + + def down + execute "DELETE FROM oidc_accounts" + end +end diff --git a/db/schema.rb b/db/schema.rb index 0aad1ebf69..0ab2fb2f2e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2024_02_13_042618) do +ActiveRecord::Schema[7.0].define(version: 2024_02_13_044159) do # These are extensions that must be enabled in order to support this database enable_extension "pg_stat_statements" enable_extension "plpgsql" diff --git a/spec/migrations/20240213044159_copy_oidc_data_to_oidc_accounts_spec.rb b/spec/migrations/20240213044159_copy_oidc_data_to_oidc_accounts_spec.rb new file mode 100644 index 0000000000..d2bbd81694 --- /dev/null +++ b/spec/migrations/20240213044159_copy_oidc_data_to_oidc_accounts_spec.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_relative '../../db/migrate/20240213044159_copy_oidc_data_to_oidc_accounts' + +describe CopyOidcDataToOidcAccounts do + describe "up" do + let!(:user) { create(:user) } + let!(:oidc_user) { + create(:user, provider: "openid_connect", uid: "ofn@example.net") + } + + it "copies data" do + expect { subject.up }.to change { + OidcAccount.count + }.from(0).to(1) + + account = OidcAccount.first + + expect(account.user).to eq oidc_user + expect(account.provider).to eq oidc_user.provider + expect(account.uid).to eq oidc_user.uid + expect(account.token).to eq nil + end + end + + describe "down" do + it "removes data" do + user = create(:user) + OidcAccount.create!(user:, provider: "oidc", uid: "ofn@exmpl.net") + + expect { subject.down }.to change { OidcAccount.count }.from(1).to(0) + end + end +end