Migrate existing OIDC account data

This commit is contained in:
Maikel Linke
2024-02-13 16:21:53 +11:00
parent b4ee24368c
commit 6c0d15b6f9
3 changed files with 51 additions and 1 deletions

View File

@@ -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

View File

@@ -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"

View File

@@ -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